Best JavaScript code snippet using backstopjs
reduction_ops_test.py
Source:reduction_ops_test.py
...108 data = np.random.rand(*shape).astype(dtype.as_numpy_dtype)109 if dtype.is_complex:110 data -= 2j * data111 return data112 def _compare(self, x, reduction_axes, keepdims, feed_dict=None):113 np_ans = self._np_reduce(x, reduction_axes, keepdims)114 with self.cached_session(use_gpu=True) as sess:115 tf_ans = self._tf_reduce(x, reduction_axes, keepdims)116 out = sess.run(tf_ans, feed_dict)117 self.assertAllClose(np_ans, out)118 self.assertShapeEqual(np_ans, tf_ans)119 def _compareAll(self, x, reduction_axes, feed_dict=None):120 if reduction_axes is not None and np.shape(reduction_axes) == (1,):121 # Test scalar reduction_axes argument122 self._compareAll(x, reduction_axes[0])123 self._compare(x, reduction_axes, keepdims=False, feed_dict=feed_dict)124 self._compare(x, reduction_axes, keepdims=True, feed_dict=feed_dict)125 def _compareAllAxes(self, x, feed_dict=None):126 self._compareAll(x, None)127 for axes in _powerset(range(x.ndim)):128 self._compareAll(x, axes, feed_dict)129 def _compareGradient(self, x, reduction_axes, rtol=1e-8, atol=1e-8):130 if reduction_axes is not None and np.shape(reduction_axes) == (1,):131 # Test scalar reduction_axes argument132 self._compareGradient(x, reduction_axes[0], rtol=rtol, atol=atol)133 with self.cached_session(use_gpu=True):134 t = ops.convert_to_tensor(x)135 su = self._tf_reduce(t, reduction_axes, False)136 jacob_t, jacob_n = gradient_checker.compute_gradient(137 t, x.shape, su, su.get_shape().as_list(), x_init_value=x, delta=1)138 self.assertAllClose(jacob_t, jacob_n, rtol=rtol, atol=atol)139 def _compareGradientAxes(self, x, rtol=1e-8, atol=1e-8):140 self._compareGradient(x, None, rtol=rtol, atol=atol)141 self._compareGradient(x, [], rtol=rtol, atol=atol)142 self._compareGradient(x, 0, rtol=rtol, atol=atol)143 self._compareGradient(x, [1], rtol=rtol, atol=atol)144 self._compareGradient(x, [2], rtol=rtol, atol=atol)145 self._compareGradient(x, [1, 2], rtol=rtol, atol=atol)146 self._compareGradient(x, [0, 1, 2, 3], rtol=rtol, atol=atol)147class SumReductionTest(BaseReductionTest):148 def _tf_reduce(self, x, reduction_axes, keepdims):149 return math_ops.reduce_sum(x, reduction_axes, keepdims)150 def _np_reduce(self, x, reduction_axes, keepdims):151 if isinstance(reduction_axes, list) or isinstance(reduction_axes,152 np.ndarray):153 reduction_axes = tuple(reduction_axes)154 return np.sum(x, axis=reduction_axes, keepdims=keepdims)155 def testAxesType(self):156 for dtype in [dtypes.int64, dtypes.int32]:157 with self.cached_session(use_gpu=True) as sess:158 v = math_ops.reduce_sum([0, 0], constant_op.constant(0, dtype=dtype))159 tf_v = self.evaluate(v)160 self.assertAllEqual(tf_v, 0)161 @test_util.run_deprecated_v1162 def testInfinity(self):163 for dtype in [np.float32, np.float64]:164 for special_value_x in [-np.inf, np.inf]:165 for special_value_y in [-np.inf, np.inf]:166 np_arr = np.array([special_value_x, special_value_y]).astype(dtype)167 self._compareAll(np_arr, None)168 @test_util.run_deprecated_v1169 def testInt32(self):170 for rank in range(1, _MAX_RANK + 1):171 np_arr = self._makeIncremental((2,) * rank, dtypes.int32)172 self._compareAllAxes(np_arr)173 @test_util.run_deprecated_v1174 def testFloat16(self):175 for rank in range(1, _MAX_RANK + 1):176 np_arr = self._makeIncremental((2,) * rank, dtypes.float16)177 self._compareAllAxes(np_arr)178 # test that mean doesn't overflow179 # only on GPU, since it has the more accurate implementation180 if not test.is_gpu_available():181 return182 arr = np.ones([68000], dtype=np.float16)183 with self.session(graph=ops.Graph(), use_gpu=True) as sess:184 tf_arr = variables.Variable(arr)185 variables.global_variables_initializer().run()186 tf_mean = math_ops.reduce_mean(tf_arr, 0, False)187 tf_out_mean = self.evaluate(tf_mean)188 self.assertAllClose(tf_out_mean, 1.)189 @test_util.run_deprecated_v1190 def testFloat32(self):191 for rank in range(1, _MAX_RANK + 1):192 np_arr = self._makeIncremental((2,) * rank, dtypes.float32)193 self._compareAllAxes(np_arr)194 for _ in range(10):195 size_x = int(2**np.random.uniform(0, 15))196 size_y = int(2**np.random.uniform(0, 15))197 if size_x * size_y > 1e7:198 size_y = int(1e7 / size_x)199 arr = np.ones([size_x, size_y], dtype=np.float32)200 col_sum = np.sum(arr, axis=0)201 row_sum = np.sum(arr, axis=1)202 with self.session(graph=ops.Graph(), use_gpu=True) as sess:203 tf_row_sum = self._tf_reduce(arr, 1, False)204 tf_col_sum = self._tf_reduce(arr, 0, False)205 tf_out_row, tf_out_col = self.evaluate([tf_row_sum, tf_col_sum])206 self.assertAllClose(col_sum, tf_out_col)207 self.assertAllClose(row_sum, tf_out_row)208 for size_x in [1, 3, 16, 33]:209 for size_y in [1, 3, 16, 33]:210 for size_z in [1, 3, 16, 33]:211 arr = np.ones([size_x, size_y, size_z], dtype=np.float32)212 sum_y = np.sum(arr, axis=1)213 sum_xz = np.sum(arr, axis=(0, 2))214 with self.session(graph=ops.Graph(), use_gpu=True) as sess:215 tf_sum_xz = self._tf_reduce(arr, [0, 2], False)216 tf_sum_y = self._tf_reduce(arr, 1, False)217 tf_out_sum_xz, tf_out_sum_y = self.evaluate([tf_sum_xz, tf_sum_y])218 self.assertAllClose(sum_y, tf_out_sum_y)219 self.assertAllClose(sum_xz, tf_out_sum_xz)220 @test_util.run_deprecated_v1221 def testFloat64(self):222 for rank in range(1, _MAX_RANK + 1):223 np_arr = self._makeIncremental((2,) * rank, dtypes.float64)224 self._compareAllAxes(np_arr)225 @test_util.run_deprecated_v1226 def testComplex64(self):227 for rank in range(1, _MAX_RANK + 1):228 np_arr = self._makeIncremental((2,) * rank, dtypes.complex64)229 self._compareAllAxes(np_arr)230 @test_util.run_deprecated_v1231 def testComplex128(self):232 for rank in range(1, _MAX_RANK + 1):233 np_arr = self._makeIncremental((2,) * rank, dtypes.complex128)234 self._compareAllAxes(np_arr)235 @test_util.run_deprecated_v1236 def testInvalidIndex(self):237 np_arr = np.arange(0, 10).reshape([2, 5]).astype(np.float32)238 input_tensor = ops.convert_to_tensor(np_arr)239 with self.assertRaisesWithPredicateMatch(240 ValueError, lambda e: "Invalid reduction dimension" in str(e)):241 math_ops.reduce_sum(input_tensor, [-3])242 with self.assertRaisesWithPredicateMatch(243 ValueError, lambda e: "Invalid reduction dimension" in str(e)):244 math_ops.reduce_sum(input_tensor, [2])245 with self.assertRaisesWithPredicateMatch(246 ValueError, lambda e: "Invalid reduction dimension" in str(e)):247 math_ops.reduce_sum(input_tensor, [0, 2])248 @test_util.run_deprecated_v1249 def testPartialShapes(self):250 np.random.seed(1618)251 # Input shape is unknown.252 reduction_axes = [1, 2]253 c_unknown = array_ops.placeholder(dtypes.float32)254 s_unknown = math_ops.reduce_sum(c_unknown, reduction_axes)255 self.assertEqual(tensor_shape.unknown_shape(), s_unknown.get_shape())256 np_input = np.random.randn(3, 3, 3)257 self._compareAll(np_input, reduction_axes, {c_unknown: np_input})258 # Input shape only has known rank.259 c_known_rank = array_ops.placeholder(dtypes.float32)260 c_known_rank.set_shape(tensor_shape.unknown_shape(rank=3))261 s_known_rank = math_ops.reduce_sum(262 c_known_rank, reduction_axes, keepdims=True)263 self.assertEqual(3, s_known_rank.get_shape().rank)264 np_input = np.random.randn(3, 3, 3)265 self._compareAll(np_input, reduction_axes, {c_known_rank: np_input})266 # Reduction indices are unknown.267 unknown_indices = array_ops.placeholder(dtypes.int32)268 c_unknown_indices = constant_op.constant([[10.0], [20.0]])269 s_unknown_indices = math_ops.reduce_sum(270 c_unknown_indices, unknown_indices, keepdims=False)271 self.assertEqual(tensor_shape.unknown_shape(),272 s_unknown_indices.get_shape())273 s_unknown_indices_keep = math_ops.reduce_sum(274 c_unknown_indices, unknown_indices, keepdims=True)275 self.assertEqual(2, s_unknown_indices_keep.get_shape().rank)276 @test_util.run_deprecated_v1277 def testWrongShapeForReductionIndices(self):278 reduction_axes = [[1], [2]]279 c_unknown = array_ops.placeholder(dtypes.float32)280 with self.assertRaisesWithPredicateMatch(ValueError,281 ".*must be at most rank 1.*"):282 math_ops.reduce_sum(c_unknown, reduction_axes)283 # Int64??284 @test_util.run_deprecated_v1285 def testGradient(self):286 for dtype in [287 dtypes.float32, dtypes.float64, dtypes.complex64, dtypes.complex128288 ]:289 x = self._makeIncremental([2, 3, 4, 2], dtype)290 self._compareGradientAxes(x)291 @test_util.run_deprecated_v1292 def testHighRank(self):293 # Do a bunch of random high dimensional reductions294 np.random.seed(42)295 for _ in range(20):296 rank = np.random.randint(4, 10 + 1)297 axes, = np.nonzero(np.random.randint(2, size=rank))298 shape = tuple(np.random.randint(1, 3 + 1, size=rank))299 data = np.random.randint(1024, size=shape)300 self._compareAll(data, axes)301 # Check some particular axis patterns302 for rank in 4, 7, 10:303 shape = tuple(np.random.randint(1, 3 + 1, size=rank))304 data = np.random.randint(1024, size=shape)305 for axes in ([], np.arange(rank), np.arange(0, rank, 2),306 np.arange(1, rank, 2)):307 self._compareAll(data, axes)308 @test_util.run_deprecated_v1309 def testExpand(self):310 # Reduce an empty tensor to a nonempty tensor311 x = np.zeros((5, 0))312 self._compareAll(x, [1])313 @test_util.run_deprecated_v1314 def testEmptyGradients(self):315 with self.session(use_gpu=True):316 x = array_ops.zeros([0, 3])317 y = math_ops.reduce_sum(x, [1])318 error = gradient_checker.compute_gradient_error(x, [0, 3], y, [0])319 self.assertEqual(error, 0)320 @test_util.run_deprecated_v1321 def testDegenerate(self):322 with self.session(use_gpu=True):323 for dtype in (dtypes.float16, dtypes.float32, dtypes.float64,324 dtypes.complex64, dtypes.complex128):325 # A large number is needed to get Eigen to die326 x = array_ops.zeros((0, 9938), dtype=dtype)327 y = math_ops.reduce_sum(x, [0])328 self.assertAllEqual(y.eval(), np.zeros(9938))329class MeanReductionTest(BaseReductionTest):330 def _tf_reduce(self, x, reduction_axes, keepdims):331 return math_ops.reduce_mean(x, reduction_axes, keepdims)332 def _np_reduce(self, x, reduction_axes, keepdims):333 if isinstance(reduction_axes, list) or isinstance(reduction_axes,334 np.ndarray):335 reduction_axes = tuple(reduction_axes)336 elif isinstance(reduction_axes, numbers.Integral):337 reduction_axes = (reduction_axes,)338 if reduction_axes is None:339 count = np.prod(x.shape)340 else:341 count = np.prod([x.shape[ax] for ax in reduction_axes])342 # np.mean automatically converts integer inputs to float, while TensorFlow's343 # reduce_mean does not. For integer inputs, we emulate TensorFlow's behavior344 # using np.sum and truncating division.345 np_sum = np.sum(x, axis=reduction_axes, keepdims=keepdims)346 if np.issubdtype(x.dtype, np.integer):347 return np_sum // count348 return np_sum / count349 def testAxesType(self):350 for dtype in [dtypes.int64, dtypes.int32]:351 with self.cached_session(use_gpu=True) as sess:352 v = math_ops.reduce_mean([0, 0], constant_op.constant(0, dtype=dtype))353 tf_v = self.evaluate(v)354 self.assertAllEqual(tf_v, 0)355 @test_util.run_deprecated_v1356 def testInfinity(self):357 for dtype in [np.float32, np.float64]:358 for special_value_x in [-np.inf, np.inf]:359 for special_value_y in [-np.inf, np.inf]:360 np_arr = np.array([special_value_x, special_value_y]).astype(dtype)361 self._compareAll(np_arr, None)362 @test_util.run_deprecated_v1363 def testInt32(self):364 for rank in range(1, _MAX_RANK + 1):365 np_arr = self._makeIncremental((2,) * rank, dtypes.int32)366 self._compareAllAxes(np_arr)367 @test_util.run_deprecated_v1368 def testUint8(self):369 for rank in range(1, _MAX_RANK + 1):370 np_arr = self._makeRandom((2,) * rank, dtypes.uint8)371 self._compareAllAxes(np_arr)372 # This tests the issue reported in b/145030710.373 @test_util.run_deprecated_v1374 def testSizeOverflowUint8(self):375 np_arr = self._makeRandom((2**8,), dtypes.uint8)376 self._compareAllAxes(np_arr)377 @test_util.run_deprecated_v1378 def testSizeOverflowInt8(self):379 np_arr = self._makeRandom((2**7,), dtypes.int8)380 self._compareAllAxes(np_arr)381 @test_util.run_deprecated_v1382 def testSizeOverflowUint16(self):383 np_arr = self._makeRandom((2**16,), dtypes.uint16)384 self._compareAllAxes(np_arr)385 @test_util.run_deprecated_v1386 def testSizeOverflowInt16(self):387 np_arr = self._makeRandom((2**15,), dtypes.int16)388 self._compareAllAxes(np_arr)389 @test_util.run_deprecated_v1390 def testFloat32(self):391 for rank in range(1, _MAX_RANK + 1):392 np_arr = self._makeIncremental((2,) * rank, dtypes.float32)393 self._compareAllAxes(np_arr)394 @test_util.run_deprecated_v1395 def testFloat64(self):396 for rank in range(1, _MAX_RANK + 1):397 np_arr = self._makeIncremental((2,) * rank, dtypes.float64)398 self._compareAllAxes(np_arr)399 @test_util.run_deprecated_v1400 def testComplex64(self):401 for rank in range(1, _MAX_RANK + 1):402 np_arr = self._makeIncremental((2,) * rank, dtypes.complex64)403 self._compareAllAxes(np_arr)404 @test_util.run_deprecated_v1405 def testComplex128(self):406 for rank in range(1, _MAX_RANK + 1):407 np_arr = self._makeIncremental((2,) * rank, dtypes.complex128)408 self._compareAllAxes(np_arr)409 @test_util.run_deprecated_v1410 def testGradient(self):411 s = [2, 3, 4, 2]412 for dtype in [dtypes.float32, dtypes.float64]:413 x = self._makeIncremental(s, dtype)414 self._compareGradientAxes(x, rtol=1e-3, atol=1e-3)415 @test_util.run_deprecated_v1416 def testEmptyGradients(self):417 with self.session(use_gpu=True):418 x = array_ops.zeros([0, 3])419 y = math_ops.reduce_mean(x, [1])420 error = gradient_checker.compute_gradient_error(x, [0, 3], y, [0])421 self.assertEqual(error, 0)422 @test_util.run_deprecated_v1423 def testDegenerate(self):424 with self.session(use_gpu=True):425 for dtype in (dtypes.float16, dtypes.float32, dtypes.float64):426 # A large number is needed to get Eigen to die427 x = array_ops.zeros((0, 9938), dtype=dtype)428 y = math_ops.reduce_mean(x, [0]).eval()429 self.assertEqual(y.shape, (9938,))430 self.assertTrue(np.all(np.isnan(y)))431class EuclideanNormReductionTest(BaseReductionTest):432 def _tf_reduce(self, x, reduction_axes, keepdims):433 return math_ops.reduce_euclidean_norm(x, reduction_axes, keepdims)434 def _np_reduce(self, x, reduction_axes, keepdims):435 if isinstance(reduction_axes, list) or isinstance(reduction_axes,436 np.ndarray):437 reduction_axes = tuple(reduction_axes)438 np_fro = np.sqrt(439 np.sum(x * np.conj(x), axis=reduction_axes, keepdims=keepdims))440 if np.issubdtype(x.dtype, np.integer):441 np_fro = np.floor(np_fro)442 return np_fro443 @test_util.run_deprecated_v1444 def testAxesType(self):445 for dtype in [dtypes.int64, dtypes.int32]:446 with self.cached_session(use_gpu=True):447 v = math_ops.reduce_mean([0, 0], constant_op.constant(0, dtype=dtype))448 tf_v = self.evaluate(v)449 self.assertAllEqual(tf_v, 0)450 @test_util.run_deprecated_v1451 def testInfinity(self):452 for dtype in [np.float32, np.float64]:453 for special_value_x in [-np.inf, np.inf]:454 for special_value_y in [-np.inf, np.inf]:455 np_arr = np.array([special_value_x, special_value_y]).astype(dtype)456 self._compareAll(np_arr, None)457 @test_util.run_deprecated_v1458 def testSingleton(self):459 for dtype in [np.float32, np.float64]:460 np_arr = np.array([-1.]).astype(dtype)461 self._compareAll(np_arr, None)462 @test_util.run_deprecated_v1463 def testInt32(self):464 for rank in range(1, _MAX_RANK + 1):465 np_arr = self._makeIncremental((2,) * rank, dtypes.int32)466 self._compareAllAxes(np_arr)467 @test_util.run_deprecated_v1468 def testFloat32(self):469 for rank in range(1, _MAX_RANK + 1):470 np_arr = self._makeIncremental((2,) * rank, dtypes.float32)471 self._compareAllAxes(np_arr)472 @test_util.run_deprecated_v1473 def testFloat64(self):474 for rank in range(1, _MAX_RANK + 1):475 np_arr = self._makeIncremental((2,) * rank, dtypes.float64)476 self._compareAllAxes(np_arr)477 @test_util.run_deprecated_v1478 def testComplex64(self):479 for rank in range(1, _MAX_RANK + 1):480 np_arr = self._makeIncremental((2,) * rank, dtypes.complex64)481 self._compareAllAxes(np_arr)482 @test_util.run_deprecated_v1483 def testComplex128(self):484 for rank in range(1, _MAX_RANK + 1):485 np_arr = self._makeIncremental((2,) * rank, dtypes.complex128)486 self._compareAllAxes(np_arr)487 with self.session(use_gpu=True):488 for dtype in (dtypes.float16, dtypes.float32, dtypes.float64):489 # A large number is needed to get Eigen to die490 x = array_ops.zeros((0, 9938), dtype=dtype)491 y = math_ops.reduce_euclidean_norm(x, [0]).eval()492 self.assertEqual(y.shape, (9938,))493 self.assertAllEqual(y, np.zeros(9938))494 @test_util.run_deprecated_v1495 def testGradient(self):496 shape = [2, 3, 4, 2]497 for dtype in [dtypes.float32, dtypes.float64]:498 # zero value entry will result NaN gradient if reduction doesn't happen.499 # e.g., `tf.math.reduce_sum([0, 1], axis=[])` so add one to avoid it.500 x = self._makeIncremental(shape, dtype) + 1.0501 self._compareGradientAxes(x, rtol=1e-2, atol=1e-2)502class ProdReductionTest(BaseReductionTest):503 def _tf_reduce(self, x, reduction_axes, keepdims):504 return math_ops.reduce_prod(x, reduction_axes, keepdims)505 def _np_reduce(self, x, reduction_axes, keepdims):506 if isinstance(reduction_axes, list) or isinstance(reduction_axes,507 np.ndarray):508 reduction_axes = tuple(reduction_axes)509 return np.prod(x, axis=reduction_axes, keepdims=keepdims)510 def testAxesType(self):511 for dtype in [dtypes.int64, dtypes.int32]:512 with self.cached_session(use_gpu=True) as sess:513 v = math_ops.reduce_prod([0, 0], constant_op.constant(0, dtype=dtype))514 tf_v = self.evaluate(v)515 self.assertAllEqual(tf_v, 0)516 @test_util.run_deprecated_v1517 def testInfinity(self):518 for dtype in [np.float32, np.float64]:519 for special_value_x in [-np.inf, np.inf]:520 for special_value_y in [-np.inf, np.inf]:521 np_arr = np.array([special_value_x, special_value_y]).astype(dtype)522 self._compareAll(np_arr, None)523 @test_util.run_deprecated_v1524 def testInt32(self):525 # Numpy automatically upgrades the type of np.prod from int32 to int64, so526 # Numpy does not overflow an int32 np.prod while TensorFlow does. To avoid527 # overflow, divide the incremental int32 array by 2.528 for rank in range(1, _MAX_RANK + 1):529 np_arr = self._makeIncremental((2,) * rank, dtypes.int32) / 2530 self._compareAllAxes(np_arr)531 @test_util.run_deprecated_v1532 def testFloat32(self):533 for rank in range(1, _MAX_RANK + 1):534 np_arr = self._makeIncremental((2,) * rank, dtypes.float32)535 self._compareAllAxes(np_arr)536 @test_util.run_deprecated_v1537 def testFloat64(self):538 for rank in range(1, _MAX_RANK + 1):539 np_arr = self._makeIncremental((2,) * rank, dtypes.float64)540 self._compareAllAxes(np_arr)541 @test_util.run_deprecated_v1542 def testComplex64(self):543 for rank in range(1, _MAX_RANK + 1):544 np_arr = self._makeIncremental((2,) * rank, dtypes.complex64)545 self._compareAllAxes(np_arr)546 @test_util.run_deprecated_v1547 def testComplex128(self):548 for rank in range(1, _MAX_RANK + 1):549 np_arr = self._makeIncremental((2,) * rank, dtypes.complex128)550 self._compareAllAxes(np_arr)551 @test_util.run_deprecated_v1552 def testGradientWithZeros(self):553 s = [2, 3, 4, 2]554 x = self._makeIncremental(s, dtypes.float32) / 20.555 # No zeros in input556 self._compareGradientAxes(x, rtol=1e-3, atol=1e-3)557 # Zero at beginning558 x1 = x.copy()559 x1[:, :, 0, :] = 0560 self._compareGradientAxes(x1, rtol=1e-3, atol=1e-3)561 # Zero at end562 x2 = x.copy()563 x2[:, :, -1, :] = 0564 self._compareGradientAxes(x2, rtol=1e-3, atol=1e-3)565 # Zero in middle566 x3 = x.copy()567 x3[:, :, 2, :] = 0568 self._compareGradientAxes(x3, rtol=1e-3, atol=1e-3)569 # All zeros570 x4 = x.copy()571 x4[:, :, :, :] = 0572 self._compareGradientAxes(x4, rtol=1e-3, atol=1e-3)573 @test_util.run_deprecated_v1574 def testEmptyGradients(self):575 with self.session(use_gpu=True):576 x = array_ops.zeros([0, 3])577 y = math_ops.reduce_prod(x, [1])578 error = gradient_checker.compute_gradient_error(x, [0, 3], y, [0])579 self.assertEqual(error, 0)580 @test_util.run_deprecated_v1581 def testDegenerate(self):582 with self.session(use_gpu=True):583 for dtype in (dtypes.float16, dtypes.float32, dtypes.float64):584 # A large number is needed to get Eigen to die585 x = array_ops.zeros((0, 9938), dtype=dtype)586 y = math_ops.reduce_prod(x, [0])587 self.assertAllEqual(y.eval(), np.ones(9938))588class MinReductionTest(test.TestCase):589 def _compare(self, x, reduction_axes, keepdims, use_gpu=False):590 np_ans = x591 if reduction_axes is None:592 np_ans = np.amin(np_ans, keepdims=keepdims)593 else:594 for ra in reduction_axes[::-1]:595 np_ans = np.amin(np_ans, axis=ra, keepdims=keepdims)596 with self.cached_session(use_gpu=use_gpu):597 if reduction_axes is not None:598 reduction_axes = np.array(reduction_axes).astype(np.int32)599 tf_ans = math_ops.reduce_min(x, reduction_axes, keepdims)600 out = self.evaluate(tf_ans)601 self.assertAllClose(np_ans, out)602 self.assertShapeEqual(np_ans, tf_ans)603 def _compareAll(self, x, reduction_axes):604 self._compare(x, reduction_axes, False, use_gpu=True)605 self._compare(x, reduction_axes, False, use_gpu=False)606 self._compare(x, reduction_axes, True, use_gpu=True)607 self._compare(x, reduction_axes, True, use_gpu=False)608 def testAxesType(self):609 for dtype in [dtypes.int64, dtypes.int32]:610 with self.cached_session(use_gpu=True) as sess:611 v = math_ops.reduce_min([0, 0], constant_op.constant(0, dtype=dtype))612 tf_v = self.evaluate(v)613 self.assertAllEqual(tf_v, 0)614 @test_util.run_deprecated_v1615 def testInfinity(self):616 for dtype in [np.float32, np.float64]:617 for special_value_x in [-np.inf, np.inf]:618 for special_value_y in [-np.inf, np.inf]:619 np_arr = np.array([special_value_x, special_value_y]).astype(dtype)620 self._compareAll(np_arr, None)621 def testFloatReduce3D(self):622 # Create a 3D array of floats and reduce across all possible623 # dimensions624 np_arr = np.arange(1, 31).reshape([2, 3, 5]).astype(np.float32)625 self._compareAll(np_arr, None)626 self._compareAll(np_arr, [])627 self._compareAll(np_arr, [0])628 self._compareAll(np_arr, [1])629 self._compareAll(np_arr, [2])630 self._compareAll(np_arr, [0, 1])631 self._compareAll(np_arr, [1, 2])632 self._compareAll(np_arr, [0, 2])633 self._compareAll(np_arr, [0, 1, 2])634 def testDoubleReduce3D(self):635 # Create a 3D array of doubles and reduce across all possible636 # dimensions637 np_arr = np.arange(1, 31).reshape([2, 3, 5]).astype(np.float64)638 self._compareAll(np_arr, None)639 self._compareAll(np_arr, [])640 self._compareAll(np_arr, [0])641 self._compareAll(np_arr, [1])642 self._compareAll(np_arr, [2])643 self._compareAll(np_arr, [0, 1])644 self._compareAll(np_arr, [1, 2])645 self._compareAll(np_arr, [0, 2])646 self._compareAll(np_arr, [0, 1, 2])647 @test_util.run_deprecated_v1648 def testGradient(self):649 s = [2, 3, 4, 2]650 x = np.arange(1.0, 49.0).reshape(s).astype(np.float64)651 with self.cached_session():652 t = ops.convert_to_tensor(x)653 su = math_ops.reduce_min(t, [1, 2])654 jacob_t, jacob_n = gradient_checker.compute_gradient(655 t, s, su, [2, 2], x_init_value=x, delta=1)656 self.assertAllClose(jacob_t, jacob_n, rtol=1e-8, atol=1e-8)657 @test_util.run_deprecated_v1658 def testGradient2(self):659 s = [2, 3, 4, 2]660 x = np.arange(1.0, 49.0).reshape(s).astype(np.float64)661 with self.cached_session():662 t = ops.convert_to_tensor(x)663 su = math_ops.reduce_min(t, [1])664 jacob_t, jacob_n = gradient_checker.compute_gradient(665 t, s, su, [2, 4, 2], x_init_value=x, delta=1)666 self.assertAllClose(jacob_t, jacob_n, rtol=1e-8, atol=1e-8)667 @test_util.run_deprecated_v1668 def testGradient3(self):669 s = [2, 3, 4, 2]670 x = np.arange(1.0, 49.0).reshape(s).astype(np.float64)671 with self.cached_session():672 t = ops.convert_to_tensor(x)673 su = math_ops.reduce_min(t, [2])674 jacob_t, jacob_n = gradient_checker.compute_gradient(675 t, s, su, [2, 3, 2], x_init_value=x, delta=1)676 self.assertAllClose(jacob_t, jacob_n, rtol=1e-8, atol=1e-8)677 @test_util.run_deprecated_v1678 def testGradient4(self):679 s = [2, 3, 4, 2]680 x = np.arange(1.0, 49.0).reshape(s).astype(np.float64)681 with self.cached_session():682 t = ops.convert_to_tensor(x)683 su = math_ops.reduce_min(t)684 jacob_t, jacob_n = gradient_checker.compute_gradient(685 t, s, su, [1], x_init_value=x, delta=1)686 self.assertAllClose(jacob_t, jacob_n, rtol=1e-8, atol=1e-8)687 @test_util.run_deprecated_v1688 def testEmptyGradients(self):689 with self.cached_session():690 x = array_ops.zeros([0, 3])691 y = math_ops.reduce_min(x, [1])692 error = gradient_checker.compute_gradient_error(x, [0, 3], y, [0])693 self.assertEqual(error, 0)694class MaxReductionTest(test.TestCase):695 def _compare(self, x, reduction_axes, keepdims, use_gpu=False):696 np_ans = x697 if reduction_axes is None:698 np_ans = np.amax(np_ans, keepdims=keepdims)699 else:700 for ra in reduction_axes[::-1]:701 np_ans = np.amax(np_ans, axis=ra, keepdims=keepdims)702 with self.cached_session(use_gpu=use_gpu):703 if reduction_axes is not None:704 reduction_axes = np.array(reduction_axes).astype(np.int32)705 tf_ans = math_ops.reduce_max(x, reduction_axes, keepdims)706 out = self.evaluate(tf_ans)707 self.assertAllClose(np_ans, out)708 self.assertShapeEqual(np_ans, tf_ans)709 def _compareAll(self, x, reduction_axes):710 self._compare(x, reduction_axes, False, use_gpu=True)711 self._compare(x, reduction_axes, False, use_gpu=False)712 self._compare(x, reduction_axes, True, use_gpu=True)713 self._compare(x, reduction_axes, True, use_gpu=False)714 def testAxesType(self):715 for dtype in [dtypes.int64, dtypes.int32]:716 with self.cached_session(use_gpu=True) as sess:717 v = math_ops.reduce_max([0, 0], constant_op.constant(0, dtype=dtype))718 tf_v = self.evaluate(v)719 self.assertAllEqual(tf_v, 0)720 @test_util.run_deprecated_v1721 def testInfinity(self):722 for dtype in [np.float32, np.float64]:723 for special_value_x in [-np.inf, np.inf]:724 for special_value_y in [-np.inf, np.inf]:725 np_arr = np.array([special_value_x, special_value_y]).astype(dtype)726 self._compareAll(np_arr, None)727 def testInt64Reduce3D(self):728 # Create a 3D array of int64s and reduce across all possible729 # dimensions730 np_arr = np.arange(-31, -1).reshape([2, 3, 5]).astype(np.int64)731 self._compareAll(np_arr, None)732 self._compareAll(np_arr, [])733 self._compareAll(np_arr, [0])734 self._compareAll(np_arr, [1])735 self._compareAll(np_arr, [2])736 self._compareAll(np_arr, [0, 1])737 self._compareAll(np_arr, [1, 2])738 self._compareAll(np_arr, [0, 2])739 self._compareAll(np_arr, [0, 1, 2])740 def testFloatReduce3D(self):741 # Create a 3D array of floats and reduce across all possible742 # dimensions743 np_arr = np.arange(-31, -1).reshape([2, 3, 5]).astype(np.float32)744 self._compareAll(np_arr, None)745 self._compareAll(np_arr, [])746 self._compareAll(np_arr, [0])747 self._compareAll(np_arr, [1])748 self._compareAll(np_arr, [2])749 self._compareAll(np_arr, [0, 1])750 self._compareAll(np_arr, [1, 2])751 self._compareAll(np_arr, [0, 2])752 self._compareAll(np_arr, [0, 1, 2])753 def testDoubleReduce3D(self):754 # Create a 3D array of doubles and reduce across all possible755 # dimensions756 np_arr = np.arange(-31, -1).reshape([2, 3, 5]).astype(np.float64)757 self._compareAll(np_arr, None)758 self._compareAll(np_arr, [])759 self._compareAll(np_arr, [0])760 self._compareAll(np_arr, [1])761 self._compareAll(np_arr, [2])762 self._compareAll(np_arr, [0, 1])763 self._compareAll(np_arr, [1, 2])764 self._compareAll(np_arr, [0, 2])765 self._compareAll(np_arr, [0, 1, 2])766 @test_util.run_deprecated_v1767 def testGradient(self):768 s = [2, 3, 4, 2]769 x = np.arange(-49.0, -1.0).reshape(s).astype(np.float64)770 with self.cached_session():771 t = ops.convert_to_tensor(x)772 su = math_ops.reduce_max(t, [1, 2])773 jacob_t, jacob_n = gradient_checker.compute_gradient(774 t, s, su, [2, 2], x_init_value=x, delta=1)775 self.assertAllClose(jacob_t, jacob_n, rtol=1e-8, atol=1e-8)776 @test_util.run_deprecated_v1777 def testGradient2(self):778 s = [2, 3, 4, 2]779 x = np.arange(-49.0, -1.0).reshape(s).astype(np.float64)780 with self.cached_session():781 t = ops.convert_to_tensor(x)782 su = math_ops.reduce_max(t, [1])783 jacob_t, jacob_n = gradient_checker.compute_gradient(784 t, s, su, [2, 4, 2], x_init_value=x, delta=1)785 self.assertAllClose(jacob_t, jacob_n, rtol=1e-8, atol=1e-8)786 @test_util.run_deprecated_v1787 def testGradient3(self):788 s = [2, 3, 4, 2]789 x = np.arange(-49.0, -1.0).reshape(s).astype(np.float64)790 with self.cached_session():791 t = ops.convert_to_tensor(x)792 su = math_ops.reduce_max(t, [2])793 jacob_t, jacob_n = gradient_checker.compute_gradient(794 t, s, su, [2, 3, 2], x_init_value=x, delta=1)795 self.assertAllClose(jacob_t, jacob_n, rtol=1e-8, atol=1e-8)796 @test_util.run_deprecated_v1797 def testGradient4(self):798 s = [2, 3, 4, 2]799 x = np.arange(-49.0, -1.0).reshape(s).astype(np.float64)800 with self.cached_session():801 t = ops.convert_to_tensor(x)802 su = math_ops.reduce_max(t)803 jacob_t, jacob_n = gradient_checker.compute_gradient(804 t, s, su, [1], x_init_value=x, delta=1)805 self.assertAllClose(jacob_t, jacob_n, rtol=1e-8, atol=1e-8)806 @test_util.run_deprecated_v1807 def testEmptyGradients(self):808 with self.cached_session():809 x = array_ops.zeros([0, 3])810 y = math_ops.reduce_max(x, [1])811 error = gradient_checker.compute_gradient_error(x, [0, 3], y, [0])812 self.assertEqual(error, 0)813class AllReductionTest(test.TestCase):814 def _compare(self, x, reduction_axes, keepdims, use_gpu=False):815 np_ans = x816 if reduction_axes is None:817 np_ans = np.all(np_ans, keepdims=keepdims)818 else:819 for ra in reduction_axes[::-1]:820 np_ans = np.all(np_ans, axis=ra, keepdims=keepdims)821 with self.cached_session(use_gpu=use_gpu):822 if reduction_axes is not None:823 reduction_axes = np.array(reduction_axes).astype(np.int32)824 tf_ans = math_ops.reduce_all(x, reduction_axes, keepdims)825 out = self.evaluate(tf_ans)826 self.assertAllEqual(np_ans, out)827 self.assertShapeEqual(np_ans, tf_ans)828 def _compareAll(self, x, reduction_axes):829 self._compare(x, reduction_axes, False, use_gpu=True)830 self._compare(x, reduction_axes, False, use_gpu=False)831 self._compare(x, reduction_axes, True, use_gpu=True)832 self._compare(x, reduction_axes, True, use_gpu=False)833 def testAxesType(self):834 for dtype in [dtypes.int64, dtypes.int32]:835 with self.session(use_gpu=True) as sess:836 v = math_ops.reduce_all([True, True],837 constant_op.constant(0, dtype=dtype))838 tf_v = self.evaluate(v)839 self.assertAllEqual(tf_v, True)840 def testAll3D(self):841 # Create a 3D array of bools and reduce across all possible842 # dimensions843 np_arr = (np.random.uniform(0, 1, 30) > 0.1).reshape([2, 3, 5])844 self._compareAll(np_arr, None)845 self._compareAll(np_arr, [])846 self._compareAll(np_arr, [0])847 self._compareAll(np_arr, [1])848 self._compareAll(np_arr, [2])849 self._compareAll(np_arr, [0, 1])850 self._compareAll(np_arr, [1, 2])851 self._compareAll(np_arr, [0, 2])852 self._compareAll(np_arr, [0, 1, 2])853 def testEmpty(self):854 self._compareAll([], [0])855class AnyReductionTest(test.TestCase):856 def _compare(self, x, reduction_axes, keepdims, use_gpu=False):857 np_ans = x858 if reduction_axes is None:859 np_ans = np.any(np_ans, keepdims=keepdims)860 else:861 for ra in reduction_axes[::-1]:862 np_ans = np.any(np_ans, axis=ra, keepdims=keepdims)863 with self.cached_session(use_gpu=use_gpu):864 if reduction_axes is not None:865 reduction_axes = np.array(reduction_axes).astype(np.int32)866 tf_ans = math_ops.reduce_any(x, reduction_axes, keepdims)867 out = self.evaluate(tf_ans)868 self.assertAllEqual(np_ans, out)869 self.assertShapeEqual(np_ans, tf_ans)870 def _compareAll(self, x, reduction_axes):871 self._compare(x, reduction_axes, False, use_gpu=True)872 self._compare(x, reduction_axes, False, use_gpu=False)873 self._compare(x, reduction_axes, True, use_gpu=True)874 self._compare(x, reduction_axes, True, use_gpu=False)875 def testAxesType(self):876 for dtype in [dtypes.int64, dtypes.int32]:877 with self.session(use_gpu=True) as sess:878 v = math_ops.reduce_any([True, True],879 constant_op.constant(0, dtype=dtype))880 tf_v = self.evaluate(v)881 self.assertAllEqual(tf_v, True)882 def testAll3D(self):883 # Create a 3D array of bools and reduce across all possible884 # dimensions885 np_arr = (np.random.uniform(0, 1, 30) > 0.9).reshape([2, 3, 5])886 self._compareAll(np_arr, None)887 self._compareAll(np_arr, [])888 self._compareAll(np_arr, [0])889 self._compareAll(np_arr, [1])890 self._compareAll(np_arr, [2])891 self._compareAll(np_arr, [0, 1])892 self._compareAll(np_arr, [1, 2])893 self._compareAll(np_arr, [0, 2])894 self._compareAll(np_arr, [0, 1, 2])895 def testEmpty(self):896 self._compareAll([], [0])897class CountNonzeroReductionTest(test.TestCase):898 def _compare(self, x, reduction_axes, keepdims, use_gpu=False, zero=0,899 feed_dict=None):900 np_ans = (x != zero).astype(np.int32)901 if reduction_axes is None:902 np_ans = np.sum(np_ans, keepdims=keepdims)903 else:904 reduction_axes = np.array(reduction_axes).astype(np.int32)905 for ra in reduction_axes.ravel()[::-1]:906 np_ans = np.sum(np_ans, axis=ra, keepdims=keepdims)907 with self.cached_session(use_gpu=use_gpu) as sess:908 tf_ans = math_ops.count_nonzero(x, reduction_axes, keepdims)909 out = sess.run(tf_ans, feed_dict)910 self.assertAllClose(np_ans, out)911 self.assertShapeEqual(np_ans, tf_ans)912 def _compareAll(self, x, reduction_axes, feed_dict=None):913 if reduction_axes is not None and np.shape(reduction_axes) == (1,):914 # Test scalar reduction_axes argument915 self._compareAll(x, reduction_axes[0])916 self._compare(x, reduction_axes, False, use_gpu=True, feed_dict=feed_dict)917 self._compare(x, reduction_axes, False, use_gpu=False, feed_dict=feed_dict)918 self._compare(x, reduction_axes, True, use_gpu=True, feed_dict=feed_dict)919 self._compare(x, reduction_axes, True, use_gpu=False, feed_dict=feed_dict)920 @test_util.run_deprecated_v1921 def testBoolReduce1D(self):922 # Create a 1D array of floats923 np_arr = np.asarray([False, False, True, False, False, True])924 self._compareAll(np_arr, None)925 self._compareAll(np_arr, [])926 self._compareAll(np_arr, [0])927 @test_util.run_deprecated_v1928 def testFloatReduce1D(self):929 # Create a 1D array of floats930 np_arr = np.asarray([0.0, 1.0, -1.0, 0.0, 0.0, 3.0]).astype(np.float32)931 self._compareAll(np_arr, [0])932 @test_util.run_deprecated_v1933 def testFloatReduce4D(self):934 # Create a 4D array of floats and reduce across some935 # dimensions936 np_arr = np.floor(np.arange(0.0, 210.0) / 100.0).reshape([2, 3, 5,937 7]).astype(938 np.float32)939 self._compareAll(np_arr, None)940 self._compareAll(np_arr, [])941 self._compareAll(np_arr, [0])942 self._compareAll(np_arr, [1])943 self._compareAll(np_arr, [2])944 self._compareAll(np_arr, [0, 1])945 self._compareAll(np_arr, [1, 2])946 # Need specialization for reduce(4D, [0, 2])947 # self._compareAll(np_arr, [0, 2])948 self._compareAll(np_arr, [0, 1, 2])949 self._compareAll(np_arr, [1, 2, 3])950 self._compareAll(np_arr, [0, 1, 2, 3])951 @test_util.run_deprecated_v1952 def testExpand(self):953 # Reduce an empty tensor to a nonempty tensor954 x = np.zeros((5, 0))955 self._compareAll(x, [1])956 @test_util.run_deprecated_v1957 def testDegenerate(self):958 for use_gpu in False, True:959 with self.cached_session(use_gpu=use_gpu):960 for dtype in (dtypes.bool,):961 # A large number is needed to get Eigen to die962 x = array_ops.zeros((0, 9938), dtype=dtype)963 y = math_ops.count_nonzero(x, [0])964 self.assertAllEqual(y.eval(), np.zeros(9938))965 def testStringReduce(self):966 # Test case for GitHub issue 18712967 with self.cached_session() as sess:968 v = math_ops.count_nonzero(constant_op.constant(["test"]))969 self.assertAllClose(self.evaluate(v), 1)970 @test_util.run_deprecated_v1971 def testStringReduce1D(self):972 # Create a 1D array of strings973 x = np.asarray(["", "", "a", "", "", "b"])974 self._compare(x, None, keepdims=False, zero=np.str(""))975 self._compare(x, [], keepdims=False, zero=np.str(""))976 self._compare(x, [0], keepdims=False, zero=np.str(""))977 self._compare(x, None, keepdims=True, zero=np.str(""))978 self._compare(x, [], keepdims=True, zero=np.str(""))979 self._compare(x, [0], keepdims=True, zero=np.str(""))980 @test_util.run_deprecated_v1981 def testStringReduce2D(self):982 # Create a 2D array of strings983 x = np.asarray([["", "", "a", "", "", "b"],984 ["", "c", "", "d", "", ""],985 ["e", "", "f", "", "", ""]])986 self._compare(x, None, keepdims=False, zero=np.str(""))987 self._compare(x, [], keepdims=False, zero=np.str(""))988 self._compare(x, [0], keepdims=False, zero=np.str(""))989 self._compare(x, [1], keepdims=False, zero=np.str(""))990 self._compare(x, [0, 1], keepdims=False, zero=np.str(""))991 self._compare(x, None, keepdims=True, zero=np.str(""))992 self._compare(x, [], keepdims=True, zero=np.str(""))993 self._compare(x, [0], keepdims=True, zero=np.str(""))994 self._compare(x, [0, 1], keepdims=True, zero=np.str(""))995if __name__ == "__main__":...
cwise_ops_binary_test.py
Source:cwise_ops_binary_test.py
...729 for y in data:730 self.assertEqual(self._compareScalar(math_ops.equal, x, y, t), x == y)731 self.assertEqual(732 self._compareScalar(math_ops.not_equal, x, y, t), x != y)733 def _compare(self, x, y, np_func, tf_func):734 np_ans = np_func(x, y)735 with test_util.use_gpu():736 out = tf_func(ops.convert_to_tensor(x), ops.convert_to_tensor(y))737 tf_ans = self.evaluate(out)738 self.assertAllEqual(np_ans, tf_ans)739 def testTensorCompareTensor(self):740 x = np.linspace(-15, 15, 6).reshape(1, 3, 2)741 y = np.linspace(20, -10, 6).reshape(1, 3, 2)742 for t in [np.float16, np.float32, np.float64, np.int32, np.int64]:743 xt = x.astype(t)744 yt = y.astype(t)745 self._compare(xt, yt, np.less, math_ops.less)746 self._compare(xt, yt, np.less_equal, math_ops.less_equal)747 self._compare(xt, yt, np.greater, math_ops.greater)748 self._compare(xt, yt, np.greater_equal, math_ops.greater_equal)749 self._compare(xt, yt, np.equal, math_ops.equal)750 self._compare(xt, yt, np.not_equal, math_ops.not_equal)751 # Complex types do not support ordering but do support equality tests.752 for t in [np.complex64, np.complex128]:753 xt = x.astype(t)754 xt -= 1j * xt755 yt = y.astype(t)756 yt -= 1j * yt757 self._compare(xt, yt, np.equal, math_ops.equal)758 self._compare(xt, yt, np.not_equal, math_ops.not_equal)759 def _compareBCast(self, xs, ys, dtype, np_func, tf_func):760 x = np.linspace(-15, 15, np.prod(xs)).astype(dtype).reshape(xs)761 y = np.linspace(20, -10, np.prod(ys)).astype(dtype).reshape(ys)762 if dtype in (np.complex64, np.complex128):763 x -= 1j * x764 y -= 1j * y765 self._compare(x, y, np_func, tf_func)766 self._compare(y, x, np_func, tf_func)767 def _testBCastByFunc(self, np_func, tf_func, include_complex=False):768 shapes = [769 ([1, 3, 2], [1]),770 ([1, 3, 2], [2]),771 ([1, 3, 2], [3, 2]),772 ([1, 3, 2], [3, 1]),773 ([1, 3, 2], [1, 3, 2]),774 ([1, 3, 2], [2, 3, 1]),775 ([1, 3, 2], [2, 1, 1]),776 ([1, 3, 2], [1, 3, 1]),777 ([2, 1, 5], [2, 3, 1]),778 ([2, 0, 5], [2, 0, 1]),779 ([2, 3, 0], [2, 3, 1]),780 ]...
cwise_ops_unary_test.py
Source:cwise_ops_unary_test.py
1# Copyright 2018 The TensorFlow Authors. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14# ==============================================================================15"""Functional tests for unary coefficient-wise operations."""16from __future__ import absolute_import17from __future__ import division18from __future__ import print_function19import math20import numpy as np21from tensorflow.python.eager import backprop22from tensorflow.python.framework import constant_op23from tensorflow.python.framework import dtypes as dtypes_lib24from tensorflow.python.framework import ops25from tensorflow.python.framework import sparse_tensor26from tensorflow.python.framework import test_util27from tensorflow.python.ops import gen_math_ops28from tensorflow.python.ops import gradient_checker29from tensorflow.python.ops import gradient_checker_v230from tensorflow.python.ops import math_ops31from tensorflow.python.ops import nn_grad # pylint: disable=unused-import32from tensorflow.python.platform import test33from tensorflow.python.platform import tf_logging34_NEG = lambda x: -x35_ABS = abs36# TODO(zongheng): it'd be great to factor out this function and various random37# SparseTensor gen funcs.38def _sparsify(x, thresh=0.5, index_dtype=np.int64):39 x[x < thresh] = 040 non_zero = np.where(x)41 x_indices = np.vstack(non_zero).astype(index_dtype).T42 x_values = x[non_zero]43 x_shape = x.shape44 return sparse_tensor.SparseTensor(45 indices=x_indices, values=x_values, dense_shape=x_shape), x_values46def _default_tolerance(dtype):47 """Returns a sensible default tolerance for comparing results of a given type.48 Args:49 dtype: A datatype.50 """51 if dtype == np.float16:52 return 5e-353 elif dtype in (np.float32, np.complex64):54 return 1e-355 elif dtype in (np.float64, np.complex128):56 return 1e-557 else:58 return None # Fail fast for unexpected types59class UnaryOpTest(test.TestCase):60 def _compareCpu(self, x, np_func, tf_func, grad_rtol=None, grad_atol=None):61 if grad_rtol is None:62 grad_rtol = _default_tolerance(x.dtype)63 if grad_atol is None:64 grad_atol = _default_tolerance(x.dtype)65 np_ans = np_func(x)66 with self.cached_session(use_gpu=False):67 inx = ops.convert_to_tensor(x)68 if x.dtype in (np.float32, np.float64,69 dtypes_lib.bfloat16.as_numpy_dtype):70 y = 1.1 * tf_func(inx)71 np_ans *= 1.172 else:73 y = tf_func(inx)74 tf_cpu = self.evaluate(y)75 self.assertShapeEqual(np_ans, y)76 if x.dtype == np.float16:77 self.assertAllClose(np_ans, tf_cpu, rtol=1e-3, atol=1e-3)78 elif x.dtype == dtypes_lib.bfloat16.as_numpy_dtype:79 self.assertAllClose(np_ans, tf_cpu, rtol=1e-2, atol=1e-2)80 else:81 self.assertAllClose(np_ans, tf_cpu)82 if x.dtype in (np.complex64, np.complex128) and tf_func == math_ops.sign:83 return # Return early84 if x.dtype == np.float16:85 s = list(np.shape(x))86 jacob_t, _ = gradient_checker.compute_gradient(87 inx, s, y, s, x_init_value=x)88 xf = x.astype(np.float)89 inxf = ops.convert_to_tensor(xf)90 yf = tf_func(inxf)91 _, jacob_n = gradient_checker.compute_gradient(92 inxf, s, yf, s, x_init_value=xf, delta=1e-2)93 jacob_n = jacob_n.astype(np.float16)94 self.assertAllClose(jacob_t, jacob_n, rtol=grad_rtol, atol=grad_atol)95 elif x.dtype in (np.float32, np.complex64):96 s = list(np.shape(x))97 jacob_t, jacob_n = gradient_checker.compute_gradient(98 inx, s, y, s, x_init_value=x, delta=1e-3)99 self.assertAllClose(jacob_t, jacob_n, rtol=grad_rtol, atol=grad_atol)100 elif x.dtype in (np.float64, np.complex128):101 s = list(np.shape(x))102 jacob_t, jacob_n = gradient_checker.compute_gradient(103 inx, s, y, s, x_init_value=x, delta=1e-5)104 self.assertAllClose(jacob_t, jacob_n, rtol=grad_rtol, atol=grad_atol)105 def _check(self, result_tensor, result_np, input_sp_t, tol):106 self.assertTrue(isinstance(result_tensor, sparse_tensor.SparseTensor))107 self.assertTrue(isinstance(input_sp_t, sparse_tensor.SparseTensor))108 self.assertAllEqual(input_sp_t.indices, result_tensor.indices)109 self.assertAllEqual(input_sp_t.dense_shape, result_tensor.dense_shape)110 if tol is None:111 self.assertAllClose(result_np, result_tensor.values)112 else:113 self.assertAllClose(result_np, result_tensor.values, rtol=tol, atol=tol)114 def _compareSparseCpu(self, x, np_func, tf_func, tol):115 x_sp, x_sp_vals = _sparsify(x)116 res_np = np_func(x_sp_vals)117 with test_util.force_cpu():118 self._check(tf_func(x_sp), res_np, x_sp, tol)119 def _compareGpu(self, x, np_func, tf_func):120 np_ans = np_func(x)121 with test_util.use_gpu():122 result = tf_func(ops.convert_to_tensor(x))123 tf_gpu = self.evaluate(result)124 if x.dtype == np.float16:125 self.assertAllClose(np_ans, tf_gpu, rtol=1e-3, atol=1e-3)126 else:127 self.assertAllClose(np_ans, tf_gpu)128 # TODO(zhifengc/ke): make gradient checker work on GPU.129 def _compareSparseGpu(self, x, np_func, tf_func, tol):130 x_sp, x_sp_vals = _sparsify(x)131 res_np = np_func(x_sp_vals)132 with test_util.use_gpu():133 self._check(tf_func(x_sp), res_np, x_sp, tol)134 def _compareBoth(self, x, np_func, tf_func):135 self._compareCpu(x, np_func, tf_func)136 self._compareGpu(x, np_func, tf_func)137 def _compareBothSparse(self, x, np_func, tf_func, tol=None):138 self._compareSparseCpu(x, np_func, tf_func, tol)139 self._compareSparseGpu(x, np_func, tf_func, tol)140 def _inv(self, x):141 return 1.0 / x142 def _rsqrt(self, x):143 return self._inv(np.sqrt(x))144 def _sigmoid(self, x):145 return 1.0 / (1.0 + np.exp(-x))146 def _log_sigmoid(self, x):147 return np.log(self._sigmoid(x))148 def _replace_domain_error_with_inf(self, fn):149 def func(x):150 try:151 return fn(x)152 except ValueError as e:153 if "domain error" in str(e):154 return np.inf * np.ones_like(x)155 else:156 raise e157 return func158 @test_util.run_deprecated_v1159 def testFloatBasic(self):160 x = np.arange(-3, 3).reshape(1, 3, 2).astype(np.float32)161 w = x - x.min() + 1.02 # all greater than 1162 y = (x + .5).astype(np.float32) # no zero163 z = (x + 15.5).astype(np.float32) # all positive164 k = np.arange(-0.90, 0.90, 0.25).astype(np.float32) # between -1 and 1165 self._compareBoth(x, np.abs, math_ops.abs)166 self._compareBoth(x, np.abs, _ABS)167 self._compareBoth(x, np.negative, math_ops.negative)168 self._compareBoth(x, np.negative, _NEG)169 self._compareBoth(y, self._inv, math_ops.reciprocal)170 self._compareBoth(x, np.square, math_ops.square)171 self._compareBoth(z, np.sqrt, math_ops.sqrt)172 self._compareBoth(z, self._rsqrt, math_ops.rsqrt)173 self._compareBoth(x, np.exp, math_ops.exp)174 self._compareBoth(x, np.expm1, math_ops.expm1)175 self._compareBoth(z, np.log, math_ops.log)176 self._compareBoth(z, np.log1p, math_ops.log1p)177 self._compareBoth(x, np.sinh, math_ops.sinh)178 self._compareBoth(x, np.cosh, math_ops.cosh)179 self._compareBoth(x, np.tanh, math_ops.tanh)180 self._compareBoth(x, np.arcsinh, math_ops.asinh)181 self._compareBoth(w, np.arccosh, math_ops.acosh)182 self._compareBoth(k, np.arctanh, math_ops.atanh)183 self._compareBoth(x, self._sigmoid, math_ops.sigmoid)184 self._compareBoth(x, self._log_sigmoid, math_ops.log_sigmoid)185 self._compareBoth(y, np.sign, math_ops.sign)186 self._compareBoth(x, np.sin, math_ops.sin)187 self._compareBoth(x, np.cos, math_ops.cos)188 self._compareBoth(k, np.arcsin, math_ops.asin)189 self._compareBoth(k, np.arccos, math_ops.acos)190 self._compareBoth(x, np.arctan, math_ops.atan)191 self._compareBoth(x, np.tan, math_ops.tan)192 self._compareBoth(193 y, np.vectorize(self._replace_domain_error_with_inf(math.lgamma)),194 math_ops.lgamma)195 self._compareBoth(x, np.vectorize(math.erf), math_ops.erf)196 self._compareBoth(x, np.vectorize(math.erfc), math_ops.erfc)197 try:198 from scipy import special # pylint: disable=g-import-not-at-top199 self._compareBoth(x, special.i0e, math_ops.bessel_i0e)200 self._compareBoth(x, special.i1e, math_ops.bessel_i1e)201 except ImportError as e:202 tf_logging.warn("Cannot test special functions: %s" % str(e))203 self._compareBothSparse(x, np.abs, math_ops.abs)204 self._compareBothSparse(x, np.negative, math_ops.negative)205 self._compareBothSparse(x, np.square, math_ops.square)206 self._compareBothSparse(z, np.sqrt, math_ops.sqrt, tol=1e-3)207 self._compareBothSparse(x, np.tanh, math_ops.tanh)208 self._compareBothSparse(y, np.sign, math_ops.sign)209 self._compareBothSparse(x, np.vectorize(math.erf), math_ops.erf)210 @test_util.run_deprecated_v1211 def testFloatTanhEdge(self):212 x = np.arange(40, 40 + 6).reshape(6).astype(np.float32)213 self._compareBoth(x, np.tanh, math_ops.tanh)214 x = np.arange(-40, -40 + 6).reshape(6).astype(np.float32)215 self._compareBoth(x, np.tanh, math_ops.tanh)216 @test_util.run_deprecated_v1217 def testFloatEmpty(self):218 x = np.empty((2, 0, 5), dtype=np.float32)219 self._compareBoth(x, np.abs, math_ops.abs)220 self._compareBoth(x, np.abs, _ABS)221 self._compareBoth(x, np.negative, math_ops.negative)222 self._compareBoth(x, np.negative, _NEG)223 self._compareBoth(x, self._inv, math_ops.reciprocal)224 self._compareBoth(x, np.square, math_ops.square)225 self._compareBoth(x, np.sqrt, math_ops.sqrt)226 self._compareBoth(x, self._rsqrt, math_ops.rsqrt)227 self._compareBoth(x, np.exp, math_ops.exp)228 self._compareBoth(x, np.expm1, math_ops.expm1)229 self._compareBoth(x, np.log, math_ops.log)230 self._compareBoth(x, np.log1p, math_ops.log1p)231 self._compareBoth(x, np.sinh, math_ops.sinh)232 self._compareBoth(x, np.arcsinh, math_ops.asinh)233 self._compareBoth(x, np.cosh, math_ops.cosh)234 self._compareBoth(x, np.tanh, math_ops.tanh)235 self._compareBoth(x, self._sigmoid, math_ops.sigmoid)236 self._compareBoth(x, np.sign, math_ops.sign)237 self._compareBoth(x, np.sin, math_ops.sin)238 self._compareBoth(x, np.cos, math_ops.cos)239 # Can't use vectorize below, so just use some arbitrary function240 self._compareBoth(x, np.sign, math_ops.lgamma)241 self._compareBoth(x, np.sign, math_ops.erf)242 self._compareBoth(x, np.sign, math_ops.erfc)243 self._compareBoth(x, np.tan, math_ops.tan)244 self._compareBoth(x, np.arcsin, math_ops.asin)245 self._compareBoth(x, np.arccos, math_ops.acos)246 self._compareBoth(x, np.arctan, math_ops.atan)247 try:248 from scipy import special # pylint: disable=g-import-not-at-top249 self._compareBoth(x, special.i0e, math_ops.bessel_i0e)250 self._compareBoth(x, special.i1e, math_ops.bessel_i1e)251 except ImportError as e:252 tf_logging.warn("Cannot test special functions: %s" % str(e))253 self._compareBothSparse(x, np.abs, math_ops.abs)254 self._compareBothSparse(x, np.negative, math_ops.negative)255 self._compareBothSparse(x, np.square, math_ops.square)256 self._compareBothSparse(x, np.sqrt, math_ops.sqrt, tol=1e-3)257 self._compareBothSparse(x, np.tanh, math_ops.tanh)258 self._compareBothSparse(x, np.sign, math_ops.sign)259 self._compareBothSparse(x, np.sign, math_ops.erf)260 @test_util.run_deprecated_v1261 def testDoubleBasic(self):262 x = np.arange(-3, 3).reshape(1, 3, 2).astype(np.float64)263 w = x - x.min() + 1.02 # all greater than 1264 y = (x + .5).astype(np.float64) # no zero265 z = (x + 15.5).astype(np.float64) # all positive266 k = np.arange(-0.90, 0.90,267 0.35).reshape(1, 3, 2).astype(np.float64) # between -1 and 1268 self._compareBoth(x, np.abs, math_ops.abs)269 self._compareBoth(x, np.abs, _ABS)270 self._compareBoth(x, np.negative, math_ops.negative)271 self._compareBoth(x, np.negative, _NEG)272 self._compareBoth(y, self._inv, math_ops.reciprocal)273 self._compareBoth(x, np.square, math_ops.square)274 self._compareBoth(z, np.sqrt, math_ops.sqrt)275 self._compareBoth(z, self._rsqrt, math_ops.rsqrt)276 self._compareBoth(x, np.exp, math_ops.exp)277 self._compareBoth(x, np.expm1, math_ops.expm1)278 self._compareBoth(z, np.log, math_ops.log)279 self._compareBoth(z, np.log1p, math_ops.log1p)280 self._compareBoth(x, np.sinh, math_ops.sinh)281 self._compareBoth(x, np.cosh, math_ops.cosh)282 self._compareBoth(x, np.tanh, math_ops.tanh)283 self._compareBoth(x, np.arcsinh, math_ops.asinh)284 self._compareBoth(w, np.arccosh, math_ops.acosh)285 self._compareBoth(k, np.arctanh, math_ops.atanh)286 self._compareBoth(x, self._sigmoid, math_ops.sigmoid)287 self._compareBoth(y, np.sign, math_ops.sign)288 self._compareBoth(x, np.sin, math_ops.sin)289 self._compareBoth(x, np.cos, math_ops.cos)290 self._compareBoth(291 y, np.vectorize(self._replace_domain_error_with_inf(math.lgamma)),292 math_ops.lgamma)293 self._compareBoth(x, np.vectorize(math.erf), math_ops.erf)294 self._compareBoth(x, np.vectorize(math.erfc), math_ops.erfc)295 self._compareBoth(x, np.arctan, math_ops.atan)296 self._compareBoth(k, np.arcsin, math_ops.asin)297 self._compareBoth(k, np.arccos, math_ops.acos)298 self._compareBoth(k, np.tan, math_ops.tan)299 try:300 from scipy import special # pylint: disable=g-import-not-at-top301 self._compareBoth(x, special.i0e, math_ops.bessel_i0e)302 self._compareBoth(x, special.i1e, math_ops.bessel_i1e)303 except ImportError as e:304 tf_logging.warn("Cannot test special functions: %s" % str(e))305 self._compareBothSparse(x, np.abs, math_ops.abs)306 self._compareBothSparse(x, np.negative, math_ops.negative)307 self._compareBothSparse(x, np.square, math_ops.square)308 self._compareBothSparse(z, np.sqrt, math_ops.sqrt, tol=1e-3)309 self._compareBothSparse(x, np.tanh, math_ops.tanh)310 self._compareBothSparse(y, np.sign, math_ops.sign)311 self._compareBothSparse(x, np.vectorize(math.erf), math_ops.erf)312 @test_util.run_deprecated_v1313 def testHalfBasic(self):314 x = np.arange(-3, 3).reshape(1, 3, 2).astype(np.float16)315 y = (x + .5).astype(np.float16) # no zero316 z = (x + 15.5).astype(np.float16) # all positive317 self._compareBoth(x, np.abs, math_ops.abs)318 self._compareBoth(x, np.abs, _ABS)319 self._compareBoth(x, np.negative, math_ops.negative)320 self._compareBoth(x, np.negative, _NEG)321 self._compareBoth(y, self._inv, math_ops.reciprocal)322 self._compareBoth(x, np.square, math_ops.square)323 self._compareBoth(z, np.sqrt, math_ops.sqrt)324 self._compareBoth(z, self._rsqrt, math_ops.rsqrt)325 self._compareBoth(x, np.exp, math_ops.exp)326 self._compareBoth(x, np.expm1, math_ops.expm1)327 self._compareBoth(z, np.log, math_ops.log)328 self._compareBoth(z, np.log1p, math_ops.log1p)329 self._compareBoth(x, np.tanh, math_ops.tanh)330 self._compareBoth(x, self._sigmoid, math_ops.sigmoid)331 self._compareBoth(y, np.sign, math_ops.sign)332 self._compareBoth(x, np.sin, math_ops.sin)333 self._compareBoth(x, np.cos, math_ops.cos)334 self._compareBoth(335 y, np.vectorize(self._replace_domain_error_with_inf(math.lgamma)),336 math_ops.lgamma)337 self._compareBoth(x, np.vectorize(math.erf), math_ops.erf)338 self._compareBoth(x, np.vectorize(math.erfc), math_ops.erfc)339 try:340 from scipy import special # pylint: disable=g-import-not-at-top341 self._compareBoth(x, special.i0e, math_ops.bessel_i0e)342 self._compareBoth(x, special.i1e, math_ops.bessel_i1e)343 except ImportError as e:344 tf_logging.warn("Cannot test special functions: %s" % str(e))345 self._compareBothSparse(x, np.abs, math_ops.abs)346 self._compareBothSparse(x, np.negative, math_ops.negative)347 self._compareBothSparse(x, np.square, math_ops.square)348 self._compareBothSparse(z, np.sqrt, math_ops.sqrt, tol=1e-3)349 self._compareBothSparse(x, np.tanh, math_ops.tanh)350 self._compareBothSparse(y, np.sign, math_ops.sign)351 self._compareBothSparse(x, np.vectorize(math.erf), math_ops.erf, tol=1e-3)352 def testBFloat16Basic(self):353 x = np.arange(-6, 6,354 2).reshape(1, 3, 2).astype(dtypes_lib.bfloat16.as_numpy_dtype)355 self._compareCpu(x, np.abs, math_ops.abs)356 self._compareCpu(x, np.abs, _ABS)357 def testInt8Basic(self):358 x = np.arange(-6, 6, 2).reshape(1, 3, 2).astype(np.int8)359 self._compareCpu(x, np.abs, math_ops.abs)360 self._compareCpu(x, np.abs, _ABS)361 def testInt16Basic(self):362 x = np.arange(-6, 6, 2).reshape(1, 3, 2).astype(np.int16)363 self._compareCpu(x, np.abs, math_ops.abs)364 self._compareCpu(x, np.abs, _ABS)365 def testInt32Basic(self):366 x = np.arange(-6, 6, 2).reshape(1, 3, 2).astype(np.int32)367 self._compareCpu(x, np.abs, math_ops.abs)368 self._compareCpu(x, np.abs, _ABS)369 self._compareBoth(x, np.negative, math_ops.negative)370 self._compareBoth(x, np.negative, _NEG)371 self._compareBoth(x, np.square, math_ops.square)372 self._compareCpu(x, np.sign, math_ops.sign)373 self._compareBothSparse(x, np.abs, math_ops.abs)374 self._compareBothSparse(x, np.negative, math_ops.negative)375 self._compareBothSparse(x, np.square, math_ops.square)376 self._compareBothSparse(x, np.sign, math_ops.sign)377 def testInt64Basic(self):378 x = np.arange(-6 << 40, 6 << 40, 2 << 40).reshape(1, 3, 2).astype(np.int64)379 self._compareCpu(x, np.abs, math_ops.abs)380 self._compareCpu(x, np.abs, _ABS)381 self._compareCpu(x, np.negative, math_ops.negative)382 self._compareCpu(x, np.negative, _NEG)383 self._compareCpu(x, np.sign, math_ops.sign)384 self._compareBothSparse(x, np.abs, math_ops.abs)385 self._compareBothSparse(x, np.negative, math_ops.negative)386 self._compareBothSparse(x, np.sign, math_ops.sign)387 def testInt64Square(self):388 x = np.arange(-6 << 20, 6 << 20, 2 << 20).reshape(1, 3, 2).astype(np.int64)389 self._compareCpu(x, np.square, math_ops.square)390 self._compareBothSparse(x, np.square, math_ops.square)391 @test_util.run_deprecated_v1392 def testComplex64Basic(self):393 x = np.complex(1, 1) * np.arange(-3, 3).reshape(1, 3, 2).astype(394 np.complex64)395 y = x + np.complex(0.5, 0.5) # no zeros396 self._compareBoth(x, np.abs, math_ops.abs)397 self._compareBoth(x, np.abs, _ABS)398 self._compareBoth(x, np.negative, math_ops.negative)399 self._compareBoth(x, np.negative, _NEG)400 self._compareCpu(y, self._inv, math_ops.reciprocal)401 self._compareCpu(x, np.square, math_ops.square)402 self._compareCpu(y, np.sqrt, math_ops.sqrt)403 self._compareCpu(y, self._rsqrt, math_ops.rsqrt)404 self._compareBoth(x, np.exp, math_ops.exp)405 self._compareCpu(x, np.expm1, math_ops.expm1)406 self._compareCpu(y, np.log, math_ops.log)407 self._compareCpu(y, np.log1p, math_ops.log1p)408 self._compareCpu(x, np.sinh, math_ops.sinh)409 self._compareCpu(x, np.cosh, math_ops.cosh)410 self._compareCpu(x, np.tanh, math_ops.tanh)411 # Complex64 versions of asinh() and acosh() in libstdc++ only have 6 digits412 # of precision.413 # Small gradient values + low precision --> High relative error414 self._compareCpu(y, np.arcsinh, math_ops.asinh, grad_rtol=1e-2)415 self._compareCpu(y, np.arccosh, math_ops.acosh, grad_rtol=1e-2)416 self._compareCpu(y, np.arctanh, math_ops.atanh)417 self._compareCpu(x, self._sigmoid, math_ops.sigmoid)418 self._compareCpu(x, np.sin, math_ops.sin)419 self._compareCpu(x, np.cos, math_ops.cos)420 self._compareBothSparse(x, np.abs, math_ops.abs)421 self._compareBothSparse(x, np.negative, math_ops.negative)422 self._compareBothSparse(x, np.square, math_ops.square)423 self._compareBothSparse(x, np.sqrt, math_ops.sqrt, 1e-3)424 self._compareBothSparse(x, np.tanh, math_ops.tanh)425 # Numpy uses an incorrect definition of sign; use the right one instead.426 def complex_sign(x):427 return x / np.abs(x)428 self._compareBoth(y, complex_sign, math_ops.sign)429 self._compareBothSparse(y, complex_sign, math_ops.sign)430 @test_util.run_deprecated_v1431 def testComplex128Basic(self):432 x = np.complex(1, 1) * np.arange(-3, 3).reshape(1, 3, 2).astype(433 np.complex128)434 y = x + np.complex(0.5, 0.5) # no zeros435 self._compareBoth(x, np.abs, math_ops.abs)436 self._compareBoth(x, np.abs, _ABS)437 self._compareBoth(x, np.negative, math_ops.negative)438 self._compareBoth(x, np.negative, _NEG)439 self._compareCpu(y, self._inv, math_ops.reciprocal)440 self._compareCpu(x, np.square, math_ops.square)441 self._compareCpu(y, np.sqrt, math_ops.sqrt)442 self._compareCpu(y, self._rsqrt, math_ops.rsqrt)443 self._compareBoth(x, np.exp, math_ops.exp)444 self._compareCpu(x, np.expm1, math_ops.expm1)445 self._compareCpu(y, np.log, math_ops.log)446 self._compareCpu(y, np.log1p, math_ops.log1p)447 self._compareCpu(x, np.sinh, math_ops.sinh)448 self._compareCpu(x, np.cosh, math_ops.cosh)449 self._compareCpu(x, np.tanh, math_ops.tanh)450 self._compareCpu(y, np.arcsinh, math_ops.asinh)451 self._compareCpu(y, np.arccosh, math_ops.acosh)452 self._compareCpu(y, np.arctanh, math_ops.atanh)453 self._compareCpu(x, self._sigmoid, math_ops.sigmoid)454 self._compareCpu(x, np.sin, math_ops.sin)455 self._compareCpu(x, np.cos, math_ops.cos)456 self._compareBothSparse(x, np.abs, math_ops.abs)457 self._compareBothSparse(x, np.negative, math_ops.negative)458 self._compareBothSparse(x, np.square, math_ops.square)459 self._compareBothSparse(x, np.sqrt, math_ops.sqrt, 1e-3)460 self._compareBothSparse(x, np.tanh, math_ops.tanh)461 # Numpy uses an incorrect definition of sign; use the right one instead.462 def complex_sign(x):463 return x / np.abs(x)464 self._compareBoth(y, complex_sign, math_ops.sign)465 self._compareBothSparse(y, complex_sign, math_ops.sign)466 @test_util.run_deprecated_v1467 def testGradGrad(self):468 np.random.seed(7)469 shape = (5,)470 dtype_tols = [(np.float32, 5e-4), (np.float64, 1e-6), (np.complex64, 5e-4),471 (np.complex128, 1e-6)]472 op_range = [473 (gen_math_ops.reciprocal_grad, [-2, 2]),474 (gen_math_ops.rsqrt_grad, [0.1, 3]),475 (gen_math_ops.sigmoid_grad, [-2, 2]),476 (gen_math_ops.sqrt_grad, [0.1, 3]),477 (gen_math_ops.tanh_grad, [-2, 2]),478 ]479 def rand(dtype, real_range):480 x = np.random.uniform(481 real_range[0], real_range[1], size=shape[0]).astype(dtype)482 if dtype in (np.complex64, np.complex128):483 x += 1j * np.random.uniform(-2, 2, size=shape[0]).astype(dtype)484 return x485 for op, real_range in op_range:486 with self.cached_session():487 for dtype, tol in dtype_tols:488 x = constant_op.constant(rand(dtype, real_range))489 y = constant_op.constant(rand(dtype, real_range))490 z = op(x, y)491 grads = gradient_checker.compute_gradient(492 [x, y], [shape, shape],493 z,494 shape,495 x_init_value=[rand(dtype, real_range),496 rand(dtype, real_range)])497 if isinstance(grads, tuple):498 grads = [grads]499 for analytical, numerical in grads:500 self.assertAllClose(analytical, numerical, rtol=tol, atol=tol)501 @test_util.run_in_graph_and_eager_modes502 def testComplexAbsGradGrad(self):503 def f(x):504 real = math_ops.cos(x)505 imag = ops.convert_to_tensor(1.)506 return math_ops.abs(math_ops.complex(real, imag))507 def g(x):508 with backprop.GradientTape() as t:509 t.watch(x)510 y = f(x)511 return t.gradient(y, x)512 err = gradient_checker_v2.max_error(513 *gradient_checker_v2.compute_gradient(g, [ops.convert_to_tensor(2.0)]))514 self.assertLess(err, 1e-3)515if __name__ == "__main__":...
buildTestData.py
Source:buildTestData.py
1#-******************************************************************************2#3# Copyright (c) 2012,4# Sony Pictures Imageworks Inc. and5# Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd.6#7# All rights reserved.8#9# Redistribution and use in source and binary forms, with or without10# modification, are permitted provided that the following conditions are11# met:12# * Redistributions of source code must retain the above copyright13# notice, this list of conditions and the following disclaimer.14# * Redistributions in binary form must reproduce the above15# copyright notice, this list of conditions and the following disclaimer16# in the documentation and/or other materials provided with the17# distribution.18# * Neither the name of Sony Pictures Imageworks, nor19# Industrial Light & Magic, nor the names of their contributors may be used20# to endorse or promote products derived from this software without specific21# prior written permission.22#23# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS24# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT25# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR26# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT27# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,28# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT29# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,30# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY31# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT32# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE33# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.34#35#-******************************************************************************36from imath import *37from alembic.AbcCoreAbstract import *38from alembic.Abc import *39from alembic.AbcGeom import *40from alembic.Util import *41C3c = Color3c42C3f = Color3f43C4c = Color4c44C4f = Color4f45# Test data for number type traits46ScalarTraitsData = {}47SmallArrayTraitsData = {}48ArrayTraitsData = {}49# Comparison measures for number type traits50TraitsComparison = {}51# -----------------------------------------------------------------52# Test helper functions53# -----------------------------------------------------------------54# Comparison functions for various data types55EPS = 0.01 56def compareExact (a0, a1):57 return a0 == a158def compareEPS (a0, a1):59 return abs (a0 - a1) <= EPS60def compareVec3Exact (a0, a1):61 return a0[0] == a1[0] and a0[1] == a1[1] and a0[2] == a1[2]62def compareVec4Exact (a0, a1):63 return a0[0] == a1[0] and a0[1] == a1[1] and a0[2] == a1[2] and \64 a0[3] == a1[3]65def compareVecEPS (a0, a1):66 return a0.equalWithRelError (a1, EPS)67def compareBoxExact (a0, a1):68 return compareExact (a0.min(), a1.min()) and \69 compareExact (a0.max(), a1.max())70def compareBoxEPS (a0, a1):71 return compareVecEPS (a0.min(), a1.min()) and \72 compareVecEPS (a0.max(), a1.max())73def compareQuatEPS (a0, a1):74 return compareEPS (a0.angle(), a1.angle()) and \75 compareVecEPS (a0.axis(), a1.axis())76def compareC3EPS (a0, a1):77 return compareEPS (a0[0], a1[0]) and compareEPS (a0[1], a1[1]) and \78 compareEPS (a0[2], a1[2])79def compareC4EPS (a0, a1):80 return compareEPS (a0[0], a1[0]) and compareEPS (a0[1], a1[1]) and \81 compareEPS (a0[2], a1[2]) and compareEPS (a0[3], a1[3])82def WRITES( *iArgs ):83 iTraits = iArgs[0];84 name = iTraits.__name__85 ScalarTraitsData[name] = iArgs86def WRITESA( iTraits, *iArgs ):87 name = iTraits.__name__88 SmallArrayTraitsData[name] = iTraits, createArray( iTraits.arrayType, iArgs )89def createArray( iArray, iList ):90 a = iArray( len( iList ) )91 for i in range( len( iList ) ):92 a[i] = iList[i]93 return a94def WRITEA( iTraits, *iArgs ):95 name = iTraits.__name__96 ArrayTraitsData[name] = iTraits, createArray( iTraits.arrayType, iArgs )97def buildTestScalarData():98 # Scalar DataType99 100 # POD types101 # extent 1102 WRITES (BooleanTPTraits, 0, 1, 0)103 WRITES (Uint8TPTraits, 3, 4)104 WRITES (Int8TPTraits, -5 )105 WRITES (Uint16TPTraits, 6 )106 WRITES (Int16TPTraits, -7, 8 )107 WRITES (Uint32TPTraits, 9, 10, 11, 12 )108 WRITES (Int32TPTraits, -13, 14, -15 )109 WRITES (Uint64TPTraits, 16 )110 WRITES (Float16TPTraits, 1.234, 6.789)111 WRITES (Float32TPTraits, -1234.5678)112 WRITES (Float64TPTraits, 1234.5678)113 WRITES (StringTPTraits, "abc", "def")114 WRITES (WstringTPTraits, u"asdf" )115 # Compound Types116 # extent 2117 # vector2118 WRITES (V2sTPTraits, V2s (1, 2), V2s (-1, 0))119 WRITES (V2iTPTraits, V2i (3, 4))120 WRITES (V2fTPTraits, V2f (1.2, 3.4), V2f (5.6, 0))121 WRITES (V2dTPTraits, V2d (5.6, 7.8))122 # point2123 WRITES (P2sTPTraits, V2s (10, 20), V2s (-10, 0))124 WRITES (P2iTPTraits, V2i (30, 40))125 WRITES (P2fTPTraits, V2f (1.02, 3.04), V2f (5.06, 0))126 WRITES (P2dTPTraits, V2d (5.06, 7.08))127 # normal2128 WRITES (N2fTPTraits, V2f (1.1, 1.2), V2f (-1.1, 0.1))129 WRITES (N2dTPTraits, V2d (3.4, 4.4))130 # extent 3131 # color3132 WRITES (C3cTPTraits, Color3c (1, 2, 3))133 # TODO: Color3h types are not wrapped. C3f is used instead.134 WRITES (C3hTPTraits, Color3f (0.1, 0.2, 0.3))135 WRITES (C3fTPTraits, Color3f (15.6, 7.8, 9.0))136 # vector3137 WRITES (V3sTPTraits, V3s (4, 5, 6))138 WRITES (V3iTPTraits, V3i (7, 8, 9))139 WRITES (V3fTPTraits, V3f (25.6, 7.8, 9.0))140 WRITES (V3dTPTraits, V3d (35.6, 7.8, 14.2))141 # point3142 WRITES (P3sTPTraits, V3s (40, 50, 60))143 WRITES (P3iTPTraits, V3i (70, 80, 90))144 WRITES (P3fTPTraits, V3f (25.06, 7.08, 90.0))145 WRITES (P3dTPTraits, V3d (35.06, 7.08, 14.02))146 # normal3147 WRITES (N3fTPTraits, V3f (11.1, 11.2, 11.3), V3f (-11.1, 0.11, 0.22))148 WRITES (N3dTPTraits, V3d (31.4, 41.4, 41.5))149 # extent 4150 # color4151 WRITES (C4cTPTraits, Color4c (1, 2, 3, 4) )152 # TODO: Color4h types are not wrapped. C4f is used instead.153 WRITES (C4hTPTraits, Color4f (5.6, 7.8, 1.2, 3.4) )154 WRITES (C4fTPTraits, Color4f (1.111, 2.222, 3.333, 4.444) )155 # box2156 WRITES (Box2sTPTraits, Box2s (V2s (0, 0), V2s (1, 1)));157 WRITES (Box2iTPTraits, Box2i (V2i (0, 0), V2i (2, 2)));158 WRITES (Box2fTPTraits, Box2f (V2f (0, 0), V2f (0.3, 0.3)));159 WRITES (Box2dTPTraits, Box2d (V2d (0, 0), V2d (0.4, 0.4)));160 # quat161 WRITES (QuatfTPTraits, Quatf (-1.0, 2.0, 3.0, -4.0))162 WRITES (QuatdTPTraits, Quatd (0.5, 0.6, -0.7, -0.8))163 # extent 6164 # box3165 WRITES (Box3sTPTraits, Box3s (V3s (-1, -1, -1), V3s (1, 1, 1)))166 WRITES (Box3iTPTraits, Box3i (V3i (0, 0, 0), V3i (1, 1, 1)))167 WRITES (Box3fTPTraits, Box3f (V3f(1.2, 3.4, 5.6), V3f(-1.2, 3.4, -5.6)))168 WRITES (Box3dTPTraits, Box3d (V3d(0, 0, 0), V3d(5.6, 7.8, 9.0)))169 # extent 9170 # matrix33171 WRITES (M33fTPTraits, M33f(1)*2)172 WRITES (M33dTPTraits, M33d(1)*3)173 # extent 16174 # matrix44175 WRITES (M44fTPTraits, M44f(1)*4)176 WRITES (M44dTPTraits, M44d(1)*5)177 178def buildTestSmallArrayData():179 # Scalar DataType180 181 # POD types182 # extent > 1183 WRITESA (BooleanTPTraits, 0, 0, 1)184 WRITESA (Uint8TPTraits, 1, 2, 3, 4, 5, 6)185 WRITESA (Int8TPTraits, -1, 2, -3, 4, -5, 6)186 WRITESA (Uint16TPTraits, 10, 20, 30, 40, 50, 60)187 WRITESA (Int16TPTraits, -10, 20, -30, 40, -50, 60)188 WRITESA (Uint32TPTraits, 11, 22, 33, 44, 55, 66)189 WRITESA (Int32TPTraits, -11, 22, -33, 44, -55, 66)190 WRITESA (Uint64TPTraits, 13, 23, 33, 43, 53)191 WRITESA (Int64TPTraits, -13, 23, -33, 43, -53)192 WRITESA (Float16TPTraits, 1.23, 2.34, 3.45, 4.56)193 WRITESA (Float32TPTraits, 1.234, 2.345, 3.456, 4.567)194 WRITESA (Float64TPTraits, 1.2345, 2.3456, 3.4567, 4.5678)195 WRITESA (StringTPTraits, "a", "b", "c", "d", "e")196 WRITESA (WstringTPTraits, u"A", u"B", u"C", u"D", u"E")197def buildTestArrayData():198 # POD types199 # extent 1200 WRITEA (BooleanTPTraits, 0, 0, 1)201 WRITEA (Uint8TPTraits, 1, 2, 3, 4, 5, 6)202 WRITEA (Int8TPTraits, -1, 2, -3, 4, -5, 6)203 WRITEA (Uint16TPTraits, 10, 20, 30, 40, 50, 60)204 WRITEA (Int16TPTraits, -10, 20, -30, 40, -50, 60)205 WRITEA (Uint32TPTraits, 11, 22, 33, 44, 55, 66)206 WRITEA (Int32TPTraits, -11, 22, -33, 44, -55, 66)207 WRITEA (Uint64TPTraits, 13, 23, 33, 43, 53)208 WRITEA (Int64TPTraits, -13, 23, -33, 43, -53)209 WRITEA (Float16TPTraits, 1.23, 2.34, 3.45, 4.56)210 WRITEA (Float32TPTraits, 1.234, 2.345, 3.456, 4.567)211 WRITEA (Float64TPTraits, 1.2345, 2.3456, 3.4567, 4.5678)212 WRITEA (StringTPTraits, "a", "b", "c", "d", "e");213 WRITEA (WstringTPTraits, u"A", u"B", u"C", u"D", u"E");214 # Compound Types215 # extent 2216 # vector2217 WRITEA (V2sTPTraits, V2s (1, 2), V2s (-1, 1))218 WRITEA (V2iTPTraits, V2i (3, 4), V2i (5, 6))219 WRITEA (V2fTPTraits, V2f (1.2, 3.4), V2f (5.6, 7.8))220 WRITEA (V2dTPTraits, V2d (5.6, 7.8), V2d (9.10, 10.11))221 # point2222 WRITEA (P2sTPTraits, V2s (10, 20), V2s (-10, 0))223 WRITEA (P2iTPTraits, V2i (30, 40), V2i (10, 20))224 WRITEA (P2fTPTraits, V2f (1.02, 3.04), V2f (5.06, 0.11))225 WRITEA (P2dTPTraits, V2d (5.06, 7.08), V2d (-102.4, -9.5))226 # normal2227 WRITEA (N2fTPTraits, V2f (1.1, 1.2), V2f (-1.1, 0.1))228 WRITEA (N2dTPTraits, V2d (3.4, 4.4), V2d (-104.2, 104.2))229 # extent 3230 # color3231 WRITEA (C3cTPTraits, C3c (1, 2, 3), C3c (4, 5, 6))232 # TODO: C3h types are not wrapped. C3f is used instead.233 WRITEA (C3hTPTraits, C3f (0.1, 0.2, 0.3), C3f(0.4, 0.5, 0.6))234 WRITEA (C3fTPTraits, C3f (15.6, 7.8, 9.0), C3f(-1.4, -235.2, 11.1))235 # vector3236 WRITEA (V3sTPTraits, V3s (4, 5, 6), V3s (14, 132, 13))237 WRITEA (V3iTPTraits, V3i (7, 8, 9), V3i (-1, -14, 32))238 WRITEA (V3fTPTraits, V3f (25.6, 7.8, 9.0), V3f (-0.2, -1.3, 1.5))239 WRITEA (V3dTPTraits, V3d (35.6, 7.8, 14.2), V3d (13.2, 1403, -12.3))240 # point3241 WRITEA (P3sTPTraits, V3s (40, 50, 60), V3s(1, 2, 3))242 WRITEA (P3iTPTraits, V3i (70, 80, 90), V3i(-1, -2, -3))243 WRITEA (P3fTPTraits, V3f (25.06, 7.08, 90.0), V3f(-0.1, -0.2, -0.3))244 WRITEA (P3dTPTraits, V3d (35.06, 7.08, 14.02), V3d(-0.3, -0.4, -0.5))245 # normal3246 WRITEA (N3fTPTraits, V3f (11.1, 11.2, 11.3), V3f (-11.1, 0.11, 0.22))247 WRITEA (N3dTPTraits, V3d (31.4, 41.4, 41.5), V3d (-0.1, -0.2, -0.4))248 # extent 4249 # color4250 WRITEA (C4cTPTraits, C4c (1, 2, 3, 4) )251 # TODO: C4h types are not wrapped. C4f is used instead.252 WRITEA (C4hTPTraits, C4f (5.6, 7.8, 1.2, 3.4) )253 WRITEA (C4fTPTraits, C4f (1.111, 2.222, 3.333, 4.444) )254 # box2255 WRITEA (Box2sTPTraits, Box2s (V2s (0, 0), V2s (1, 1)));256 WRITEA (Box2iTPTraits, Box2i (V2i (0, 0), V2i (2, 2)));257 WRITEA (Box2fTPTraits, Box2f (V2f (0, 0), V2f (0.3, 0.3)));258 WRITEA (Box2dTPTraits, Box2d (V2d (0, 0), V2d (0.4, 0.4)));259 # quat260 WRITEA (QuatfTPTraits, Quatf (-1.0, 2.0, 3.0, -4.0))261 WRITEA (QuatdTPTraits, Quatd (0.5, 0.6, -0.7, -0.8))262 # extent 6263 # box3264 WRITEA (Box3sTPTraits, Box3s (V3s (-1, -1, -1), V3s (1, 1, 1)))265 WRITEA (Box3iTPTraits, Box3i (V3i (0, 0, 0), V3i (1, 1, 1)))266 WRITEA (Box3fTPTraits, Box3f (V3f (1.2, 3.4, 5.6), V3f (1.2, 4.4, 5.6)))267 WRITEA (Box3dTPTraits, Box3d (V3d (0, 0, 0), V3d (5.6, 7.8, 9.0)))268 # extent 9269 # matrix33270 WRITEA (M33fTPTraits, M33f (1)*2, M33f (0.1))271 WRITEA (M33dTPTraits, M33d (1)*3, M33d (3.4))272 # extent 16273 # matrix44274 WRITEA (M44fTPTraits, M44f (1)*4, M44f (1.2))275 WRITEA (M44dTPTraits, M44d (1)*5, M44d (5.6))276def buildComparisonMeasure():277 # Assign the property name and proper comparison function278 # for each type279 TraitsComparison[BooleanTPTraits.__name__] = compareExact280 TraitsComparison[Uint8TPTraits.__name__] = compareExact281 TraitsComparison[Int8TPTraits.__name__] = compareExact282 TraitsComparison[Uint16TPTraits.__name__] = compareExact283 TraitsComparison[Int16TPTraits.__name__] = compareExact284 TraitsComparison[Uint32TPTraits.__name__] = compareExact285 TraitsComparison[Int32TPTraits.__name__] = compareExact286 TraitsComparison[Uint64TPTraits.__name__] = compareExact287 TraitsComparison[Int64TPTraits.__name__] = compareExact288 TraitsComparison[Float16TPTraits.__name__] = compareEPS289 TraitsComparison[Float32TPTraits.__name__] = compareEPS290 TraitsComparison[Float64TPTraits.__name__] = compareEPS291 TraitsComparison[StringTPTraits.__name__] = compareExact292 TraitsComparison[WstringTPTraits.__name__] = compareExact293 TraitsComparison[V2sTPTraits.__name__] = compareExact294 TraitsComparison[V2iTPTraits.__name__] = compareExact295 TraitsComparison[V2fTPTraits.__name__] = compareVecEPS296 TraitsComparison[V2dTPTraits.__name__] = compareVecEPS297 TraitsComparison[P2sTPTraits.__name__] = compareExact298 TraitsComparison[P2iTPTraits.__name__] = compareExact 299 TraitsComparison[P2fTPTraits.__name__] = compareVecEPS300 TraitsComparison[P2dTPTraits.__name__] = compareVecEPS301 TraitsComparison[N2fTPTraits.__name__] = compareVecEPS302 TraitsComparison[N2dTPTraits.__name__] = compareVecEPS303 TraitsComparison[C3cTPTraits.__name__] = compareVec3Exact304 TraitsComparison[C3hTPTraits.__name__] = compareVecEPS305 TraitsComparison[C3fTPTraits.__name__] = compareC3EPS306 TraitsComparison[V3sTPTraits.__name__] = compareExact307 TraitsComparison[V3iTPTraits.__name__] = compareExact308 TraitsComparison[V3fTPTraits.__name__] = compareVecEPS309 TraitsComparison[V3dTPTraits.__name__] = compareVecEPS310 TraitsComparison[P3sTPTraits.__name__] = compareExact311 TraitsComparison[P3iTPTraits.__name__] = compareExact312 TraitsComparison[P3fTPTraits.__name__] = compareVecEPS313 TraitsComparison[P3dTPTraits.__name__] = compareVecEPS314 TraitsComparison[N3fTPTraits.__name__] = compareVecEPS315 TraitsComparison[N3dTPTraits.__name__] = compareVecEPS316 TraitsComparison[C4cTPTraits.__name__] = compareVec4Exact317 TraitsComparison[C4hTPTraits.__name__] = compareC4EPS318 TraitsComparison[C4fTPTraits.__name__] = compareC4EPS319 TraitsComparison[Box2sTPTraits.__name__] = compareBoxExact320 TraitsComparison[Box2iTPTraits.__name__] = compareBoxExact321 TraitsComparison[Box2fTPTraits.__name__] = compareBoxEPS322 TraitsComparison[Box2dTPTraits.__name__] = compareBoxEPS323 TraitsComparison[QuatfTPTraits.__name__] = compareQuatEPS324 TraitsComparison[QuatdTPTraits.__name__] = compareQuatEPS325 TraitsComparison[Box3sTPTraits.__name__] = compareBoxExact326 TraitsComparison[Box3iTPTraits.__name__] = compareBoxExact327 TraitsComparison[Box3fTPTraits.__name__] = compareBoxEPS328 TraitsComparison[Box3dTPTraits.__name__] = compareBoxEPS329 TraitsComparison[M33fTPTraits.__name__] = compareVecEPS330 TraitsComparison[M33dTPTraits.__name__] = compareVecEPS331 TraitsComparison[M44fTPTraits.__name__] = compareVecEPS332 TraitsComparison[M44dTPTraits.__name__] = compareVecEPS333# -------------------------------------------------------------------------334# Main loop335buildTestScalarData()336buildTestSmallArrayData()337buildTestArrayData()...
basic_gpu_test.py
Source:basic_gpu_test.py
...74 self._compareGPU(x, y, np.subtract, math_ops.subtract)75 self._compareGPU(x, y, np.multiply, math_ops.multiply)76 self._compareGPU(x, y + 0.1, np.true_divide, math_ops.truediv)77class MathBuiltinUnaryTest(test.TestCase):78 def _compare(self, x, np_func, tf_func, use_gpu):79 np_out = np_func(x)80 with self.cached_session(use_gpu=use_gpu) as sess:81 inx = ops.convert_to_tensor(x)82 ofunc = tf_func(inx)83 tf_out = self.evaluate(ofunc)84 self.assertAllClose(np_out, tf_out)85 def _inv(self, x):86 return 1.0 / x87 def _rsqrt(self, x):88 return self._inv(np.sqrt(x))89 def _testDtype(self, dtype, use_gpu):90 data = (np.arange(-3, 3) / 4.).reshape([1, 3, 2]).astype(dtype)91 data_gt_1 = data + 2 # for x > 192 self._compare(data, np.abs, math_ops.abs, use_gpu)93 self._compare(data, np.arccos, math_ops.acos, use_gpu)94 self._compare(data, np.arcsin, math_ops.asin, use_gpu)95 self._compare(data, np.arcsinh, math_ops.asinh, use_gpu)96 self._compare(data_gt_1, np.arccosh, math_ops.acosh, use_gpu)97 self._compare(data, np.arctan, math_ops.atan, use_gpu)98 self._compare(data, np.ceil, math_ops.ceil, use_gpu)99 self._compare(data, np.cos, math_ops.cos, use_gpu)100 self._compare(data, np.cosh, math_ops.cosh, use_gpu)101 self._compare(data, np.exp, math_ops.exp, use_gpu)102 self._compare(data, np.floor, math_ops.floor, use_gpu)103 self._compare(data, np.log, math_ops.log, use_gpu)104 self._compare(data, np.log1p, math_ops.log1p, use_gpu)105 self._compare(data, np.negative, math_ops.negative, use_gpu)106 self._compare(data, self._rsqrt, math_ops.rsqrt, use_gpu)107 self._compare(data, np.sin, math_ops.sin, use_gpu)108 self._compare(data, np.sinh, math_ops.sinh, use_gpu)109 self._compare(data, np.sqrt, math_ops.sqrt, use_gpu)110 self._compare(data, np.square, math_ops.square, use_gpu)111 self._compare(data, np.tan, math_ops.tan, use_gpu)112 self._compare(data, np.tanh, math_ops.tanh, use_gpu)113 self._compare(data, np.arctanh, math_ops.atanh, use_gpu)114 def testTypes(self):115 for dtype in [np.float32]:116 self._testDtype(dtype, use_gpu=True)117 def testFloorDivide(self):118 x = (1 + np.linspace(0, 5, np.prod([1, 3, 2]))).astype(np.float32).reshape(119 [1, 3, 2])120 y = (1 + np.linspace(0, 5, np.prod([1, 3, 2]))).astype(np.float32).reshape(121 [1, 3, 2])122 np_out = np.floor_divide(x, y + 0.1)123 with self.session(use_gpu=True) as sess:124 inx = ops.convert_to_tensor(x)125 iny = ops.convert_to_tensor(y + 0.1)126 ofunc = inx / iny127 out_func2 = math_ops.floor(ofunc)...
li_std_containers_int_runme.py
Source:li_std_containers_int_runme.py
1# Check std::vector and std::list behaves the same as Python iterable2# types (list)3from li_std_containers_int import *4import sys5def failed(a, b, msg):6 raise RuntimeError, msg + " " + str(list(a)) + " " + str(list(b))7def compare_sequences(a, b):8 if len(a) != len(b):9 failed(a, b, "different sizes")10 for i in range(len(a)):11 if a[i] != b[i]:12 failed(a, b, "elements are different")13def compare_containers(pythonlist, swigvector, swiglist):14 compare_sequences(pythonlist, swigvector)15 compare_sequences(pythonlist, swiglist)16# Check std::vector and std::list assignment behaves same as Python list17# assignment including exceptions18def container_insert_step(i, j, step, newval):19 ps = range(6)20 iv = vector_int(ps)21 il = list_int(ps)22 # Python slice23 try:24 if step == None:25 if j == None:26 ps[i] = newval27 else:28 ps[i:j] = newval29 else:30 if j == None:31 ps[i::step] = newval32 else:33 ps[i:j:step] = newval34 ps_error = None35 except ValueError, e:36 ps_error = e37 except IndexError, e:38 ps_error = e39 # std::vector<int>40 try:41 if step == None:42 if j == None:43 iv[i] = newval44 else:45 iv[i:j] = newval46 else:47 if j == None:48 iv[i::step] = newval49 else:50 iv[i:j:step] = newval51 iv_error = None52 except ValueError, e:53 iv_error = e54 except IndexError, e:55 iv_error = e56 # std::list<int>57 try:58 if step == None:59 if j == None:60 il[i] = newval61 else:62 il[i:j] = newval63 else:64 if j == None:65 il[i::step] = newval66 else:67 il[i:j:step] = newval68 il_error = None69 except ValueError, e:70 il_error = e71 except IndexError, e:72 il_error = e73 # Python 2.6 contains bug fixes in extended slicing syntax:74 # http://docs.python.org/2/whatsnew/2.6.html75 skip_check = ps_error != None and(76 iv_error == il_error == None) and step > 0 and (sys.version_info[0:2] < (2, 6))77 if not(skip_check):78 if not((type(ps_error) == type(iv_error)) and (type(ps_error) == type(il_error))):79 raise RuntimeError, "ValueError exception not consistently thrown: " + \80 str(ps_error) + " " + str(iv_error) + " " + str(il_error)81 compare_containers(ps, iv, il)82# Check std::vector and std::list delete behaves same as Python list83# delete including exceptions84def container_delete_step(i, j, step):85 ps = range(6)86 iv = vector_int(ps)87 il = list_int(ps)88 # Python slice89 try:90 if step == None:91 if j == None:92 del ps[i]93 else:94 del ps[i:j]95 else:96 if j == None:97 del ps[i::step]98 else:99 del ps[i:j:step]100 ps_error = None101 except ValueError, e:102 ps_error = e103 except IndexError, e:104 ps_error = e105 # std::vector<int>106 try:107 if step == None:108 if j == None:109 del iv[i]110 else:111 del iv[i:j]112 else:113 if j == None:114 del iv[i::step]115 else:116 del iv[i:j:step]117 iv_error = None118 except ValueError, e:119 iv_error = e120 except IndexError, e:121 iv_error = e122 # std::list<int>123 try:124 if step == None:125 if j == None:126 del il[i]127 else:128 del il[i:j]129 else:130 if j == None:131 del il[i::step]132 else:133 del il[i:j:step]134 il_error = None135 except ValueError, e:136 il_error = e137 except IndexError, e:138 il_error = e139 if not((type(ps_error) == type(iv_error)) and (type(ps_error) == type(il_error))):140 raise RuntimeError, "ValueError exception not consistently thrown: " + \141 str(ps_error) + " " + str(iv_error) + " " + str(il_error)142 compare_containers(ps, iv, il)143ps = [0, 1, 2, 3, 4, 5]144iv = vector_int(ps)145il = list_int(ps)146# slices147compare_containers(ps[0:0], iv[0:0], il[0:0])148compare_containers(ps[1:1], iv[1:1], il[1:1])149compare_containers(ps[1:3], iv[1:3], il[1:3])150compare_containers(ps[2:4], iv[2:4], il[2:4])151compare_containers(ps[0:3], iv[0:3], il[0:3])152compare_containers(ps[3:6], iv[3:6], il[3:6])153compare_containers(ps[3:10], iv[3:10], il[3:10]) # beyond end of range154# before beginning of range (negative indexing)155compare_containers(ps[-1:7], iv[-1:7], il[-1:7])156compare_containers(ps[-2:7], iv[-2:7], il[-2:7])157compare_containers(ps[-5:7], iv[-5:7], il[-5:7])158compare_containers(ps[-6:7], iv[-6:7], il[-6:7])159# before beginning of range (negative indexing, negative index is >160# container size)161compare_containers(ps[-7:7], iv[-7:7], il[-7:7])162compare_containers(ps[-100:7], iv[-100:7], il[-100:7])163compare_containers(ps[3:], iv[3:], il[3:])164compare_containers(ps[:3], iv[:3], il[:3])165compare_containers(ps[:], iv[:], il[:])166compare_containers(ps[-3:], iv[-3:], il[-3:])167compare_containers(ps[-7:], iv[-7:], il[-7:])168compare_containers(ps[:-1], iv[:-1], il[:-1])169compare_containers(ps[:-7], iv[:-7], il[:-7])170# step slicing171compare_containers(ps[1:5:1], iv[1:5:1], il[1:5:1])172compare_containers(ps[1:5:2], iv[1:5:2], il[1:5:2])173compare_containers(ps[1:5:3], iv[1:5:3], il[1:5:3])174compare_containers(ps[1:5:4], iv[1:5:4], il[1:5:4])175compare_containers(ps[1:6:5], iv[1:6:5], il[1:6:5])176compare_containers(ps[1:7:5], iv[1:7:5], il[1:7:5])177compare_containers(ps[-1:7:1], iv[-1:7:1], il[-1:7:1])178compare_containers(ps[-1:7:2], iv[-1:7:2], il[-1:7:2])179compare_containers(ps[-6:7:2], iv[-6:7:2], il[-6:7:2])180compare_containers(ps[-100:7:2], iv[-100:7:2], il[-100:7:2])181compare_containers(ps[::1], iv[::1], il[::1])182compare_containers(ps[::2], iv[::2], il[::2])183compare_containers(ps[::-1], iv[::-1], il[::-1])184compare_containers(ps[6::-1], iv[6::-1], il[6::-1])185compare_containers(ps[:-3:-1], iv[:-3:-1], il[:-3:-1])186compare_containers(ps[:-6:-1], iv[:-6:-1], il[:-6:-1])187compare_containers(ps[:-7:-1], iv[:-7:-1], il[:-7:-1])188compare_containers(ps[:-8:-1], iv[:-8:-1], il[:-8:-1])189compare_containers(ps[:-100:-1], iv[:-100:-1], il[:-100:-1])190compare_containers(ps[4:6:-1], iv[4:6:-1], il[4:6:-1])191compare_containers(ps[4:5:-1], iv[4:5:-1], il[4:5:-1])192compare_containers(ps[4:4:-1], iv[4:4:-1], il[4:4:-1])193compare_containers(ps[4:3:-1], iv[4:3:-1], il[4:3:-1])194compare_containers(ps[4:2:-1], iv[4:2:-1], il[4:2:-1])195compare_containers(ps[100:104:-1], iv[100:104:-1], il[100:104:-1])196compare_containers(ps[104:100:-1], iv[104:100:-1], il[104:100:-1])197compare_containers(ps[-100:-104:-1], iv[-100:-104:-1], il[-100:-104:-1])198compare_containers(ps[-104:-100:-1], iv[-104:-100:-1], il[-104:-100:-1])199compare_containers(ps[::-2], iv[::-2], il[::-2])200compare_containers(ps[::-3], iv[::-3], il[::-3])201compare_containers(ps[::-4], iv[::-4], il[::-4])202compare_containers(ps[::-5], iv[::-5], il[::-5])203# insert sequences (growing, shrinking and staying same size)204for start in [-102, -100, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 100, 102]:205 # single element set/replace206 container_insert_step(start, None, None, 111)207 for end in [-102, -100, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 100, 102]:208 container_insert_step(209 start, end, None, [111, 222, 333, 444, 555, 666, 777])210 container_insert_step(start, end, None, [111, 222, 333, 444, 555, 666])211 container_insert_step(start, end, None, [111, 222, 333, 444, 555])212 container_insert_step(start, end, None, [111, 222, 333, 444])213 container_insert_step(start, end, None, [111, 222, 333])214 container_insert_step(start, end, None, [111, 222])215 container_insert_step(start, end, None, [111])216 container_insert_step(start, end, None, [])217# delete sequences (growing, shrinking and staying same size)218for start in [-102, -100, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 100, 102]:219 # single element delete220 container_delete_step(start, None, None)221 for end in [-102, -100, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 100, 102]:222 container_delete_step(start, end, None)223 for step in range(-7, 7):224 container_delete_step(start, end, step)225ps = range(6)226iv = vector_int(ps)227il = list_int(ps)228del ps[:]229del iv[:]230del il[:]231compare_containers(ps, iv, il)232for end in range(7):233 for step in range(-7, 7):234 for start in range(7):235 container_insert_step(236 start, end, step, [111, 222, 333, 444, 555, 666, 777])237 container_insert_step(238 start, end, step, [111, 222, 333, 444, 555, 666])239 container_insert_step(start, end, step, [111, 222, 333, 444, 555])240 container_insert_step(start, end, step, [111, 222, 333, 444])241 container_insert_step(start, end, step, [111, 222, 333])242 container_insert_step(start, end, step, [111, 222])243 container_insert_step(start, end, step, [111])244 container_insert_step(start, end, step, [])245try:246 x = iv[::0]247 raise RuntimeError("Zero step not caught")248except ValueError:...
cpp11_li_std_array_runme.py
Source:cpp11_li_std_array_runme.py
1from cpp11_li_std_array import *2import sys3def failed(a, b, msg):4 raise RuntimeError, msg + " " + str(list(a)) + " " + str(list(b))5def compare_sequences(a, b):6 if len(a) != len(b):7 failed(a, b, "different sizes")8 for i in range(len(a)):9 if a[i] != b[i]:10 failed(a, b, "elements are different")11def compare_containers(pythonlist, swigarray):12 compare_sequences(pythonlist, swigarray)13def steps_exception(swigarray, i, j, step):14 try:15 if i == None and j == None:16 a = swigarray[::step]17 elif i == None:18 a = swigarray[:j:step]19 elif j == None:20 a = swigarray[i::step]21 else:22 a = swigarray[i:j:step]23 raise RuntimeError, "swigarray[" + str(i) + ":" + str(j) + ":" + str(step) + "] missed steps exception for " + str(list(swigarray))24 except ValueError, e:25# print("exception: {}".format(e))26 pass27def del_exception(swigarray, i, j, step):28 try:29 if i == None and j == None:30 del swigarray[::step]31 elif j == None and step == None:32 del swigarray[i]33 elif i == None:34 del swigarray[:j:step]35 elif j == None:36 del swigarray[i::step]37 else:38 del swigarray[i:j:step]39 raise RuntimeError, "swigarray[" + str(i) + ":" + str(j) + ":" + str(step) + "] missed del exception for " + str(list(swigarray))40 except ValueError, e:41# print("exception: {}".format(e))42 pass43def setslice_exception(swigarray, newval):44 try:45 swigarray[::] = newval46 raise RuntimeError, "swigarray[::] = " + str(newval) + " missed set exception for swigarray:" + str(list(swigarray))47 except TypeError, e:48# print("exception: {}".format(e))49 pass50# Check std::array has similar behaviour to a Python list51# except it is not resizable52ps = [0, 1, 2, 3, 4, 5]53ai = ArrayInt6(ps)54compare_containers(ps, ai)55# slices56compare_containers(ps[0:6], ai[0:6])57compare_containers(ps[0:10], ai[0:10])58compare_containers(ps[-10:6], ai[-10:6])59compare_containers(ps[-10:10], ai[-10:10])60compare_containers(ps[0:6:1], ai[0:6:1])61compare_containers(ps[::], ai[::])62compare_containers(ps[::1], ai[::1])63compare_containers([x for x in ps], [x for x in ai])64# Reverse65compare_containers(ps[::-1], ai[::-1])66compare_containers(ps[5::-1], ai[5::-1])67compare_containers(ps[10::-1], ai[10::-1])68# Steps other than +1 and -1 not supported69steps_exception(ai, 0, 6, 3)70steps_exception(ai, None, None, 0)71steps_exception(ai, None, None, 2)72steps_exception(ai, None, None, -2)73steps_exception(ai, 1, 3, 1)74steps_exception(ai, 3, 1, -1)75# Modify content76for i in range(len(ps)):77 ps[i] = (ps[i] + 1) * 1078 ai[i] = (ai[i] + 1) * 1079compare_containers(ps, ai)80# Delete81del_exception(ai, 0, 6, 3)82del_exception(ai, None, None, 0)83del_exception(ai, None, None, 2)84del_exception(ai, None, None, -2)85del_exception(ai, 1, 3, 1)86del_exception(ai, 3, 1, -1)87del_exception(ai, 0, None, None)88del_exception(ai, 5, None, None)89# Empty90ai = ArrayInt6()91compare_containers([0, 0, 0, 0, 0, 0], ai)92# Set slice93newvals = [10, 20, 30, 40, 50, 60]94ai[::] = newvals95compare_containers(ai, newvals)96newvals = [100, 200, 300, 400, 500, 600]97ai[0:6:1] = newvals98compare_containers(ai, newvals)99newvals = [1000, 2000, 3000, 4000, 5000, 6000]100ai[::-1] = newvals101compare_containers(ai, newvals[::-1])102newvals = [10000, 20000, 30000, 40000, 50000, 60000]103ai[-10:100:1] = newvals104compare_containers(ai, newvals[-10:100:1])105setslice_exception(ai, [1, 2, 3, 4, 5, 6, 7])106setslice_exception(ai, [1, 2, 3, 4, 5])107setslice_exception(ai, [1, 2, 3, 4])108setslice_exception(ai, [1, 2, 3])109setslice_exception(ai, [1, 2])110setslice_exception(ai, [1])111setslice_exception(ai, [])112# Check return113compare_containers(arrayOutVal(), [-2, -1, 0, 0, 1, 2])114compare_containers(arrayOutConstRef(), [-2, -1, 0, 0, 1, 2])115compare_containers(arrayOutRef(), [-2, -1, 0, 0, 1, 2])116compare_containers(arrayOutPtr(), [-2, -1, 0, 0, 1, 2])117# Check passing arguments118ai = arrayInVal([9, 8, 7, 6, 5, 4])119compare_containers(ai, [90, 80, 70, 60, 50, 40])120ai = arrayInConstRef([9, 8, 7, 6, 5, 4])121compare_containers(ai, [90, 80, 70, 60, 50, 40])122ai = ArrayInt6([9, 8, 7, 6, 5, 4])123arrayInRef(ai)124compare_containers(ai, [90, 80, 70, 60, 50, 40])125ai = ArrayInt6([9, 8, 7, 6, 5, 4])126arrayInPtr(ai)127compare_containers(ai, [90, 80, 70, 60, 50, 40])128# fill129ai.fill(111)...
test_util.py
Source:test_util.py
1"""2Tests capa util3"""4import unittest5import textwrap6from . import test_capa_system7from capa.util import compare_with_tolerance, sanitize_html8class UtilTest(unittest.TestCase):9 """Tests for util"""10 def setUp(self):11 super(UtilTest, self).setUp()12 self.system = test_capa_system()13 def test_compare_with_tolerance(self):14 # Test default tolerance '0.001%' (it is relative)15 result = compare_with_tolerance(100.0, 100.0)16 self.assertTrue(result)17 result = compare_with_tolerance(100.001, 100.0)18 self.assertTrue(result)19 result = compare_with_tolerance(101.0, 100.0)20 self.assertFalse(result)21 # Test absolute percentage tolerance22 result = compare_with_tolerance(109.9, 100.0, '10%', False)23 self.assertTrue(result)24 result = compare_with_tolerance(110.1, 100.0, '10%', False)25 self.assertFalse(result)26 # Test relative percentage tolerance27 result = compare_with_tolerance(111.0, 100.0, '10%', True)28 self.assertTrue(result)29 result = compare_with_tolerance(112.0, 100.0, '10%', True)30 self.assertFalse(result)31 # Test absolute tolerance (string)32 result = compare_with_tolerance(109.9, 100.0, '10.0', False)33 self.assertTrue(result)34 result = compare_with_tolerance(110.1, 100.0, '10.0', False)35 self.assertFalse(result)36 # Test relative tolerance (string)37 result = compare_with_tolerance(111.0, 100.0, '0.1', True)38 self.assertTrue(result)39 result = compare_with_tolerance(112.0, 100.0, '0.1', True)40 self.assertFalse(result)41 # Test absolute tolerance (float)42 result = compare_with_tolerance(109.9, 100.0, 10.0, False)43 self.assertTrue(result)44 result = compare_with_tolerance(110.1, 100.0, 10.0, False)45 self.assertFalse(result)46 # Test relative tolerance (float)47 result = compare_with_tolerance(111.0, 100.0, 0.1, True)48 self.assertTrue(result)49 result = compare_with_tolerance(112.0, 100.0, 0.1, True)50 self.assertFalse(result)51 ##### Infinite values #####52 infinity = float('Inf')53 # Test relative tolerance (float)54 result = compare_with_tolerance(infinity, 100.0, 1.0, True)55 self.assertFalse(result)56 result = compare_with_tolerance(100.0, infinity, 1.0, True)57 self.assertFalse(result)58 result = compare_with_tolerance(infinity, infinity, 1.0, True)59 self.assertTrue(result)60 # Test absolute tolerance (float)61 result = compare_with_tolerance(infinity, 100.0, 1.0, False)62 self.assertFalse(result)63 result = compare_with_tolerance(100.0, infinity, 1.0, False)64 self.assertFalse(result)65 result = compare_with_tolerance(infinity, infinity, 1.0, False)66 self.assertTrue(result)67 # Test relative tolerance (string)68 result = compare_with_tolerance(infinity, 100.0, '1.0', True)69 self.assertFalse(result)70 result = compare_with_tolerance(100.0, infinity, '1.0', True)71 self.assertFalse(result)72 result = compare_with_tolerance(infinity, infinity, '1.0', True)73 self.assertTrue(result)74 # Test absolute tolerance (string)75 result = compare_with_tolerance(infinity, 100.0, '1.0', False)76 self.assertFalse(result)77 result = compare_with_tolerance(100.0, infinity, '1.0', False)78 self.assertFalse(result)79 result = compare_with_tolerance(infinity, infinity, '1.0', False)80 self.assertTrue(result)81 def test_sanitize_html(self):82 """83 Test for html sanitization with bleach.84 """85 allowed_tags = ['div', 'p', 'audio', 'pre', 'span']86 for tag in allowed_tags:87 queue_msg = "<{0}>Test message</{0}>".format(tag)88 self.assertEqual(sanitize_html(queue_msg), queue_msg)89 not_allowed_tag = 'script'90 queue_msg = "<{0}>Test message</{0}>".format(not_allowed_tag)91 expected = "<script>Test message</script>"...
Using AI Code Generation
1module.exports = {2 {3 },4 {5 },6 {7 },8 {9 }10 {11 }12 "paths": {13 },14};15module.exports = function (chromy, scenario) {16 console.log('onBefore.js');17 var cookies = [];18 scenario.cookiePath = scenario.cookiePath || 'backstop_data/engine_scripts/cookies.json';19 try {20 cookies = require(scenario.cookiePath);
Using AI Code Generation
1var backstop = require('backstopjs');2backstop('test', {config: './backstop.json'});3var backstop = require('backstopjs');4backstop('reference', {config: './backstop.json'});5var backstop = require('backstopjs');6backstop('approve', {config: './backstop.json'});7{8 {9 }10 {11 }12 "paths": {13 },14 "engineOptions": {15 },16}
Using AI Code Generation
1var fs = require('fs');2var path = require('path');3var backstopjs = require('backstopjs');4var backstopConfig = require('./backstop.json');5var backstopConfigPath = path.resolve('./backstop.json');6var backstopConfig = require(backstopConfigPath);7var backstopConfig = {8 {9 },10 {11 },12 {13 },14 {15 },16 {17 }18 {19 }20 "paths": {21 },22 "engineOptions": {23 },24};
Using AI Code Generation
1var compare = require('backstopjs/compare');2var config = require('./config.js');3var referenceUrl = config.scenarios[0].referenceUrl;4var testUrl = config.scenarios[0].testUrl;5var readyEvent = config.scenarios[0].readyEvent;6var readySelector = config.scenarios[0].readySelector;7var delay = config.scenarios[0].delay;8var misMatchThreshold = config.scenarios[0].misMatchThreshold;9var requireSameDimensions = config.scenarios[0].requireSameDimensions;10var label = config.scenarios[0].label;11var fileName = config.scenarios[0].fileName;12var viewports = config.viewports;13var viewportsLength = viewports.length;14var viewport = viewports[0];15var viewportsIndex = 0;16var options = {17};18compare(options);19var fs = require('fs');20var scenarios = [];21var scenario = {22};23scenarios.push(scenario);24var config = {25 {26 },27 {28 },29 {30 }
Using AI Code Generation
1var fs = require('fs');2var path = require('path');3var backstop = require('backstopjs');4var config = require('./backstop.json');5var args = process.argv.slice(2);6var testType = args[0];7var scenarioLabel = args[1];8var scenario = config.scenarios.find(function (scenario) {9 return scenario.label === scenarioLabel;10});11if (!scenario) {12 throw new Error('Scenario not found: ' + scenarioLabel);13}14var scenarios = [scenario];15var backstopConfig = Object.assign({}, config, {scenarios: scenarios});16backstop(testType, {config: backstopConfig})17 .then(function () {18 console.log('BackstopJS finished.');19 })20 .catch(function (error) {21 console.error(error);22 });23{24 {25 },26 {27 },28 {29 }30 {31 }32 "paths": {33 },34 "engineOptions": {
Using AI Code Generation
1var backstop = require('backstopjs');2var fs = require('fs');3var config = require('./backstop.json');4var url = require('url');5var path = require('path');6var request = require('request');7var async = require('async');8var _ = require('lodash');
Using AI Code Generation
1var fs = require("fs");2var backstop = require("backstopjs");3var backstop_config = require("./backstop.json");4var date = new Date().toISOString().replace(/T/, '_').replace(/\..+/, '').replace(/:/g, '-');5var report_path = "reports/report_" + date + "/";6var report_json = report_path + "report.json";7var report_html = report_path + "report.html";8var report_json_data = {};9var report_html_data = "";10function createReportDirectory() {11 var dir = './reports';12 if (!fs.existsSync(dir)) {13 fs.mkdirSync(dir);14 }15 fs.mkdirSync(report_path);16}17function createReportJson() {18 report_json_data = {19 };20 fs.writeFileSync(report_json, JSON.stringify(report_json_data, null, 4));21}22function createReportHtml() {23 report_html_data = "<html><head><title>BackstopJS Report</title></head><body>";24 fs.writeFileSync(report_html, report_html_data);25}26function updateReportJson(test_data) {27 report_json_data.tests.push(test_data);28 fs.writeFileSync(report_json, JSON.stringify(report_json_data, null, 4));29}30function updateReportHtml(test_data) {31 report_html_data += "<div>";32 report_html_data += "<div>" + test_data.label + "</div>";33 report_html_data += "<div>Test Status: " + test_data.status + "</div>";34 report_html_data += "<div>Test Result: " + test_data.message + "</div>";35 report_html_data += "<div>Reference Image: <a href='" + test_data.reference + "'>Reference Image</a></div>";36 report_html_data += "<div>Test Image: <a href='" + test_data.test + "'>Test Image</a></div>";37 report_html_data += "<div>Diff Image: <a href='" + test_data.diff + "'>Diff Image</a></div>";38 report_html_data += "</div>";39 fs.writeFileSync(report_html, report_html_data);40}41function closeReportHtml() {42 report_html_data += "</body></html>";43 fs.writeFileSync(report_html,
Using AI Code Generation
1var backstop = require('backstopjs');2var childProcess = require('child_process');3var fs = require('fs');4var path = require('path');5var config = require('./backstop.json');6var scenarios = config.scenarios;7var scenario = scenarios[0];8var scenarioPath = path.join(__dirname, 'backstop_data', 'bitmaps_test', scenario.label);9var files = fs.readdirSync(scenarioPath);10var referenceFilename = files[0];11var testFilename = files[1];12var referencePath = path.join(scenarioPath, referenceFilename);13var testPath = path.join(scenarioPath, testFilename);14var command = 'compare';15];16var result = childProcess.spawnSync(command, args);17var output = result.stdout.toString();18var metric = parseInt(output.split(' ')[1]);19console.log('AE metric: ' + metric);20var threshold = 10000;21if (metric < threshold) {22 console.log('Test passed');23} else {24 console.log('Test failed');25}26{27 {28 },29 {30 },31 {32 },33 {34 }35 {
Using AI Code Generation
1var fs = require('fs');2var request = require('request');3var path = require('path');4var config = require('./config.json');5var url = config.url;6var testname = config.testname;7var testurl = config.testurl;8var testurl2 = config.testurl2;9var testurl3 = config.testurl3;10var testurl4 = config.testurl4;11var testurl5 = config.testurl5;12var testurl6 = config.testurl6;13var testurl7 = config.testurl7;14var testurl8 = config.testurl8;15var testurl9 = config.testurl9;16var testurl10 = config.testurl10;17var testurl11 = config.testurl11;18var testurl12 = config.testurl12;19var testurl13 = config.testurl13;20var testurl14 = config.testurl14;21var testurl15 = config.testurl15;22var testurl16 = config.testurl16;23var testurl17 = config.testurl17;24var testurl18 = config.testurl18;25var testurl19 = config.testurl19;26var testurl20 = config.testurl20;27var testurl21 = config.testurl21;28var testurl22 = config.testurl22;29var testurl23 = config.testurl23;30var testurl24 = config.testurl24;31var testurl25 = config.testurl25;32var testurl26 = config.testurl26;33var testurl27 = config.testurl27;34var testurl28 = config.testurl28;35var testurl29 = config.testurl29;36var testurl30 = config.testurl30;37var testurl31 = config.testurl31;38var testurl32 = config.testurl32;39var testurl33 = config.testurl33;40var testurl34 = config.testurl34;41var testurl35 = config.testurl35;42var testurl36 = config.testurl36;43var testurl37 = config.testurl37;44var testurl38 = config.testurl38;45var testurl39 = config.testurl39;46var testurl40 = config.testurl40;47var testurl41 = config.testurl41;48var testurl42 = config.testurl42;49var testurl43 = config.testurl43;50var testurl44 = config.testurl44;51var testurl45 = config.testurl45;
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!