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.0 kB

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

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