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.

GradientsTest.cs 28 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using NumSharp;
  6. using Tensorflow;
  7. using static Tensorflow.Binding;
  8. namespace TensorFlowNET.UnitTest.gradients_test
  9. {
  10. [TestClass]
  11. public class GradientsTest : PythonTest
  12. {
  13. [TestMethod]
  14. public void BroadcastToGrad()
  15. {
  16. var graph = tf.Graph().as_default();
  17. var x = tf.constant(2, dtype: dtypes.float32);
  18. var y = tf.broadcast_to(x, (2, 4, 3));
  19. var grad = tf.gradients(y, x);
  20. using (var sess = tf.Session(graph))
  21. {
  22. float result = sess.run(grad[0]);
  23. Assert.AreEqual(result, 24.0f);
  24. }
  25. }
  26. [TestMethod]
  27. public void CumsumGrad()
  28. {
  29. var graph = tf.Graph().as_default();
  30. var x = tf.constant(2, dtype: dtypes.float32);
  31. var y = tf.broadcast_to(x, (2, 4, 3));
  32. var z = tf.cumsum(y, axis: 1);
  33. var grad = tf.gradients(z, x);
  34. using (var sess = tf.Session(graph))
  35. {
  36. float result = sess.run(grad[0]);
  37. Assert.AreEqual(result, 60.0f);
  38. }
  39. }
  40. [Ignore("TODO")]
  41. [TestMethod]
  42. public void testGradients()
  43. {
  44. var g = tf.Graph().as_default();
  45. var inp = tf.constant(1.0, shape: new[] { 32, 100 }, name: "in");
  46. var w = tf.constant(1.0, shape: new[] { 100, 10 }, name: "w");
  47. var b = tf.constant(1.0, shape: new[] { 10 }, name: "b");
  48. var xw = math_ops.matmul(inp, w, name: "xw");
  49. var h = nn_ops.bias_add(xw, b, name: "h");
  50. var w_grad = gradients_impl.gradients(new[] { h }, new[] { w })[0];
  51. self.assertEquals("MatMul", w_grad.op.type);
  52. // TODO: Operation._original_op
  53. //self.assertEquals(w_grad.op._original_op, xw.op);
  54. self.assertTrue((bool)w_grad.op.get_attr("transpose_a"));
  55. self.assertFalse((bool)w_grad.op.get_attr("transpose_b"));
  56. }
  57. [TestMethod]
  58. public void testBatchMatMulGradient()
  59. {
  60. var a = tf.constant(np.array(Enumerable.Range(1, 18).Select(elem => (float)elem).ToArray()), shape: new[] { 2, 3, 3 });
  61. var b = tf.divide(a, tf.constant(2.0f));
  62. var c = tf.batch_matmul(a, b);
  63. var g = tf.gradients(c, new[] { a, b }, stop_gradients: new[] { a, b });
  64. var checkG = new[]
  65. {
  66. 3.0f, 7.5f, 12.0f,
  67. 3.0f, 7.5f, 12.0f,
  68. 3.0f, 7.5f, 12.0f,
  69. 16.5f, 21.0f, 25.5f,
  70. 16.5f, 21.0f, 25.5f,
  71. 16.5f, 21.0f, 25.5f,
  72. 12.0f, 12.0f, 12.0f,
  73. 15.0f, 15.0f, 15.0f,
  74. 18.0f, 18.0f, 18.0f,
  75. 39.0f, 39.0f, 39.0f,
  76. 42.0f, 42.0f, 42.0f,
  77. 45.0f, 45.0f, 45.0f
  78. };
  79. using (var sess = tf.Session())
  80. {
  81. var result = sess.run(g);
  82. var resultList = result[0].GetData<float>().ToList();
  83. resultList.AddRange(result[1].GetData<float>());
  84. Console.WriteLine(result.ToString());
  85. CollectionAssert.AreEqual(resultList.ToArray(), checkG);
  86. }
  87. }
  88. [TestMethod]
  89. public void testSimpleGradients()
  90. {
  91. (T, T) evaluateDerivatives<T>(Func<Tensor, Tensor> f, T xval) where T : unmanaged
  92. {
  93. var x = tf.constant(xval);
  94. var y = f(x);
  95. var g = tf.gradients(y, x);
  96. using (var session = tf.Session())
  97. {
  98. var result = session.run(new[] { y, g[0] });
  99. return (result[0].GetData<T>()[0], result[1].GetData<T>()[0]);
  100. }
  101. }
  102. void assertFloat32Equal(float expected, float actual, string msg)
  103. {
  104. float eps = 1e-6f;
  105. Assert.IsTrue(Math.Abs(expected - actual) < eps * Math.Max(1.0f, Math.Abs(expected)), $"{msg}: expected {expected} vs actual {actual}");
  106. }
  107. void test(string name, Func<Tensor, Tensor> tfF, Func<double, (double, double)> targetF, double[] values)
  108. {
  109. foreach (var x in values)
  110. {
  111. var (expectedY, expectedDY) = targetF(x);
  112. {
  113. var (actualY, actualDY) = evaluateDerivatives(tfF, x);
  114. Assert.AreEqual(expectedY, actualY, $"value {name}/float64 at {x}");
  115. Assert.AreEqual(expectedDY, actualDY, $"derivative {name}/float64 at {x}");
  116. }
  117. {
  118. var (actualY, actualDY) = evaluateDerivatives(tfF, (float)x);
  119. assertFloat32Equal((float)expectedY, actualY, $"value {name}/float32 at {x}");
  120. assertFloat32Equal((float)expectedDY, actualDY, $"derivative {name}/float32 at {x}");
  121. }
  122. }
  123. }
  124. test("tf.exp",
  125. x => tf.exp(5 * x),
  126. x => (Math.Exp(5.0 * x), 5.0 * Math.Exp(5.0 * x)),
  127. new[] { -1.0, 0.0, 1.0, 1.5 });
  128. test("tf.log",
  129. x => tf.log(x),
  130. x => (Math.Log(x), 1.0 / x),
  131. new[] { 0.5, 1.0, 1.5, 2.0 });
  132. test("tf.sqrt",
  133. x => tf.sqrt(x),
  134. x => (Math.Sqrt(x), 0.5 / Math.Sqrt(x)),
  135. new[] { 0.5, 1.0, 1.1, 1.5, 2.0 });
  136. test("tf.sin",
  137. x => tf.sin(x),
  138. x => (Math.Sin(x), Math.Cos(x)),
  139. new[] { -1.0, 0.0, 1.0, 1.5, 2.0 });
  140. test("tf.sinh",
  141. x => tf.sinh(x),
  142. x => (Math.Sinh(x), Math.Cosh(x)),
  143. new[] { -1.0, 0.0, 1.0, 1.5, 2.0 });
  144. test("tf.cos",
  145. x => tf.cos(x),
  146. x => (Math.Cos(x), -Math.Sin(x)),
  147. new[] { -1.0, 0.0, 1.0, 1.5, 2.0 });
  148. test("tf.cosh",
  149. x => tf.cosh(x),
  150. x => (Math.Cosh(x), Math.Sinh(x)),
  151. new[] { -1.0, 0.0, 1.0, 1.5, 2.0 });
  152. test("tf.tanh",
  153. x => tf.tanh(x),
  154. x => (Math.Tanh(x), 1.0 - Math.Pow(Math.Tanh(x), 2.0)),
  155. new[] { -1.0, 0.0, 1.0, 1.5, 2.0 });
  156. test("tf.maximum",
  157. x => tf.maximum(x, tf.constant(0.0, dtype: x.dtype)),
  158. x => (Math.Max(x, 0.0), (x > 0.0) ? 1.0 : 0.0),
  159. new[] { -1.0, 1.0 });
  160. test("tf.minimum",
  161. x => tf.minimum(x, tf.constant(0.0, dtype: x.dtype)),
  162. x => (Math.Min(x, 0.0), (x < 0.0) ? 1.0 : 0.0),
  163. new[] { -1.0, 1.0 });
  164. }
  165. [TestMethod]
  166. public void testTanhGradient()
  167. {
  168. var a = tf.constant(1f);
  169. var b = tf.tanh(a);
  170. var g = tf.gradients(b, a);
  171. using (var sess = tf.Session())
  172. {
  173. var result = sess.run(g);
  174. var actual = result[0].GetData<float>()[0];
  175. self.assertEquals(0.41997434127f, actual);
  176. }
  177. }
  178. [TestMethod]
  179. public void testLgammaGrad()
  180. {
  181. var a = tf.constant(5f);
  182. var b = tf.lgamma(a);
  183. var g = tf.gradients(b, a);
  184. using (var sess = tf.Session())
  185. {
  186. var result = sess.run(new object[] { g, b });
  187. var actualDeriv = result[0].GetData<float>()[0];
  188. var actual = result[1].GetData<float>()[0];
  189. self.assertEquals(1.5061177f, actualDeriv);
  190. self.assertEquals(3.17805386f, actual);
  191. }
  192. }
  193. [TestMethod]
  194. public void testSliceGrad()
  195. {
  196. var a = tf.tanh(tf.constant(new[] { 2f, 3f }, shape: new[] { 2, 1 }));
  197. var b = tf.strided_slice(a,
  198. tf.constant(new[] { 0 }, tf.int32, new[] { 1 }),
  199. tf.constant(new[] { 1 }, tf.int32, new[] { 1 }),
  200. tf.constant(new[] { 1 }, tf.int32, new[] { 1 })
  201. );
  202. var g = tf.gradients(b, a);
  203. using (var sess = tf.Session())
  204. {
  205. var result = sess.run(new object[] { g, b });
  206. var actualDeriv = np.squeeze(result[0]);
  207. var actual = np.squeeze(result[1]);
  208. self.assertEquals(new float[] { 1, 0 }, new float[] { actualDeriv[0], actualDeriv[1] });
  209. self.assertEquals(0.9640276f, (float)actual);
  210. }
  211. }
  212. [TestMethod]
  213. public void testConcatGrad()
  214. {
  215. var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
  216. var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 });
  217. var a = tf.concat(new List<Tensor>(new[] { a1, a2 }), 0);
  218. var g = tf.gradients(a, a1);
  219. using (var sess = tf.Session())
  220. {
  221. var result = sess.run(new object[] { g, a });
  222. var actualDeriv = result[0].GetData<float>()[0];
  223. var actual = result[1].GetData<float>()[0];
  224. self.assertEquals(1f, actualDeriv);
  225. self.assertEquals(2f, actual);
  226. }
  227. }
  228. [TestMethod]
  229. public void testStopGradientFunction()
  230. {
  231. var ap = tf.constant(1f);
  232. var b = tf.tanh(ap) + gen_array_ops.stop_gradient(ap);
  233. var g = tf.gradients(b, ap);
  234. using (var sess = tf.Session())
  235. {
  236. var result = sess.run(g);
  237. var actual = result[0].GetData<float>()[0];
  238. self.assertEquals(0.41997434127f, actual);
  239. }
  240. }
  241. [Ignore("TODO")]
  242. [TestMethod]
  243. public void testUnusedOutput()
  244. {
  245. //def testUnusedOutput(self):
  246. // with ops.Graph().as_default():
  247. // w = constant(1.0, shape=[2, 2])
  248. // x = constant(1.0, shape=[2, 2])
  249. // wx = math_ops.matmul(w, x)
  250. // split_wx = array_ops.split(value=wx, num_or_size_splits=2, axis=0)
  251. // c = math_ops.reduce_sum(split_wx[1])
  252. // gw = gradients.gradients(c, [w])[0]
  253. // self.assertEquals("MatMul", gw.op.type)
  254. }
  255. [Ignore("TODO")]
  256. [TestMethod]
  257. public void testColocateGradients()
  258. {
  259. //def testColocateGradients(self):
  260. // with ops.Graph().as_default() as g:
  261. // w = constant(1.0, shape=[1, 1])
  262. // x = constant(1.0, shape=[1, 2])
  263. // with g.device("/device:GPU:0"):
  264. // wx = math_ops.matmul(w, x)
  265. // gw = gradients.gradients(wx, [w], colocate_gradients_with_ops=True)[0]
  266. // self.assertEqual(gw.op.colocation_groups(), wx.op.colocation_groups())
  267. }
  268. [Ignore("TODO")]
  269. [TestMethod]
  270. public void testColocateGradientsWithAggregation()
  271. {
  272. //def testColocateGradientsWithAggregation(self):
  273. // with ops.Graph().as_default() as g:
  274. // with g.device("/device:GPU:1"):
  275. // w = constant(1.0, shape=[1, 1])
  276. // x = constant(1.0, shape=[1, 2])
  277. // y = constant(1.0, shape=[1, 2])
  278. // wx = math_ops.matmul(w, x)
  279. // wy = math_ops.matmul(w, y)
  280. // with g.device("/device:GPU:0"):
  281. // z = wx + wy
  282. // gw1 = gradients.gradients(z, [w], colocate_gradients_with_ops=True)[0]
  283. // self.assertEqual(gw1.op.colocation_groups(), wx.op.colocation_groups())
  284. // gw2 = gradients.gradients(z, [w], colocate_gradients_with_ops=False)[0]
  285. // self.assertTrue(wx.op.colocation_groups() != gw2.op.colocation_groups())
  286. }
  287. [Ignore("TODO")]
  288. [TestMethod]
  289. public void testColocateGradientsWithAggregationInMultipleDevices()
  290. {
  291. //def testColocateGradientsWithAggregationInMultipleDevices(self):
  292. // with ops.Graph().as_default() as g:
  293. // with g.device("/device:GPU:1"):
  294. // w = constant(1.0, shape=[1, 1])
  295. // x = constant(1.0, shape=[1, 2])
  296. // y = constant(1.0, shape=[1, 2])
  297. // with g.device("/task:1"):
  298. // wx = math_ops.matmul(w, x)
  299. // with g.device("/task:2"):
  300. // wy = math_ops.matmul(w, y)
  301. // with g.device("/device:GPU:0"):
  302. // z = wx + wy
  303. // gw1 = gradients.gradients(z, [w], colocate_gradients_with_ops=True)[0]
  304. // self.assertEqual(gw1.op.colocation_groups(), w.op.colocation_groups())
  305. // gw2 = gradients.gradients(z, [w], colocate_gradients_with_ops=False)[0]
  306. // self.assertTrue(w.op.colocation_groups() != gw2.op.colocation_groups())
  307. }
  308. [Ignore("TODO")]
  309. [TestMethod]
  310. public void testColocateGradientsWithGateGradients()
  311. {
  312. //def testColocateGradientsWithGateGradients(self):
  313. // if not test_util.is_gpu_available():
  314. // self.skipTest("No GPU available")
  315. // with ops.Graph().as_default() as g:
  316. // with g.device("/device:CPU:0"):
  317. // x = constant(1.0, shape=[1, 1])
  318. // y = constant(1.0, shape=[1, 1])
  319. // s = x + y
  320. // with g.device("/device:GPU:0"):
  321. // z = math_ops.reduce_sum(s)
  322. // gz_x = gradients.gradients(z, [x], colocate_gradients_with_ops=True,
  323. // gate_gradients=True)[0]
  324. // with session.Session():
  325. // # Make sure the placer doesn't complain.
  326. // self.evaluate(gz_x)
  327. }
  328. [Ignore("TODO")]
  329. [TestMethod]
  330. public void testBoundaryStop()
  331. {
  332. //def testBoundaryStop(self):
  333. // # Test that we don't differentiate 'x'. The gradient function for 'x' is
  334. // # set explicitly to None so we will get an exception if the gradient code
  335. // # tries to differentiate 'x'.
  336. // with ops.Graph().as_default():
  337. // c = constant(1.0)
  338. // x = array_ops.identity(c)
  339. // y = x + 1.0
  340. // z = y + 1
  341. // grads = gradients.gradients(z, [x])
  342. // self.assertTrue(all(x is not None for x in grads))
  343. }
  344. [Ignore("TODO")]
  345. [TestMethod]
  346. public void testBoundaryContinue()
  347. {
  348. //@test_util.run_v1_only("b/120545219")
  349. //def testBoundaryContinue(self):
  350. // # Test that we differentiate both 'x' and 'y' correctly when x is a
  351. // # predecessor of y.
  352. // with self.cached_session():
  353. // x = constant(1.0)
  354. // y = x * 2.0
  355. // z = y * 3.0
  356. // grads = gradients.gradients(z, [x, y])
  357. // self.assertTrue(all(x is not None for x in grads))
  358. // self.assertEqual(6.0, grads[0].eval())
  359. }
  360. [Ignore("TODO")]
  361. [TestMethod]
  362. public void testAggregationMethodAccumulateN()
  363. {
  364. //@test_util.run_v1_only("b/120545219")
  365. //def testAggregationMethodAccumulateN(self):
  366. // with self.cached_session():
  367. // x = constant(1.0)
  368. // y = x * 2.0
  369. // z = y + y + y + y + y + y + y + y + y + y
  370. // grads = gradients.gradients(
  371. // z, [x, y],
  372. // aggregation_method=gradients.AggregationMethod.
  373. // EXPERIMENTAL_ACCUMULATE_N)
  374. // self.assertTrue(all(x is not None for x in grads))
  375. // self.assertEqual(20.0, grads[0].eval())
  376. // self.assertEqual(10.0, grads[1].eval())
  377. }
  378. [Ignore("TODO")]
  379. [TestMethod]
  380. public void testAggregationMethodAddN()
  381. {
  382. //@test_util.run_v1_only("b/120545219")
  383. //def testAggregationMethodAddN(self):
  384. // with self.cached_session():
  385. // x = constant(1.0)
  386. // y = x * 2.0
  387. // z = y + y + y + y + y + y + y + y + y + y
  388. // grads = gradients.gradients(
  389. // z, [x, y], aggregation_method=gradients.AggregationMethod.ADD_N)
  390. // self.assertTrue(all(x is not None for x in grads))
  391. // self.assertEqual(20.0, grads[0].eval())
  392. // self.assertEqual(10.0, grads[1].eval())
  393. }
  394. [Ignore("TODO")]
  395. [TestMethod]
  396. public void testAggregationMethodTree()
  397. {
  398. //@test_util.run_v1_only("b/120545219")
  399. //def testAggregationMethodTree(self):
  400. // with self.cached_session():
  401. // x = constant(1.0)
  402. // y = x * 2.0
  403. // z = y + y + y + y + y + y + y + y + y + y
  404. // grads = gradients.gradients(
  405. // z, [x, y],
  406. // aggregation_method=gradients.AggregationMethod.EXPERIMENTAL_TREE)
  407. // self.assertTrue(all(x is not None for x in grads))
  408. // self.assertEqual(20.0, grads[0].eval())
  409. // self.assertEqual(10.0, grads[1].eval())
  410. }
  411. [Ignore("TODO")]
  412. [TestMethod]
  413. public void testNoGradientForStringOutputs()
  414. {
  415. //def testNoGradientForStringOutputs(self):
  416. // with ops.Graph().as_default():
  417. // def _TestOpGrad(_, float_grad, string_grad):
  418. // """Gradient function for TestStringOutput."""
  419. // self.assertEquals(float_grad.dtype, dtypes.float32)
  420. // self.assertFalse(string_grad)
  421. // return float_grad
  422. // ops.RegisterGradient("TestStringOutput")(_TestOpGrad)
  423. // c = constant(1.0)
  424. // x, _ = test_ops.test_string_output(c)
  425. // z = x * 2.0
  426. // w = z * 3.0
  427. // grads = gradients.gradients(z, [c])
  428. // self.assertTrue(isinstance(grads[0], ops.Tensor))
  429. // grads = gradients.gradients(w, [c])
  430. // self.assertTrue(isinstance(grads[0], ops.Tensor))
  431. }
  432. [Ignore("TODO")]
  433. [TestMethod]
  434. public void testSingletonIndexedSlices()
  435. {
  436. //def testSingletonIndexedSlices(self):
  437. // with ops.Graph().as_default():
  438. // x = array_ops.placeholder(dtypes.float32)
  439. // y = array_ops.identity(x)
  440. // dy = ops.IndexedSlices(
  441. // array_ops.placeholder(dtypes.float32),
  442. // array_ops.placeholder(dtypes.int32))
  443. // dx, = gradients.gradients(y, x, grad_ys=dy)
  444. // # The IndexedSlices gradient of tf.identity is the identity map.
  445. // with self.cached_session() as sess:
  446. // vdx, vdy = sess.run(
  447. // [dx, dy], feed_dict={x: [1.0], dy.indices: [0], dy.values: [2.0]})
  448. // self.assertEqual(vdx, vdy)
  449. }
  450. [Ignore("TODO")]
  451. [TestMethod]
  452. public void testNonDifferentiableSwitchInWhileLoop()
  453. {
  454. //@test_util.run_v1_only("b/120545219")
  455. //def testNonDifferentiableSwitchInWhileLoop(self):
  456. // with ops.Graph().as_default():
  457. // v = array_ops.placeholder(dtypes.float32, [])
  458. // def _Step(i, a, ta):
  459. // a += math_ops.cast(v, dtypes.int32)
  460. // return (i + 1, a, ta.write(i, a))
  461. // n = 4
  462. // i, _, ta = control_flow_ops.while_loop(
  463. // lambda i, *_: i < n,
  464. // _Step, [0, 0, tensor_array_ops.TensorArray(
  465. // dtypes.int32, size=n)])
  466. // target = ta.read(i - 1)
  467. // grad, = gradients.gradients(target, v)
  468. // self.assertIsNone(grad)
  469. }
  470. [Ignore("TODO")]
  471. [TestMethod]
  472. public void testVariableReadValueGradient()
  473. {
  474. //def testVariableReadValueGradient(self):
  475. // with ops.Graph().as_default():
  476. // init = constant_op.constant(100.0)
  477. // var = variables.Variable(init)
  478. // gradient = gradients.gradients(var.read_value(), var)
  479. // self.assertIsNotNone(gradient)
  480. }
  481. [Ignore("TODO")]
  482. [TestMethod]
  483. public void testVariableAsGraphElementGradient()
  484. {
  485. //def testVariableAsGraphElementGradient(self):
  486. // with ops.Graph().as_default() as graph:
  487. // init = constant_op.constant(100.0)
  488. // var = variables.Variable(init)
  489. // gradient = gradients.gradients(graph.as_graph_element(var), var)
  490. // self.assertIsNotNone(gradient)
  491. }
  492. [Ignore("TODO")]
  493. [TestMethod]
  494. public void testVariableRefGradient()
  495. {
  496. //@test_util.run_v1_only("b/120545219")
  497. //def testVariableRefGradient(self):
  498. // with ops.Graph().as_default():
  499. // init = constant_op.constant(100.0)
  500. // var = variables.VariableV1(init)
  501. // gradient = gradients.gradients(var._ref(), var)
  502. // self.assertIsNotNone(gradient)
  503. }
  504. [Ignore("TODO")]
  505. [TestMethod]
  506. public void testDependentYs()
  507. {
  508. //@test_util.run_v1_only("b/120545219")
  509. //def testDependentYs(self):
  510. // with self.cached_session():
  511. // x = constant_op.constant(3.0)
  512. // y = math_ops.square(x)
  513. // y1 = math_ops.square(y)
  514. // y2 = math_ops.square(y1)
  515. // g = gradients.gradients([y, y2], x)
  516. // self.assertAllClose(17502.0, g[0].eval())
  517. // g = gradients.gradients(y + y2, x)
  518. // self.assertAllClose(17502.0, g[0].eval())
  519. // z = array_ops.identity(y)
  520. // z2 = array_ops.identity(y2)
  521. // g = gradients.gradients([z, z2], x)
  522. // self.assertAllClose(17502.0, g[0].eval())
  523. }
  524. [Ignore("TODO")]
  525. [TestMethod]
  526. public void testPartialDerivatives()
  527. {
  528. //@test_util.run_v1_only("b/120545219")
  529. //def testPartialDerivatives(self):
  530. // with self.cached_session():
  531. // x = constant_op.constant(1.)
  532. // y = 2 * x
  533. // z = x + y
  534. // totalg = gradients.gradients(z, [x, y])
  535. // self.assertEqual([3.0, 1.0], [g.eval() for g in totalg])
  536. // partialg = gradients.gradients(z, [x, y], stop_gradients=[x, y])
  537. // self.assertEqual([1.0, 1.0], [g.eval() for g in partialg])
  538. }
  539. [Ignore("TODO")]
  540. [TestMethod]
  541. public void testStopGradients()
  542. {
  543. //@test_util.run_v1_only("b/120545219")
  544. //def testStopGradients(self):
  545. // def _MakeGraph(rng, stop_gradients=()):
  546. // def _FunctionOf(xs, k=3):
  547. // return ops.convert_to_tensor(
  548. // sum(math_ops.matmul(rng.rand(k, k), x) for x in xs)
  549. // + rng.rand(k, k))
  550. // a = _FunctionOf([])
  551. // if "a" in stop_gradients: a = array_ops.stop_gradient(a)
  552. // b = _FunctionOf([a])
  553. // if "b" in stop_gradients: b = array_ops.stop_gradient(b)
  554. // c = _FunctionOf([a, b])
  555. // if "c" in stop_gradients: c = array_ops.stop_gradient(c)
  556. // d = _FunctionOf([b, c])
  557. // if "d" in stop_gradients: d = array_ops.stop_gradient(d)
  558. // return dict(a=a, b=b, c=c, d=d)
  559. // def _Gradients(ys, xs, **kwargs):
  560. // dydxs = gradients.gradients(ys, xs, **kwargs)
  561. // dydxs = [0. * x if dydx is None else dydx
  562. // for x, dydx in zip(xs, dydxs)]
  563. // return dydxs
  564. // seed = np.random.randint(1000)
  565. // cases = []
  566. // subsets = [""] + "a b c d ab ac ad bc bd cd abc abd acd bcd abcd".split()
  567. // graph = _MakeGraph(np.random.RandomState(seed))
  568. // for constants in subsets:
  569. // graph_with_stops = _MakeGraph(np.random.RandomState(seed), constants)
  570. // for variables_ in subsets:
  571. // # compute the gradient when stopped using tf.stop_gradients
  572. // grad1 = _Gradients([graph_with_stops["d"]],
  573. // [graph_with_stops[v] for v in variables_])
  574. // # compute the gradient when stopped using the stop_gradients kwarg
  575. // grad2 = _Gradients([graph["d"]],
  576. // [graph[v] for v in variables_],
  577. // stop_gradients=[graph[v] for v in constants])
  578. // cases.append(dict(grad1=grad1, grad2=grad2,
  579. // constants=constants, variables=variables_))
  580. // # evaluate all tensors in one call to session.run for speed
  581. // with self.cached_session() as sess:
  582. // results = sess.run([(case["grad1"], case["grad2"]) for case in cases])
  583. // for (npgrad1, npgrad2), case in zip(results, cases):
  584. // for a, b in zip(npgrad1, npgrad2):
  585. // np.testing.assert_allclose(a, b)
  586. }
  587. [Ignore("TODO")]
  588. [TestMethod]
  589. public void testUnconnectedGradientsNoneUnconnectedGradients()
  590. {
  591. //def testUnconnectedGradientsNoneUnconnectedGradients(self):
  592. // with ops.Graph().as_default():
  593. // x = constant(1.0, shape=[2, 2])
  594. // y = constant(3.0, shape=[3, 1])
  595. // grad = gradients.gradients(
  596. // [y], [x], unconnected_gradients="none")
  597. // self.assertIsNone(grad[0])
  598. }
  599. [Ignore("TODO")]
  600. [TestMethod]
  601. public void testUnconnectedGradientsZerosUnconnectedGradients()
  602. {
  603. //def testUnconnectedGradientsZerosUnconnectedGradients(self):
  604. // with ops.Graph().as_default():
  605. // x = constant(1.0, shape=[2, 2])
  606. // y = constant(3.0, shape=[3, 1])
  607. // grads = gradients.gradients(
  608. // [y], [x], unconnected_gradients="zero")
  609. // with self.cached_session() as sess:
  610. // self.assertAllEqual([[0.0, 0.0], [0.0, 0.0]], self.evaluate(grads)[0])
  611. }
  612. [Ignore("TODO")]
  613. [TestMethod]
  614. public void testUnconnectedGradientsZeroConnectedGradients()
  615. {
  616. //def testUnconnectedGradientsZeroConnectedGradients(self):
  617. // with ops.Graph().as_default():
  618. // x = constant(1.0)
  619. // y = x * 3.0
  620. // grad = gradients.gradients(
  621. // [y], [x], unconnected_gradients="zero")
  622. // with self.cached_session() as sess:
  623. // self.assertEquals(3.0, self.evaluate(grad)[0])
  624. }
  625. [Ignore("TODO")]
  626. [TestMethod]
  627. public void testUnknownUnconnectedGradientsValueGiven()
  628. {
  629. //def testUnknownUnconnectedGradientsValueGiven(self):
  630. // with ops.Graph().as_default():
  631. // x = constant(1.0)
  632. // y = constant(1.0)
  633. // with self.assertRaisesRegexp(
  634. // ValueError, "Unknown value for unconnected_gradients: 'nonsense'"):
  635. // gradients.gradients([y], [x], unconnected_gradients="nonsense")
  636. }
  637. /*
  638. */
  639. }
  640. }