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 8.9 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ![logo](docs/assets/tf.net.logo.png)
  2. **TensorFlow.NET** (TF.NET) provides a .NET Standard binding for [TensorFlow](https://www.tensorflow.org/). It aims to implement the complete Tensorflow API in C# which allows .NET developers to develop, train and deploy Machine Learning models with the cross-platform .NET Standard framework.
  3. [![Join the chat at https://gitter.im/publiclab/publiclab](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/sci-sharp/community)
  4. [![Tensorflow.NET](https://ci.appveyor.com/api/projects/status/wx4td43v2d3f2xj6?svg=true)](https://ci.appveyor.com/project/Haiping-Chen/tensorflow-net)
  5. [![codecov](https://codecov.io/gh/SciSharp/NumSharp/branch/master/graph/badge.svg)](https://codecov.io/gh/SciSharp/NumSharp)
  6. [![NuGet](https://img.shields.io/nuget/dt/TensorFlow.NET.svg)](https://www.nuget.org/packages/TensorFlow.NET)
  7. [![Documentation Status](https://readthedocs.org/projects/tensorflownet/badge/?version=latest)](https://tensorflownet.readthedocs.io/en/latest/?badge=latest)
  8. [![Badge](https://img.shields.io/badge/link-996.icu-red.svg)](https://996.icu/#/en_US)
  9. TF.NET is a member project of [SciSharp STACK](https://github.com/SciSharp). <a href="http://scisharpstack.org"><img src="https://github.com/SciSharp/SciSharp/blob/master/art/scisharp_badge.png" width="200" height="200" align="right" /></a>
  10. ![tensors_flowing](docs/assets/tensors_flowing.gif)
  11. ### Why TensorFlow.NET ?
  12. `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.
  13. ![pythn vs csharp](docs/assets/syntax-comparision.png)
  14. 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.
  15. 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#.
  16. ### How to use
  17. Install TF.NET and TensorFlow binary through NuGet.
  18. ```sh
  19. ### install tensorflow C# binding
  20. PM> Install-Package TensorFlow.NET
  21. ### Install tensorflow binary
  22. ### For CPU version
  23. PM> Install-Package SciSharp.TensorFlow.Redist
  24. ### For GPU version (CUDA and cuDNN are required)
  25. PM> Install-Package SciSharp.TensorFlow.Redist-Windows-GPU
  26. ```
  27. Import TF.NET in your project.
  28. ```cs
  29. using static Tensorflow.Binding;
  30. ```
  31. Linear Regression:
  32. ```c#
  33. // We can set a fixed init value in order to debug
  34. var W = tf.Variable(-0.06f, name: "weight");
  35. var b = tf.Variable(-0.73f, name: "bias");
  36. // Construct a linear model
  37. var pred = tf.add(tf.multiply(X, W), b);
  38. // Mean squared error
  39. var cost = tf.reduce_sum(tf.pow(pred - Y, 2.0f)) / (2.0f * n_samples);
  40. // Gradient descent
  41. // Note, minimize() knows to modify W and b because Variable objects are trainable=True by default
  42. var optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost);
  43. // Initialize the variables (i.e. assign their default value)
  44. var init = tf.global_variables_initializer();
  45. // Start training
  46. using(tf.Session())
  47. {
  48. // Run the initializer
  49. sess.run(init);
  50. // Fit all training data
  51. for (int epoch = 0; epoch < training_epochs; epoch++)
  52. {
  53. foreach (var (x, y) in zip<float>(train_X, train_Y))
  54. sess.run(optimizer, (X, x), (Y, y));
  55. // Display logs per epoch step
  56. if ((epoch + 1) % display_step == 0)
  57. {
  58. var c = sess.run(cost, (X, train_X), (Y, train_Y));
  59. Console.WriteLine($"Epoch: {epoch + 1} cost={c} " + $"W={sess.run(W)} b={sess.run(b)}");
  60. }
  61. }
  62. Console.WriteLine("Optimization Finished!");
  63. var training_cost = sess.run(cost, (X, train_X), (Y, train_Y));
  64. Console.WriteLine($"Training cost={training_cost} W={sess.run(W)} b={sess.run(b)}");
  65. // Testing example
  66. var test_X = np.array(6.83f, 4.668f, 8.9f, 7.91f, 5.7f, 8.7f, 3.1f, 2.1f);
  67. var test_Y = np.array(1.84f, 2.273f, 3.2f, 2.831f, 2.92f, 3.24f, 1.35f, 1.03f);
  68. Console.WriteLine("Testing... (Mean square loss Comparison)");
  69. var testing_cost = sess.run(tf.reduce_sum(tf.pow(pred - Y, 2.0f)) / (2.0f * test_X.shape[0]),
  70. (X, test_X), (Y, test_Y));
  71. Console.WriteLine($"Testing cost={testing_cost}");
  72. var diff = Math.Abs((float)training_cost - (float)testing_cost);
  73. Console.WriteLine($"Absolute mean square loss difference: {diff}");
  74. return diff < 0.01;
  75. });
  76. ```
  77. Run this example in [Jupyter Notebook](https://github.com/SciSharp/SciSharpCube).
  78. Read the docs & book [The Definitive Guide to Tensorflow.NET](https://tensorflownet.readthedocs.io/en/latest/FrontCover.html).
  79. ### More examples:
  80. Run specific example in shell:
  81. ```cs
  82. dotnet TensorFlowNET.Examples.dll -ex "MNIST CNN"
  83. ```
  84. Example runner will download all the required files like training data and model pb files.
  85. * [Hello World](test/TensorFlowNET.Examples/HelloWorld.cs)
  86. * [Basic Operations](test/TensorFlowNET.Examples/BasicOperations.cs)
  87. * [Linear Regression](test/TensorFlowNET.Examples/BasicModels/LinearRegression.cs)
  88. * [Logistic Regression](test/TensorFlowNET.Examples/BasicModels/LogisticRegression.cs)
  89. * [Nearest Neighbor](test/TensorFlowNET.Examples/BasicModels/NearestNeighbor.cs)
  90. * [Naive Bayes Classification](test/TensorFlowNET.Examples/BasicModels/NaiveBayesClassifier.cs)
  91. * [Full Connected Neural Network](test/TensorFlowNET.Examples/ImageProcess/DigitRecognitionNN.cs)
  92. * [Image Processing](test/TensorFlowNET.Examples/ImageProcessing)
  93. * [K-means Clustering](test/TensorFlowNET.Examples/BasicModels/KMeansClustering.cs)
  94. * [NN XOR](test/TensorFlowNET.Examples/BasicModels/NeuralNetXor.cs)
  95. * [Object Detection](test/TensorFlowNET.Examples/ImageProcessing/ObjectDetection.cs)
  96. * [Text Classification](test/TensorFlowNET.Examples/TextProcessing/BinaryTextClassification.cs)
  97. * [CNN Text Classification](test/TensorFlowNET.Examples/TextProcessing/cnn_models/VdCnn.cs)
  98. * [MNIST CNN](test/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionCNN.cs)
  99. * [Named Entity Recognition](test/TensorFlowNET.Examples/TextProcessing/NER)
  100. * [Transfer Learning for Image Classification in InceptionV3](test/TensorFlowNET.Examples/ImageProcessing/RetrainImageClassifier.cs)
  101. More troubleshooting of running example refer [here](tensorflowlib/README.md).
  102. ### Contribute:
  103. 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.
  104. You can:
  105. * Let everyone know about this project
  106. * Port Tensorflow unit tests from Python to C#
  107. * Port missing Tensorflow code from Python to C#
  108. * Port Tensorflow examples to C# and raise issues if you come accross missing parts of the API
  109. * Debug one of the unit tests that is marked as Ignored to get it to work
  110. * Debug one of the not yet working examples and get it to work
  111. ### How to debug unit tests:
  112. 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.
  113. ### Git Knowhow for Contributors
  114. Add SciSharp/TensorFlow.NET as upstream to your local repo ...
  115. ```git
  116. git remote add upstream git@github.com:SciSharp/TensorFlow.NET.git
  117. ```
  118. Please make sure you keep your fork up to date by regularly pulling from upstream.
  119. ```git
  120. git pull upstream master
  121. ```
  122. ### Contact
  123. Feel free to star or raise issue on [Github](https://github.com/SciSharp/TensorFlow.NET).
  124. Follow us on [Medium](https://medium.com/scisharp).
  125. Join our chat on [Gitter](https://gitter.im/sci-sharp/community).
  126. Scan QR code to join Tencent TIM group:
  127. ![SciSharp STACK](docs/TIM.jpg)
  128. TensorFlow.NET is a part of [SciSharp STACK](https://scisharp.github.io/SciSharp/)
  129. <br>
  130. <a href="http://scisharpstack.org"><img src="https://github.com/SciSharp/SciSharp/blob/master/art/scisharp-stack.png" width="391" height="100" /></a>