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.

README.md 9.0 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # TensorFlow.NET
  2. ![logo](docs/assets/tf.net.logo.svg)
  3. TensorFlow.NET (TF.NET) provides a .NET Standard binding for [TensorFlow](https://www.tensorflow.org/). It aims to implement the complete Tensorflow API in CSharp which allows .NET developers to develop, train and deploy Machine Learning models with the cross-platform .NET Standard framework.
  4. [![Join the chat at https://gitter.im/publiclab/publiclab](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/sci-sharp/community)
  5. [![Tensorflow.NET](https://ci.appveyor.com/api/projects/status/wx4td43v2d3f2xj6?svg=true)](https://ci.appveyor.com/project/Haiping-Chen/tensorflow-net)
  6. [![codecov](https://codecov.io/gh/SciSharp/NumSharp/branch/master/graph/badge.svg)](https://codecov.io/gh/SciSharp/NumSharp)
  7. [![NuGet](https://img.shields.io/nuget/dt/TensorFlow.NET.svg)](https://www.nuget.org/packages/TensorFlow.NET)
  8. [![Documentation Status](https://readthedocs.org/projects/tensorflownet/badge/?version=latest)](https://tensorflownet.readthedocs.io/en/latest/?badge=latest)
  9. [![Badge](https://img.shields.io/badge/link-996.icu-red.svg)](https://996.icu/#/en_US)
  10. TF.NET is a member project of [SciSharp STACK](https://github.com/SciSharp).
  11. ![tensors_flowing](docs/assets/tensors_flowing.gif)
  12. ### Why TensorFlow.NET ?
  13. `SciSharp STACK`'s mission is to bring popular data science technology into the .NET world and to provide .NET developers with a powerful Machine Learning tool set without reinventing the wheel. Scince the APIs are kept as similar as possible you can immediately adapt any existing Tensorflow code in C# with a zero learning curve. Take a look at a comparison picture and see how comfortably a Tensorflow/Python script translates into a C# program with TensorFlow.NET.
  14. ![pythn vs csharp](docs/assets/syntax-comparision.png)
  15. SciSharp's philosophy allows a large number of machine learning code written in Python to be quickly migrated to .NET, enabling .NET developers to use cutting edge machine learning models and access a vast number of Tensorflow resources which would not be possible without this project.
  16. In comparison to other projects, like for instance TensorFlowSharp which only provide Tensorflow's low-level C++ API and can only run models that were built using Python, Tensorflow.NET also implements Tensorflow's high level API where all the magic happens. This computation graph building layer is still under active development. Once it is completely implemented you can build new Machine Learning models in C#.
  17. ### How to use
  18. Install TF.NET and TensorFlow binary through NuGet.
  19. ```sh
  20. PM> Install-Package TensorFlow.NET
  21. PM> Install-Package SciSharp.TensorFlow.Redist
  22. ```
  23. Import TF.NET.
  24. ```cs
  25. using Tensorflow;
  26. ```
  27. Add two constants:
  28. ```cs
  29. // Create a Constant op
  30. var a = tf.constant(4.0f);
  31. var b = tf.constant(5.0f);
  32. var c = tf.add(a, b);
  33. using (var sess = tf.Session())
  34. {
  35. var o = sess.run(c);
  36. }
  37. ```
  38. Feed placeholder:
  39. ```cs
  40. // Create a placeholder op
  41. var a = tf.placeholder(tf.float32);
  42. var b = tf.placeholder(tf.float32);
  43. var c = tf.add(a, b);
  44. using(var sess = tf.Session())
  45. {
  46. var o = sess.run(c, new FeedItem(a, 3.0f), new FeedItem(b, 2.0f));
  47. }
  48. ```
  49. Linear Regression:
  50. ```c#
  51. // We can set a fixed init value in order to debug
  52. var W = tf.Variable(-0.06f, name: "weight");
  53. var b = tf.Variable(-0.73f, name: "bias");
  54. // Construct a linear model
  55. var pred = tf.add(tf.multiply(X, W), b);
  56. // Mean squared error
  57. var cost = tf.reduce_sum(tf.pow(pred - Y, 2.0f)) / (2.0f * n_samples);
  58. // Gradient descent
  59. // Note, minimize() knows to modify W and b because Variable objects are trainable=True by default
  60. var optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost);
  61. // Initialize the variables (i.e. assign their default value)
  62. var init = tf.global_variables_initializer();
  63. // Start training
  64. with(tf.Session(), sess =>
  65. {
  66. // Run the initializer
  67. sess.run(init);
  68. // Fit all training data
  69. for (int epoch = 0; epoch < training_epochs; epoch++)
  70. {
  71. foreach (var (x, y) in zip<float>(train_X, train_Y))
  72. sess.run(optimizer, new FeedItem(X, x), new FeedItem(Y, y));
  73. // Display logs per epoch step
  74. if ((epoch + 1) % display_step == 0)
  75. {
  76. var c = sess.run(cost, new FeedItem(X, train_X), new FeedItem(Y, train_Y));
  77. Console.WriteLine($"Epoch: {epoch + 1} cost={c} " + $"W={sess.run(W)} b={sess.run(b)}");
  78. }
  79. Console.WriteLine("Optimization Finished!");
  80. var training_cost = sess.run(cost, new FeedItem(X, train_X), new FeedItem(Y, train_Y));
  81. Console.WriteLine($"Training cost={training_cost} W={sess.run(W)} b={sess.run(b)}");
  82. // Testing example
  83. var test_X = np.array(6.83f, 4.668f, 8.9f, 7.91f, 5.7f, 8.7f, 3.1f, 2.1f);
  84. var test_Y = np.array(1.84f, 2.273f, 3.2f, 2.831f, 2.92f, 3.24f, 1.35f, 1.03f);
  85. Console.WriteLine("Testing... (Mean square loss Comparison)");
  86. var testing_cost = sess.run(tf.reduce_sum(tf.pow(pred - Y, 2.0f)) / (2.0f * test_X.shape[0]), new FeedItem(X, test_X), new FeedItem(Y, test_Y));
  87. Console.WriteLine($"Testing cost={testing_cost}");
  88. var diff = Math.Abs((float)training_cost - (float)testing_cost);
  89. Console.WriteLine($"Absolute mean square loss difference: {diff}");
  90. }
  91. });
  92. ```
  93. Run this example in [Jupyter Notebook](https://github.com/SciSharp/SciSharpCube).
  94. Read the docs & book [The Definitive Guide to Tensorflow.NET](https://tensorflownet.readthedocs.io/en/latest/FrontCover.html).
  95. ### More examples:
  96. Run specific example in shell:
  97. ```cs
  98. dotnet TensorFlowNET.Examples.dll -ex "MNIST CNN"
  99. ```
  100. Example runner will download all the required files like training data and model pb files.
  101. * [Hello World](test/TensorFlowNET.Examples/HelloWorld.cs)
  102. * [Basic Operations](test/TensorFlowNET.Examples/BasicOperations.cs)
  103. * [Linear Regression](test/TensorFlowNET.Examples/BasicModels/LinearRegression.cs)
  104. * [Logistic Regression](test/TensorFlowNET.Examples/BasicModels/LogisticRegression.cs)
  105. * [Nearest Neighbor](test/TensorFlowNET.Examples/BasicModels/NearestNeighbor.cs)
  106. * [Naive Bayes Classification](test/TensorFlowNET.Examples/BasicModels/NaiveBayesClassifier.cs)
  107. * [Full Connected Neural Network](test/TensorFlowNET.Examples/ImageProcess/DigitRecognitionNN.cs)
  108. * [Image Processing](test/TensorFlowNET.Examples/ImageProcessing)
  109. * [K-means Clustering](test/TensorFlowNET.Examples/BasicModels/KMeansClustering.cs)
  110. * [NN XOR](test/TensorFlowNET.Examples/BasicModels/NeuralNetXor.cs)
  111. * [Object Detection](test/TensorFlowNET.Examples/ImageProcessing/ObjectDetection.cs)
  112. * [Text Classification](test/TensorFlowNET.Examples/TextProcessing/BinaryTextClassification.cs)
  113. * [CNN Text Classification](test/TensorFlowNET.Examples/TextProcessing/cnn_models/VdCnn.cs)
  114. * [MNIST CNN](test/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionCNN.cs)
  115. * [Named Entity Recognition](test/TensorFlowNET.Examples/TextProcessing/NER)
  116. * [Transfer Learning for Image Classification in InceptionV3](test/TensorFlowNET.Examples/ImageProcessing/RetrainImageClassifier.cs)
  117. More troubleshooting of running example refer [here](tensorflowlib/README.md).
  118. ### Contribute:
  119. Feel like contributing to one of the hottest projects in the Machine Learning field? Want to know how Tensorflow magically creates the computational graph? We appreciate every contribution however small. There are tasks for novices to experts alike, if everyone tackles only a small task the sum of contributions will be huge.
  120. You can:
  121. * Let everyone know about this project
  122. * Port Tensorflow unit tests from Python to C#
  123. * Port missing Tensorflow code from Python to C#
  124. * Port Tensorflow examples to C# and raise issues if you come accross missing parts of the API
  125. * Debug one of the unit tests that is marked as Ignored to get it to work
  126. * Debug one of the not yet working examples and get it to work
  127. ### How to debug unit tests:
  128. The best way to find out why a unit test is failing is to single step it in C# and its pendant Python at the same time to see where the flow of execution digresses or where variables exhibit different values. Good Python IDEs like PyCharm let you single step into the tensorflow library code.
  129. ### Git Knowhow for Contributors
  130. Add SciSharp/TensorFlow.NET as upstream to your local repo ...
  131. ```git
  132. git remote add upstream git@github.com:SciSharp/TensorFlow.NET.git
  133. ```
  134. Please make sure you keep your fork up to date by regularly pulling from upstream.
  135. ```git
  136. git pull upstream master
  137. ```
  138. ### Contact
  139. Feel free to star or raise issue on [Github](https://github.com/SciSharp/TensorFlow.NET).
  140. Follow us on [Medium](https://medium.com/scisharp).
  141. Join our chat on [Gitter](https://gitter.im/sci-sharp/community).
  142. Scan QR code to join Tencent TIM group:
  143. ![SciSharp STACK](docs/TIM.jpg)
  144. ![SciSharp](https://avatars3.githubusercontent.com/u/44989469) TensorFlow.NET is a part of [SciSharp STACK](https://scisharp.github.io/SciSharp/)