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.

BasicOperations.cs 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using NumSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using Tensorflow;
  6. using static Tensorflow.Python;
  7. namespace TensorFlowNET.Examples
  8. {
  9. /// <summary>
  10. /// Basic Operations example using TensorFlow library.
  11. /// https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/1_Introduction/basic_operations.py
  12. /// </summary>
  13. public class BasicOperations : IExample
  14. {
  15. public bool Enabled { get; set; } = true;
  16. public int Priority => 2;
  17. public string Name => "Basic Operations";
  18. public bool ImportGraph { get; set; } = false;
  19. private Session sess;
  20. public bool Run()
  21. {
  22. // Basic constant operations
  23. // The value returned by the constructor represents the output
  24. // of the Constant op.
  25. var a = tf.constant(2);
  26. var b = tf.constant(3);
  27. // Launch the default graph.
  28. using (sess = tf.Session())
  29. {
  30. Console.WriteLine("a=2, b=3");
  31. Console.WriteLine($"Addition with constants: {sess.run(a + b)}");
  32. Console.WriteLine($"Multiplication with constants: {sess.run(a * b)}");
  33. }
  34. // Basic Operations with variable as graph input
  35. // The value returned by the constructor represents the output
  36. // of the Variable op. (define as input when running session)
  37. // tf Graph input
  38. a = tf.placeholder(tf.int16);
  39. b = tf.placeholder(tf.int16);
  40. // Define some operations
  41. var add = tf.add(a, b);
  42. var mul = tf.multiply(a, b);
  43. // Launch the default graph.
  44. using(sess = tf.Session())
  45. {
  46. var feed_dict = new FeedItem[]
  47. {
  48. new FeedItem(a, (short)2),
  49. new FeedItem(b, (short)3)
  50. };
  51. // Run every operation with variable input
  52. Console.WriteLine($"Addition with variables: {sess.run(add, feed_dict)}");
  53. Console.WriteLine($"Multiplication with variables: {sess.run(mul, feed_dict)}");
  54. }
  55. // ----------------
  56. // More in details:
  57. // Matrix Multiplication from TensorFlow official tutorial
  58. // Create a Constant op that produces a 1x2 matrix. The op is
  59. // added as a node to the default graph.
  60. //
  61. // The value returned by the constructor represents the output
  62. // of the Constant op.
  63. var nd1 = np.array(3, 3).reshape(1, 2);
  64. var matrix1 = tf.constant(nd1);
  65. // Create another Constant that produces a 2x1 matrix.
  66. var nd2 = np.array(2, 2).reshape(2, 1);
  67. var matrix2 = tf.constant(nd2);
  68. // Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs.
  69. // The returned value, 'product', represents the result of the matrix
  70. // multiplication.
  71. var product = tf.matmul(matrix1, matrix2);
  72. // To run the matmul op we call the session 'run()' method, passing 'product'
  73. // which represents the output of the matmul op. This indicates to the call
  74. // that we want to get the output of the matmul op back.
  75. //
  76. // All inputs needed by the op are run automatically by the session. They
  77. // typically are run in parallel.
  78. //
  79. // The call 'run(product)' thus causes the execution of threes ops in the
  80. // graph: the two constants and matmul.
  81. //
  82. // The output of the op is returned in 'result' as a numpy `ndarray` object.
  83. return with(tf.Session(), sess =>
  84. {
  85. var result = sess.run(product);
  86. Console.WriteLine(result.ToString()); // ==> [[ 12.]]
  87. return result.Data<int>()[0] == 12;
  88. });
  89. }
  90. public void PrepareData()
  91. {
  92. }
  93. }
  94. }

tensorflow框架的.NET版本,提供了丰富的特性和API,可以借此很方便地在.NET平台下搭建深度学习训练与推理流程。