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