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.

BasicEagerApi.cs 1.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Tensorflow;
  5. namespace TensorFlowNET.Examples
  6. {
  7. /// <summary>
  8. /// Basic introduction to TensorFlow's Eager API.
  9. /// https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/1_Introduction/basic_eager_api.py
  10. /// </summary>
  11. public class BasicEagerApi : IExample
  12. {
  13. public int Priority => 100;
  14. public bool Enabled { get; set; } = false;
  15. public string Name => "Basic Eager";
  16. public bool ImportGraph { get; set; } = false;
  17. private Tensor a, b, c, d;
  18. public bool Run()
  19. {
  20. // Set Eager API
  21. Console.WriteLine("Setting Eager mode...");
  22. tf.enable_eager_execution();
  23. // Define constant tensors
  24. Console.WriteLine("Define constant tensors");
  25. a = tf.constant(2);
  26. Console.WriteLine($"a = {a}");
  27. b = tf.constant(3);
  28. Console.WriteLine($"b = {b}");
  29. // Run the operation without the need for tf.Session
  30. Console.WriteLine("Running operations, without tf.Session");
  31. c = a + b;
  32. Console.WriteLine($"a + b = {c}");
  33. d = a * b;
  34. Console.WriteLine($"a * b = {d}");
  35. // Full compatibility with Numpy
  36. return true;
  37. }
  38. public void PrepareData()
  39. {
  40. }
  41. }
  42. }

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