当前位置:   article > 正文

TensorFlow.NET机器学习环境搭建(1)C#_在c#上部署tensorflow模型

在c#上部署tensorflow模型

测试环境:

vistual studio 2019

win10  64位

.net framework 4.7.2

安装参考:TensorFlow.NET

测试程序参考:TensorFlow.NET

1  在包管理工具中安装:Install-Package TensorFlow.NET

本次测试安装的版本是:0.70.2

 

2  在包管理中安装:Install-Package TensorFlow.Keras

本次测试安装的版本是:

 

3  在包管理中安装:Install-Package SciSharp.TensorFlow.Redist

本次测试安装的版本是:2.10.1.0

 

4  选择项目生成的目标平台,选Any CPU好像不行,根据window平台选择x64或者x86

 

5  输入测试代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Tensorflow;
  7. using Tensorflow.NumPy;
  8. using static Tensorflow.Binding;
  9. using static Tensorflow.KerasApi;
  10. namespace TFDemo
  11. {
  12. class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16. // Parameters
  17. var training_steps = 1000;
  18. var learning_rate = 0.01f;
  19. var display_step = 100;
  20. // Sample data
  21. var X = np.array(3.3f, 4.4f, 5.5f, 6.71f, 6.93f, 4.168f, 9.779f, 6.182f, 7.59f, 2.167f,
  22. 7.042f, 10.791f, 5.313f, 7.997f, 5.654f, 9.27f, 3.1f);
  23. var Y = np.array(1.7f, 2.76f, 2.09f, 3.19f, 1.694f, 1.573f, 3.366f, 2.596f, 2.53f, 1.221f,
  24. 2.827f, 3.465f, 1.65f, 2.904f, 2.42f, 2.94f, 1.3f);
  25. var n_samples = X.shape[0];
  26. // We can set a fixed init value in order to demo
  27. var W = Tensorflow.Binding.tf.Variable(-0.06f, name: "weight");
  28. var b = Tensorflow.Binding.tf.Variable(-0.73f, name: "bias");
  29. var optimizer = Tensorflow.KerasApi.keras.optimizers.SGD(learning_rate);
  30. // Run training for the given number of steps.
  31. foreach (var step in Tensorflow.Binding.range(1, training_steps + 1))
  32. {
  33. // Run the optimization to update W and b values.
  34. // Wrap computation inside a GradientTape for automatic differentiation.
  35. var g = Tensorflow.Binding.tf.GradientTape();
  36. // Linear regression (Wx + b).
  37. var pred = W * X + b;
  38. // Mean square error.
  39. var loss = Tensorflow.Binding.tf.reduce_sum(Tensorflow.Binding.tf.pow(pred - Y, 2)) / (2 * n_samples);
  40. // should stop recording
  41. // Compute gradients.
  42. var gradients = g.gradient(loss, (W, b));
  43. // Update W and b following gradients.
  44. optimizer.apply_gradients(Tensorflow.Binding.zip(gradients, (W, b)));
  45. if (step % display_step == 0)
  46. {
  47. pred = W * X + b;
  48. loss = Tensorflow.Binding.tf.reduce_sum(Tensorflow.Binding.tf.pow(pred - Y, 2)) / (2 * n_samples);
  49. Console.WriteLine($"step: {step}, loss: {loss.numpy()}, W: {W.numpy()}, b: {b.numpy()}");
  50. }
  51. }
  52. Console.ReadLine();
  53. }
  54. }
  55. }

如果没有报错的话,运行结果如下:

 

当我把生成的dll拷贝到另外一台电脑运行时,很不幸,报了如下的错误:

未经处理的异常:  System.DllNotFoundException: 无法加载 DLL“tensorflow”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。
   在 Tensorflow.c_api.TF_AllocateTensor(TF_DataType dtype, Int64[] dims, Int32 num_dims, UInt64 len)
   在 Tensorflow.c_api.TF_NewTensor(Shape shape, TF_DataType dtype, Void* data)
   在 Tensorflow.Tensor.InitTensor(Array array, Shape shape)
   在 Tensorflow.NumPy.np.array[T](T[] data)
   在 TFDemo.Program.Main(String[] args) 位置 E:\VSProject\TFDemo\Program.cs:行号 26

查阅stack overflow,找到了相应的解决方案:原来是要安装:vc_redist.x64.exe

下载链接:https://download.visualstudio.microsoft.com/download/pr/89a3b9df-4a09-492e-8474-8f92c115c51d/B1A32C71A6B7D5978904FB223763263EA5A7EB23B2C44A0D60E90D234AD99178/VC_redist.x64.exe 

stack overflow原链接如下:c# - Unable to load DLL 'tensorflow' or one of its dependencies (ML.NET) - Stack Overflow

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/779549
推荐阅读
相关标签
  

闽ICP备14008679号