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 7.6 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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).
  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. Since 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. | TensorFlow | tf 1.13 | tf 1.14 | tf 1.15 | tf 2.0 |
  18. | ----------- | ------- | ------- | ------- | ------ |
  19. | tf.net 0.12 | | x | | |
  20. | tf.net 0.11 | x | x | | |
  21. | tf.net 0.10 | x | x | | |
  22. | tf.net 0.9 | x | | | |
  23. Install TF.NET and TensorFlow binary through NuGet.
  24. ```sh
  25. ### install tensorflow C# binding
  26. PM> Install-Package TensorFlow.NET
  27. ### Install tensorflow binary
  28. ### For CPU version
  29. PM> Install-Package SciSharp.TensorFlow.Redist
  30. ### For GPU version (CUDA and cuDNN are required)
  31. PM> Install-Package SciSharp.TensorFlow.Redist-Windows-GPU
  32. ```
  33. Import TF.NET in your project.
  34. ```cs
  35. using static Tensorflow.Binding;
  36. ```
  37. Linear Regression:
  38. ```c#
  39. // We can set a fixed init value in order to debug
  40. var W = tf.Variable(-0.06f, name: "weight");
  41. var b = tf.Variable(-0.73f, name: "bias");
  42. // Construct a linear model
  43. var pred = tf.add(tf.multiply(X, W), b);
  44. // Mean squared error
  45. var cost = tf.reduce_sum(tf.pow(pred - Y, 2.0f)) / (2.0f * n_samples);
  46. // Gradient descent
  47. // Note, minimize() knows to modify W and b because Variable objects are trainable=True by default
  48. var optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost);
  49. // Initialize the variables (i.e. assign their default value)
  50. var init = tf.global_variables_initializer();
  51. // Start training
  52. using(tf.Session())
  53. {
  54. // Run the initializer
  55. sess.run(init);
  56. // Fit all training data
  57. for (int epoch = 0; epoch < training_epochs; epoch++)
  58. {
  59. foreach (var (x, y) in zip<float>(train_X, train_Y))
  60. sess.run(optimizer, (X, x), (Y, y));
  61. // Display logs per epoch step
  62. if ((epoch + 1) % display_step == 0)
  63. {
  64. var c = sess.run(cost, (X, train_X), (Y, train_Y));
  65. Console.WriteLine($"Epoch: {epoch + 1} cost={c} " + $"W={sess.run(W)} b={sess.run(b)}");
  66. }
  67. }
  68. Console.WriteLine("Optimization Finished!");
  69. var training_cost = sess.run(cost, (X, train_X), (Y, train_Y));
  70. Console.WriteLine($"Training cost={training_cost} W={sess.run(W)} b={sess.run(b)}");
  71. // Testing example
  72. var test_X = np.array(6.83f, 4.668f, 8.9f, 7.91f, 5.7f, 8.7f, 3.1f, 2.1f);
  73. var test_Y = np.array(1.84f, 2.273f, 3.2f, 2.831f, 2.92f, 3.24f, 1.35f, 1.03f);
  74. Console.WriteLine("Testing... (Mean square loss Comparison)");
  75. var testing_cost = sess.run(tf.reduce_sum(tf.pow(pred - Y, 2.0f)) / (2.0f * test_X.shape[0]),
  76. (X, test_X), (Y, test_Y));
  77. Console.WriteLine($"Testing cost={testing_cost}");
  78. var diff = Math.Abs((float)training_cost - (float)testing_cost);
  79. Console.WriteLine($"Absolute mean square loss difference: {diff}");
  80. return diff < 0.01;
  81. });
  82. ```
  83. Run this example in [Jupyter Notebook](https://github.com/SciSharp/SciSharpCube).
  84. Read the docs & book [The Definitive Guide to Tensorflow.NET](https://tensorflownet.readthedocs.io/en/latest/FrontCover.html).
  85. There are many examples reside at [TensorFlow.NET Examples](https://github.com/SciSharp/TensorFlow.NET-Examples).
  86. Troubleshooting of running example or installation, please refer [here](tensorflowlib/README.md).
  87. ### Contribute:
  88. 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.
  89. You can:
  90. * Let everyone know about this project
  91. * Port Tensorflow unit tests from Python to C#
  92. * Port missing Tensorflow code from Python to C#
  93. * Port Tensorflow examples to C# and raise issues if you come accross missing parts of the API
  94. * Debug one of the unit tests that is marked as Ignored to get it to work
  95. * Debug one of the not yet working examples and get it to work
  96. ### How to debug unit tests:
  97. 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.
  98. ### Git Knowhow for Contributors
  99. Add SciSharp/TensorFlow.NET as upstream to your local repo ...
  100. ```git
  101. git remote add upstream git@github.com:SciSharp/TensorFlow.NET.git
  102. ```
  103. Please make sure you keep your fork up to date by regularly pulling from upstream.
  104. ```git
  105. git pull upstream master
  106. ```
  107. ### Contact
  108. Feel free to star or raise issue on [Github](https://github.com/SciSharp/TensorFlow.NET).
  109. Follow us on [Medium](https://medium.com/scisharp).
  110. Join our chat on [Gitter](https://gitter.im/sci-sharp/community).
  111. Scan QR code to join Tencent TIM group:
  112. ![SciSharp STACK](docs/TIM.jpg)
  113. TensorFlow.NET is a part of [SciSharp STACK](https://scisharp.github.io/SciSharp/)
  114. <br>
  115. <a href="http://scisharpstack.org"><img src="https://github.com/SciSharp/SciSharp/blob/master/art/scisharp-stack.png" width="391" height="100" /></a>