C # writing TensorFlow AI applications

Source: Internet
Author: User
Tags tmp folder

C # writing TensorFlow AI applications

Tensorflowsharp get started using C # to write TensorFlow AI application learning.

TensorFlow Brief Introduction

TensorFlow is Google's second-generation machine learning system, according to Google, in some benchmarks, tensorflow performance is twice times faster than the first generation of Distbelief.

TensorFlow built-in deep learning extension support, any calculation that can be expressed with computational flow graphs can use TensorFlow. Any gradient-based machine learning algorithm can benefit from TensorFlow's automatic differentiation (auto-differentiation). With a flexible Python interface, it can be easy to express ideas in TensorFlow.

TensorFlow is also very meaningful for the actual product. Move your ideas seamlessly from desktop GPU training to mobile phone running.

Sample Python code:

Import TensorFlow as Tfimport NumPy as np# Create phony x, y data points in numpy, y = x * 0.1 + 0.3x_data = Np.random . Rand. Astype (np.float32) y_data = X_data * 0.1 + 0.3# Try to find values for W and b that compute y_data = w * x_data + b# (We know that W should is 0.1 and B 0.3, but TensorFlow will# figure this out for us.) W = tf. Variable (Tf.random_uniform ([1], -1.0, 1.0)) B = tf. Variable (Tf.zeros ([1])) y = W * x_data + b# Minimize the mean squared Errors.loss = Tf.reduce_mean (Tf.square (y-y_data)) op Timizer = Tf.train.GradientDescentOptimizer (0.5) train = Optimizer.minimize (loss) # before starting, initialize the Variables.  We'll ' run ' this first.init = Tf.global_variables_initializer () # Launch the graph.sess = tf. Session () Sess.run (init) # Fit The Line.for step in range (201):    Sess.run (train)    if step% = = 0:        print (step, s Ess.run (W), Sess.run (b)) # Learns best fit is W: [0.1], B: [0.3]

Using Tensorflowsharp

Github:https://github.com/migueldeicaza/tensorflowsharp

The official source repository, the project supports cross-platform, using mono.

You can use NuGet to install Tensorflowsharp, as follows:

Install-package Tensorflowsharp

Write a simple application

Use VS2017 to create a new. NET Framework console to apply Tensorflowdemo, and then add Tensorflowsharp references.

Tensorflowsharp bag is bigger, need to wait patiently.

Then create the platform targetin the project properties to change to x64.

Open Program.cs Write the following code:

        static void Main (string[] args) {using (var session = new Tfsession ()) { var graph = Session.                Graph;                Console.WriteLine (tfcore.version); var a = graph.                Const (2); var b = Graph.                Const (3);                Console.WriteLine ("A=2 b=3"); Two Constants plus var addingresults = session. Getrunner (). Run (graph.                Add (A, b)); var addingresultvalue = addingresults[0].                GetValue ();                Console.WriteLine ("A+b={0}", Addingresultvalue); Two constants multiply var multiplyresults = session. Getrunner (). Run (graph.                Mul (A, b)); var multiplyresultvalue = multiplyresults[0].                GetValue ();                Console.WriteLine ("A*b={0}", Multiplyresultvalue); var TFT = new Tftensor (Encoding.UTF8.GetBytes ($ "Hello tensorflow Version {tfcore.version}!                Linezero ")); var hello = graph.                CONST (TFT); var helloresults = SessIon. Getrunner ().                Run (hello); Console.WriteLine (Encoding.UTF8.GetString ((byte[]) helloresults[0].            GetValue ()));        } console.readkey ();         }

The result of running the program is as follows:

TensorFlow C # image recognition

Image recognition Sample Experience

Https://github.com/migueldeicaza/TensorFlowSharp/tree/master/Examples/ExampleInceptionInference

The following is an example of an actual AI application that is very simple, image recognition.

Create a new imagerecognition. NET Framework Console Application project, and then add the Tensorflowsharp reference.

Then create the platform targetin the project properties to change to x64.

Then write the following code:

View Code

It is important to note that there are some special tools that need to be used to download the initial graph and tags, and Google sites.

Eventually I downloaded a few pictures and put them in bin\debug\img.

Then run the program, first make sure that the Bin\debug\tmp folder has TENSORFLOW_INCEPTION_GRAPH.PB and Imagenet_comp_graph_label_strings.txt.

Ai is very attractive, this article is just a primer, copy the above code, you can not train the model and so on. So the road is still very far, need step by step.

More to see HTTPS://GITHUB.COM/MIGUELDEICAZA/TENSORFLOWSHARP and Https://github.com/tensorflow/models

Reference Documentation:

TensorFlow Official website: https://www.tensorflow.org/get_started/

TensorFlow Chinese Community: http://www.tensorfly.cn/

TensorFlow Official Document Chinese version: http://wiki.jikexueyuan.com/project/tensorflow-zh/

C # writing TensorFlow AI applications

Related Article

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.