You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

HelloWorld.md 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Get started with TensorFlow.NET
  2. I would describe TensorFlow as an open source machine learning framework developed by Google which can be used to build neural networks and perform a variety of machine learning tasks. it works on data flow graph where nodes are the mathematical operations and the edges are the data in the form of tensor, hence the name Tensor-Flow.
  3. Let's run a classic HelloWorld program first and see if TensorFlow is running on .NET. I can't think of a simpler way to be a HelloWorld.
  4. ### Install the TensorFlow.NET SDK
  5. TensorFlow.NET uses the .NET Standard 2.0 standard, so your new project Target Framework can be .NET Framework or .NET Core. All the examples in this book are using .NET Core 2.2 and Microsoft Visual Studio Community 2017. To start building TensorFlow program you just need to download and install the .NET SDK (Software Development Kit). You have to download the latest .NET Core SDK from offical website: https://dotnet.microsoft.com/download.
  6. 1. New a project
  7. ![New Project](_static/new-project.png)
  8. 2. Choose Console App (.NET Core)
  9. ![Console App](_static/new-project-console.png)
  10. ```cmd
  11. ### install tensorflow C# binding
  12. PM> Install-Package TensorFlow.NET
  13. ### Install tensorflow binary
  14. ### For CPU version
  15. PM> Install-Package SciSharp.TensorFlow.Redist
  16. ### For GPU version (CUDA and cuDNN are required)
  17. PM> Install-Package SciSharp.TensorFlow.Redist-Windows-GPU
  18. ```
  19. ### Start coding Hello World
  20. After installing the TensorFlow.NET package, you can use the `using Tensorflow` to introduce the TensorFlow library.
  21. ```csharp
  22. using System;
  23. using static Tensorflow.Binding;
  24. namespace TensorFlowNET.Examples
  25. {
  26. /// <summary>
  27. /// Simple hello world using TensorFlow
  28. /// </summary>
  29. public class HelloWorld : IExample
  30. {
  31. public void Run()
  32. {
  33. /* Create a Constant op
  34. The op is added as a node to the default graph.
  35. The value returned by the constructor represents the output
  36. of the Constant op. */
  37. var hello = tf.constant("Hello, TensorFlow!");
  38. // Start tf session
  39. using (var sess = tf.Session())
  40. {
  41. // Run the op
  42. var result = sess.run(hello);
  43. Console.WriteLine(result);
  44. }
  45. }
  46. }
  47. }
  48. ```
  49. After CTRL + F5 run, you will get the output.
  50. ```cmd
  51. 2019-01-05 10:53:42.145931: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
  52. Hello, TensorFlow!
  53. Press any key to continue . . .
  54. ```
  55. This sample code can be found at [here](https://github.com/SciSharp/SciSharp-Stack-Examples/blob/master/src/TensorFlowNET.Examples/HelloWorld.cs).