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

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