Example of C # calling TensorFlow __c#

Source: Internet
Author: User

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)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.