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

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

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