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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Get started with TensorFlow.NET
  2. 让我们先运行一个经典的HelloWorld程序,看看TensorFlow在.NET上面运行的效果,我想不出有比做个HelloWorld更简单的方式了。
  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. TensorFlow.NET采用.NET标准库2.0版本,因此你的新建工程可以是.NET Framework或者是基于.NET Core的。本文中的所有例子都是用的.NET Core 2.2的,IDE用的是Microsoft Visual Studio Community 2017。为了能编译和运行TensorFlow工程,你需要从这里下载最新的.NET Core SDK: https://dotnet.microsoft.com/download。
  7. 1. New a project
  8. ![New Project](_static/new-project.png)
  9. 2. Choose Console App (.NET Core)
  10. ![Console App](_static/new-project-console.png)
  11. ```cmd
  12. PM> Install-Package TensorFlow.NET
  13. ```
  14. ### Start coding Hello World 开始编写Hello World
  15. After installing the TensorFlow.NET package, you can use the `using Tensorflow` to introduce the TensorFlow library.
  16. 安装完TensorFlow.NET包后,你就可以使用`using Tensorflow`来引入TensorFlow库了。
  17. ```csharp
  18. using System;
  19. using Tensorflow;
  20. namespace TensorFlowNET.Examples
  21. {
  22. /// <summary>
  23. /// Simple hello world using TensorFlow
  24. /// </summary>
  25. public class HelloWorld : IExample
  26. {
  27. public void Run()
  28. {
  29. /* Create a Constant op
  30. The op is added as a node to the default graph.
  31. The value returned by the constructor represents the output
  32. of the Constant op. */
  33. var hello = tf.constant("Hello, TensorFlow!");
  34. // Start tf session
  35. using (var sess = tf.Session())
  36. {
  37. // Run the op
  38. var result = sess.run(hello);
  39. Console.WriteLine(result);
  40. }
  41. }
  42. }
  43. }
  44. ```
  45. After CTRL + F5 run, you will get the output.
  46. ```cmd
  47. 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
  48. Hello, TensorFlow!
  49. Press any key to continue . . .
  50. ```

tensorflow框架的.NET版本,提供了丰富的特性和API,可以借此很方便地在.NET平台下搭建深度学习训练与推理流程。