A very simple example of using C # to invoke TensorFlow. 1. Install TensorFlow
First you need to install the Windows version of Tensowflow, use 64-bit python3.5, and if not installed, you need to first install python3.5
Then go to the command line as an administrator and run
Pip Install TensorFlow 2.c# calling code initialization CLE and PYTHON35
Starcorefactory Starcore = Starcorefactory.getfactory ();
Starserviceclass Service = starcore._initsimple ("Test", "123", 0, 0, null);
Starsrvgroupclass Srvgroup = (starsrvgroupclass) service._get ("_servicegroup");
--init Python Raw interface
srvgroup._initraw ("Python35", Service);
Starobjectclass python = service._importrawcontext ("Python", "", False, "");
Call TensorFlow
--import TensorFlow as TF
python._call ("eval", "Import TensorFlow as TF");
Starobjectclass tf = python._getobject ("TF");
Console.WriteLine (TF);
--A = Tf.add (2,5)
starobjectclass a = (Starobjectclass) tf._call ("Add", 2, 5);
--B = tf.multiply (a,5)
starobjectclass b = (starobjectclass) tf._call ("Multiply", A, 3);
--C = tf.constant (2,name= "Node_c")
starobjectclass C = (starobjectclass) tf._call ("Constant", 2, Srvgroup._ Newparapkg ("name", "Node_c"). _asdict (true);
Console.WriteLine (c);
--sess = tf. Session ()
Starobjectclass sessions = (Starobjectclass) Tf._get ("session");
Starobjectclass sess = Session._new ();
--result = Sess.run (b,feed_dict={a:25});
Starparapkgclass pkg = srvgroup._newparapkg (A, a). _asdict (true);
Object result = Sess._call ("Run", B, Srvgroup._newparapkg ("Feed_dict", pkg). _asdict (true);
Console.WriteLine (result);
If you use the dynamic keyword, the preceding code is simplified as follows:
--import TensorFlow as TF
python.eval ("Import TensorFlow as TF");
Dynamic TF = PYTHON.TF;
--A = Tf.add (2,5)
dynamic a = Tf.add (2, 5);
--B = tf.multiply (a,5)
Dynamic B = tf.multiply (A, 3);
--C = tf.constant (2,name= "Node_c")
dynamic C = tf.constant (2, srvgroup._newparapkg ("name", "Node_c"). _asdict ( true));
--sess = tf. Session ()
dynamic session = TF. session;
Dynamic Sess = Session._new ();
--result = Sess.run (b,feed_dict={a:25});
var pkg = srvgroup._newparapkg (A, a). _asdict (true);
Dynamic result = Sess.run (b, srvgroup._newparapkg ("Feed_dict", pkg). _asdict (true);
Console.WriteLine (result);
3. Example Download
Download (vs2017 project)