| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9458e83 commit 89c68cd
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,12 +9,14 @@ | |||
| 9 | 9 | [](https://996.icu/#/en_US) | |
| 10 | 10 | [](https://mybinder.org/v2/gh/javiercp/BinderTF.NET/master?urlpath=lab) | |
| 11 | 11 | ||
| 12 | + English | [中文](docs/Readme-CN.md) | ||
| 13 | + | ||
| 12 | 14 | *master branch is based on tensorflow v2.x, v0.6x branch is based on tensorflow v2.6, v0.15-tensorflow1.15 is from tensorflow1.15.* | |
| 13 | 15 | ||
| 14 | 16 | ||
| 15 | 17 |  | |
| 16 | 18 | ||
| 17 | - ### Why TensorFlow in C# and F# ? | ||
| 19 | + ### Why TensorFlow.NET ? | ||
| 18 | 20 | ||
| 19 | 21 | `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# or F# 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. | |
| 20 | 22 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,215 @@ | |||
| 1 | +  | ||
| 2 | + | ||
| 3 | + **Tensorflow.NET**是AI框架[TensorFlow](https://www.tensorflow.org/)在.NET平台上的实现,支持C#和F#,可以用来搭建深度学习模型并进行训练和推理,并内置了Numpy API,可以用来进行其它科学计算。 | ||
| 4 | + | ||
| 5 | + Tensorflow.NET并非对于Python的简单封装,而是基于C API的pure C#实现,因此使用时无需额外的环境,可以很方便地用NuGet直接安装使用。并且dotnet团队提供的[ML.NET](https://github.com/dotnet/machinelearning)也依赖于Tensorflow.NET,支持调用Tensorflow.NET进行训练和推理,可以很方便地融入.NET生态。 | ||
| 6 | + | ||
| 7 | + 与tensorflow相同,Tensorflow.NET也内置了Keras这一高级API,只要在安装Tensorflow.NET的同时安装Tensorflow.Keras就可以使用,Keras支持以模块化的方式调用模型,给模型的搭建提供了极大的便利。 | ||
| 8 | + | ||
| 9 | + [](https://gitter.im/sci-sharp/community) | ||
| 10 | + [](https://ci.appveyor.com/project/Haiping-Chen/tensorflow-net) | ||
| 11 | + [](https://www.nuget.org/packages/TensorFlow.NET) | ||
| 12 | + [](https://tensorflownet.readthedocs.io/en/latest/?badge=latest) | ||
| 13 | + [](https://996.icu/#/en_US) | ||
| 14 | + [](https://mybinder.org/v2/gh/javiercp/BinderTF.NET/master?urlpath=lab) | ||
| 15 | + | ||
| 16 | + 中文 | [English](https://github.com/SciSharp/TensorFlow.NET#readme) | ||
| 17 | + | ||
| 18 | + *当前主分支与Tensorflow2.10版本相对应,支持Eager Mode,同时也支持v1的静态图。* | ||
| 19 | + | ||
| 20 | + | ||
| 21 | +  | ||
| 22 | + | ||
| 23 | + ### Why TensorFlow.NET? | ||
| 24 | + | ||
| 25 | + `SciSharp STACK`开源社区的目标是构建.NET平台下易用的科学计算库,而Tensorflow.NET就是其中最具代表性的仓库之一。在深度学习领域Python是主流,无论是初学者还是资深开发者,模型的搭建和训练都常常使用Python写就的AI框架,比如tensorflow。但在实际应用深度学习模型的时候,又可能希望用到.NET生态,亦或只是因为.NET是自己最熟悉的领域,这时候Tensorflow.NET就有显著的优点,因为它不仅可以和.NET生态很好地贴合,其API还使得开发者很容易将Python代码迁移过来。下面的对比就是很好的例子,Python代码和C#代码有着高度相似的API,这会使得迁移的时候无需做过多修改。 | ||
| 26 | + | ||
| 27 | +  | ||
| 28 | + | ||
| 29 | + 除了高度相似的API外,Tensorflow.NET与tensorflow也已经打通数据通道,tensorflow训练并保存的模型可以在Tensorflow.NET中直接读取并继续训练或推理,反之Tensorflow.NET保存的模型也可以在tensorflow中读取,这大大方便了模型的训练和部署。 | ||
| 30 | + | ||
| 31 | + 与其它类似的库比如[TensorFlowSharp](https://www.nuget.org/packages/TensorFlowSharp/)相比,Tensorflow.NET的实现更加完全,提供了更多的高级API,使用起来更为方便,更新也更加迅速。 | ||
| 32 | + | ||
| 33 | + | ||
| 34 | + ### 文档 | ||
| 35 | + | ||
| 36 | + 基本介绍与简单用例:[Tensorflow.NET Documents](https://scisharp.github.io/tensorflow-net-docs) | ||
| 37 | + | ||
| 38 | + 详细文档:[The Definitive Guide to Tensorflow.NET](https://tensorflownet.readthedocs.io/en/latest/FrontCover.html) | ||
| 39 | + | ||
| 40 | + 例程:[TensorFlow.NET Examples](https://github.com/SciSharp/TensorFlow.NET-Examples) | ||
| 41 | + | ||
| 42 | + 运行例程常见问题:[Tensorflow.NET FAQ](tensorflowlib/README.md) | ||
| 43 | + | ||
| 44 | + ### 安装与使用 | ||
| 45 | + | ||
| 46 | + 安装可以在NuGet包管理器中搜索包名安装,也可以用下面命令行的方式。 | ||
| 47 | + | ||
| 48 | + 安装分为两个部分,第一部分是Tensorflow.NET的主体: | ||
| 49 | + | ||
| 50 | + ```sh | ||
| 51 | + ### 安装Tensorflow.NET | ||
| 52 | + PM> Install-Package TensorFlow.NET | ||
| 53 | + ### 安装Tensorflow.Keras | ||
| 54 | + PM> Install-Package TensorFlow.Keras | ||
| 55 | + ``` | ||
| 56 | + | ||
| 57 | + 第二部分是计算支持部分,只需要根据自己的设备和系统选择下面之一即可: | ||
| 58 | + | ||
| 59 | + ``` | ||
| 60 | + ### CPU版本 | ||
| 61 | + PM> Install-Package SciSharp.TensorFlow.Redist | ||
| 62 | + | ||
| 63 | + ### Windows下的GPU版本(需要安装CUDA和CUDNN) | ||
| 64 | + PM> Install-Package SciSharp.TensorFlow.Redist-Windows-GPU | ||
| 65 | + | ||
| 66 | + ### Linux下的GPU版本(需要安装CUDA和CUDNN) | ||
| 67 | + PM> Install-Package SciSharp.TensorFlow.Redist-Linux-GPU | ||
| 68 | + ``` | ||
| 69 | + | ||
| 70 | + 下面给出两个简单的例子,更多例子可以在[TensorFlow.NET Examples]中查看。 | ||
| 71 | + | ||
| 72 | + #### 简单例子(使用Eager Mode进行线性回归) | ||
| 73 | + | ||
| 74 | + ```csharp | ||
| 75 | + using static Tensorflow.Binding; | ||
| 76 | + using static Tensorflow.KerasApi; | ||
| 77 | + using Tensorflow; | ||
| 78 | + using Tensorflow.NumPy; | ||
| 79 | + | ||
| 80 | + // Parameters | ||
| 81 | + var training_steps = 1000; | ||
| 82 | + var learning_rate = 0.01f; | ||
| 83 | + var display_step = 100; | ||
| 84 | + | ||
| 85 | + // Sample data | ||
| 86 | + var X = np.array(3.3f, 4.4f, 5.5f, 6.71f, 6.93f, 4.168f, 9.779f, 6.182f, 7.59f, 2.167f, | ||
| 87 | + 7.042f, 10.791f, 5.313f, 7.997f, 5.654f, 9.27f, 3.1f); | ||
| 88 | + var Y = np.array(1.7f, 2.76f, 2.09f, 3.19f, 1.694f, 1.573f, 3.366f, 2.596f, 2.53f, 1.221f, | ||
| 89 | + 2.827f, 3.465f, 1.65f, 2.904f, 2.42f, 2.94f, 1.3f); | ||
| 90 | + var n_samples = X.shape[0]; | ||
| 91 | + | ||
| 92 | + // We can set a fixed init value in order to demo | ||
| 93 | + var W = tf.Variable(-0.06f, name: "weight"); | ||
| 94 | + var b = tf.Variable(-0.73f, name: "bias"); | ||
| 95 | + var optimizer = keras.optimizers.SGD(learning_rate); | ||
| 96 | + | ||
| 97 | + // Run training for the given number of steps. | ||
| 98 | + foreach (var step in range(1, training_steps + 1)) | ||
| 99 | + { | ||
| 100 | + // Run the optimization to update W and b values. | ||
| 101 | + // Wrap computation inside a GradientTape for automatic differentiation. | ||
| 102 | + using var g = tf.GradientTape(); | ||
| 103 | + // Linear regression (Wx + b). | ||
| 104 | + var pred = W * X + b; | ||
| 105 | + // Mean square error. | ||
| 106 | + var loss = tf.reduce_sum(tf.pow(pred - Y, 2)) / (2 * n_samples); | ||
| 107 | + // should stop recording | ||
| 108 | + // Compute gradients. | ||
| 109 | + var gradients = g.gradient(loss, (W, b)); | ||
| 110 | + | ||
| 111 | + // Update W and b following gradients. | ||
| 112 | + optimizer.apply_gradients(zip(gradients, (W, b))); | ||
| 113 | + | ||
| 114 | + if (step % display_step == 0) | ||
| 115 | + { | ||
| 116 | + pred = W * X + b; | ||
| 117 | + loss = tf.reduce_sum(tf.pow(pred - Y, 2)) / (2 * n_samples); | ||
| 118 | + print($"step: {step}, loss: {loss.numpy()}, W: {W.numpy()}, b: {b.numpy()}"); | ||
| 119 | + } | ||
| 120 | + } | ||
| 121 | + ``` | ||
| 122 | + | ||
| 123 | + 这一用例也可以在[Jupyter Notebook Example](https://github.com/SciSharp/SciSharpCube)进行运行. | ||
| 124 | + | ||
| 125 | + #### 简单例子(使用Keras搭建Resnet) | ||
| 126 | + | ||
| 127 | + ```csharp | ||
| 128 | + using static Tensorflow.Binding; | ||
| 129 | + using static Tensorflow.KerasApi; | ||
| 130 | + using Tensorflow; | ||
| 131 | + using Tensorflow.NumPy; | ||
| 132 | + | ||
| 133 | + var layers = new LayersApi(); | ||
| 134 | + // input layer | ||
| 135 | + var inputs = keras.Input(shape: (32, 32, 3), name: "img"); | ||
| 136 | + // convolutional layer | ||
| 137 | + var x = layers.Conv2D(32, 3, activation: "relu").Apply(inputs); | ||
| 138 | + x = layers.Conv2D(64, 3, activation: "relu").Apply(x); | ||
| 139 | + var block_1_output = layers.MaxPooling2D(3).Apply(x); | ||
| 140 | + x = layers.Conv2D(64, 3, activation: "relu", padding: "same").Apply(block_1_output); | ||
| 141 | + x = layers.Conv2D(64, 3, activation: "relu", padding: "same").Apply(x); | ||
| 142 | + var block_2_output = layers.Add().Apply(new Tensors(x, block_1_output)); | ||
| 143 | + x = layers.Conv2D(64, 3, activation: "relu", padding: "same").Apply(block_2_output); | ||
| 144 | + x = layers.Conv2D(64, 3, activation: "relu", padding: "same").Apply(x); | ||
| 145 | + var block_3_output = layers.Add().Apply(new Tensors(x, block_2_output)); | ||
| 146 | + x = layers.Conv2D(64, 3, activation: "relu").Apply(block_3_output); | ||
| 147 | + x = layers.GlobalAveragePooling2D().Apply(x); | ||
| 148 | + x = layers.Dense(256, activation: "relu").Apply(x); | ||
| 149 | + x = layers.Dropout(0.5f).Apply(x); | ||
| 150 | + // output layer | ||
| 151 | + var outputs = layers.Dense(10).Apply(x); | ||
| 152 | + // build keras model | ||
| 153 | + var model = keras.Model(inputs, outputs, name: "toy_resnet"); | ||
| 154 | + model.summary(); | ||
| 155 | + // compile keras model in tensorflow static graph | ||
| 156 | + model.compile(optimizer: keras.optimizers.RMSprop(1e-3f), | ||
| 157 | + loss: keras.losses.CategoricalCrossentropy(from_logits: true), | ||
| 158 | + metrics: new[] { "acc" }); | ||
| 159 | + // prepare dataset | ||
| 160 | + var ((x_train, y_train), (x_test, y_test)) = keras.datasets.cifar10.load_data(); | ||
| 161 | + x_train = x_train / 255.0f; | ||
| 162 | + y_train = np_utils.to_categorical(y_train, 10); | ||
| 163 | + // training | ||
| 164 | + model.fit(x_train[new Slice(0, 2000)], y_train[new Slice(0, 2000)], | ||
| 165 | + batch_size: 64, | ||
| 166 | + epochs: 10, | ||
| 167 | + validation_split: 0.2f); | ||
| 168 | + ``` | ||
| 169 | + | ||
| 170 | + 此外,Tensorflow.NET也支持用F#搭建上述模型进行训练和推理。 | ||
| 171 | + | ||
| 172 | + ### Tensorflow.NET版本对应关系 | ||
| 173 | + | ||
| 174 | + | TensorFlow.NET Versions | tensorflow 1.14, cuda 10.0 | tensorflow 1.15, cuda 10.0 | tensorflow 2.3, cuda 10.1 | tensorflow 2.4, cuda 11 | tensorflow 2.10, cuda 11 | | ||
| 175 | + | -------------------------- | ------------- | -------------- | ------------- | ------------- | ------------ | | ||
| 176 | + | tf.net 0.7+, tf.keras 0.7+ | | | | | x | | ||
| 177 | + | tf.net 0.4x, tf.keras 0.5 | | | | x | | | ||
| 178 | + | tf.net 0.3x, tf.keras 0.4 | | | x | | | | ||
| 179 | + | tf.net 0.2x | | x | x | | | | ||
| 180 | + | tf.net 0.15 | x | x | | | | | ||
| 181 | + | tf.net 0.14 | x | | | | | | ||
| 182 | + | ||
| 183 | + 如果使用过程中发现有缺失的版本,请告知我们,谢谢! | ||
| 184 | + | ||
| 185 | + 请注意Tensorflow.NET与Tensorflow.Keras版本存在一一对应关系,请安装与Tensorflow.NET对应的Tensorflow.Keras版本。 | ||
| 186 | + | ||
| 187 | + ### 参与我们的开发: | ||
| 188 | + | ||
| 189 | + 我们欢迎任何人的任何形式的贡献!无论是文档中的错误纠正,新特性提议,还是BUG修复等等,都会使得Tensorflow.NET项目越来越好,Tensorflow.NET的全体开发者也会积极帮助解决您提出的问题。 | ||
| 190 | + | ||
| 191 | + 下面任何一种形式都可以帮助Tensorflow.NET越来越好: | ||
| 192 | + | ||
| 193 | + * Star和分享Tensorflow.NET项目 | ||
| 194 | + * 为Tensorflow.NET添加更多的用例 | ||
| 195 | + * 在issue中告知我们Tensorflow.NET目前相比tensorflow缺少的API或者没有对齐的特性 | ||
| 196 | + * 在issue中提出Tensorflow.NET存在的BUG或者可以改进的地方 | ||
| 197 | + * 在待办事项清单中选择一个进行或者解决某个issue | ||
| 198 | + * 帮助我们完善文档,这也十分重要 | ||
| 199 | + | ||
| 200 | + | ||
| 201 | + ### 支持我们 | ||
| 202 | + 我们推出了[TensorFlow.NET实战](https://item.jd.com/13441549.html)这本书,包含了Tensorflow.NET主要开发者编写的讲解与实战例程,欢迎您的购买,希望这本书可以给您带来帮助。 | ||
| 203 | + <p float="left"> | ||
| 204 | + <img src="https://user-images.githubusercontent.com/1705364/198852429-91741881-c196-401e-8e9e-2f8656196613.png" width="250" /> | ||
| 205 | + <img src="https://user-images.githubusercontent.com/1705364/198852521-2f842043-3ace-49d2-8533-039c6a043a3f.png" width="260" /> | ||
| 206 | + <img src="https://user-images.githubusercontent.com/1705364/198852721-54cd9e7e-9210-4931-a86c-77584b25b8e1.png" width="260" /> | ||
| 207 | + </p> | ||
| 208 | + | ||
| 209 | + ### 联系我们 | ||
| 210 | + | ||
| 211 | + 可以在 [Twitter](https://twitter.com/ScisharpStack), [Facebook](https://www.facebook.com/scisharp.stack.9), [Medium](https://medium.com/scisharp), [LinkedIn](https://www.linkedin.com/company/scisharp-stack/)中关注我们,也可以在[Gitter](https://gitter.im/sci-sharp/community)中与项目开发者以及其它使用者进行沟通交流,也欢迎在仓库中提起issue。 | ||
| 212 | + | ||
| 213 | + TensorFlow.NET is a part of [SciSharp STACK](https://scisharp.github.io/SciSharp/) | ||
| 214 | + <br> | ||
| 215 | + <a href="http://scisharpstack.org"><img src="https://github.com/SciSharp/SciSharp/blob/master/art/scisharp-stack.png" width="391" height="100" /></a> | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments