| 478 | |
| 479 | @test_util.run_deprecated_v1 |
| 480 | def testGrad(self): |
| 481 | shapes = ((3,), (7, 4)) |
| 482 | with self.session(use_gpu=True): |
| 483 | for shape in shapes: |
| 484 | x = constant_op.constant(np.random.rand(*shape), np.float32) |
| 485 | y = array_ops.matrix_diag(x) |
| 486 | error = gradient_checker.compute_gradient_error(x, |
| 487 | x.get_shape().as_list(), |
| 488 | y, |
| 489 | y.get_shape().as_list()) |
| 490 | self.assertLess(error, 1e-4) |
| 491 | |
| 492 | # LINT.IfChange |
| 493 | if compat.forward_compatible(2019, 8, 31): |
| 494 | # LINT.ThenChange(//tensorflow/python/ops/array_ops.py) |
| 495 | |
| 496 | # {Sub,super}diagonals/band. |
| 497 | tests = dict() # tests[shape] = (d_lower, d_upper) |
| 498 | tests[(3,)] = (-1, -1) |
| 499 | tests[(7, 3, 4)] = (-1, 1) |
| 500 | with self.session(use_gpu=True): |
| 501 | for shape, diags in tests.items(): |
| 502 | x = constant_op.constant(np.random.rand(*shape), np.float32) |
| 503 | y = array_ops.matrix_diag(x, k=diags) |
| 504 | error = gradient_checker.compute_gradient_error( |
| 505 | x, |
| 506 | x.get_shape().as_list(), y, |
| 507 | y.get_shape().as_list()) |
| 508 | self.assertLess(error, 1e-4) |
| 509 | |
| 510 | |
| 511 | class MatrixSetDiagTest(test.TestCase): |