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 1.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Tensorflow;
  5. namespace TensorFlowNET.Examples
  6. {
  7. /// <summary>
  8. /// Basic Operations example using TensorFlow library.
  9. /// https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/1_Introduction/basic_operations.py
  10. /// </summary>
  11. public class BasicOperations : IExample
  12. {
  13. private Session sess;
  14. public void Run()
  15. {
  16. // Basic constant operations
  17. // The value returned by the constructor represents the output
  18. // of the Constant op.
  19. var a = tf.constant(2);
  20. var b = tf.constant(3);
  21. // Launch the default graph.
  22. using (sess = tf.Session())
  23. {
  24. Console.WriteLine("a=2, b=3");
  25. Console.WriteLine($"Addition with constants: {sess.run(a + b)}");
  26. Console.WriteLine($"Multiplication with constants: {sess.run(a * b)}");
  27. }
  28. // Basic Operations with variable as graph input
  29. // The value returned by the constructor represents the output
  30. // of the Variable op. (define as input when running session)
  31. // tf Graph input
  32. a = tf.placeholder(tf.int16);
  33. b = tf.placeholder(tf.int16);
  34. // Define some operations
  35. var add = tf.add(a, b);
  36. var mul = tf.multiply(a, b);
  37. // Launch the default graph.
  38. using(sess = tf.Session())
  39. {
  40. // var feed_dict = new Dictionary<string, >
  41. // Run every operation with variable input
  42. // Console.WriteLine($"Addition with variables: {sess.run(add, feed_dict: {a: 2, b: 3})}");
  43. // Console.WriteLine($"Multiplication with variables: {}");
  44. }
  45. }
  46. }
  47. }

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