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

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using NumSharp;
  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. [TestMethod]
  57. public void addOpTests()
  58. {
  59. const int rows = 2; // to avoid broadcasting effect
  60. const int cols = 10;
  61. #region intTest
  62. const int firstIntVal = 2;
  63. const int secondIntVal = 3;
  64. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  65. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  66. var intResult = firstIntFeed.Sum() + secondIntFeed.Sum();
  67. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  68. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  69. var c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
  70. using (var sess = tf.Session())
  71. {
  72. var o = sess.run(c,
  73. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  74. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  75. Assert.AreEqual((int)o, intResult);
  76. }
  77. // Testing `operator +(Tensor x, Tensor y)`
  78. c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
  79. using (var sess = tf.Session())
  80. {
  81. var o = sess.run(c,
  82. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  83. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  84. Assert.AreEqual((int)o, intResult);
  85. }
  86. // Testing `operator +(Tensor x, int y)`
  87. c = tf.reduce_sum(tf.reduce_sum(a + secondIntVal, 1));
  88. using (var sess = tf.Session())
  89. {
  90. var o = sess.run(c,
  91. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  92. Assert.AreEqual((int)o, intResult);
  93. }
  94. // Testing `operator +(int x, Tensor y)`
  95. c = tf.reduce_sum(tf.reduce_sum(secondIntVal + a, 1));
  96. using (var sess = tf.Session())
  97. {
  98. var o = sess.run(c,
  99. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  100. Assert.AreEqual((int)o, intResult);
  101. }
  102. #endregion
  103. #region floatTest
  104. const float firstFloatVal = 2.0f;
  105. const float secondFloatVal = 3.0f;
  106. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  107. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  108. var floatResult = firstFloatFeed.Sum() + secondFloatFeed.Sum();
  109. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  110. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  111. c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
  112. using (var sess = tf.Session())
  113. {
  114. var o = sess.run(c,
  115. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  116. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  117. Assert.AreEqual((float)o, floatResult);
  118. }
  119. // Testing `operator +(Tensor x, Tensor y)
  120. c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
  121. using (var sess = tf.Session())
  122. {
  123. var o = sess.run(c,
  124. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  125. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  126. Assert.AreEqual((float)o, floatResult);
  127. }
  128. // Testing `operator +(Tensor x, float y)
  129. c = tf.reduce_sum(tf.reduce_sum(a + secondFloatVal, 1));
  130. using (var sess = tf.Session())
  131. {
  132. var o = sess.run(c,
  133. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  134. Assert.AreEqual((float)o, floatResult);
  135. }
  136. // Testing `operator +(float x, Tensor y)
  137. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal + a, 1));
  138. using (var sess = tf.Session())
  139. {
  140. var o = sess.run(c,
  141. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  142. Assert.AreEqual((float)o, floatResult);
  143. }
  144. #endregion
  145. #region doubleTest
  146. const double firstDoubleVal = 2.0;
  147. const double secondDoubleVal = 3.0;
  148. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  149. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  150. var doubleResult = firstDoubleFeed.Sum() + secondDoubleFeed.Sum();
  151. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  152. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  153. c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
  154. using (var sess = tf.Session())
  155. {
  156. var o = sess.run(c,
  157. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  158. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  159. Assert.AreEqual((double)o, doubleResult);
  160. }
  161. // Testing `operator +(Tensor x, Tensor y)
  162. c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
  163. using (var sess = tf.Session())
  164. {
  165. var o = sess.run(c,
  166. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  167. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  168. Assert.AreEqual((double)o, doubleResult);
  169. }
  170. // Testing `operator +(Tensor x, double y)
  171. c = tf.reduce_sum(tf.reduce_sum(a + secondFloatVal, 1));
  172. using (var sess = tf.Session())
  173. {
  174. var o = sess.run(c,
  175. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  176. Assert.AreEqual((double)o, doubleResult);
  177. }
  178. // Testing `operator +(double x, Tensor y)
  179. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal + a, 1));
  180. using (var sess = tf.Session())
  181. {
  182. var o = sess.run(c,
  183. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  184. Assert.AreEqual((double)o, doubleResult);
  185. }
  186. #endregion
  187. }
  188. [TestMethod]
  189. public void subOpTests()
  190. {
  191. const int rows = 2; // to avoid broadcasting effect
  192. const int cols = 10;
  193. #region intTest
  194. const int firstIntVal = -2;
  195. const int secondIntVal = 3;
  196. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  197. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  198. var intResult = firstIntFeed.Sum() - secondIntFeed.Sum();
  199. var intResultTwo = -firstIntFeed.Sum();
  200. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  201. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  202. var c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
  203. using (var sess = tf.Session())
  204. {
  205. var o = sess.run(c,
  206. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  207. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  208. Assert.AreEqual((int)o, intResult);
  209. }
  210. // Testing `operator -(Tensor x, Tensor y)
  211. c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
  212. using (var sess = tf.Session())
  213. {
  214. var o = sess.run(c,
  215. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  216. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  217. Assert.AreEqual((int)o, intResult);
  218. }
  219. // Testing `operator -(Tensor x, int y)
  220. c = tf.reduce_sum(tf.reduce_sum(a - secondIntVal, 1));
  221. using (var sess = tf.Session())
  222. {
  223. var o = sess.run(c,
  224. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  225. Assert.AreEqual((int)o, intResult);
  226. }
  227. // Testing `operator -(int x, Tensor y)
  228. c = tf.reduce_sum(tf.reduce_sum(secondIntVal - a, 1));
  229. using (var sess = tf.Session())
  230. {
  231. var o = sess.run(c,
  232. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  233. Assert.AreEqual((int)o, Math.Abs(intResult));
  234. }
  235. // Testing `operator -(Tensor x)
  236. c = tf.reduce_sum(tf.reduce_sum(-a, 1));
  237. using (var sess = tf.Session())
  238. {
  239. var o = sess.run(c,
  240. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  241. Assert.AreEqual((int)o, intResultTwo);
  242. }
  243. #endregion
  244. #region floatTest
  245. const float firstFloatVal = -2.0f;
  246. const float secondFloatVal = 3.0f;
  247. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  248. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  249. var floatResult = firstFloatFeed.Sum() - secondFloatFeed.Sum();
  250. var floatResultTwo = -firstFloatFeed.Sum();
  251. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  252. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  253. c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
  254. using (var sess = tf.Session())
  255. {
  256. var o = sess.run(c,
  257. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  258. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  259. Assert.AreEqual((float)o, floatResult);
  260. }
  261. // Testing `operator -(Tensor x, Tensor y)
  262. c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
  263. using (var sess = tf.Session())
  264. {
  265. var o = sess.run(c,
  266. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  267. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  268. Assert.AreEqual((float)o, floatResult);
  269. }
  270. // Testing `operator -(Tensor x, float y)
  271. c = tf.reduce_sum(tf.reduce_sum(a - secondFloatVal, 1));
  272. using (var sess = tf.Session())
  273. {
  274. var o = sess.run(c,
  275. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  276. Assert.AreEqual((float)o, floatResult);
  277. }
  278. // Testing `operator -(float x, Tensor y)
  279. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal - a, 1));
  280. using (var sess = tf.Session())
  281. {
  282. var o = sess.run(c,
  283. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  284. Assert.AreEqual((float)o, Math.Abs(floatResult));
  285. }
  286. // Testing `operator -(Tensor x)
  287. c = tf.reduce_sum(tf.reduce_sum(-a, 1));
  288. using (var sess = tf.Session())
  289. {
  290. var o = sess.run(c,
  291. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  292. Assert.AreEqual((float)o, floatResultTwo);
  293. }
  294. #endregion
  295. #region doubleTest
  296. const double firstDoubleVal = -2.0;
  297. const double secondDoubleVal = 3.0;
  298. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  299. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  300. var doubleResult = firstDoubleFeed.Sum() - secondDoubleFeed.Sum();
  301. var doubleResultTwo = -firstDoubleFeed.Sum();
  302. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  303. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  304. c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
  305. using (var sess = tf.Session())
  306. {
  307. var o = sess.run(c,
  308. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  309. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  310. Assert.AreEqual((double)o, doubleResult);
  311. }
  312. // Testing `operator -(Tensor x, Tensor y)
  313. c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
  314. using (var sess = tf.Session())
  315. {
  316. var o = sess.run(c,
  317. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  318. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  319. Assert.AreEqual((double)o, doubleResult);
  320. }
  321. // Testing `operator -(Tensor x, double y)
  322. c = tf.reduce_sum(tf.reduce_sum(a - secondFloatVal, 1));
  323. using (var sess = tf.Session())
  324. {
  325. var o = sess.run(c,
  326. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  327. Assert.AreEqual((double)o, doubleResult);
  328. }
  329. // Testing `operator -(double x, Tensor y)
  330. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal - a, 1));
  331. using (var sess = tf.Session())
  332. {
  333. var o = sess.run(c,
  334. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  335. Assert.AreEqual((double)o, Math.Abs(doubleResult));
  336. }
  337. // Testing `operator -(Tensor x)
  338. c = tf.reduce_sum(tf.reduce_sum(-a, 1));
  339. using (var sess = tf.Session())
  340. {
  341. var o = sess.run(c,
  342. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  343. Assert.AreEqual((double)o, doubleResultTwo);
  344. }
  345. #endregion
  346. }
  347. }
  348. }