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.

OperationsTest.cs 1.8 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using Tensorflow;
  7. using Buffer = Tensorflow.Buffer;
  8. namespace TensorFlowNET.UnitTest
  9. {
  10. [TestClass]
  11. public class OperationsTest
  12. {
  13. /// <summary>
  14. /// Port from tensorflow\c\c_api_test.cc
  15. /// `TEST(CAPI, GetAllOpList)`
  16. /// </summary>
  17. [TestMethod]
  18. public void GetAllOpList()
  19. {
  20. var handle = c_api.TF_GetAllOpList();
  21. var buffer = new Buffer(handle);
  22. var op_list = OpList.Parser.ParseFrom(buffer);
  23. var _registered_ops = new Dictionary<string, OpDef>();
  24. foreach (var op_def in op_list.Op)
  25. _registered_ops[op_def.Name] = op_def;
  26. // r1.14 added NN op
  27. var op = _registered_ops.FirstOrDefault(x => x.Key == "NearestNeighbors");
  28. Assert.IsTrue(op_list.Op.Count > 1000);
  29. }
  30. [TestMethod]
  31. public void addInPlaceholder()
  32. {
  33. var a = tf.placeholder(tf.float32);
  34. var b = tf.placeholder(tf.float32);
  35. var c = tf.add(a, b);
  36. using(var sess = tf.Session())
  37. {
  38. var o = sess.run(c,
  39. new FeedItem(a, 3.0f),
  40. new FeedItem(b, 2.0f));
  41. Assert.AreEqual((float)o, 5.0f);
  42. }
  43. }
  44. [TestMethod]
  45. public void addInConstant()
  46. {
  47. var a = tf.constant(4.0f);
  48. var b = tf.constant(5.0f);
  49. var c = tf.add(a, b);
  50. using (var sess = tf.Session())
  51. {
  52. var o = sess.run(c);
  53. Assert.AreEqual((float)o, 9.0f);
  54. }
  55. }
  56. }
  57. }

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