public static void Main(string[] args) { // CreateHostBuilder(args).Build().Run(); //CreateWebHostBuilder(args).Build().Run(); Sequential Seq = new Sequential(); Seq.Add(new Dense(32, activation: "relu", input_shape: new Shape(250, 250, 3))); Seq.Add(new Dense(64, activation: "relu")); Seq.Add(new Dense(1, activation: "sigmoid")); Console.WriteLine(Backend.GetBackend()); var function = Backend.Function(Seq.Layers(0), Seq.Layers(1)); Console.WriteLine(function); NDarray x = np.array(new float[, ] { { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 } }); NDarray y = np.array(new float[] { 0, 1, 1, 0 }); var val = (42, 2); // var data = tf.ones(new TensorShape(new int[] {1, 259, 259, 3})); var data = Backend.ones(); KerasIterator iter = new KerasIterator(data); var z = new PyIter(iter.PyObject); z.MoveNext(); var output = z.Current; var res = function(data); Console.WriteLine("function Results:"); Console.WriteLine(res); }
private static void GetTTupleList <T>(PyObject obj, ref List <T> result) { PyIter iter = new PyIter(obj); while (iter.MoveNext()) { var r = iter.Current.ToPython(); if (PyTuple.IsTupleType(r)) { GetTTupleList <T>(r, ref result); continue; } switch (typeof(T).Name) { case "Single": case "Double": case "Int32": case "Int64": case "UInt32": case "UInt64": case "Byte": case "Object": case "String": case "SByte": result.Add(r.As <T>()); break; default: break; } } }
public static NDarray[] TupleToList(PyObject obj) { PyIter iter = new PyIter(obj); List <NDarray> result = new List <NDarray>(); GetNdListFromTuple(new PyIter(obj), ref result); return(result.ToArray()); }
protected Dictionary <string, string> GetPipModules() { Contract.Requires(this.Initialized); Dictionary <string, string> results = new Dictionary <string, string>(); using (Py.GIL()) using (PyIter modules = new PyIter(Py.Import("pip").InvokeMethod("get_installed_distributions").GetIterator())) { while (modules.MoveNext()) { dynamic m = (PyObject)modules.Current; results.Add(m.key.As <string>(), m.version.As <string>()); } } return(results); }
protected List <string> GetModules() { Contract.Requires(this.Initialized); List <string> results = new List <string>(); using (Py.GIL()) using (PyIter modules = new PyIter(Py.Import("pkgutil").InvokeMethod("iter_modules"))) { while (modules.MoveNext()) { PyObject m = (PyObject)modules.Current; results.Add(m[1].As <string>()); } } return(results); }
private static void GetNdListFromTuple(PyObject obj, ref List <NDarray> result) { PyIter iter = new PyIter(obj); while (iter.MoveNext()) { var r = iter.Current.ToPython(); if (PyTuple.IsTupleType(r)) { GetNdListFromTuple(r, ref result); continue; } result.Add(new NDarray(r)); } }
public bool TryDecode <T>(PyObject pyObj, out T value) { if (!PyIter.IsIterable(pyObj)) { value = default; return(false); } using (var pyList = PyList.AsList(pyObj)) { if (typeof(T).IsGenericType) { value = pyList.ToList <T>(); } else { value = (T)pyList.ToList(); } return(true); } }
public IEnumerator <T> GetEnumerator() { PyIter iterObject; using (Py.GIL()) { iterObject = PyIter.GetIter(pyObject); } using var _ = iterObject; while (true) { using var GIL = Py.GIL(); if (!iterObject.MoveNext()) { iterObject.Dispose(); break; } yield return(iterObject.Current.As <T>() !); } }
/// <summary> /// Initializes a new instance of the <see cref="StringOrInstance"/> class. /// </summary> /// <param name="obj">The object.</param> public KerasIterator(PyObject obj) { PyObject = obj; PyIter = new PyIter(PyObject); }