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.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/ .NET 5. All the examples in this book are using .NET Core 3.1 and Microsoft Visual Studio Community 2019. 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 static Tensorflow.Binding` to introduce the TensorFlow .NET library.
  21. TensorFlow 2.x enabled `Eager Mode` by default. About what eager mode is, I will introduce it in detail in the following chapters.
  22. ```csharp
  23. using System;
  24. using static Tensorflow.Binding;
  25. namespace TensorFlowNET.Examples
  26. {
  27. /// <summary>
  28. /// Simple hello world using TensorFlow
  29. /// </summary>
  30. class Program
  31. {
  32. static void Main(string[] args)
  33. {
  34. var hello = tf.constant("Hello, TensorFlow!");
  35. Console.WriteLine(hello);
  36. }
  37. }
  38. }
  39. ```
  40. After CTRL + F5 run, you will get the output.
  41. ```cmd
  42. 9/20/2020 2:15:09 AM Starting Hello World
  43. tf.Tensor: shape=(), dtype=string, numpy=Hello, TensorFlow.NET!
  44. 9/20/2020 2:15:09 AM Completed Hello World
  45. Example: Hello World in 0.1273463s is OK!
  46. TensorFlow.NET v0.20.1.0
  47. TensorFlow Binary v2.3.0
  48. 1 of 21 example(s) are completed.
  49. Press [Enter] to continue...
  50. ```
  51. This sample code can be found at [here](https://github.com/SciSharp/SciSharp-Stack-Examples/blob/master/src/TensorFlowNET.Examples/HelloWorld.cs).