Best JavaScript code snippet using wpt
metrics_test.py
Source:metrics_test.py
...44 self.assertEqual(m.name, 'my_mean')45 self.assertTrue(m.stateful)46 self.assertEqual(m.dtype, dtypes.float32)47 self.assertEqual(len(m.variables), 2)48 self.evaluate(variables.variables_initializer(m.variables))49 # check initial state50 self.assertEqual(self.evaluate(m.total), 0)51 self.assertEqual(self.evaluate(m.count), 0)52 # check __call__()53 self.assertEqual(self.evaluate(m(100)), 100)54 self.assertEqual(self.evaluate(m.total), 100)55 self.assertEqual(self.evaluate(m.count), 1)56 # check update_state() and result() + state accumulation + tensor input57 update_op = m.update_state(ops.convert_n_to_tensor([1, 5]))58 self.evaluate(update_op)59 self.assertAlmostEqual(self.evaluate(m.result()), 106 / 3, 2)60 self.assertEqual(self.evaluate(m.total), 106) # 100 + 1 + 561 self.assertEqual(self.evaluate(m.count), 3)62 # check reset_states()63 m.reset_states()64 self.assertEqual(self.evaluate(m.total), 0)65 self.assertEqual(self.evaluate(m.count), 0)66 # Check save and restore config67 m2 = metrics.Mean.from_config(m.get_config())68 self.assertEqual(m2.name, 'my_mean')69 self.assertTrue(m2.stateful)70 self.assertEqual(m2.dtype, dtypes.float32)71 self.assertEqual(len(m2.variables), 2)72 def test_mean_with_sample_weight(self):73 m = metrics.Mean(dtype=dtypes.float64)74 self.assertEqual(m.dtype, dtypes.float64)75 self.evaluate(variables.variables_initializer(m.variables))76 # check scalar weight77 result_t = m(100, sample_weight=0.5)78 self.assertEqual(self.evaluate(result_t), 50 / 0.5)79 self.assertEqual(self.evaluate(m.total), 50)80 self.assertEqual(self.evaluate(m.count), 0.5)81 # check weights not scalar and weights rank matches values rank82 result_t = m([1, 5], sample_weight=[1, 0.2])83 result = self.evaluate(result_t)84 self.assertAlmostEqual(result, 52 / 1.7, 2)85 self.assertAlmostEqual(self.evaluate(m.total), 52, 2) # 50 + 1 + 5 * 0.286 self.assertAlmostEqual(self.evaluate(m.count), 1.7, 2) # 0.5 + 1.287 # check weights broadcast88 result_t = m([1, 2], sample_weight=0.5)89 self.assertAlmostEqual(self.evaluate(result_t), 53.5 / 2.7, 2)90 self.assertAlmostEqual(self.evaluate(m.total), 53.5, 2) # 52 + 0.5 + 191 self.assertAlmostEqual(self.evaluate(m.count), 2.7, 2) # 1.7 + 0.5 + 0.592 # check weights squeeze93 result_t = m([1, 5], sample_weight=[[1], [0.2]])94 self.assertAlmostEqual(self.evaluate(result_t), 55.5 / 3.9, 2)95 self.assertAlmostEqual(self.evaluate(m.total), 55.5, 2) # 53.5 + 1 + 196 self.assertAlmostEqual(self.evaluate(m.count), 3.9, 2) # 2.7 + 1.297 # check weights expand98 result_t = m([[1], [5]], sample_weight=[1, 0.2])99 self.assertAlmostEqual(self.evaluate(result_t), 57.5 / 5.1, 2)100 self.assertAlmostEqual(self.evaluate(m.total), 57.5, 2) # 55.5 + 1 + 1101 self.assertAlmostEqual(self.evaluate(m.count), 5.1, 2) # 3.9 + 1.2102 # check values reduced to the dimensions of weight103 result_t = m([[[1., 2.], [3., 2.], [0.5, 4.]]], sample_weight=[0.5])104 result = np.round(self.evaluate(result_t), decimals=2) # 58.5 / 5.6105 self.assertEqual(result, 10.45)106 self.assertEqual(np.round(self.evaluate(m.total), decimals=2), 58.54)107 self.assertEqual(np.round(self.evaluate(m.count), decimals=2), 5.6)108 def test_mean_graph_with_placeholder(self):109 with context.graph_mode(), self.cached_session() as sess:110 m = metrics.Mean()111 v = array_ops.placeholder(dtypes.float32)112 w = array_ops.placeholder(dtypes.float32)113 self.evaluate(variables.variables_initializer(m.variables))114 # check __call__()115 result_t = m(v, sample_weight=w)116 result = sess.run(result_t, feed_dict=({v: 100, w: 0.5}))117 self.assertEqual(self.evaluate(m.total), 50)118 self.assertEqual(self.evaluate(m.count), 0.5)119 self.assertEqual(result, 50 / 0.5)120 # check update_state() and result()121 result = sess.run(result_t, feed_dict=({v: [1, 5], w: [1, 0.2]}))122 self.assertAlmostEqual(self.evaluate(m.total), 52, 2) # 50 + 1 + 5 * 0.2123 self.assertAlmostEqual(self.evaluate(m.count), 1.7, 2) # 0.5 + 1.2124 self.assertAlmostEqual(result, 52 / 1.7, 2)125 def test_save_restore(self):126 checkpoint_directory = self.get_temp_dir()127 checkpoint_prefix = os.path.join(checkpoint_directory, 'ckpt')128 m = metrics.Mean()129 checkpoint = checkpointable_utils.Checkpoint(mean=m)130 self.evaluate(variables.variables_initializer(m.variables))131 # update state132 self.evaluate(m(100.))133 self.evaluate(m(200.))134 # save checkpoint and then add an update135 save_path = checkpoint.save(checkpoint_prefix)136 self.evaluate(m(1000.))137 # restore to the same checkpoint mean object138 checkpoint.restore(save_path).assert_consumed().run_restore_ops()139 self.evaluate(m(300.))140 self.assertEqual(200., self.evaluate(m.result()))141 # restore to a different checkpoint mean object142 restore_mean = metrics.Mean()143 restore_checkpoint = checkpointable_utils.Checkpoint(mean=restore_mean)144 status = restore_checkpoint.restore(save_path)145 restore_update = restore_mean(300.)146 status.assert_consumed().run_restore_ops()147 self.evaluate(restore_update)148 self.assertEqual(200., self.evaluate(restore_mean.result()))149 self.assertEqual(3, self.evaluate(restore_mean.count))150@test_util.run_all_in_graph_and_eager_modes151class KerasAccuracyTest(test.TestCase):152 def test_accuracy(self):153 acc_obj = metrics.Accuracy(name='my acc')154 # check config155 self.assertEqual(acc_obj.name, 'my acc')156 self.assertTrue(acc_obj.stateful)157 self.assertEqual(len(acc_obj.variables), 2)158 self.assertEqual(acc_obj.dtype, dtypes.float32)159 self.evaluate(variables.variables_initializer(acc_obj.variables))160 # verify that correct value is returned161 update_op = acc_obj.update_state([[1], [2], [3], [4]], [[1], [2], [3], [4]])162 self.evaluate(update_op)163 result = self.evaluate(acc_obj.result())164 self.assertEqual(result, 1) # 2/2165 # Check save and restore config166 a2 = metrics.Accuracy.from_config(acc_obj.get_config())167 self.assertEqual(a2.name, 'my acc')168 self.assertTrue(a2.stateful)169 self.assertEqual(len(a2.variables), 2)170 self.assertEqual(a2.dtype, dtypes.float32)171 # check with sample_weight172 result_t = acc_obj([[2], [1]], [[2], [0]], sample_weight=[[0.5], [0.2]])173 result = self.evaluate(result_t)174 self.assertAlmostEqual(result, 0.96, 2) # 4.5/4.7175 def test_binary_accuracy(self):176 acc_obj = metrics.BinaryAccuracy(name='my acc')177 # check config178 self.assertEqual(acc_obj.name, 'my acc')179 self.assertTrue(acc_obj.stateful)180 self.assertEqual(len(acc_obj.variables), 2)181 self.assertEqual(acc_obj.dtype, dtypes.float32)182 self.evaluate(variables.variables_initializer(acc_obj.variables))183 # verify that correct value is returned184 update_op = acc_obj.update_state([[1], [0]], [[1], [0]])185 self.evaluate(update_op)186 result = self.evaluate(acc_obj.result())187 self.assertEqual(result, 1) # 2/2188 # check y_pred squeeze189 update_op = acc_obj.update_state([[1], [1]], [[[1]], [[0]]])190 self.evaluate(update_op)191 result = self.evaluate(acc_obj.result())192 self.assertAlmostEqual(result, 0.75, 2) # 3/4193 # check y_true squeeze194 result_t = acc_obj([[[1]], [[1]]], [[1], [0]])195 result = self.evaluate(result_t)196 self.assertAlmostEqual(result, 0.67, 2) # 4/6197 # check with sample_weight198 result_t = acc_obj([[1], [1]], [[1], [0]], [[0.5], [0.2]])199 result = self.evaluate(result_t)200 self.assertAlmostEqual(result, 0.67, 2) # 4.5/6.7201 def test_binary_accuracy_threshold(self):202 acc_obj = metrics.BinaryAccuracy(threshold=0.7)203 self.evaluate(variables.variables_initializer(acc_obj.variables))204 result_t = acc_obj([[1], [1], [0], [0]], [[0.9], [0.6], [0.4], [0.8]])205 result = self.evaluate(result_t)206 self.assertAlmostEqual(result, 0.5, 2)207 def test_categorical_accuracy(self):208 acc_obj = metrics.CategoricalAccuracy(name='my acc')209 # check config210 self.assertEqual(acc_obj.name, 'my acc')211 self.assertTrue(acc_obj.stateful)212 self.assertEqual(len(acc_obj.variables), 2)213 self.assertEqual(acc_obj.dtype, dtypes.float32)214 self.evaluate(variables.variables_initializer(acc_obj.variables))215 # verify that correct value is returned216 update_op = acc_obj.update_state([[0, 0, 1], [0, 1, 0]],217 [[0.1, 0.1, 0.8], [0.05, 0.95, 0]])218 self.evaluate(update_op)219 result = self.evaluate(acc_obj.result())220 self.assertEqual(result, 1) # 2/2221 # check with sample_weight222 result_t = acc_obj([[0, 0, 1], [0, 1, 0]],223 [[0.1, 0.1, 0.8], [0.05, 0, 0.95]], [[0.5], [0.2]])224 result = self.evaluate(result_t)225 self.assertAlmostEqual(result, 0.93, 2) # 2.5/2.7226 def test_sparse_categorical_accuracy(self):227 acc_obj = metrics.SparseCategoricalAccuracy(name='my acc')228 # check config229 self.assertEqual(acc_obj.name, 'my acc')230 self.assertTrue(acc_obj.stateful)231 self.assertEqual(len(acc_obj.variables), 2)232 self.assertEqual(acc_obj.dtype, dtypes.float32)233 self.evaluate(variables.variables_initializer(acc_obj.variables))234 # verify that correct value is returned235 update_op = acc_obj.update_state([[2], [1]],236 [[0.1, 0.1, 0.8], [0.05, 0.95, 0]])237 self.evaluate(update_op)238 result = self.evaluate(acc_obj.result())239 self.assertEqual(result, 1) # 2/2240 # check with sample_weight241 result_t = acc_obj([[2], [1]], [[0.1, 0.1, 0.8], [0.05, 0, 0.95]],242 [[0.5], [0.2]])243 result = self.evaluate(result_t)244 self.assertAlmostEqual(result, 0.93, 2) # 2.5/2.7245 def test_sparse_categorical_accuracy_mismatched_dims(self):246 acc_obj = metrics.SparseCategoricalAccuracy(name='my acc')247 # check config248 self.assertEqual(acc_obj.name, 'my acc')249 self.assertTrue(acc_obj.stateful)250 self.assertEqual(len(acc_obj.variables), 2)251 self.assertEqual(acc_obj.dtype, dtypes.float32)252 self.evaluate(variables.variables_initializer(acc_obj.variables))253 # verify that correct value is returned254 update_op = acc_obj.update_state([2, 1], [[0.1, 0.1, 0.8], [0.05, 0.95, 0]])255 self.evaluate(update_op)256 result = self.evaluate(acc_obj.result())257 self.assertEqual(result, 1) # 2/2258 # check with sample_weight259 result_t = acc_obj([2, 1], [[0.1, 0.1, 0.8], [0.05, 0, 0.95]],260 [[0.5], [0.2]])261 result = self.evaluate(result_t)262 self.assertAlmostEqual(result, 0.93, 2) # 2.5/2.7263 def test_sparse_categorical_accuracy_mismatched_dims_dynamic(self):264 with context.graph_mode(), self.cached_session() as sess:265 acc_obj = metrics.SparseCategoricalAccuracy(name='my acc')266 self.evaluate(variables.variables_initializer(acc_obj.variables))267 t = array_ops.placeholder(dtypes.float32)268 p = array_ops.placeholder(dtypes.float32)269 w = array_ops.placeholder(dtypes.float32)270 result_t = acc_obj(t, p, w)271 result = sess.run(272 result_t,273 feed_dict=({274 t: [2, 1],275 p: [[0.1, 0.1, 0.8], [0.05, 0, 0.95]],276 w: [[0.5], [0.2]]277 }))278 self.assertAlmostEqual(result, 0.71, 2) # 2.5/2.7279@test_util.run_all_in_graph_and_eager_modes280class CosineProximityTest(test.TestCase):281 def l2_norm(self, x, axis):282 epsilon = 1e-12283 square_sum = np.sum(np.square(x), axis=axis, keepdims=True)284 x_inv_norm = 1 / np.sqrt(np.maximum(square_sum, epsilon))285 return np.multiply(x, x_inv_norm)286 def setup(self, axis=1):287 self.np_y_true = np.asarray([[1, 9, 2], [-5, -2, 6]], dtype=np.float32)288 self.np_y_pred = np.asarray([[4, 8, 12], [8, 1, 3]], dtype=np.float32)289 y_true = self.l2_norm(self.np_y_true, axis)290 y_pred = self.l2_norm(self.np_y_pred, axis)291 self.expected_loss = -np.sum(np.multiply(y_true, y_pred), axis=(axis,))292 self.y_true = constant_op.constant(self.np_y_true)293 self.y_pred = constant_op.constant(self.np_y_pred)294 def test_config(self):295 cosine_obj = metrics.CosineProximity(296 axis=2, name='my_cos', dtype=dtypes.int32)297 self.assertEqual(cosine_obj.name, 'my_cos')298 self.assertEqual(cosine_obj._dtype, dtypes.int32)299 # Check save and restore config300 cosine_obj2 = metrics.CosineProximity.from_config(cosine_obj.get_config())301 self.assertEqual(cosine_obj2.name, 'my_cos')302 self.assertEqual(cosine_obj2._dtype, dtypes.int32)303 def test_unweighted(self):304 self.setup()305 cosine_obj = metrics.CosineProximity()306 self.evaluate(variables.variables_initializer(cosine_obj.variables))307 loss = cosine_obj(self.y_true, self.y_pred)308 expected_loss = np.mean(self.expected_loss)309 self.assertAlmostEqual(self.evaluate(loss), expected_loss, 3)310 def test_weighted(self):311 self.setup()312 cosine_obj = metrics.CosineProximity()313 self.evaluate(variables.variables_initializer(cosine_obj.variables))314 sample_weight = np.asarray([1.2, 3.4])315 loss = cosine_obj(316 self.y_true,317 self.y_pred,318 sample_weight=constant_op.constant(sample_weight))319 expected_loss = np.sum(320 self.expected_loss * sample_weight) / np.sum(sample_weight)321 self.assertAlmostEqual(self.evaluate(loss), expected_loss, 3)322 def test_axis(self):323 self.setup(axis=1)324 cosine_obj = metrics.CosineProximity(axis=1)325 self.evaluate(variables.variables_initializer(cosine_obj.variables))326 loss = cosine_obj(self.y_true, self.y_pred)327 expected_loss = np.mean(self.expected_loss)328 self.assertAlmostEqual(self.evaluate(loss), expected_loss, 3)329@test_util.run_all_in_graph_and_eager_modes330class MeanAbsoluteErrorTest(test.TestCase):331 def test_config(self):332 mae_obj = metrics.MeanAbsoluteError(name='my_mae', dtype=dtypes.int32)333 self.assertEqual(mae_obj.name, 'my_mae')334 self.assertEqual(mae_obj._dtype, dtypes.int32)335 # Check save and restore config336 mae_obj2 = metrics.MeanAbsoluteError.from_config(mae_obj.get_config())337 self.assertEqual(mae_obj2.name, 'my_mae')338 self.assertEqual(mae_obj2._dtype, dtypes.int32)339 def test_unweighted(self):340 mae_obj = metrics.MeanAbsoluteError()341 self.evaluate(variables.variables_initializer(mae_obj.variables))342 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),343 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))344 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),345 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))346 update_op = mae_obj.update_state(y_true, y_pred)347 self.evaluate(update_op)348 result = mae_obj.result()349 self.assertAllClose(0.5, result, atol=1e-5)350 def test_weighted(self):351 mae_obj = metrics.MeanAbsoluteError()352 self.evaluate(variables.variables_initializer(mae_obj.variables))353 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),354 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))355 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),356 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))357 sample_weight = constant_op.constant((1., 1.5, 2., 2.5))358 result = mae_obj(y_true, y_pred, sample_weight=sample_weight)359 self.assertAllClose(0.54285, self.evaluate(result), atol=1e-5)360@test_util.run_all_in_graph_and_eager_modes361class MeanAbsolutePercentageErrorTest(test.TestCase):362 def test_config(self):363 mape_obj = metrics.MeanAbsolutePercentageError(364 name='my_mape', dtype=dtypes.int32)365 self.assertEqual(mape_obj.name, 'my_mape')366 self.assertEqual(mape_obj._dtype, dtypes.int32)367 # Check save and restore config368 mape_obj2 = metrics.MeanAbsolutePercentageError.from_config(369 mape_obj.get_config())370 self.assertEqual(mape_obj2.name, 'my_mape')371 self.assertEqual(mape_obj2._dtype, dtypes.int32)372 def test_unweighted(self):373 mape_obj = metrics.MeanAbsolutePercentageError()374 self.evaluate(variables.variables_initializer(mape_obj.variables))375 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),376 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))377 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),378 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))379 update_op = mape_obj.update_state(y_true, y_pred)380 self.evaluate(update_op)381 result = mape_obj.result()382 self.assertAllClose(35e7, result, atol=1e-5)383 def test_weighted(self):384 mape_obj = metrics.MeanAbsolutePercentageError()385 self.evaluate(variables.variables_initializer(mape_obj.variables))386 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),387 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))388 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),389 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))390 sample_weight = constant_op.constant((1., 1.5, 2., 2.5))391 result = mape_obj(y_true, y_pred, sample_weight=sample_weight)392 self.assertAllClose(40e7, self.evaluate(result), atol=1e-5)393@test_util.run_all_in_graph_and_eager_modes394class MeanSquaredErrorTest(test.TestCase):395 def test_config(self):396 mse_obj = metrics.MeanSquaredError(name='my_mse', dtype=dtypes.int32)397 self.assertEqual(mse_obj.name, 'my_mse')398 self.assertEqual(mse_obj._dtype, dtypes.int32)399 # Check save and restore config400 mse_obj2 = metrics.MeanSquaredError.from_config(mse_obj.get_config())401 self.assertEqual(mse_obj2.name, 'my_mse')402 self.assertEqual(mse_obj2._dtype, dtypes.int32)403 def test_unweighted(self):404 mse_obj = metrics.MeanSquaredError()405 self.evaluate(variables.variables_initializer(mse_obj.variables))406 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),407 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))408 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),409 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))410 update_op = mse_obj.update_state(y_true, y_pred)411 self.evaluate(update_op)412 result = mse_obj.result()413 self.assertAllClose(0.5, result, atol=1e-5)414 def test_weighted(self):415 mse_obj = metrics.MeanSquaredError()416 self.evaluate(variables.variables_initializer(mse_obj.variables))417 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),418 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))419 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),420 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))421 sample_weight = constant_op.constant((1., 1.5, 2., 2.5))422 result = mse_obj(y_true, y_pred, sample_weight=sample_weight)423 self.assertAllClose(0.54285, self.evaluate(result), atol=1e-5)424@test_util.run_all_in_graph_and_eager_modes425class MeanSquaredLogarithmicErrorTest(test.TestCase):426 def test_config(self):427 msle_obj = metrics.MeanSquaredLogarithmicError(428 name='my_msle', dtype=dtypes.int32)429 self.assertEqual(msle_obj.name, 'my_msle')430 self.assertEqual(msle_obj._dtype, dtypes.int32)431 # Check save and restore config432 msle_obj2 = metrics.MeanSquaredLogarithmicError.from_config(433 msle_obj.get_config())434 self.assertEqual(msle_obj2.name, 'my_msle')435 self.assertEqual(msle_obj2._dtype, dtypes.int32)436 def test_unweighted(self):437 msle_obj = metrics.MeanSquaredLogarithmicError()438 self.evaluate(variables.variables_initializer(msle_obj.variables))439 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),440 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))441 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),442 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))443 update_op = msle_obj.update_state(y_true, y_pred)444 self.evaluate(update_op)445 result = msle_obj.result()446 self.assertAllClose(0.24022, result, atol=1e-5)447 def test_weighted(self):448 msle_obj = metrics.MeanSquaredLogarithmicError()449 self.evaluate(variables.variables_initializer(msle_obj.variables))450 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),451 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))452 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),453 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))454 sample_weight = constant_op.constant((1., 1.5, 2., 2.5))455 result = msle_obj(y_true, y_pred, sample_weight=sample_weight)456 self.assertAllClose(0.26082, self.evaluate(result), atol=1e-5)457@test_util.run_all_in_graph_and_eager_modes458class HingeTest(test.TestCase):459 def test_config(self):460 hinge_obj = metrics.Hinge(name='hinge', dtype=dtypes.int32)461 self.assertEqual(hinge_obj.name, 'hinge')462 self.assertEqual(hinge_obj._dtype, dtypes.int32)463 # Check save and restore config464 hinge_obj2 = metrics.Hinge.from_config(hinge_obj.get_config())465 self.assertEqual(hinge_obj2.name, 'hinge')466 self.assertEqual(hinge_obj2._dtype, dtypes.int32)467 def test_unweighted(self):468 hinge_obj = metrics.Hinge()469 self.evaluate(variables.variables_initializer(hinge_obj.variables))470 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),471 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))472 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),473 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))474 update_op = hinge_obj.update_state(y_true, y_pred)475 self.evaluate(update_op)476 result = hinge_obj.result()477 self.assertAllClose(0.65, result, atol=1e-5)478 def test_weighted(self):479 hinge_obj = metrics.Hinge()480 self.evaluate(variables.variables_initializer(hinge_obj.variables))481 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),482 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))483 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),484 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))485 sample_weight = constant_op.constant((1., 1.5, 2., 2.5))486 result = hinge_obj(y_true, y_pred, sample_weight=sample_weight)487 self.assertAllClose(0.65714, self.evaluate(result), atol=1e-5)488@test_util.run_all_in_graph_and_eager_modes489class SquaredHingeTest(test.TestCase):490 def test_config(self):491 sq_hinge_obj = metrics.SquaredHinge(name='sq_hinge', dtype=dtypes.int32)492 self.assertEqual(sq_hinge_obj.name, 'sq_hinge')493 self.assertEqual(sq_hinge_obj._dtype, dtypes.int32)494 # Check save and restore config495 sq_hinge_obj2 = metrics.SquaredHinge.from_config(sq_hinge_obj.get_config())496 self.assertEqual(sq_hinge_obj2.name, 'sq_hinge')497 self.assertEqual(sq_hinge_obj2._dtype, dtypes.int32)498 def test_unweighted(self):499 sq_hinge_obj = metrics.SquaredHinge()500 self.evaluate(variables.variables_initializer(sq_hinge_obj.variables))501 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),502 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))503 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),504 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))505 update_op = sq_hinge_obj.update_state(y_true, y_pred)506 self.evaluate(update_op)507 result = sq_hinge_obj.result()508 self.assertAllClose(0.65, result, atol=1e-5)509 def test_weighted(self):510 sq_hinge_obj = metrics.SquaredHinge()511 self.evaluate(variables.variables_initializer(sq_hinge_obj.variables))512 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),513 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))514 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),515 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))516 sample_weight = constant_op.constant((1., 1.5, 2., 2.5))517 result = sq_hinge_obj(y_true, y_pred, sample_weight=sample_weight)518 self.assertAllClose(0.65714, self.evaluate(result), atol=1e-5)519@test_util.run_all_in_graph_and_eager_modes520class CategoricalHingeTest(test.TestCase):521 def test_config(self):522 cat_hinge_obj = metrics.CategoricalHinge(523 name='cat_hinge', dtype=dtypes.int32)524 self.assertEqual(cat_hinge_obj.name, 'cat_hinge')525 self.assertEqual(cat_hinge_obj._dtype, dtypes.int32)526 # Check save and restore config527 cat_hinge_obj2 = metrics.CategoricalHinge.from_config(528 cat_hinge_obj.get_config())529 self.assertEqual(cat_hinge_obj2.name, 'cat_hinge')530 self.assertEqual(cat_hinge_obj2._dtype, dtypes.int32)531 def test_unweighted(self):532 cat_hinge_obj = metrics.CategoricalHinge()533 self.evaluate(variables.variables_initializer(cat_hinge_obj.variables))534 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),535 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))536 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),537 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))538 update_op = cat_hinge_obj.update_state(y_true, y_pred)539 self.evaluate(update_op)540 result = cat_hinge_obj.result()541 self.assertAllClose(0.5, result, atol=1e-5)542 def test_weighted(self):543 cat_hinge_obj = metrics.CategoricalHinge()544 self.evaluate(variables.variables_initializer(cat_hinge_obj.variables))545 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),546 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))547 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),548 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))549 sample_weight = constant_op.constant((1., 1.5, 2., 2.5))550 result = cat_hinge_obj(y_true, y_pred, sample_weight=sample_weight)551 self.assertAllClose(0.5, self.evaluate(result), atol=1e-5)552@test_util.run_all_in_graph_and_eager_modes553class RootMeanSquaredErrorTest(test.TestCase):554 def test_config(self):555 rmse_obj = metrics.RootMeanSquaredError(name='rmse', dtype=dtypes.int32)556 self.assertEqual(rmse_obj.name, 'rmse')557 self.assertEqual(rmse_obj._dtype, dtypes.int32)558 rmse_obj2 = metrics.RootMeanSquaredError.from_config(rmse_obj.get_config())559 self.assertEqual(rmse_obj2.name, 'rmse')560 self.assertEqual(rmse_obj2._dtype, dtypes.int32)561 def test_unweighted(self):562 rmse_obj = metrics.RootMeanSquaredError()563 self.evaluate(variables.variables_initializer(rmse_obj.variables))564 y_true = constant_op.constant((2, 4, 6))565 y_pred = constant_op.constant((1, 3, 2))566 update_op = rmse_obj.update_state(y_true, y_pred)567 self.evaluate(update_op)568 result = rmse_obj.result()569 # error = [-1, -1, -4], square(error) = [1, 1, 16], mean = 18/3 = 6570 self.assertAllClose(math.sqrt(6), result, atol=1e-3)571 def test_weighted(self):572 rmse_obj = metrics.RootMeanSquaredError()573 self.evaluate(variables.variables_initializer(rmse_obj.variables))574 y_true = constant_op.constant((2, 4, 6, 8))575 y_pred = constant_op.constant((1, 3, 2, 3))576 sample_weight = constant_op.constant((0, 1, 0, 1))577 result = rmse_obj(y_true, y_pred, sample_weight=sample_weight)578 self.assertAllClose(math.sqrt(13), self.evaluate(result), atol=1e-3)579@test_util.run_all_in_graph_and_eager_modes580class TopKCategoricalAccuracyTest(test.TestCase):581 def test_config(self):582 a_obj = metrics.TopKCategoricalAccuracy(name='topkca', dtype=dtypes.int32)583 self.assertEqual(a_obj.name, 'topkca')584 self.assertEqual(a_obj._dtype, dtypes.int32)585 a_obj2 = metrics.TopKCategoricalAccuracy.from_config(a_obj.get_config())586 self.assertEqual(a_obj2.name, 'topkca')587 self.assertEqual(a_obj2._dtype, dtypes.int32)588 def test_correctness(self):589 a_obj = metrics.TopKCategoricalAccuracy()590 self.evaluate(variables.variables_initializer(a_obj.variables))591 y_true = constant_op.constant([[0, 0, 1], [0, 1, 0]])592 y_pred = constant_op.constant([[0.1, 0.9, 0.8], [0.05, 0.95, 0]])593 result = a_obj(y_true, y_pred)594 self.assertEqual(1, self.evaluate(result)) # both the samples match595 # With `k` < 5.596 a_obj = metrics.TopKCategoricalAccuracy(k=1)597 self.evaluate(variables.variables_initializer(a_obj.variables))598 result = a_obj(y_true, y_pred)599 self.assertEqual(0.5, self.evaluate(result)) # only sample #2 matches600 # With `k` > 5.601 y_true = constant_op.constant([[0, 0, 1, 0, 0, 0, 0],602 [0, 1, 0, 0, 0, 0, 0]])603 y_pred = constant_op.constant([[0.5, 0.9, 0.1, 0.7, 0.6, 0.5, 0.4],604 [0.05, 0.95, 0, 0, 0, 0, 0]])605 a_obj = metrics.TopKCategoricalAccuracy(k=6)606 self.evaluate(variables.variables_initializer(a_obj.variables))607 result = a_obj(y_true, y_pred)608 self.assertEqual(0.5, self.evaluate(result)) # only 1 sample matches.609@test_util.run_all_in_graph_and_eager_modes610class SparseTopKCategoricalAccuracyTest(test.TestCase):611 def test_config(self):612 a_obj = metrics.SparseTopKCategoricalAccuracy(613 name='stopkca', dtype=dtypes.int32)614 self.assertEqual(a_obj.name, 'stopkca')615 self.assertEqual(a_obj._dtype, dtypes.int32)616 a_obj2 = metrics.SparseTopKCategoricalAccuracy.from_config(617 a_obj.get_config())618 self.assertEqual(a_obj2.name, 'stopkca')619 self.assertEqual(a_obj2._dtype, dtypes.int32)620 def test_correctness(self):621 a_obj = metrics.SparseTopKCategoricalAccuracy()622 self.evaluate(variables.variables_initializer(a_obj.variables))623 y_true = constant_op.constant([2, 1])624 y_pred = constant_op.constant([[0.1, 0.9, 0.8], [0.05, 0.95, 0]])625 result = a_obj(y_true, y_pred)626 self.assertEqual(1, self.evaluate(result)) # both the samples match627 # With `k` < 5.628 a_obj = metrics.SparseTopKCategoricalAccuracy(k=1)629 self.evaluate(variables.variables_initializer(a_obj.variables))630 result = a_obj(y_true, y_pred)631 self.assertEqual(0.5, self.evaluate(result)) # only sample #2 matches632 # With `k` > 5.633 y_pred = constant_op.constant([[0.5, 0.9, 0.1, 0.7, 0.6, 0.5, 0.4],634 [0.05, 0.95, 0, 0, 0, 0, 0]])635 a_obj = metrics.SparseTopKCategoricalAccuracy(k=6)636 self.evaluate(variables.variables_initializer(a_obj.variables))637 result = a_obj(y_true, y_pred)638 self.assertEqual(0.5, self.evaluate(result)) # only 1 sample matches.639@test_util.run_all_in_graph_and_eager_modes640class LogcoshTest(test.TestCase):641 def setup(self):642 y_pred = np.asarray([1, 9, 2, -5, -2, 6]).reshape((2, 3))643 y_true = np.asarray([4, 8, 12, 8, 1, 3]).reshape((2, 3))644 self.batch_size = 6645 error = y_pred - y_true646 self.expected_results = np.log((np.exp(error) + np.exp(-error)) / 2)647 self.y_pred = constant_op.constant(y_pred, dtype=dtypes.float32)648 self.y_true = constant_op.constant(y_true)649 def test_config(self):650 logcosh_obj = metrics.Logcosh(name='logcosh', dtype=dtypes.int32)651 self.assertEqual(logcosh_obj.name, 'logcosh')652 self.assertEqual(logcosh_obj._dtype, dtypes.int32)653 def test_unweighted(self):654 self.setup()655 logcosh_obj = metrics.Logcosh()656 self.evaluate(variables.variables_initializer(logcosh_obj.variables))657 update_op = logcosh_obj.update_state(self.y_true, self.y_pred)658 self.evaluate(update_op)659 result = logcosh_obj.result()660 expected_result = np.sum(self.expected_results) / self.batch_size661 self.assertAllClose(result, expected_result, atol=1e-3)662 def test_weighted(self):663 self.setup()664 logcosh_obj = metrics.Logcosh()665 self.evaluate(variables.variables_initializer(logcosh_obj.variables))666 sample_weight = constant_op.constant([1.2, 3.4], shape=(2, 1))667 result = logcosh_obj(self.y_true, self.y_pred, sample_weight=sample_weight)668 sample_weight = np.asarray([1.2, 1.2, 1.2, 3.4, 3.4, 3.4]).reshape((2, 3))669 expected_result = np.multiply(self.expected_results, sample_weight)670 expected_result = np.sum(expected_result) / np.sum(sample_weight)671 self.assertAllClose(self.evaluate(result), expected_result, atol=1e-3)672@test_util.run_all_in_graph_and_eager_modes673class PoissonTest(test.TestCase):674 def setup(self):675 y_pred = np.asarray([1, 9, 2, 5, 2, 6]).reshape((2, 3))676 y_true = np.asarray([4, 8, 12, 8, 1, 3]).reshape((2, 3))677 self.batch_size = 6678 self.expected_results = y_pred - np.multiply(y_true, np.log(y_pred))679 self.y_pred = constant_op.constant(y_pred, dtype=dtypes.float32)680 self.y_true = constant_op.constant(y_true)681 def test_config(self):682 poisson_obj = metrics.Poisson(name='poisson', dtype=dtypes.int32)683 self.assertEqual(poisson_obj.name, 'poisson')684 self.assertEqual(poisson_obj._dtype, dtypes.int32)685 poisson_obj2 = metrics.Poisson.from_config(poisson_obj.get_config())686 self.assertEqual(poisson_obj2.name, 'poisson')687 self.assertEqual(poisson_obj2._dtype, dtypes.int32)688 def test_unweighted(self):689 self.setup()690 poisson_obj = metrics.Poisson()691 self.evaluate(variables.variables_initializer(poisson_obj.variables))692 update_op = poisson_obj.update_state(self.y_true, self.y_pred)693 self.evaluate(update_op)694 result = poisson_obj.result()695 expected_result = np.sum(self.expected_results) / self.batch_size696 self.assertAllClose(result, expected_result, atol=1e-3)697 def test_weighted(self):698 self.setup()699 poisson_obj = metrics.Poisson()700 self.evaluate(variables.variables_initializer(poisson_obj.variables))701 sample_weight = constant_op.constant([1.2, 3.4], shape=(2, 1))702 result = poisson_obj(self.y_true, self.y_pred, sample_weight=sample_weight)703 sample_weight = np.asarray([1.2, 1.2, 1.2, 3.4, 3.4, 3.4]).reshape((2, 3))704 expected_result = np.multiply(self.expected_results, sample_weight)705 expected_result = np.sum(expected_result) / np.sum(sample_weight)706 self.assertAllClose(self.evaluate(result), expected_result, atol=1e-3)707@test_util.run_all_in_graph_and_eager_modes708class KullbackLeiblerDivergenceTest(test.TestCase):709 def setup(self):710 y_pred = np.asarray([.4, .9, .12, .36, .3, .4]).reshape((2, 3))711 y_true = np.asarray([.5, .8, .12, .7, .43, .8]).reshape((2, 3))712 self.batch_size = 2713 self.expected_results = np.multiply(y_true, np.log(y_true / y_pred))714 self.y_pred = constant_op.constant(y_pred, dtype=dtypes.float32)715 self.y_true = constant_op.constant(y_true)716 def test_config(self):717 k_obj = metrics.KullbackLeiblerDivergence(name='kld', dtype=dtypes.int32)718 self.assertEqual(k_obj.name, 'kld')719 self.assertEqual(k_obj._dtype, dtypes.int32)720 k_obj2 = metrics.KullbackLeiblerDivergence.from_config(k_obj.get_config())721 self.assertEqual(k_obj2.name, 'kld')722 self.assertEqual(k_obj2._dtype, dtypes.int32)723 def test_unweighted(self):724 self.setup()725 k_obj = metrics.KullbackLeiblerDivergence()726 self.evaluate(variables.variables_initializer(k_obj.variables))727 update_op = k_obj.update_state(self.y_true, self.y_pred)728 self.evaluate(update_op)729 result = k_obj.result()730 expected_result = np.sum(self.expected_results) / self.batch_size731 self.assertAllClose(result, expected_result, atol=1e-3)732 def test_weighted(self):733 self.setup()734 k_obj = metrics.KullbackLeiblerDivergence()735 self.evaluate(variables.variables_initializer(k_obj.variables))736 sample_weight = constant_op.constant([1.2, 3.4], shape=(2, 1))737 result = k_obj(self.y_true, self.y_pred, sample_weight=sample_weight)738 sample_weight = np.asarray([1.2, 1.2, 1.2, 3.4, 3.4, 3.4]).reshape((2, 3))739 expected_result = np.multiply(self.expected_results, sample_weight)740 expected_result = np.sum(expected_result) / (1.2 + 3.4)741 self.assertAllClose(self.evaluate(result), expected_result, atol=1e-3)742@test_util.run_all_in_graph_and_eager_modes743class MeanRelativeErrorTest(test.TestCase):744 def test_config(self):745 normalizer = constant_op.constant([1, 3], dtype=dtypes.float32)746 mre_obj = metrics.MeanRelativeError(normalizer=normalizer, name='mre')747 self.assertEqual(mre_obj.name, 'mre')748 self.assertArrayNear(self.evaluate(mre_obj.normalizer), [1, 3], 1e-1)749 mre_obj2 = metrics.MeanRelativeError.from_config(mre_obj.get_config())750 self.assertEqual(mre_obj2.name, 'mre')751 self.assertArrayNear(self.evaluate(mre_obj2.normalizer), [1, 3], 1e-1)752 def test_unweighted(self):753 np_y_pred = np.asarray([2, 4, 6, 8], dtype=np.float32)754 np_y_true = np.asarray([1, 3, 2, 3], dtype=np.float32)755 expected_error = np.mean(756 np.divide(np.absolute(np_y_pred - np_y_true), np_y_true))757 y_pred = constant_op.constant(np_y_pred, shape=(1, 4), dtype=dtypes.float32)758 y_true = constant_op.constant(np_y_true, shape=(1, 4))759 mre_obj = metrics.MeanRelativeError(normalizer=y_true)760 self.evaluate(variables.variables_initializer(mre_obj.variables))761 result = mre_obj(y_true, y_pred)762 self.assertAllClose(self.evaluate(result), expected_error, atol=1e-3)763 def test_weighted(self):764 np_y_pred = np.asarray([2, 4, 6, 8], dtype=np.float32)765 np_y_true = np.asarray([1, 3, 2, 3], dtype=np.float32)766 sample_weight = np.asarray([0.2, 0.3, 0.5, 0], dtype=np.float32)767 rel_errors = np.divide(np.absolute(np_y_pred - np_y_true), np_y_true)768 expected_error = np.sum(rel_errors * sample_weight)769 y_pred = constant_op.constant(np_y_pred, dtype=dtypes.float32)770 y_true = constant_op.constant(np_y_true)771 mre_obj = metrics.MeanRelativeError(normalizer=y_true)772 self.evaluate(variables.variables_initializer(mre_obj.variables))773 result = mre_obj(774 y_true, y_pred, sample_weight=constant_op.constant(sample_weight))775 self.assertAllClose(self.evaluate(result), expected_error, atol=1e-3)776 def test_zero_normalizer(self):777 y_pred = constant_op.constant([2, 4], dtype=dtypes.float32)778 y_true = constant_op.constant([1, 3])779 mre_obj = metrics.MeanRelativeError(normalizer=array_ops.zeros_like(y_true))780 self.evaluate(variables.variables_initializer(mre_obj.variables))781 result = mre_obj(y_true, y_pred)782 self.assertEqual(self.evaluate(result), 0)783@test_util.run_all_in_graph_and_eager_modes784class MeanIoUTest(test.TestCase):785 def test_config(self):786 m_obj = metrics.MeanIoU(num_classes=2, name='mean_iou')787 self.assertEqual(m_obj.name, 'mean_iou')788 self.assertEqual(m_obj.num_classes, 2)789 m_obj2 = metrics.MeanIoU.from_config(m_obj.get_config())790 self.assertEqual(m_obj2.name, 'mean_iou')791 self.assertEqual(m_obj2.num_classes, 2)792 def test_unweighted(self):793 y_pred = constant_op.constant([0, 1, 0, 1], dtype=dtypes.float32)794 y_true = constant_op.constant([0, 0, 1, 1])795 m_obj = metrics.MeanIoU(num_classes=2)796 self.evaluate(variables.variables_initializer(m_obj.variables))797 result = m_obj(y_true, y_pred)798 # cm = [[1, 1],799 # [1, 1]]800 # sum_row = [2, 2], sum_col = [2, 2], true_positives = [1, 1]801 # iou = true_positives / (sum_row + sum_col - true_positives))802 expected_result = (1 / (2 + 2 - 1) + 1 / (2 + 2 - 1)) / 2803 self.assertAllClose(self.evaluate(result), expected_result, atol=1e-3)804 def test_weighted(self):805 y_pred = constant_op.constant([0, 1, 0, 1], dtype=dtypes.float32)806 y_true = constant_op.constant([0, 0, 1, 1])807 sample_weight = constant_op.constant([0.2, 0.3, 0.4, 0.1])808 m_obj = metrics.MeanIoU(num_classes=2)809 self.evaluate(variables.variables_initializer(m_obj.variables))810 result = m_obj(y_true, y_pred, sample_weight=sample_weight)811 # cm = [[0.2, 0.3],812 # [0.4, 0.1]]813 # sum_row = [0.6, 0.4], sum_col = [0.5, 0.5], true_positives = [0.2, 0.1]814 # iou = true_positives / (sum_row + sum_col - true_positives))815 expected_result = (0.2 / (0.6 + 0.5 - 0.2) + 0.1 / (0.4 + 0.5 - 0.1)) / 2816 self.assertAllClose(self.evaluate(result), expected_result, atol=1e-3)817 def test_multi_dim_input(self):818 y_pred = constant_op.constant([[0, 1], [0, 1]], dtype=dtypes.float32)819 y_true = constant_op.constant([[0, 0], [1, 1]])820 sample_weight = constant_op.constant([[0.2, 0.3], [0.4, 0.1]])821 m_obj = metrics.MeanIoU(num_classes=2)822 self.evaluate(variables.variables_initializer(m_obj.variables))823 result = m_obj(y_true, y_pred, sample_weight=sample_weight)824 # cm = [[0.2, 0.3],825 # [0.4, 0.1]]826 # sum_row = [0.6, 0.4], sum_col = [0.5, 0.5], true_positives = [0.2, 0.1]827 # iou = true_positives / (sum_row + sum_col - true_positives))828 expected_result = (0.2 / (0.6 + 0.5 - 0.2) + 0.1 / (0.4 + 0.5 - 0.1)) / 2829 self.assertAllClose(self.evaluate(result), expected_result, atol=1e-3)830 def test_zero_valid_entries(self):831 m_obj = metrics.MeanIoU(num_classes=2)832 self.evaluate(variables.variables_initializer(m_obj.variables))833 self.assertAllClose(self.evaluate(m_obj.result()), 0, atol=1e-3)834 def test_zero_and_non_zero_entries(self):835 y_pred = constant_op.constant([1], dtype=dtypes.float32)836 y_true = constant_op.constant([1])837 m_obj = metrics.MeanIoU(num_classes=2)838 self.evaluate(variables.variables_initializer(m_obj.variables))839 result = m_obj(y_true, y_pred)840 # cm = [[0, 0],841 # [0, 1]]842 # sum_row = [0, 1], sum_col = [0, 1], true_positives = [0, 1]843 # iou = true_positives / (sum_row + sum_col - true_positives))844 expected_result = (0 + 1 / (1 + 1 - 1)) / 1845 self.assertAllClose(self.evaluate(result), expected_result, atol=1e-3)846class MeanTensorTest(keras_parameterized.TestCase):847 @test_util.run_in_graph_and_eager_modes848 def test_config(self):849 m = metrics.MeanTensor(name='mean_by_element')850 # check config851 self.assertEqual(m.name, 'mean_by_element')852 self.assertTrue(m.stateful)853 self.assertEqual(m.dtype, dtypes.float32)854 self.assertEqual(len(m.variables), 0)855 with self.assertRaisesRegexp(ValueError, 'does not have any result yet'):856 m.result()857 self.evaluate(m([[3], [5], [3]]))858 self.assertAllEqual(m._shape, [3, 1])859 m2 = metrics.MeanTensor.from_config(m.get_config())860 self.assertEqual(m2.name, 'mean_by_element')861 self.assertTrue(m2.stateful)862 self.assertEqual(m2.dtype, dtypes.float32)863 self.assertEqual(len(m2.variables), 0)864 @test_util.run_in_graph_and_eager_modes865 def test_unweighted(self):866 m = metrics.MeanTensor(dtype=dtypes.float64)867 # check __call__()868 self.assertAllClose(self.evaluate(m([100, 40])), [100, 40])869 self.assertAllClose(self.evaluate(m.total), [100, 40])870 self.assertAllClose(self.evaluate(m.count), [1, 1])871 # check update_state() and result() + state accumulation + tensor input872 update_op = m.update_state(ops.convert_n_to_tensor([1, 5]))873 self.evaluate(update_op)874 self.assertAllClose(self.evaluate(m.result()), [50.5, 22.5])875 self.assertAllClose(self.evaluate(m.total), [101, 45])876 self.assertAllClose(self.evaluate(m.count), [2, 2])877 # check reset_states()878 m.reset_states()879 self.assertAllClose(self.evaluate(m.total), [0, 0])880 self.assertAllClose(self.evaluate(m.count), [0, 0])881 @test_util.run_in_graph_and_eager_modes882 def test_weighted(self):883 m = metrics.MeanTensor(dtype=dtypes.float64)884 self.assertEqual(m.dtype, dtypes.float64)885 # check scalar weight886 result_t = m([100, 30], sample_weight=0.5)887 self.assertAllClose(self.evaluate(result_t), [100, 30])888 self.assertAllClose(self.evaluate(m.total), [50, 15])889 self.assertAllClose(self.evaluate(m.count), [0.5, 0.5])890 # check weights not scalar and weights rank matches values rank891 result_t = m([1, 5], sample_weight=[1, 0.2])892 result = self.evaluate(result_t)893 self.assertAllClose(result, [51 / 1.5, 16 / 0.7], 2)894 self.assertAllClose(self.evaluate(m.total), [51, 16])895 self.assertAllClose(self.evaluate(m.count), [1.5, 0.7])896 # check weights broadcast897 result_t = m([1, 2], sample_weight=0.5)898 self.assertAllClose(self.evaluate(result_t), [51.5 / 2, 17 / 1.2])899 self.assertAllClose(self.evaluate(m.total), [51.5, 17])900 self.assertAllClose(self.evaluate(m.count), [2, 1.2])901 # check weights squeeze902 result_t = m([1, 5], sample_weight=[[1], [0.2]])903 self.assertAllClose(self.evaluate(result_t), [52.5 / 3, 18 / 1.4])904 self.assertAllClose(self.evaluate(m.total), [52.5, 18])905 self.assertAllClose(self.evaluate(m.count), [3, 1.4])906 # check weights expand907 m = metrics.MeanTensor((2, 1), dtype=dtypes.float64)908 self.evaluate(variables.variables_initializer(m.variables))909 result_t = m([[1], [5]], sample_weight=[1, 0.2])910 self.assertAllClose(self.evaluate(result_t), [[1], [5]])911 self.assertAllClose(self.evaluate(m.total), [[1], [1]])912 self.assertAllClose(self.evaluate(m.count), [[1], [0.2]])913 @test_util.run_in_graph_and_eager_modes914 def test_invalid_value_shape(self):915 m = metrics.MeanTensor(dtype=dtypes.float64)916 m([1])917 with self.assertRaisesRegexp(918 ValueError, 'MeanTensor input values must always have the same shape'):919 m([1, 5])920 @test_util.run_in_graph_and_eager_modes921 def test_build_in_tf_function(self):922 """Ensure that variables are created correctly in a tf function."""923 m = metrics.MeanTensor(dtype=dtypes.float64)924 @eager_function.defun925 def call_metric(x):926 return m(x)927 self.assertAllClose(self.evaluate(call_metric([100, 40])), [100, 40])928 self.assertAllClose(self.evaluate(m.total), [100, 40])929 self.assertAllClose(self.evaluate(m.count), [1, 1])930 self.assertAllClose(self.evaluate(call_metric([20, 2])), [60, 21])931 def test_in_keras_model(self):932 with context.eager_mode():933 class ModelWithMetric(Model):934 def __init__(self):935 super(ModelWithMetric, self).__init__()936 self.dense1 = layers.Dense(937 3, activation='relu', kernel_initializer='ones')938 self.dense2 = layers.Dense(939 1, activation='sigmoid', kernel_initializer='ones')940 self.mean_tensor = metrics.MeanTensor()941 def call(self, x):942 x = self.dense1(x)943 x = self.dense2(x)944 self.mean_tensor(self.dense1.kernel)945 return x946 model = ModelWithMetric()947 model.compile(948 loss='mae',949 optimizer='rmsprop',950 run_eagerly=True)951 x = np.ones((100, 4))952 y = np.zeros((100, 1))953 model.evaluate(x, y, batch_size=50)954 self.assertAllClose(self.evaluate(model.mean_tensor.result()),955 np.ones((4, 3)))956 self.assertAllClose(self.evaluate(model.mean_tensor.total),957 np.full((4, 3), 2))958 self.assertAllClose(self.evaluate(model.mean_tensor.count),959 np.full((4, 3), 2))960 model.evaluate(x, y, batch_size=25)961 self.assertAllClose(self.evaluate(model.mean_tensor.result()),962 np.ones((4, 3)))963 self.assertAllClose(self.evaluate(model.mean_tensor.total),964 np.full((4, 3), 4))965 self.assertAllClose(self.evaluate(model.mean_tensor.count),966 np.full((4, 3), 4))967@test_util.run_all_in_graph_and_eager_modes968class BinaryCrossentropyTest(test.TestCase):969 def test_config(self):970 bce_obj = metrics.BinaryCrossentropy(971 name='bce', dtype=dtypes.int32, label_smoothing=0.2)972 self.assertEqual(bce_obj.name, 'bce')973 self.assertEqual(bce_obj._dtype, dtypes.int32)974 old_config = bce_obj.get_config()975 self.assertAllClose(self.evaluate(old_config['label_smoothing']), 0.2, 1e-3)976 # Check save and restore config977 bce_obj2 = metrics.BinaryCrossentropy.from_config(old_config)978 self.assertEqual(bce_obj2.name, 'bce')979 self.assertEqual(bce_obj2._dtype, dtypes.int32)980 new_config = bce_obj2.get_config()981 self.assertAllClose(self.evaluate(new_config['label_smoothing']), 0.2, 1e-3)982 def test_unweighted(self):983 bce_obj = metrics.BinaryCrossentropy()984 self.evaluate(variables.variables_initializer(bce_obj.variables))985 y_true = np.asarray([1, 0, 1, 0]).reshape([2, 2])986 y_pred = np.asarray([1, 1, 1, 0], dtype=np.float32).reshape([2, 2])987 result = bce_obj(y_true, y_pred)988 # EPSILON = 1e-7, y = y_true, y` = y_pred, Y_MAX = 0.9999999989 # y` = clip_ops.clip_by_value(output, EPSILON, 1. - EPSILON)990 # y` = [Y_MAX, Y_MAX, Y_MAX, EPSILON]991 # Metric = -(y log(y` + EPSILON) + (1 - y) log(1 - y` + EPSILON))992 # = [-log(Y_MAX + EPSILON), -log(1 - Y_MAX + EPSILON),993 # -log(Y_MAX + EPSILON), -log(1)]994 # = [(0 + 15.33) / 2, (0 + 0) / 2]995 # Reduced metric = 7.665 / 2996 self.assertAllClose(self.evaluate(result), 3.833, atol=1e-3)997 def test_unweighted_with_logits(self):998 bce_obj = metrics.BinaryCrossentropy(from_logits=True)999 self.evaluate(variables.variables_initializer(bce_obj.variables))1000 y_true = constant_op.constant([[1, 0, 1], [0, 1, 1]])1001 y_pred = constant_op.constant([[100.0, -100.0, 100.0],1002 [100.0, 100.0, -100.0]])1003 result = bce_obj(y_true, y_pred)1004 # Metric = max(x, 0) - x * z + log(1 + exp(-abs(x)))1005 # (where x = logits and z = y_true)1006 # = [((100 - 100 * 1 + log(1 + exp(-100))) +1007 # (0 + 100 * 0 + log(1 + exp(-100))) +1008 # (100 - 100 * 1 + log(1 + exp(-100))),1009 # ((100 - 100 * 0 + log(1 + exp(-100))) +1010 # (100 - 100 * 1 + log(1 + exp(-100))) +1011 # (0 + 100 * 1 + log(1 + exp(-100))))]1012 # = [(0 + 0 + 0) / 3, 200 / 3]1013 # Reduced metric = (0 + 66.666) / 21014 self.assertAllClose(self.evaluate(result), 33.333, atol=1e-3)1015 def test_weighted(self):1016 bce_obj = metrics.BinaryCrossentropy()1017 self.evaluate(variables.variables_initializer(bce_obj.variables))1018 y_true = np.asarray([1, 0, 1, 0]).reshape([2, 2])1019 y_pred = np.asarray([1, 1, 1, 0], dtype=np.float32).reshape([2, 2])1020 sample_weight = constant_op.constant([1.5, 2.])1021 result = bce_obj(y_true, y_pred, sample_weight=sample_weight)1022 # EPSILON = 1e-7, y = y_true, y` = y_pred, Y_MAX = 0.99999991023 # y` = clip_ops.clip_by_value(output, EPSILON, 1. - EPSILON)1024 # y` = [Y_MAX, Y_MAX, Y_MAX, EPSILON]1025 # Metric = -(y log(y` + EPSILON) + (1 - y) log(1 - y` + EPSILON))1026 # = [-log(Y_MAX + EPSILON), -log(1 - Y_MAX + EPSILON),1027 # -log(Y_MAX + EPSILON), -log(1)]1028 # = [(0 + 15.33) / 2, (0 + 0) / 2]1029 # Weighted metric = [7.665 * 1.5, 0]1030 # Reduced metric = 7.665 * 1.5 / (1.5 + 2)1031 self.assertAllClose(self.evaluate(result), 3.285, atol=1e-3)1032 def test_weighted_from_logits(self):1033 bce_obj = metrics.BinaryCrossentropy(from_logits=True)1034 self.evaluate(variables.variables_initializer(bce_obj.variables))1035 y_true = constant_op.constant([[1, 0, 1], [0, 1, 1]])1036 y_pred = constant_op.constant([[100.0, -100.0, 100.0],1037 [100.0, 100.0, -100.0]])1038 sample_weight = constant_op.constant([2., 2.5])1039 result = bce_obj(y_true, y_pred, sample_weight=sample_weight)1040 # Metric = max(x, 0) - x * z + log(1 + exp(-abs(x)))1041 # (where x = logits and z = y_true)1042 # = [(0 + 0 + 0) / 3, 200 / 3]1043 # Weighted metric = [0, 66.666 * 2.5]1044 # Reduced metric = 66.666 * 2.5 / (2 + 2.5)1045 self.assertAllClose(self.evaluate(result), 37.037, atol=1e-3)1046 def test_label_smoothing(self):1047 logits = constant_op.constant(((100., -100., -100.)))1048 y_true = constant_op.constant(((1, 0, 1)))1049 label_smoothing = 0.11050 # Metric: max(x, 0) - x * z + log(1 + exp(-abs(x)))1051 # (where x = logits and z = y_true)1052 # Label smoothing: z' = z * (1 - L) + 0.5L1053 # After label smoothing, label 1 becomes 1 - 0.5L1054 # label 0 becomes 0.5L1055 # Applying the above two fns to the given input:1056 # (100 - 100 * (1 - 0.5 L) + 0 +1057 # 0 + 100 * (0.5 L) + 0 +1058 # 0 + 100 * (1 - 0.5 L) + 0) * (1/3)1059 # = (100 + 50L) * 1/31060 bce_obj = metrics.BinaryCrossentropy(1061 from_logits=True, label_smoothing=label_smoothing)1062 self.evaluate(variables.variables_initializer(bce_obj.variables))1063 result = bce_obj(y_true, logits)1064 expected_value = (100.0 + 50.0 * label_smoothing) / 3.01065 self.assertAllClose(expected_value, self.evaluate(result), atol=1e-3)1066def _get_model(compile_metrics):1067 model_layers = [1068 layers.Dense(3, activation='relu', kernel_initializer='ones'),1069 layers.Dense(1, activation='sigmoid', kernel_initializer='ones')]1070 model = testing_utils.get_model_from_layers(model_layers, input_shape=(4,))1071 model.compile(1072 loss='mae',1073 metrics=compile_metrics,1074 optimizer='rmsprop',1075 run_eagerly=testing_utils.should_run_eagerly())1076 return model1077@keras_parameterized.run_with_all_model_types1078@keras_parameterized.run_all_keras_modes1079class ResetStatesTest(keras_parameterized.TestCase):1080 def test_reset_states_false_positives(self):1081 fp_obj = metrics.FalsePositives()1082 model = _get_model([fp_obj])1083 x = np.ones((100, 4))1084 y = np.zeros((100, 1))1085 model.evaluate(x, y)1086 self.assertEqual(self.evaluate(fp_obj.accumulator), 100.)1087 model.evaluate(x, y)1088 self.assertEqual(self.evaluate(fp_obj.accumulator), 100.)1089 def test_reset_states_false_negatives(self):1090 fn_obj = metrics.FalseNegatives()1091 model = _get_model([fn_obj])1092 x = np.zeros((100, 4))1093 y = np.ones((100, 1))1094 model.evaluate(x, y)1095 self.assertEqual(self.evaluate(fn_obj.accumulator), 100.)1096 model.evaluate(x, y)1097 self.assertEqual(self.evaluate(fn_obj.accumulator), 100.)1098 def test_reset_states_true_negatives(self):1099 tn_obj = metrics.TrueNegatives()1100 model = _get_model([tn_obj])1101 x = np.zeros((100, 4))1102 y = np.zeros((100, 1))1103 model.evaluate(x, y)1104 self.assertEqual(self.evaluate(tn_obj.accumulator), 100.)1105 model.evaluate(x, y)1106 self.assertEqual(self.evaluate(tn_obj.accumulator), 100.)1107 def test_reset_states_true_positives(self):1108 tp_obj = metrics.TruePositives()1109 model = _get_model([tp_obj])1110 x = np.ones((100, 4))1111 y = np.ones((100, 1))1112 model.evaluate(x, y)1113 self.assertEqual(self.evaluate(tp_obj.accumulator), 100.)1114 model.evaluate(x, y)1115 self.assertEqual(self.evaluate(tp_obj.accumulator), 100.)1116 def test_reset_states_precision(self):1117 p_obj = metrics.Precision()1118 model = _get_model([p_obj])1119 x = np.concatenate((np.ones((50, 4)), np.ones((50, 4))))1120 y = np.concatenate((np.ones((50, 1)), np.zeros((50, 1))))1121 model.evaluate(x, y)1122 self.assertEqual(self.evaluate(p_obj.true_positives), 50.)1123 self.assertEqual(self.evaluate(p_obj.false_positives), 50.)1124 model.evaluate(x, y)1125 self.assertEqual(self.evaluate(p_obj.true_positives), 50.)1126 self.assertEqual(self.evaluate(p_obj.false_positives), 50.)1127 def test_reset_states_recall(self):1128 r_obj = metrics.Recall()1129 model = _get_model([r_obj])1130 x = np.concatenate((np.ones((50, 4)), np.zeros((50, 4))))1131 y = np.concatenate((np.ones((50, 1)), np.ones((50, 1))))1132 model.evaluate(x, y)1133 self.assertEqual(self.evaluate(r_obj.true_positives), 50.)1134 self.assertEqual(self.evaluate(r_obj.false_negatives), 50.)1135 model.evaluate(x, y)1136 self.assertEqual(self.evaluate(r_obj.true_positives), 50.)1137 self.assertEqual(self.evaluate(r_obj.false_negatives), 50.)1138 def test_reset_states_sensitivity_at_specificity(self):1139 s_obj = metrics.SensitivityAtSpecificity(0.5, num_thresholds=1)1140 model = _get_model([s_obj])1141 x = np.concatenate((np.ones((25, 4)), np.zeros((25, 4)), np.zeros((25, 4)),1142 np.ones((25, 4))))1143 y = np.concatenate((np.ones((25, 1)), np.zeros((25, 1)), np.ones((25, 1)),1144 np.zeros((25, 1))))1145 for _ in range(2):1146 model.evaluate(x, y)1147 self.assertEqual(self.evaluate(s_obj.true_positives), 25.)1148 self.assertEqual(self.evaluate(s_obj.false_positives), 25.)1149 self.assertEqual(self.evaluate(s_obj.false_negatives), 25.)1150 self.assertEqual(self.evaluate(s_obj.true_negatives), 25.)1151 def test_reset_states_specificity_at_sensitivity(self):1152 s_obj = metrics.SpecificityAtSensitivity(0.5, num_thresholds=1)1153 model = _get_model([s_obj])1154 x = np.concatenate((np.ones((25, 4)), np.zeros((25, 4)), np.zeros((25, 4)),1155 np.ones((25, 4))))1156 y = np.concatenate((np.ones((25, 1)), np.zeros((25, 1)), np.ones((25, 1)),1157 np.zeros((25, 1))))1158 for _ in range(2):1159 model.evaluate(x, y)1160 self.assertEqual(self.evaluate(s_obj.true_positives), 25.)1161 self.assertEqual(self.evaluate(s_obj.false_positives), 25.)1162 self.assertEqual(self.evaluate(s_obj.false_negatives), 25.)1163 self.assertEqual(self.evaluate(s_obj.true_negatives), 25.)1164 def test_reset_states_auc(self):1165 auc_obj = metrics.AUC(num_thresholds=3)1166 model = _get_model([auc_obj])1167 x = np.concatenate((np.ones((25, 4)), np.zeros((25, 4)), np.zeros((25, 4)),1168 np.ones((25, 4))))1169 y = np.concatenate((np.ones((25, 1)), np.zeros((25, 1)), np.ones((25, 1)),1170 np.zeros((25, 1))))1171 for _ in range(2):1172 model.evaluate(x, y)1173 self.assertEqual(self.evaluate(auc_obj.true_positives[1]), 25.)1174 self.assertEqual(self.evaluate(auc_obj.false_positives[1]), 25.)1175 self.assertEqual(self.evaluate(auc_obj.false_negatives[1]), 25.)1176 self.assertEqual(self.evaluate(auc_obj.true_negatives[1]), 25.)1177 def test_reset_states_mean_iou(self):1178 m_obj = metrics.MeanIoU(num_classes=2)1179 model = _get_model([m_obj])1180 x = np.asarray([[0, 0, 0, 0], [1, 1, 1, 1], [1, 0, 1, 0], [0, 1, 0, 1]],1181 dtype=np.float32)1182 y = np.asarray([[0], [1], [1], [1]], dtype=np.float32)1183 model.evaluate(x, y)1184 self.assertArrayNear(self.evaluate(m_obj.total_cm)[0], [1, 0], 1e-1)1185 self.assertArrayNear(self.evaluate(m_obj.total_cm)[1], [3, 0], 1e-1)1186 model.evaluate(x, y)1187 self.assertArrayNear(self.evaluate(m_obj.total_cm)[0], [1, 0], 1e-1)1188 self.assertArrayNear(self.evaluate(m_obj.total_cm)[1], [3, 0], 1e-1)1189if __name__ == '__main__':...
metrics_confusion_matrix_test.py
Source:metrics_confusion_matrix_test.py
...40 self.assertEqual(len(fp_obj2.variables), 1)41 self.assertEqual(fp_obj2.thresholds, [0.4, 0.9])42 def test_unweighted(self):43 fp_obj = metrics.FalsePositives()44 self.evaluate(variables.variables_initializer(fp_obj.variables))45 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),46 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))47 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),48 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))49 update_op = fp_obj.update_state(y_true, y_pred)50 self.evaluate(update_op)51 result = fp_obj.result()52 self.assertAllClose(7., result)53 def test_weighted(self):54 fp_obj = metrics.FalsePositives()55 self.evaluate(variables.variables_initializer(fp_obj.variables))56 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),57 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))58 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),59 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))60 sample_weight = constant_op.constant((1., 1.5, 2., 2.5))61 result = fp_obj(y_true, y_pred, sample_weight=sample_weight)62 self.assertAllClose(14., self.evaluate(result))63 def test_unweighted_with_thresholds(self):64 fp_obj = metrics.FalsePositives(thresholds=[0.15, 0.5, 0.85])65 self.evaluate(variables.variables_initializer(fp_obj.variables))66 y_pred = constant_op.constant(((0.9, 0.2, 0.8, 0.1), (0.2, 0.9, 0.7, 0.6),67 (0.1, 0.2, 0.4, 0.3), (0, 1, 0.7, 0.3)))68 y_true = constant_op.constant(((0, 1, 1, 0), (1, 0, 0, 0), (0, 0, 0, 0),69 (1, 1, 1, 1)))70 update_op = fp_obj.update_state(y_true, y_pred)71 self.evaluate(update_op)72 result = fp_obj.result()73 self.assertAllClose([7., 4., 2.], result)74 def test_weighted_with_thresholds(self):75 fp_obj = metrics.FalsePositives(thresholds=[0.15, 0.5, 0.85])76 self.evaluate(variables.variables_initializer(fp_obj.variables))77 y_pred = constant_op.constant(((0.9, 0.2, 0.8, 0.1), (0.2, 0.9, 0.7, 0.6),78 (0.1, 0.2, 0.4, 0.3), (0, 1, 0.7, 0.3)))79 y_true = constant_op.constant(((0, 1, 1, 0), (1, 0, 0, 0), (0, 0, 0, 0),80 (1, 1, 1, 1)))81 sample_weight = ((1.0, 2.0, 3.0, 5.0), (7.0, 11.0, 13.0, 17.0),82 (19.0, 23.0, 29.0, 31.0), (5.0, 15.0, 10.0, 0))83 result = fp_obj(y_true, y_pred, sample_weight=sample_weight)84 self.assertAllClose([125., 42., 12.], self.evaluate(result))85 def test_threshold_limit(self):86 with self.assertRaisesRegexp(87 ValueError,88 r'Threshold values must be in \[0, 1\]. Invalid values: \[-1, 2\]'):89 metrics.FalsePositives(thresholds=[-1, 0.5, 2])90 with self.assertRaisesRegexp(91 ValueError,92 r'Threshold values must be in \[0, 1\]. Invalid values: \[None\]'):93 metrics.FalsePositives(thresholds=[None])94@test_util.run_all_in_graph_and_eager_modes95class FalseNegativesTest(test.TestCase):96 def test_config(self):97 fn_obj = metrics.FalseNegatives(name='my_fn', thresholds=[0.4, 0.9])98 self.assertEqual(fn_obj.name, 'my_fn')99 self.assertEqual(len(fn_obj.variables), 1)100 self.assertEqual(fn_obj.thresholds, [0.4, 0.9])101 # Check save and restore config102 fn_obj2 = metrics.FalseNegatives.from_config(fn_obj.get_config())103 self.assertEqual(fn_obj2.name, 'my_fn')104 self.assertEqual(len(fn_obj2.variables), 1)105 self.assertEqual(fn_obj2.thresholds, [0.4, 0.9])106 def test_unweighted(self):107 fn_obj = metrics.FalseNegatives()108 self.evaluate(variables.variables_initializer(fn_obj.variables))109 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),110 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))111 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),112 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))113 update_op = fn_obj.update_state(y_true, y_pred)114 self.evaluate(update_op)115 result = fn_obj.result()116 self.assertAllClose(3., result)117 def test_weighted(self):118 fn_obj = metrics.FalseNegatives()119 self.evaluate(variables.variables_initializer(fn_obj.variables))120 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),121 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))122 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),123 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))124 sample_weight = constant_op.constant((1., 1.5, 2., 2.5))125 result = fn_obj(y_true, y_pred, sample_weight=sample_weight)126 self.assertAllClose(5., self.evaluate(result))127 def test_unweighted_with_thresholds(self):128 fn_obj = metrics.FalseNegatives(thresholds=[0.15, 0.5, 0.85])129 self.evaluate(variables.variables_initializer(fn_obj.variables))130 y_pred = constant_op.constant(((0.9, 0.2, 0.8, 0.1), (0.2, 0.9, 0.7, 0.6),131 (0.1, 0.2, 0.4, 0.3), (0, 1, 0.7, 0.3)))132 y_true = constant_op.constant(((0, 1, 1, 0), (1, 0, 0, 0), (0, 0, 0, 0),133 (1, 1, 1, 1)))134 update_op = fn_obj.update_state(y_true, y_pred)135 self.evaluate(update_op)136 result = fn_obj.result()137 self.assertAllClose([1., 4., 6.], result)138 def test_weighted_with_thresholds(self):139 fn_obj = metrics.FalseNegatives(thresholds=[0.15, 0.5, 0.85])140 self.evaluate(variables.variables_initializer(fn_obj.variables))141 y_pred = constant_op.constant(((0.9, 0.2, 0.8, 0.1), (0.2, 0.9, 0.7, 0.6),142 (0.1, 0.2, 0.4, 0.3), (0, 1, 0.7, 0.3)))143 y_true = constant_op.constant(((0, 1, 1, 0), (1, 0, 0, 0), (0, 0, 0, 0),144 (1, 1, 1, 1)))145 sample_weight = ((3.0,), (5.0,), (7.0,), (4.0,))146 result = fn_obj(y_true, y_pred, sample_weight=sample_weight)147 self.assertAllClose([4., 16., 23.], self.evaluate(result))148@test_util.run_all_in_graph_and_eager_modes149class TrueNegativesTest(test.TestCase):150 def test_config(self):151 tn_obj = metrics.TrueNegatives(name='my_tn', thresholds=[0.4, 0.9])152 self.assertEqual(tn_obj.name, 'my_tn')153 self.assertEqual(len(tn_obj.variables), 1)154 self.assertEqual(tn_obj.thresholds, [0.4, 0.9])155 # Check save and restore config156 tn_obj2 = metrics.TrueNegatives.from_config(tn_obj.get_config())157 self.assertEqual(tn_obj2.name, 'my_tn')158 self.assertEqual(len(tn_obj2.variables), 1)159 self.assertEqual(tn_obj2.thresholds, [0.4, 0.9])160 def test_unweighted(self):161 tn_obj = metrics.TrueNegatives()162 self.evaluate(variables.variables_initializer(tn_obj.variables))163 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),164 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))165 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),166 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))167 update_op = tn_obj.update_state(y_true, y_pred)168 self.evaluate(update_op)169 result = tn_obj.result()170 self.assertAllClose(3., result)171 def test_weighted(self):172 tn_obj = metrics.TrueNegatives()173 self.evaluate(variables.variables_initializer(tn_obj.variables))174 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),175 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))176 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),177 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))178 sample_weight = constant_op.constant((1., 1.5, 2., 2.5))179 result = tn_obj(y_true, y_pred, sample_weight=sample_weight)180 self.assertAllClose(4., self.evaluate(result))181 def test_unweighted_with_thresholds(self):182 tn_obj = metrics.TrueNegatives(thresholds=[0.15, 0.5, 0.85])183 self.evaluate(variables.variables_initializer(tn_obj.variables))184 y_pred = constant_op.constant(((0.9, 0.2, 0.8, 0.1), (0.2, 0.9, 0.7, 0.6),185 (0.1, 0.2, 0.4, 0.3), (0, 1, 0.7, 0.3)))186 y_true = constant_op.constant(((0, 1, 1, 0), (1, 0, 0, 0), (0, 0, 0, 0),187 (1, 1, 1, 1)))188 update_op = tn_obj.update_state(y_true, y_pred)189 self.evaluate(update_op)190 result = tn_obj.result()191 self.assertAllClose([2., 5., 7.], result)192 def test_weighted_with_thresholds(self):193 tn_obj = metrics.TrueNegatives(thresholds=[0.15, 0.5, 0.85])194 self.evaluate(variables.variables_initializer(tn_obj.variables))195 y_pred = constant_op.constant(((0.9, 0.2, 0.8, 0.1), (0.2, 0.9, 0.7, 0.6),196 (0.1, 0.2, 0.4, 0.3), (0, 1, 0.7, 0.3)))197 y_true = constant_op.constant(((0, 1, 1, 0), (1, 0, 0, 0), (0, 0, 0, 0),198 (1, 1, 1, 1)))199 sample_weight = ((0.0, 2.0, 3.0, 5.0),)200 result = tn_obj(y_true, y_pred, sample_weight=sample_weight)201 self.assertAllClose([5., 15., 23.], self.evaluate(result))202@test_util.run_all_in_graph_and_eager_modes203class TruePositivesTest(test.TestCase):204 def test_config(self):205 tp_obj = metrics.TruePositives(name='my_tp', thresholds=[0.4, 0.9])206 self.assertEqual(tp_obj.name, 'my_tp')207 self.assertEqual(len(tp_obj.variables), 1)208 self.assertEqual(tp_obj.thresholds, [0.4, 0.9])209 # Check save and restore config210 tp_obj2 = metrics.TruePositives.from_config(tp_obj.get_config())211 self.assertEqual(tp_obj2.name, 'my_tp')212 self.assertEqual(len(tp_obj2.variables), 1)213 self.assertEqual(tp_obj2.thresholds, [0.4, 0.9])214 def test_unweighted(self):215 tp_obj = metrics.TruePositives()216 self.evaluate(variables.variables_initializer(tp_obj.variables))217 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),218 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))219 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),220 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))221 update_op = tp_obj.update_state(y_true, y_pred)222 self.evaluate(update_op)223 result = tp_obj.result()224 self.assertAllClose(7., result)225 def test_weighted(self):226 tp_obj = metrics.TruePositives()227 self.evaluate(variables.variables_initializer(tp_obj.variables))228 y_true = constant_op.constant(((0, 1, 0, 1, 0), (0, 0, 1, 1, 1),229 (1, 1, 1, 1, 0), (0, 0, 0, 0, 1)))230 y_pred = constant_op.constant(((0, 0, 1, 1, 0), (1, 1, 1, 1, 1),231 (0, 1, 0, 1, 0), (1, 1, 1, 1, 1)))232 sample_weight = constant_op.constant((1., 1.5, 2., 2.5))233 result = tp_obj(y_true, y_pred, sample_weight=sample_weight)234 self.assertAllClose(12., self.evaluate(result))235 def test_unweighted_with_thresholds(self):236 tp_obj = metrics.TruePositives(thresholds=[0.15, 0.5, 0.85])237 self.evaluate(variables.variables_initializer(tp_obj.variables))238 y_pred = constant_op.constant(((0.9, 0.2, 0.8, 0.1), (0.2, 0.9, 0.7, 0.6),239 (0.1, 0.2, 0.4, 0.3), (0, 1, 0.7, 0.3)))240 y_true = constant_op.constant(((0, 1, 1, 0), (1, 0, 0, 0), (0, 0, 0, 0),241 (1, 1, 1, 1)))242 update_op = tp_obj.update_state(y_true, y_pred)243 self.evaluate(update_op)244 result = tp_obj.result()245 self.assertAllClose([6., 3., 1.], result)246 def test_weighted_with_thresholds(self):247 tp_obj = metrics.TruePositives(thresholds=[0.15, 0.5, 0.85])248 self.evaluate(variables.variables_initializer(tp_obj.variables))249 y_pred = constant_op.constant(((0.9, 0.2, 0.8, 0.1), (0.2, 0.9, 0.7, 0.6),250 (0.1, 0.2, 0.4, 0.3), (0, 1, 0.7, 0.3)))251 y_true = constant_op.constant(((0, 1, 1, 0), (1, 0, 0, 0), (0, 0, 0, 0),252 (1, 1, 1, 1)))253 result = tp_obj(y_true, y_pred, sample_weight=37.)254 self.assertAllClose([222., 111., 37.], self.evaluate(result))255@test_util.run_all_in_graph_and_eager_modes256class PrecisionTest(test.TestCase):257 def test_config(self):258 p_obj = metrics.Precision(259 name='my_precision', thresholds=[0.4, 0.9], top_k=15, class_id=12)260 self.assertEqual(p_obj.name, 'my_precision')261 self.assertEqual(len(p_obj.variables), 2)262 self.assertEqual([v.name for v in p_obj.variables],263 ['true_positives:0', 'false_positives:0'])264 self.assertEqual(p_obj.thresholds, [0.4, 0.9])265 self.assertEqual(p_obj.top_k, 15)266 self.assertEqual(p_obj.class_id, 12)267 # Check save and restore config268 p_obj2 = metrics.Precision.from_config(p_obj.get_config())269 self.assertEqual(p_obj2.name, 'my_precision')270 self.assertEqual(len(p_obj2.variables), 2)271 self.assertEqual(p_obj2.thresholds, [0.4, 0.9])272 self.assertEqual(p_obj2.top_k, 15)273 self.assertEqual(p_obj2.class_id, 12)274 def test_value_is_idempotent(self):275 p_obj = metrics.Precision(thresholds=[0.3, 0.72])276 y_pred = random_ops.random_uniform(shape=(10, 3))277 y_true = random_ops.random_uniform(shape=(10, 3))278 update_op = p_obj.update_state(y_true, y_pred)279 self.evaluate(variables.variables_initializer(p_obj.variables))280 # Run several updates.281 for _ in range(10):282 self.evaluate(update_op)283 # Then verify idempotency.284 initial_precision = self.evaluate(p_obj.result())285 for _ in range(10):286 self.assertArrayNear(initial_precision, self.evaluate(p_obj.result()),287 1e-3)288 def test_unweighted(self):289 p_obj = metrics.Precision()290 y_pred = constant_op.constant([1, 0, 1, 0], shape=(1, 4))291 y_true = constant_op.constant([0, 1, 1, 0], shape=(1, 4))292 self.evaluate(variables.variables_initializer(p_obj.variables))293 result = p_obj(y_true, y_pred)294 self.assertAlmostEqual(0.5, self.evaluate(result))295 def test_unweighted_all_incorrect(self):296 p_obj = metrics.Precision(thresholds=[0.5])297 inputs = np.random.randint(0, 2, size=(100, 1))298 y_pred = constant_op.constant(inputs)299 y_true = constant_op.constant(1 - inputs)300 self.evaluate(variables.variables_initializer(p_obj.variables))301 result = p_obj(y_true, y_pred)302 self.assertAlmostEqual(0, self.evaluate(result))303 def test_weighted(self):304 p_obj = metrics.Precision()305 y_pred = constant_op.constant([[1, 0, 1, 0], [1, 0, 1, 0]])306 y_true = constant_op.constant([[0, 1, 1, 0], [1, 0, 0, 1]])307 self.evaluate(variables.variables_initializer(p_obj.variables))308 result = p_obj(309 y_true,310 y_pred,311 sample_weight=constant_op.constant([[1, 2, 3, 4], [4, 3, 2, 1]]))312 weighted_tp = 3.0 + 4.0313 weighted_positives = (1.0 + 3.0) + (4.0 + 2.0)314 expected_precision = weighted_tp / weighted_positives315 self.assertAlmostEqual(expected_precision, self.evaluate(result))316 def test_div_by_zero(self):317 p_obj = metrics.Precision()318 y_pred = constant_op.constant([0, 0, 0, 0])319 y_true = constant_op.constant([0, 0, 0, 0])320 self.evaluate(variables.variables_initializer(p_obj.variables))321 result = p_obj(y_true, y_pred)322 self.assertEqual(0, self.evaluate(result))323 def test_unweighted_with_threshold(self):324 p_obj = metrics.Precision(thresholds=[0.5, 0.7])325 y_pred = constant_op.constant([1, 0, 0.6, 0], shape=(1, 4))326 y_true = constant_op.constant([0, 1, 1, 0], shape=(1, 4))327 self.evaluate(variables.variables_initializer(p_obj.variables))328 result = p_obj(y_true, y_pred)329 self.assertArrayNear([0.5, 0.], self.evaluate(result), 0)330 def test_weighted_with_threshold(self):331 p_obj = metrics.Precision(thresholds=[0.5, 1.])332 y_true = constant_op.constant([[0, 1], [1, 0]], shape=(2, 2))333 y_pred = constant_op.constant([[1, 0], [0.6, 0]],334 shape=(2, 2),335 dtype=dtypes.float32)336 weights = constant_op.constant([[4, 0], [3, 1]],337 shape=(2, 2),338 dtype=dtypes.float32)339 self.evaluate(variables.variables_initializer(p_obj.variables))340 result = p_obj(y_true, y_pred, sample_weight=weights)341 weighted_tp = 0 + 3.342 weighted_positives = (0 + 3.) + (4. + 0.)343 expected_precision = weighted_tp / weighted_positives344 self.assertArrayNear([expected_precision, 0], self.evaluate(result), 1e-3)345 def test_multiple_updates(self):346 p_obj = metrics.Precision(thresholds=[0.5, 1.])347 y_true = constant_op.constant([[0, 1], [1, 0]], shape=(2, 2))348 y_pred = constant_op.constant([[1, 0], [0.6, 0]],349 shape=(2, 2),350 dtype=dtypes.float32)351 weights = constant_op.constant([[4, 0], [3, 1]],352 shape=(2, 2),353 dtype=dtypes.float32)354 self.evaluate(variables.variables_initializer(p_obj.variables))355 update_op = p_obj.update_state(y_true, y_pred, sample_weight=weights)356 for _ in range(2):357 self.evaluate(update_op)358 weighted_tp = (0 + 3.) + (0 + 3.)359 weighted_positives = ((0 + 3.) + (4. + 0.)) + ((0 + 3.) + (4. + 0.))360 expected_precision = weighted_tp / weighted_positives361 self.assertArrayNear([expected_precision, 0], self.evaluate(p_obj.result()),362 1e-3)363 def test_unweighted_top_k(self):364 p_obj = metrics.Precision(top_k=3)365 y_pred = constant_op.constant([0.2, 0.1, 0.5, 0, 0.2], shape=(1, 5))366 y_true = constant_op.constant([0, 1, 1, 0, 0], shape=(1, 5))367 self.evaluate(variables.variables_initializer(p_obj.variables))368 result = p_obj(y_true, y_pred)369 self.assertAlmostEqual(1. / 3, self.evaluate(result))370 def test_weighted_top_k(self):371 p_obj = metrics.Precision(top_k=3)372 y_pred1 = constant_op.constant([0.2, 0.1, 0.4, 0, 0.2], shape=(1, 5))373 y_true1 = constant_op.constant([0, 1, 1, 0, 1], shape=(1, 5))374 self.evaluate(variables.variables_initializer(p_obj.variables))375 self.evaluate(376 p_obj(377 y_true1,378 y_pred1,379 sample_weight=constant_op.constant([[1, 4, 2, 3, 5]])))380 y_pred2 = constant_op.constant([0.2, 0.6, 0.4, 0.2, 0.2], shape=(1, 5))381 y_true2 = constant_op.constant([1, 0, 1, 1, 1], shape=(1, 5))382 result = p_obj(y_true2, y_pred2, sample_weight=constant_op.constant(3))383 tp = (2 + 5) + (3 + 3)384 predicted_positives = (1 + 2 + 5) + (3 + 3 + 3)385 expected_precision = tp / predicted_positives386 self.assertAlmostEqual(expected_precision, self.evaluate(result))387 def test_unweighted_class_id(self):388 p_obj = metrics.Precision(class_id=2)389 self.evaluate(variables.variables_initializer(p_obj.variables))390 y_pred = constant_op.constant([0.2, 0.1, 0.6, 0, 0.2], shape=(1, 5))391 y_true = constant_op.constant([0, 1, 1, 0, 0], shape=(1, 5))392 result = p_obj(y_true, y_pred)393 self.assertAlmostEqual(1, self.evaluate(result))394 self.assertAlmostEqual(1, self.evaluate(p_obj.true_positives))395 self.assertAlmostEqual(0, self.evaluate(p_obj.false_positives))396 y_pred = constant_op.constant([0.2, 0.1, 0, 0, 0.2], shape=(1, 5))397 y_true = constant_op.constant([0, 1, 1, 0, 0], shape=(1, 5))398 result = p_obj(y_true, y_pred)399 self.assertAlmostEqual(1, self.evaluate(result))400 self.assertAlmostEqual(1, self.evaluate(p_obj.true_positives))401 self.assertAlmostEqual(0, self.evaluate(p_obj.false_positives))402 y_pred = constant_op.constant([0.2, 0.1, 0.6, 0, 0.2], shape=(1, 5))403 y_true = constant_op.constant([0, 1, 0, 0, 0], shape=(1, 5))404 result = p_obj(y_true, y_pred)405 self.assertAlmostEqual(0.5, self.evaluate(result))406 self.assertAlmostEqual(1, self.evaluate(p_obj.true_positives))407 self.assertAlmostEqual(1, self.evaluate(p_obj.false_positives))408 def test_unweighted_top_k_and_class_id(self):409 p_obj = metrics.Precision(class_id=2, top_k=2)410 self.evaluate(variables.variables_initializer(p_obj.variables))411 y_pred = constant_op.constant([0.2, 0.6, 0.3, 0, 0.2], shape=(1, 5))412 y_true = constant_op.constant([0, 1, 1, 0, 0], shape=(1, 5))413 result = p_obj(y_true, y_pred)414 self.assertAlmostEqual(1, self.evaluate(result))415 self.assertAlmostEqual(1, self.evaluate(p_obj.true_positives))416 self.assertAlmostEqual(0, self.evaluate(p_obj.false_positives))417 y_pred = constant_op.constant([1, 1, 0.9, 1, 1], shape=(1, 5))418 y_true = constant_op.constant([0, 1, 1, 0, 0], shape=(1, 5))419 result = p_obj(y_true, y_pred)420 self.assertAlmostEqual(1, self.evaluate(result))421 self.assertAlmostEqual(1, self.evaluate(p_obj.true_positives))422 self.assertAlmostEqual(0, self.evaluate(p_obj.false_positives))423 def test_unweighted_top_k_and_threshold(self):424 p_obj = metrics.Precision(thresholds=.7, top_k=2)425 self.evaluate(variables.variables_initializer(p_obj.variables))426 y_pred = constant_op.constant([0.2, 0.8, 0.6, 0, 0.2], shape=(1, 5))427 y_true = constant_op.constant([0, 1, 1, 0, 1], shape=(1, 5))428 result = p_obj(y_true, y_pred)429 self.assertAlmostEqual(1, self.evaluate(result))430 self.assertAlmostEqual(1, self.evaluate(p_obj.true_positives))431 self.assertAlmostEqual(0, self.evaluate(p_obj.false_positives))432@test_util.run_all_in_graph_and_eager_modes433class RecallTest(test.TestCase):434 def test_config(self):435 r_obj = metrics.Recall(436 name='my_recall', thresholds=[0.4, 0.9], top_k=15, class_id=12)437 self.assertEqual(r_obj.name, 'my_recall')438 self.assertEqual(len(r_obj.variables), 2)439 self.assertEqual([v.name for v in r_obj.variables],440 ['true_positives:0', 'false_negatives:0'])441 self.assertEqual(r_obj.thresholds, [0.4, 0.9])442 self.assertEqual(r_obj.top_k, 15)443 self.assertEqual(r_obj.class_id, 12)444 # Check save and restore config445 r_obj2 = metrics.Recall.from_config(r_obj.get_config())446 self.assertEqual(r_obj2.name, 'my_recall')447 self.assertEqual(len(r_obj2.variables), 2)448 self.assertEqual(r_obj2.thresholds, [0.4, 0.9])449 self.assertEqual(r_obj2.top_k, 15)450 self.assertEqual(r_obj2.class_id, 12)451 def test_value_is_idempotent(self):452 r_obj = metrics.Recall(thresholds=[0.3, 0.72])453 y_pred = random_ops.random_uniform(shape=(10, 3))454 y_true = random_ops.random_uniform(shape=(10, 3))455 update_op = r_obj.update_state(y_true, y_pred)456 self.evaluate(variables.variables_initializer(r_obj.variables))457 # Run several updates.458 for _ in range(10):459 self.evaluate(update_op)460 # Then verify idempotency.461 initial_recall = self.evaluate(r_obj.result())462 for _ in range(10):463 self.assertArrayNear(initial_recall, self.evaluate(r_obj.result()), 1e-3)464 def test_unweighted(self):465 r_obj = metrics.Recall()466 y_pred = constant_op.constant([1, 0, 1, 0], shape=(1, 4))467 y_true = constant_op.constant([0, 1, 1, 0], shape=(1, 4))468 self.evaluate(variables.variables_initializer(r_obj.variables))469 result = r_obj(y_true, y_pred)470 self.assertAlmostEqual(0.5, self.evaluate(result))471 def test_unweighted_all_incorrect(self):472 r_obj = metrics.Recall(thresholds=[0.5])473 inputs = np.random.randint(0, 2, size=(100, 1))474 y_pred = constant_op.constant(inputs)475 y_true = constant_op.constant(1 - inputs)476 self.evaluate(variables.variables_initializer(r_obj.variables))477 result = r_obj(y_true, y_pred)478 self.assertAlmostEqual(0, self.evaluate(result))479 def test_weighted(self):480 r_obj = metrics.Recall()481 y_pred = constant_op.constant([[1, 0, 1, 0], [0, 1, 0, 1]])482 y_true = constant_op.constant([[0, 1, 1, 0], [1, 0, 0, 1]])483 self.evaluate(variables.variables_initializer(r_obj.variables))484 result = r_obj(485 y_true,486 y_pred,487 sample_weight=constant_op.constant([[1, 2, 3, 4], [4, 3, 2, 1]]))488 weighted_tp = 3.0 + 1.0489 weighted_t = (2.0 + 3.0) + (4.0 + 1.0)490 expected_recall = weighted_tp / weighted_t491 self.assertAlmostEqual(expected_recall, self.evaluate(result))492 def test_div_by_zero(self):493 r_obj = metrics.Recall()494 y_pred = constant_op.constant([0, 0, 0, 0])495 y_true = constant_op.constant([0, 0, 0, 0])496 self.evaluate(variables.variables_initializer(r_obj.variables))497 result = r_obj(y_true, y_pred)498 self.assertEqual(0, self.evaluate(result))499 def test_unweighted_with_threshold(self):500 r_obj = metrics.Recall(thresholds=[0.5, 0.7])501 y_pred = constant_op.constant([1, 0, 0.6, 0], shape=(1, 4))502 y_true = constant_op.constant([0, 1, 1, 0], shape=(1, 4))503 self.evaluate(variables.variables_initializer(r_obj.variables))504 result = r_obj(y_true, y_pred)505 self.assertArrayNear([0.5, 0.], self.evaluate(result), 0)506 def test_weighted_with_threshold(self):507 r_obj = metrics.Recall(thresholds=[0.5, 1.])508 y_true = constant_op.constant([[0, 1], [1, 0]], shape=(2, 2))509 y_pred = constant_op.constant([[1, 0], [0.6, 0]],510 shape=(2, 2),511 dtype=dtypes.float32)512 weights = constant_op.constant([[1, 4], [3, 2]],513 shape=(2, 2),514 dtype=dtypes.float32)515 self.evaluate(variables.variables_initializer(r_obj.variables))516 result = r_obj(y_true, y_pred, sample_weight=weights)517 weighted_tp = 0 + 3.518 weighted_positives = (0 + 3.) + (4. + 0.)519 expected_recall = weighted_tp / weighted_positives520 self.assertArrayNear([expected_recall, 0], self.evaluate(result), 1e-3)521 def test_multiple_updates(self):522 r_obj = metrics.Recall(thresholds=[0.5, 1.])523 y_true = constant_op.constant([[0, 1], [1, 0]], shape=(2, 2))524 y_pred = constant_op.constant([[1, 0], [0.6, 0]],525 shape=(2, 2),526 dtype=dtypes.float32)527 weights = constant_op.constant([[1, 4], [3, 2]],528 shape=(2, 2),529 dtype=dtypes.float32)530 self.evaluate(variables.variables_initializer(r_obj.variables))531 update_op = r_obj.update_state(y_true, y_pred, sample_weight=weights)532 for _ in range(2):533 self.evaluate(update_op)534 weighted_tp = (0 + 3.) + (0 + 3.)535 weighted_positives = ((0 + 3.) + (4. + 0.)) + ((0 + 3.) + (4. + 0.))536 expected_recall = weighted_tp / weighted_positives537 self.assertArrayNear([expected_recall, 0], self.evaluate(r_obj.result()),538 1e-3)539 def test_unweighted_top_k(self):540 r_obj = metrics.Recall(top_k=3)541 y_pred = constant_op.constant([0.2, 0.1, 0.5, 0, 0.2], shape=(1, 5))542 y_true = constant_op.constant([0, 1, 1, 0, 0], shape=(1, 5))543 self.evaluate(variables.variables_initializer(r_obj.variables))544 result = r_obj(y_true, y_pred)545 self.assertAlmostEqual(0.5, self.evaluate(result))546 def test_weighted_top_k(self):547 r_obj = metrics.Recall(top_k=3)548 y_pred1 = constant_op.constant([0.2, 0.1, 0.4, 0, 0.2], shape=(1, 5))549 y_true1 = constant_op.constant([0, 1, 1, 0, 1], shape=(1, 5))550 self.evaluate(variables.variables_initializer(r_obj.variables))551 self.evaluate(552 r_obj(553 y_true1,554 y_pred1,555 sample_weight=constant_op.constant([[1, 4, 2, 3, 5]])))556 y_pred2 = constant_op.constant([0.2, 0.6, 0.4, 0.2, 0.2], shape=(1, 5))557 y_true2 = constant_op.constant([1, 0, 1, 1, 1], shape=(1, 5))558 result = r_obj(y_true2, y_pred2, sample_weight=constant_op.constant(3))559 tp = (2 + 5) + (3 + 3)560 positives = (4 + 2 + 5) + (3 + 3 + 3 + 3)561 expected_recall = tp / positives562 self.assertAlmostEqual(expected_recall, self.evaluate(result))563 def test_unweighted_class_id(self):564 r_obj = metrics.Recall(class_id=2)565 self.evaluate(variables.variables_initializer(r_obj.variables))566 y_pred = constant_op.constant([0.2, 0.1, 0.6, 0, 0.2], shape=(1, 5))567 y_true = constant_op.constant([0, 1, 1, 0, 0], shape=(1, 5))568 result = r_obj(y_true, y_pred)569 self.assertAlmostEqual(1, self.evaluate(result))570 self.assertAlmostEqual(1, self.evaluate(r_obj.true_positives))571 self.assertAlmostEqual(0, self.evaluate(r_obj.false_negatives))572 y_pred = constant_op.constant([0.2, 0.1, 0, 0, 0.2], shape=(1, 5))573 y_true = constant_op.constant([0, 1, 1, 0, 0], shape=(1, 5))574 result = r_obj(y_true, y_pred)575 self.assertAlmostEqual(0.5, self.evaluate(result))576 self.assertAlmostEqual(1, self.evaluate(r_obj.true_positives))577 self.assertAlmostEqual(1, self.evaluate(r_obj.false_negatives))578 y_pred = constant_op.constant([0.2, 0.1, 0.6, 0, 0.2], shape=(1, 5))579 y_true = constant_op.constant([0, 1, 0, 0, 0], shape=(1, 5))580 result = r_obj(y_true, y_pred)581 self.assertAlmostEqual(0.5, self.evaluate(result))582 self.assertAlmostEqual(1, self.evaluate(r_obj.true_positives))583 self.assertAlmostEqual(1, self.evaluate(r_obj.false_negatives))584 def test_unweighted_top_k_and_class_id(self):585 r_obj = metrics.Recall(class_id=2, top_k=2)586 self.evaluate(variables.variables_initializer(r_obj.variables))587 y_pred = constant_op.constant([0.2, 0.6, 0.3, 0, 0.2], shape=(1, 5))588 y_true = constant_op.constant([0, 1, 1, 0, 0], shape=(1, 5))589 result = r_obj(y_true, y_pred)590 self.assertAlmostEqual(1, self.evaluate(result))591 self.assertAlmostEqual(1, self.evaluate(r_obj.true_positives))592 self.assertAlmostEqual(0, self.evaluate(r_obj.false_negatives))593 y_pred = constant_op.constant([1, 1, 0.9, 1, 1], shape=(1, 5))594 y_true = constant_op.constant([0, 1, 1, 0, 0], shape=(1, 5))595 result = r_obj(y_true, y_pred)596 self.assertAlmostEqual(0.5, self.evaluate(result))597 self.assertAlmostEqual(1, self.evaluate(r_obj.true_positives))598 self.assertAlmostEqual(1, self.evaluate(r_obj.false_negatives))599 def test_unweighted_top_k_and_threshold(self):600 r_obj = metrics.Recall(thresholds=.7, top_k=2)601 self.evaluate(variables.variables_initializer(r_obj.variables))602 y_pred = constant_op.constant([0.2, 0.8, 0.6, 0, 0.2], shape=(1, 5))603 y_true = constant_op.constant([1, 1, 1, 0, 1], shape=(1, 5))604 result = r_obj(y_true, y_pred)605 self.assertAlmostEqual(0.25, self.evaluate(result))606 self.assertAlmostEqual(1, self.evaluate(r_obj.true_positives))607 self.assertAlmostEqual(3, self.evaluate(r_obj.false_negatives))608@test_util.run_all_in_graph_and_eager_modes609class SensitivityAtSpecificityTest(test.TestCase, parameterized.TestCase):610 def test_config(self):611 s_obj = metrics.SensitivityAtSpecificity(612 0.4, num_thresholds=100, name='sensitivity_at_specificity_1')613 self.assertEqual(s_obj.name, 'sensitivity_at_specificity_1')614 self.assertLen(s_obj.variables, 4)615 self.assertEqual(s_obj.specificity, 0.4)616 self.assertEqual(s_obj.num_thresholds, 100)617 # Check save and restore config618 s_obj2 = metrics.SensitivityAtSpecificity.from_config(s_obj.get_config())619 self.assertEqual(s_obj2.name, 'sensitivity_at_specificity_1')620 self.assertLen(s_obj2.variables, 4)621 self.assertEqual(s_obj2.specificity, 0.4)622 self.assertEqual(s_obj2.num_thresholds, 100)623 def test_value_is_idempotent(self):624 s_obj = metrics.SensitivityAtSpecificity(0.7)625 y_pred = random_ops.random_uniform((10, 3),626 maxval=1,627 dtype=dtypes.float32,628 seed=1)629 y_true = random_ops.random_uniform((10, 3),630 maxval=2,631 dtype=dtypes.int64,632 seed=1)633 update_op = s_obj.update_state(y_true, y_pred)634 self.evaluate(variables.variables_initializer(s_obj.variables))635 # Run several updates.636 for _ in range(10):637 self.evaluate(update_op)638 # Then verify idempotency.639 initial_sensitivity = self.evaluate(s_obj.result())640 for _ in range(10):641 self.assertAlmostEqual(initial_sensitivity, self.evaluate(s_obj.result()),642 1e-3)643 def test_unweighted_all_correct(self):644 s_obj = metrics.SensitivityAtSpecificity(0.7)645 inputs = np.random.randint(0, 2, size=(100, 1))646 y_pred = constant_op.constant(inputs, dtype=dtypes.float32)647 y_true = constant_op.constant(inputs)648 self.evaluate(variables.variables_initializer(s_obj.variables))649 result = s_obj(y_true, y_pred)650 self.assertAlmostEqual(1, self.evaluate(result))651 def test_unweighted_high_specificity(self):652 s_obj = metrics.SensitivityAtSpecificity(0.8)653 pred_values = [0.0, 0.1, 0.2, 0.3, 0.4, 0.1, 0.45, 0.5, 0.8, 0.9]654 label_values = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]655 y_pred = constant_op.constant(pred_values, dtype=dtypes.float32)656 y_true = constant_op.constant(label_values)657 self.evaluate(variables.variables_initializer(s_obj.variables))658 result = s_obj(y_true, y_pred)659 self.assertAlmostEqual(0.8, self.evaluate(result))660 def test_unweighted_low_specificity(self):661 s_obj = metrics.SensitivityAtSpecificity(0.4)662 pred_values = [0.0, 0.1, 0.2, 0.3, 0.4, 0.01, 0.02, 0.25, 0.26, 0.26]663 label_values = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]664 y_pred = constant_op.constant(pred_values, dtype=dtypes.float32)665 y_true = constant_op.constant(label_values)666 self.evaluate(variables.variables_initializer(s_obj.variables))667 result = s_obj(y_true, y_pred)668 self.assertAlmostEqual(0.6, self.evaluate(result))669 @parameterized.parameters([dtypes.bool, dtypes.int32, dtypes.float32])670 def test_weighted(self, label_dtype):671 s_obj = metrics.SensitivityAtSpecificity(0.4)672 pred_values = [0.0, 0.1, 0.2, 0.3, 0.4, 0.01, 0.02, 0.25, 0.26, 0.26]673 label_values = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]674 weight_values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]675 y_pred = constant_op.constant(pred_values, dtype=dtypes.float32)676 y_true = math_ops.cast(label_values, dtype=label_dtype)677 weights = constant_op.constant(weight_values)678 self.evaluate(variables.variables_initializer(s_obj.variables))679 result = s_obj(y_true, y_pred, sample_weight=weights)680 self.assertAlmostEqual(0.675, self.evaluate(result))681 def test_invalid_specificity(self):682 with self.assertRaisesRegexp(683 ValueError, r'`specificity` must be in the range \[0, 1\].'):684 metrics.SensitivityAtSpecificity(-1)685 def test_invalid_num_thresholds(self):686 with self.assertRaisesRegexp(ValueError, '`num_thresholds` must be > 0.'):687 metrics.SensitivityAtSpecificity(0.4, num_thresholds=-1)688@test_util.run_all_in_graph_and_eager_modes689class SpecificityAtSensitivityTest(test.TestCase, parameterized.TestCase):690 def test_config(self):691 s_obj = metrics.SpecificityAtSensitivity(692 0.4, num_thresholds=100, name='specificity_at_sensitivity_1')693 self.assertEqual(s_obj.name, 'specificity_at_sensitivity_1')694 self.assertLen(s_obj.variables, 4)695 self.assertEqual(s_obj.sensitivity, 0.4)696 self.assertEqual(s_obj.num_thresholds, 100)697 # Check save and restore config698 s_obj2 = metrics.SpecificityAtSensitivity.from_config(s_obj.get_config())699 self.assertEqual(s_obj2.name, 'specificity_at_sensitivity_1')700 self.assertLen(s_obj2.variables, 4)701 self.assertEqual(s_obj2.sensitivity, 0.4)702 self.assertEqual(s_obj2.num_thresholds, 100)703 def test_value_is_idempotent(self):704 s_obj = metrics.SpecificityAtSensitivity(0.7)705 y_pred = random_ops.random_uniform((10, 3),706 maxval=1,707 dtype=dtypes.float32,708 seed=1)709 y_true = random_ops.random_uniform((10, 3),710 maxval=2,711 dtype=dtypes.int64,712 seed=1)713 update_op = s_obj.update_state(y_true, y_pred)714 self.evaluate(variables.variables_initializer(s_obj.variables))715 # Run several updates.716 for _ in range(10):717 self.evaluate(update_op)718 # Then verify idempotency.719 initial_specificity = self.evaluate(s_obj.result())720 for _ in range(10):721 self.assertAlmostEqual(initial_specificity, self.evaluate(s_obj.result()),722 1e-3)723 def test_unweighted_all_correct(self):724 s_obj = metrics.SpecificityAtSensitivity(0.7)725 inputs = np.random.randint(0, 2, size=(100, 1))726 y_pred = constant_op.constant(inputs, dtype=dtypes.float32)727 y_true = constant_op.constant(inputs)728 self.evaluate(variables.variables_initializer(s_obj.variables))729 result = s_obj(y_true, y_pred)730 self.assertAlmostEqual(1, self.evaluate(result))731 def test_unweighted_high_sensitivity(self):732 s_obj = metrics.SpecificityAtSensitivity(0.8)733 pred_values = [0.0, 0.1, 0.2, 0.3, 0.4, 0.1, 0.45, 0.5, 0.8, 0.9]734 label_values = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]735 y_pred = constant_op.constant(pred_values, dtype=dtypes.float32)736 y_true = constant_op.constant(label_values)737 self.evaluate(variables.variables_initializer(s_obj.variables))738 result = s_obj(y_true, y_pred)739 self.assertAlmostEqual(0.4, self.evaluate(result))740 def test_unweighted_low_sensitivity(self):741 s_obj = metrics.SpecificityAtSensitivity(0.4)742 pred_values = [0.0, 0.1, 0.2, 0.3, 0.4, 0.01, 0.02, 0.25, 0.26, 0.26]743 label_values = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]744 y_pred = constant_op.constant(pred_values, dtype=dtypes.float32)745 y_true = constant_op.constant(label_values)746 self.evaluate(variables.variables_initializer(s_obj.variables))747 result = s_obj(y_true, y_pred)748 self.assertAlmostEqual(0.6, self.evaluate(result))749 @parameterized.parameters([dtypes.bool, dtypes.int32, dtypes.float32])750 def test_weighted(self, label_dtype):751 s_obj = metrics.SpecificityAtSensitivity(0.4)752 pred_values = [0.0, 0.1, 0.2, 0.3, 0.4, 0.01, 0.02, 0.25, 0.26, 0.26]753 label_values = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]754 weight_values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]755 y_pred = constant_op.constant(pred_values, dtype=dtypes.float32)756 y_true = math_ops.cast(label_values, dtype=label_dtype)757 weights = constant_op.constant(weight_values)758 self.evaluate(variables.variables_initializer(s_obj.variables))759 result = s_obj(y_true, y_pred, sample_weight=weights)760 self.assertAlmostEqual(0.4, self.evaluate(result))761 def test_invalid_sensitivity(self):762 with self.assertRaisesRegexp(763 ValueError, r'`sensitivity` must be in the range \[0, 1\].'):764 metrics.SpecificityAtSensitivity(-1)765 def test_invalid_num_thresholds(self):766 with self.assertRaisesRegexp(ValueError, '`num_thresholds` must be > 0.'):767 metrics.SpecificityAtSensitivity(0.4, num_thresholds=-1)768@test_util.run_all_in_graph_and_eager_modes769class AUCTest(test.TestCase):770 def setup(self):771 self.num_thresholds = 3772 self.y_pred = constant_op.constant([0, 0.5, 0.3, 0.9], dtype=dtypes.float32)773 self.y_true = constant_op.constant([0, 0, 1, 1])774 self.sample_weight = [1, 2, 3, 4]775 # threshold values are [0 - 1e-7, 0.5, 1 + 1e-7]776 # y_pred when threshold = 0 - 1e-7 : [1, 1, 1, 1]777 # y_pred when threshold = 0.5 : [0, 0, 0, 1]778 # y_pred when threshold = 1 + 1e-7 : [0, 0, 0, 0]779 # without sample_weight:780 # tp = np.sum([[0, 0, 1, 1], [0, 0, 0, 1], [0, 0, 0, 0]], axis=1)781 # fp = np.sum([[1, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], axis=1)782 # fn = np.sum([[0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 1, 1]], axis=1)783 # tn = np.sum([[0, 0, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]], axis=1)784 # tp = [2, 1, 0], fp = [2, 0, 0], fn = [0, 1, 2], tn = [0, 2, 2]785 # with sample_weight:786 # tp = np.sum([[0, 0, 3, 4], [0, 0, 0, 4], [0, 0, 0, 0]], axis=1)787 # fp = np.sum([[1, 2, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], axis=1)788 # fn = np.sum([[0, 0, 0, 0], [0, 0, 3, 0], [0, 0, 3, 4]], axis=1)789 # tn = np.sum([[0, 0, 0, 0], [1, 2, 0, 0], [1, 2, 0, 0]], axis=1)790 # tp = [7, 4, 0], fp = [3, 0, 0], fn = [0, 3, 7], tn = [0, 3, 3]791 def test_config(self):792 auc_obj = metrics.AUC(793 num_thresholds=100,794 curve=metrics_utils.AUCCurve.PR,795 summation_method=metrics_utils.AUCSummationMethod.MAJORING,796 name='auc_1')797 self.assertEqual(auc_obj.name, 'auc_1')798 self.assertEqual(len(auc_obj.variables), 4)799 self.assertEqual(auc_obj.num_thresholds, 100)800 self.assertEqual(auc_obj.curve, metrics_utils.AUCCurve.PR)801 self.assertEqual(auc_obj.summation_method,802 metrics_utils.AUCSummationMethod.MAJORING)803 # Check save and restore config804 auc_obj2 = metrics.AUC.from_config(auc_obj.get_config())805 self.assertEqual(auc_obj2.name, 'auc_1')806 self.assertEqual(len(auc_obj2.variables), 4)807 self.assertEqual(auc_obj2.num_thresholds, 100)808 self.assertEqual(auc_obj2.curve, metrics_utils.AUCCurve.PR)809 self.assertEqual(auc_obj2.summation_method,810 metrics_utils.AUCSummationMethod.MAJORING)811 def test_value_is_idempotent(self):812 self.setup()813 auc_obj = metrics.AUC(num_thresholds=3)814 self.evaluate(variables.variables_initializer(auc_obj.variables))815 # Run several updates.816 update_op = auc_obj.update_state(self.y_true, self.y_pred)817 for _ in range(10):818 self.evaluate(update_op)819 # Then verify idempotency.820 initial_auc = self.evaluate(auc_obj.result())821 for _ in range(10):822 self.assertAllClose(initial_auc, self.evaluate(auc_obj.result()), 1e-3)823 def test_unweighted_all_correct(self):824 self.setup()825 auc_obj = metrics.AUC()826 self.evaluate(variables.variables_initializer(auc_obj.variables))827 result = auc_obj(self.y_true, self.y_true)828 self.assertEqual(self.evaluate(result), 1)829 def test_unweighted(self):830 self.setup()831 auc_obj = metrics.AUC(num_thresholds=self.num_thresholds)832 self.evaluate(variables.variables_initializer(auc_obj.variables))833 result = auc_obj(self.y_true, self.y_pred)834 # tp = [2, 1, 0], fp = [2, 0, 0], fn = [0, 1, 2], tn = [0, 2, 2]835 # recall = [2/2, 1/(1+1), 0] = [1, 0.5, 0]836 # fp_rate = [2/2, 0, 0] = [1, 0, 0]837 # heights = [(1 + 0.5)/2, (0.5 + 0)/2] = [0.75, 0.25]838 # widths = [(1 - 0), (0 - 0)] = [1, 0]839 expected_result = (0.75 * 1 + 0.25 * 0)840 self.assertAllClose(self.evaluate(result), expected_result, 1e-3)841 def test_weighted_roc_interpolation(self):842 self.setup()843 auc_obj = metrics.AUC(num_thresholds=self.num_thresholds)844 self.evaluate(variables.variables_initializer(auc_obj.variables))845 result = auc_obj(self.y_true, self.y_pred, sample_weight=self.sample_weight)846 # tp = [7, 4, 0], fp = [3, 0, 0], fn = [0, 3, 7], tn = [0, 3, 3]847 # recall = [7/7, 4/(4+3), 0] = [1, 0.571, 0]848 # fp_rate = [3/3, 0, 0] = [1, 0, 0]849 # heights = [(1 + 0.571)/2, (0.571 + 0)/2] = [0.7855, 0.2855]850 # widths = [(1 - 0), (0 - 0)] = [1, 0]851 expected_result = (0.7855 * 1 + 0.2855 * 0)852 self.assertAllClose(self.evaluate(result), expected_result, 1e-3)853 def test_weighted_roc_majoring(self):854 self.setup()855 auc_obj = metrics.AUC(856 num_thresholds=self.num_thresholds,857 summation_method=metrics_utils.AUCSummationMethod.MAJORING)858 self.evaluate(variables.variables_initializer(auc_obj.variables))859 result = auc_obj(self.y_true, self.y_pred, sample_weight=self.sample_weight)860 # tp = [7, 4, 0], fp = [3, 0, 0], fn = [0, 3, 7], tn = [0, 3, 3]861 # recall = [7/7, 4/(4+3), 0] = [1, 0.571, 0]862 # fp_rate = [3/3, 0, 0] = [1, 0, 0]863 # heights = [max(1, 0.571), max(0.571, 0)] = [1, 0.571]864 # widths = [(1 - 0), (0 - 0)] = [1, 0]865 expected_result = (1 * 1 + 0.571 * 0)866 self.assertAllClose(self.evaluate(result), expected_result, 1e-3)867 def test_weighted_roc_minoring(self):868 self.setup()869 auc_obj = metrics.AUC(870 num_thresholds=self.num_thresholds,871 summation_method=metrics_utils.AUCSummationMethod.MINORING)872 self.evaluate(variables.variables_initializer(auc_obj.variables))873 result = auc_obj(self.y_true, self.y_pred, sample_weight=self.sample_weight)874 # tp = [7, 4, 0], fp = [3, 0, 0], fn = [0, 3, 7], tn = [0, 3, 3]875 # recall = [7/7, 4/(4+3), 0] = [1, 0.571, 0]876 # fp_rate = [3/3, 0, 0] = [1, 0, 0]877 # heights = [min(1, 0.571), min(0.571, 0)] = [0.571, 0]878 # widths = [(1 - 0), (0 - 0)] = [1, 0]879 expected_result = (0.571 * 1 + 0 * 0)880 self.assertAllClose(self.evaluate(result), expected_result, 1e-3)881 def test_weighted_pr_majoring(self):882 self.setup()883 auc_obj = metrics.AUC(884 num_thresholds=self.num_thresholds,885 curve=metrics_utils.AUCCurve.PR,886 summation_method=metrics_utils.AUCSummationMethod.MAJORING)887 self.evaluate(variables.variables_initializer(auc_obj.variables))888 result = auc_obj(self.y_true, self.y_pred, sample_weight=self.sample_weight)889 # tp = [7, 4, 0], fp = [3, 0, 0], fn = [0, 3, 7], tn = [0, 3, 3]890 # precision = [7/(7+3), 4/4, 0] = [0.7, 1, 0]891 # recall = [7/7, 4/(4+3), 0] = [1, 0.571, 0]892 # heights = [max(0.7, 1), max(1, 0)] = [1, 1]893 # widths = [(1 - 0.571), (0.571 - 0)] = [0.429, 0.571]894 expected_result = (1 * 0.429 + 1 * 0.571)895 self.assertAllClose(self.evaluate(result), expected_result, 1e-3)896 def test_weighted_pr_minoring(self):897 self.setup()898 auc_obj = metrics.AUC(899 num_thresholds=self.num_thresholds,900 curve=metrics_utils.AUCCurve.PR,901 summation_method=metrics_utils.AUCSummationMethod.MINORING)902 self.evaluate(variables.variables_initializer(auc_obj.variables))903 result = auc_obj(self.y_true, self.y_pred, sample_weight=self.sample_weight)904 # tp = [7, 4, 0], fp = [3, 0, 0], fn = [0, 3, 7], tn = [0, 3, 3]905 # precision = [7/(7+3), 4/4, 0] = [0.7, 1, 0]906 # recall = [7/7, 4/(4+3), 0] = [1, 0.571, 0]907 # heights = [min(0.7, 1), min(1, 0)] = [0.7, 0]908 # widths = [(1 - 0.571), (0.571 - 0)] = [0.429, 0.571]909 expected_result = (0.7 * 0.429 + 0 * 0.571)910 self.assertAllClose(self.evaluate(result), expected_result, 1e-3)911 def test_weighted_pr_interpolation(self):912 self.setup()913 auc_obj = metrics.AUC(914 num_thresholds=self.num_thresholds,915 curve=metrics_utils.AUCCurve.PR,916 summation_method=metrics_utils.AUCSummationMethod.INTERPOLATION)917 self.evaluate(variables.variables_initializer(auc_obj.variables))918 result = auc_obj(self.y_true, self.y_pred, sample_weight=self.sample_weight)919 # auc = (slope / Total Pos) * [dTP - intercept * log(Pb/Pa)]920 # tp = [7, 4, 0], fp = [3, 0, 0], fn = [0, 3, 7], tn = [0, 3, 3]921 # P = tp + fp = [10, 4, 0]922 # dTP = [7-4, 4-0] = [3, 4]923 # dP = [10-4, 4-0] = [6, 4]924 # slope = dTP/dP = [0.5, 1]925 # intercept = (TPa+(slope*Pa) = [(4 - 0.5*4), (0 - 1*0)] = [2, 0]926 # (Pb/Pa) = (Pb/Pa) if Pb > 0 AND Pa > 0 else 1 = [10/4, 4/0] = [2.5, 1]927 # auc * TotalPos = [(0.5 * (3 + 2 * log(2.5))), (1 * (4 + 0))]928 # = [2.416, 4]929 # auc = [2.416, 4]/(tp[1:]+fn[1:])930 expected_result = (2.416/7 + 4/7)931 self.assertAllClose(self.evaluate(result), expected_result, 1e-3)932 def test_invalid_num_thresholds(self):933 with self.assertRaisesRegexp(ValueError, '`num_thresholds` must be > 1.'):934 metrics.AUC(num_thresholds=-1)935 with self.assertRaisesRegexp(ValueError, '`num_thresholds` must be > 1.'):936 metrics.AUC(num_thresholds=1)937if __name__ == '__main__':...
variables_test.py
Source:variables_test.py
...55 self.assertEqual([], var1.get_shape())56 self.assertEqual([], var1.get_shape())57 self.assertEqual([], var1.shape)58 with self.assertRaisesOpError("Attempting to use uninitialized value"):59 self.evaluate(var0)60 with self.assertRaisesOpError("Attempting to use uninitialized value"):61 self.evaluate(var1)62 self.evaluate(variables.global_variables_initializer())63 self.assertAllClose(0.0, self.evaluate(var0))64 self.assertAllClose(1.1, self.evaluate(var1))65 @test_util.run_v1_only("b/120545219")66 def testInitializationOrder(self):67 with self.cached_session():68 rnd = variables.Variable(random_ops.random_uniform([3, 6]), name="rnd")69 self.assertEqual("rnd:0", rnd.name)70 self.assertEqual([3, 6], rnd.get_shape())71 self.assertEqual([3, 6], rnd.get_shape())72 self.assertEqual([3, 6], rnd.shape)73 dep = variables.Variable(rnd.initialized_value(), name="dep")74 self.assertEqual("dep:0", dep.name)75 self.assertEqual([3, 6], dep.get_shape())76 self.assertEqual([3, 6], dep.get_shape())77 self.assertEqual([3, 6], dep.shape)78 # Currently have to set the shape manually for Add.79 added_val = rnd.initialized_value() + dep.initialized_value() + 2.080 added_val.set_shape(rnd.get_shape())81 depdep = variables.Variable(added_val, name="depdep")82 self.assertEqual("depdep:0", depdep.name)83 self.assertEqual([3, 6], depdep.get_shape())84 self.assertEqual([3, 6], depdep.get_shape())85 self.assertEqual([3, 6], depdep.shape)86 self.evaluate(variables.global_variables_initializer())87 self.assertAllClose(self.evaluate(rnd), self.evaluate(dep))88 self.assertAllClose(89 self.evaluate(rnd) + self.evaluate(dep) + 2.0, self.evaluate(depdep))90 def testIterable(self):91 with self.assertRaisesRegexp(TypeError, "not iterable"):92 for _ in variables.Variable(0.0):93 pass94 with self.assertRaisesRegexp(TypeError, "not iterable"):95 for _ in variables.Variable([0.0, 1.0]):96 pass97 @test_util.run_deprecated_v198 def testAssignments(self):99 with self.cached_session():100 var = variables.Variable(0.0)101 plus_one = var.assign_add(1.0)102 minus_one = var.assign_sub(2.0)103 four = var.assign(4.0)104 self.evaluate(variables.global_variables_initializer())105 self.assertAllClose(0.0, self.evaluate(var))106 self.assertAllClose(1.0, self.evaluate(plus_one))107 self.assertAllClose(1.0, self.evaluate(var))108 self.assertAllClose(-1.0, self.evaluate(minus_one))109 self.assertAllClose(-1.0, self.evaluate(var))110 self.assertAllClose(4.0, self.evaluate(four))111 self.assertAllClose(4.0, self.evaluate(var))112 @test_util.run_deprecated_v1113 def testResourceAssignments(self):114 with self.session(use_gpu=True):115 var = resource_variable_ops.ResourceVariable(0.0)116 plus_one = var.assign_add(1.0)117 minus_one = var.assign_sub(2.0)118 four = var.assign(4.0)119 self.evaluate(variables.global_variables_initializer())120 self.assertAllClose(0.0, self.evaluate(var))121 self.evaluate(plus_one)122 self.assertAllClose(1.0, self.evaluate(var))123 self.evaluate(minus_one)124 self.assertAllClose(-1.0, self.evaluate(var))125 self.evaluate(four)126 self.assertAllClose(4.0, self.evaluate(var))127 def testZeroSizeStringAssign(self):128 with self.cached_session() as sess:129 array = variables.VariableV1(130 initial_value=array_ops.zeros((0,), dtype=dtypes.string),131 name="foo",132 trainable=False,133 collections=[ops.GraphKeys.LOCAL_VARIABLES])134 self.evaluate(variables.local_variables_initializer())135 old_value = array.value()136 copy_op = array.assign(old_value)137 self.assertEqual([], list(self.evaluate(copy_op)))138 def _countUpToTest(self, dtype):139 with self.cached_session():140 zero = constant_op.constant(0, dtype=dtype)141 var = variables.Variable(zero)142 count_up_to = var.count_up_to(3)143 self.evaluate(variables.global_variables_initializer())144 self.assertEqual(0, self.evaluate(var))145 self.assertEqual(0, self.evaluate(count_up_to))146 self.assertEqual(1, self.evaluate(var))147 self.assertEqual(1, self.evaluate(count_up_to))148 self.assertEqual(2, self.evaluate(var))149 self.assertEqual(2, self.evaluate(count_up_to))150 self.assertEqual(3, self.evaluate(var))151 with self.assertRaisesOpError("Reached limit of 3"):152 self.evaluate(count_up_to)153 self.assertEqual(3, self.evaluate(var))154 with self.assertRaisesOpError("Reached limit of 3"):155 self.evaluate(count_up_to)156 self.assertEqual(3, self.evaluate(var))157 @test_util.run_deprecated_v1158 def testCountUpToInt32(self):159 self._countUpToTest(dtypes.int32)160 @test_util.run_deprecated_v1161 def testCountUpToInt64(self):162 self._countUpToTest(dtypes.int64)163 @test_util.run_v1_only("b/120545219")164 def testControlDepsNone(self):165 with self.cached_session():166 c = constant_op.constant(1.0)167 with ops.control_dependencies([c]):168 # d get the control dep.169 d = constant_op.constant(2.0)170 # variables do not.171 var_x = variables.VariableV1(2.0)172 self.assertEqual([c.op], d.op.control_inputs)173 self.assertEqual([], var_x.initializer.control_inputs)174 self.assertEqual([], var_x.value().op.control_inputs)175 self.assertEqual([], var_x._ref().op.control_inputs) # pylint: disable=protected-access176 @test_util.run_v1_only("b/120545219")177 def testControlFlow(self):178 with self.cached_session() as sess:179 v0 = variables.Variable(0, name="v0")180 var_dict = {}181 # Call get_variable in each of the cond clauses.182 def var_in_then_clause():183 v1 = variables.Variable(1, name="v1")184 var_dict["v1"] = v1185 return v1 + v0186 def var_in_else_clause():187 v2 = variables.Variable(2, name="v2")188 var_dict["v2"] = v2189 return v2 + v0190 add = control_flow_ops.cond(191 math_ops.less(v0, 10), var_in_then_clause, var_in_else_clause)192 v1 = var_dict["v1"]193 v2 = var_dict["v2"]194 # We should be able to initialize and run v1 and v2 without initializing195 # v0, even if the variable was created with a control dep on v0.196 self.evaluate(v1.initializer)197 self.assertEqual([1], self.evaluate(v1))198 self.evaluate(v2.initializer)199 self.assertEqual([2], self.evaluate(v2))200 # v0 should still be uninitialized.201 with self.assertRaisesRegexp(errors_impl.OpError, "uninitialized"):202 self.evaluate(v0)203 # We should not be able to run 'add' yet.204 with self.assertRaisesRegexp(errors_impl.OpError, "uninitialized"):205 self.evaluate(add)206 # If we initialize v0 we should be able to run 'add'.207 self.evaluate(v0.initializer)208 self.evaluate(add)209 @test_util.run_v1_only("b/120545219")210 def testControlFlowInitialization(self):211 """Expects an error if an initializer is in a control-flow scope."""212 def cond(i, _):213 return i < 10214 def body(i, _):215 zero = array_ops.zeros([], dtype=dtypes.int32)216 v = variables.Variable(initial_value=zero)217 return (i + 1, v.read_value())218 with self.assertRaisesRegexp(ValueError, "inside a control-flow"):219 control_flow_ops.while_loop(cond, body, [0, 0])220 @test_util.run_deprecated_v1221 def testUseVariableAsTensor(self):222 with self.cached_session():223 var_x = variables.Variable(2.0)224 var_y = variables.Variable(3.0)225 self.evaluate(variables.global_variables_initializer())226 self.assertAllClose(2.0, self.evaluate(var_x))227 self.assertAllClose(3.0, self.evaluate(var_y))228 self.assertAllClose(5.0, self.evaluate(math_ops.add(var_x, var_y)))229 @test_util.run_deprecated_v1230 def testZeroSizeVarSameAsConst(self):231 with self.cached_session():232 zero_size_var = variables.Variable(array_ops.zeros([0, 2]))233 zero_size_const = array_ops.ones([2, 0])234 variable_mul = math_ops.matmul(zero_size_const, zero_size_var)235 const_mul = math_ops.matmul(236 zero_size_const, zero_size_const, transpose_b=True)237 self.evaluate(variables.global_variables_initializer())238 variable_output = self.evaluate(variable_mul)239 self.assertAllClose(self.evaluate(const_mul), variable_output)240 self.assertAllClose([[0., 0.], [0., 0.]], variable_output)241 @test_util.run_deprecated_v1242 def testCachingDevice(self):243 with self.cached_session():244 var = variables.Variable(2.0)245 self.assertEqual(var.device, var.value().device)246 self.assertEqual(var.device, var.initialized_value().device)247 var_cached = variables.Variable(2.0, caching_device="/job:foo")248 self.assertFalse(var_cached.device.startswith("/job:foo"))249 self.assertTrue(var_cached.value().device.startswith("/job:foo"))250 @test_util.run_deprecated_v1251 def testCollections(self):252 with self.cached_session():253 var_x = variables.VariableV1(2.0)254 var_y = variables.VariableV1(2.0, trainable=False)255 var_z = variables.VariableV1(2.0, trainable=True)256 var_t = variables.VariableV1(257 2.0,258 trainable=True,259 collections=[260 ops.GraphKeys.TRAINABLE_VARIABLES, ops.GraphKeys.GLOBAL_VARIABLES261 ])262 self.assertEqual([var_x, var_y, var_z, var_t],263 variables.global_variables())264 self.assertEqual([var_x, var_z, var_t], variables.trainable_variables())265 @test_util.run_deprecated_v1266 def testCollectionsWithScope(self):267 with self.cached_session():268 with ops.name_scope("scope_1"):269 var_x = variables.VariableV1(2.0)270 with ops.name_scope("scope_2"):271 var_y = variables.VariableV1(2.0)272 self.assertEqual([var_x, var_y], variables.global_variables())273 self.assertEqual([var_x], variables.global_variables("scope_1"))274 self.assertEqual([var_y], variables.global_variables("scope_2"))275 self.assertEqual([var_x, var_y], variables.trainable_variables())276 self.assertEqual([var_x], variables.trainable_variables("scope_1"))277 self.assertEqual([var_y], variables.trainable_variables("scope_2"))278 def testOperatorWrapping(self):279 for attr in functools.WRAPPER_ASSIGNMENTS:280 self.assertEqual(281 getattr(variables.Variable.__add__, attr),282 getattr(ops.Tensor.__add__, attr))283 @test_util.run_deprecated_v1284 def testOperators(self):285 with self.cached_session():286 var_f = variables.Variable([2.0])287 add = var_f + 0.0288 radd = 1.0 + var_f289 sub = var_f - 1.0290 rsub = 1.0 - var_f291 mul = var_f * 10.0292 rmul = 10.0 * var_f293 div = var_f / 10.0294 rdiv = 10.0 / var_f295 lt = var_f < 3.0296 rlt = 3.0 < var_f297 le = var_f <= 2.0298 rle = 2.0 <= var_f299 gt = var_f > 3.0300 rgt = 3.0 > var_f301 ge = var_f >= 2.0302 rge = 2.0 >= var_f303 neg = -var_f304 abs_v = abs(var_f)305 var_i = variables.Variable([20])306 mod = var_i % 7307 rmod = 103 % var_i308 var_b = variables.Variable([True, False])309 and_v = operator.and_(var_b, [True, True])310 or_v = operator.or_(var_b, [False, True])311 xor_v = operator.xor(var_b, [False, False])312 invert_v = ~var_b313 rnd = np.random.rand(4, 4).astype("f")314 var_t = variables.Variable(rnd)315 slice_v = var_t[2, 0:0]316 var_m = variables.Variable([[2.0, 3.0]])317 matmul = var_m.__matmul__([[10.0], [20.0]])318 rmatmul = var_m.__rmatmul__([[10.0], [20.0]])319 self.evaluate(variables.global_variables_initializer())320 self.assertAllClose([2.0], self.evaluate(add))321 self.assertAllClose([3.0], self.evaluate(radd))322 self.assertAllClose([1.0], self.evaluate(sub))323 self.assertAllClose([-1.0], self.evaluate(rsub))324 self.assertAllClose([20.0], self.evaluate(mul))325 self.assertAllClose([20.0], self.evaluate(rmul))326 self.assertAllClose([0.2], self.evaluate(div))327 self.assertAllClose([5.0], self.evaluate(rdiv))328 self.assertAllClose([-2.0], self.evaluate(neg))329 self.assertAllClose([2.0], self.evaluate(abs_v))330 self.assertAllClose([True], self.evaluate(lt))331 self.assertAllClose([False], self.evaluate(rlt))332 self.assertAllClose([True], self.evaluate(le))333 self.assertAllClose([True], self.evaluate(rle))334 self.assertAllClose([False], self.evaluate(gt))335 self.assertAllClose([True], self.evaluate(rgt))336 self.assertAllClose([True], self.evaluate(ge))337 self.assertAllClose([True], self.evaluate(rge))338 self.assertAllClose([6], self.evaluate(mod))339 self.assertAllClose([3], self.evaluate(rmod))340 self.assertAllClose([True, False], self.evaluate(and_v))341 self.assertAllClose([True, True], self.evaluate(or_v))342 self.assertAllClose([True, False], self.evaluate(xor_v))343 self.assertAllClose([False, True], self.evaluate(invert_v))344 self.assertAllClose(rnd[2, 0:0], self.evaluate(slice_v))345 self.assertAllClose([[80.0]], self.evaluate(matmul))346 self.assertAllClose([[20.0, 30.0], [40.0, 60.0]], self.evaluate(rmatmul))347 @test_util.run_deprecated_v1348 def testSession(self):349 with self.cached_session() as sess:350 var = variables.Variable([1, 12])351 self.evaluate(variables.global_variables_initializer())352 self.assertAllClose([1, 12], self.evaluate(var))353 @test_util.run_v1_only("b/120545219")354 def testColocation(self):355 with ops.device("/job:ps"):356 var = variables.VariableV1(0, name="v")357 with ops.device("/job:worker/task:7"):358 assign_op = var.assign(1)359 self.assertDeviceEqual("/job:ps", assign_op.device)360 self.assertEqual([b"loc:@v"], assign_op.op.colocation_groups())361 @test_util.run_v1_only("b/120545219")362 def testInitializerFunction(self):363 value = [[-42], [133.7]]364 shape = [2, 1]365 with self.cached_session():366 initializer = lambda: constant_op.constant(value)367 v1 = variables.Variable(initializer, dtype=dtypes.float32)368 self.assertEqual(shape, v1.get_shape())369 self.assertEqual(shape, v1.shape)370 self.assertAllClose(value, self.evaluate(v1.initial_value))371 with self.assertRaises(errors_impl.FailedPreconditionError):372 self.evaluate(v1)373 v2 = variables.Variable(374 math_ops.negative(v1.initialized_value()), dtype=dtypes.float32)375 self.assertEqual(v1.get_shape(), v2.get_shape())376 self.assertEqual(v1.shape, v2.shape)377 self.assertAllClose(np.negative(value), self.evaluate(v2.initial_value))378 with self.assertRaises(errors_impl.FailedPreconditionError):379 self.evaluate(v2)380 self.evaluate(variables.global_variables_initializer())381 self.assertAllClose(np.negative(value), self.evaluate(v2))382 def testConstraintArg(self):383 constraint = lambda x: x384 v = variables.Variable(385 lambda: constant_op.constant(1.),386 constraint=constraint)387 self.assertEqual(v.constraint, constraint)388 constraint = 0389 with self.assertRaises(ValueError):390 v = variables.Variable(391 lambda: constant_op.constant(1.),392 constraint=constraint)393 @test_util.run_v1_only("b/120545219")394 def testNoRefDataRace(self):395 with self.cached_session():396 a = variables.Variable([1, 2, 3], dtype=dtypes.float32)397 b = variables.Variable(a.initialized_value() + 2)398 c = variables.Variable(b.initialized_value() + 2)399 self.evaluate(variables.global_variables_initializer())400 self.assertAllEqual(self.evaluate(a), [1, 2, 3])401 self.assertAllEqual(self.evaluate(b), [3, 4, 5])402 self.assertAllEqual(self.evaluate(c), [5, 6, 7])403 @test_util.run_deprecated_v1404 def testInitializerFunctionDevicePlacement(self):405 with self.cached_session():406 initializer = lambda: constant_op.constant(42.0)407 with ops.device("/cpu:100"):408 v1 = variables.Variable(initializer, dtype=dtypes.float32, name="v1")409 expected_device = "/device:CPU:100"410 expected_group_v1 = [b"loc:@v1"]411 self.assertEqual(expected_device, v1.op.device)412 self.assertEqual(expected_group_v1, v1.op.colocation_groups())413 for i in v1.initializer.inputs:414 self.assertEqual(expected_group_v1, i.op.colocation_groups())415 v2 = variables.Variable(initializer, dtype=dtypes.float32, name="v2")416 expected_group_v2 = [b"loc:@v2"]417 self.assertEqual(expected_group_v2, v2.op.colocation_groups())418 for i in v2.initializer.inputs:419 self.assertEqual(expected_group_v2, i.op.colocation_groups())420 @test_util.run_v1_only("b/120545219")421 def testVariableDefInitializedInstances(self):422 with ops.Graph().as_default(), self.cached_session() as sess:423 v_def = variables.Variable(424 initial_value=constant_op.constant(3.0)).to_proto()425 with ops.Graph().as_default(), self.cached_session() as sess:426 # v describes a VariableDef-based variable without an initial value.427 v = variables.Variable(variable_def=v_def)428 self.assertEqual(3.0, self.evaluate(v.initialized_value()))429 # initialized_value should not rerun the initializer_op if the variable430 # has already been initialized elsewhere.431 self.evaluate(v.assign(1.0))432 self.assertEqual(1.0, self.evaluate(v.initialized_value()))433 v_def.ClearField("initial_value_name")434 with ops.Graph().as_default(), self.cached_session() as sess:435 # Restoring a legacy VariableDef proto that does not have436 # initial_value_name set should still work.437 v = variables.Variable(variable_def=v_def)438 # We should also be able to re-export the variable to a new meta graph.439 self.assertProtoEquals(v_def, v.to_proto())440 # But attempts to use initialized_value will result in errors.441 with self.assertRaises(ValueError):442 self.evaluate(v.initialized_value())443 def testTrainableInProto(self):444 with ops.Graph().as_default():445 non_trainable_variable = variables.Variable(446 trainable=False,447 initial_value=constant_op.constant(10.0))448 self.assertEqual(449 False,450 variables.Variable(variable_def=non_trainable_variable.to_proto())451 .trainable)452 trainable_variable = variables.Variable(453 trainable=True,454 initial_value=constant_op.constant(10.0))455 self.assertEqual(456 True,457 variables.Variable(variable_def=trainable_variable.to_proto())458 .trainable)459 @test_util.run_deprecated_v1460 def testLoad(self):461 with self.cached_session():462 var = variables.Variable(np.zeros((5, 5), np.float32))463 self.evaluate(variables.global_variables_initializer())464 var.load(np.ones((5, 5), np.float32))465 self.assertAllClose(np.ones((5, 5), np.float32), self.evaluate(var))466 @test_util.run_v1_only("b/120545219")467 def testRepr(self):468 var = variables.VariableV1(np.zeros((5, 5), np.float32), name="noop")469 self.assertEqual(470 "<tf.Variable 'noop:0' shape=(5, 5) dtype=float32_ref>",471 repr(var))472 def testVariableNamesPreserveNameScopesWithDefun(self):473 @function.defun474 def create_variable():475 with ops.name_scope("foo"):476 v = variables.Variable(0.0, name="bar")477 self.assertEqual(v.name, "foo/bar:0")478 with ops.get_default_graph().as_default():479 create_variable()480class IsInitializedTest(test.TestCase):481 def testNoVars(self):482 with ops.Graph().as_default(), self.cached_session() as sess:483 uninited = variables.report_uninitialized_variables()484 self.assertEqual(0, self.evaluate(uninited).size)485 def testAssertVariablesInitialized(self):486 with ops.Graph().as_default(), self.cached_session() as sess:487 v = variables.Variable([1, 2], name="v")488 w = variables.Variable([3, 4], name="w")489 _ = v, w490 uninited = variables.report_uninitialized_variables()491 self.assertAllEqual(np.array([b"v", b"w"]), self.evaluate(uninited))492 self.evaluate(variables.global_variables_initializer())493 self.assertEqual(0, self.evaluate(uninited).size)494 @test_util.run_v1_only("b/120545219")495 def testVariableList(self):496 with ops.Graph().as_default(), self.cached_session() as sess:497 v = variables.VariableV1([1, 2], name="v")498 w = variables.VariableV1([3, 4], name="w")499 uninited = variables.report_uninitialized_variables()500 self.assertAllEqual(np.array([b"v", b"w"]), self.evaluate(uninited))501 self.evaluate(w.initializer)502 self.assertAllEqual(np.array([b"v"]), self.evaluate(uninited))503 v.initializer.run()504 self.assertEqual(0, self.evaluate(uninited).size)505 def testZeroSizeVarInitialized(self):506 with ops.Graph().as_default(), self.cached_session() as sess:507 v = variables.Variable(array_ops.zeros([0, 2]), name="v")508 uninited = variables.report_uninitialized_variables()509 v.initializer.run() # not strictly necessary510 self.assertEqual(0, self.evaluate(uninited).size)511 def testTrainingWithZeroSizeVar(self):512 with ops.Graph().as_default(), self.cached_session() as sess:513 a = variables.Variable(array_ops.zeros([0, 2]))514 b = variables.Variable(array_ops.ones([2, 2]))515 objective = math_ops.reduce_sum(b + math_ops.matmul(516 a, a, transpose_a=True))517 self.evaluate(variables.global_variables_initializer())518 do_opt = gradient_descent.GradientDescentOptimizer(0.1).minimize(519 objective)520 self.evaluate([do_opt])521 self.assertAllClose([[0.9, 0.9], [0.9, 0.9]], self.evaluate(b))522@test_util.run_v1_only("b/120545219")523class ObsoleteIsInitializedTest(test.TestCase):524 def testNoVars(self):525 with ops.Graph().as_default():526 self.assertEqual(None, variables.assert_variables_initialized())527 def testVariables(self):528 with ops.Graph().as_default(), self.cached_session() as sess:529 v = variables.VariableV1([1, 2])530 w = variables.VariableV1([3, 4])531 _ = v, w532 inited = variables.assert_variables_initialized()533 with self.assertRaisesOpError("Attempting to use uninitialized value"):534 self.evaluate(inited)535 self.evaluate(variables.global_variables_initializer())536 self.evaluate(inited)537 def testVariableList(self):538 with ops.Graph().as_default(), self.cached_session() as sess:539 v = variables.VariableV1([1, 2])540 w = variables.VariableV1([3, 4])541 inited = variables.assert_variables_initialized([v])542 with self.assertRaisesOpError("Attempting to use uninitialized value"):543 inited.op.run()544 self.evaluate(w.initializer)545 with self.assertRaisesOpError("Attempting to use uninitialized value"):546 inited.op.run()547 v.initializer.run()548 inited.op.run()549class PartitionedVariableTest(test.TestCase):550 def testPartitionedVariable(self):551 with ops.Graph().as_default():552 v0 = variables.Variable([0])553 v1 = variables.Variable([1])554 v0._set_save_slice_info(555 variables.Variable.SaveSliceInfo(v0.name, [2], [0], [1]))556 v1._set_save_slice_info(557 variables.Variable.SaveSliceInfo(v0.name, [2], [1], [1]))558 partitions = [2]559 # Pass variable_list as [v1, v0] to ensure they are properly560 # re-sorted to [v0, v1] based on their slice info offsets.561 partitioned_variable = variables.PartitionedVariable(562 name="two_vars",563 shape=[2],564 dtype=v0.dtype,565 variable_list=[v1, v0],566 partitions=partitions)567 concatenated = ops.convert_to_tensor(partitioned_variable)568 num_partitions = len(partitioned_variable)569 iterated_partitions = list(partitioned_variable)570 self.assertEqual(2, num_partitions)571 self.assertEqual([v0, v1], iterated_partitions)572 self.assertEqual([2], partitioned_variable.get_shape())573 self.assertEqual([2], partitioned_variable.shape)574 self.assertEqual([2], concatenated.get_shape())575 self.assertEqual([2], concatenated.shape)576 def testPartitionedVariableFailures(self):577 with ops.Graph().as_default():578 with self.assertRaisesRegexp(ValueError, "empty"):579 variables.PartitionedVariable(580 name="fail",581 shape=2,582 dtype=dtypes.int32,583 variable_list=[],584 partitions=[])585 with self.assertRaisesRegexp(ValueError, "must have a save_slice_info"):586 v0 = variables.Variable([0])587 partitions = [1]588 variables.PartitionedVariable(589 name="two_vars",590 shape=[1],591 dtype=v0.dtype,592 variable_list=[v0],593 partitions=partitions)594 with self.assertRaisesRegexp(ValueError, "full shapes must match"):595 v0 = variables.Variable([0])596 v1 = variables.Variable([1])597 v0._set_save_slice_info(598 variables.Variable.SaveSliceInfo(v0.name, [2], [0], [1]))599 v1._set_save_slice_info(600 variables.Variable.SaveSliceInfo(v0.name, [2], [1], [1]))601 partitions = [2]602 variables.PartitionedVariable(603 name="two_vars",604 shape=[3],605 dtype=v0.dtype,606 variable_list=[v1, v0],607 partitions=partitions)608 with self.assertRaisesRegexp(ValueError, "must be positive"):609 v0 = variables.Variable([0])610 v0._set_save_slice_info(611 variables.Variable.SaveSliceInfo(v0.name, [2], [0], [1]))612 partitions = [0]613 variables.PartitionedVariable(614 name="two_vars",615 shape=[2],616 dtype=v0.dtype,617 variable_list=[v0],618 partitions=partitions)619 def testPartitionedVariableAssignments(self):620 with ops.Graph().as_default(), self.cached_session():621 v0 = variables.Variable(initial_value=[0.0])622 v1 = variables.Variable(initial_value=[1.0])623 v2 = variables.Variable(initial_value=[20.0])624 v3 = variables.Variable(initial_value=[30.0])625 v0._set_save_slice_info(626 variables.Variable.SaveSliceInfo(v0.name, [2], [0], [1]))627 v1._set_save_slice_info(628 variables.Variable.SaveSliceInfo(v1.name, [2], [1], [1]))629 v2._set_save_slice_info(630 variables.Variable.SaveSliceInfo(v2.name, [2], [0], [1]))631 v3._set_save_slice_info(632 variables.Variable.SaveSliceInfo(v3.name, [2], [1], [1]))633 partitions = [2]634 # Pass variable_list as [v1, v0] to ensure they are properly635 # re-sorted to [v0, v1] based on their slice info offsets.636 pv_0 = variables.PartitionedVariable(637 name="two_vars",638 shape=[2],639 dtype=v0.dtype,640 variable_list=[v0, v1],641 partitions=partitions)642 pv_1 = variables.PartitionedVariable(643 name="two_vars",644 shape=[2],645 dtype=v0.dtype,646 variable_list=[v2, v3],647 partitions=partitions)648 deltas_a = constant_op.constant([1.0, 2.0])649 deltas_b = constant_op.constant([3.0, 4.0])650 ones = array_ops.ones([2])651 plus_delta = pv_0.assign_add(deltas_a)652 minus_delta = pv_0.assign_sub(deltas_b)653 assign_ones = pv_0.assign(ones)654 c_0 = constant_op.constant([2.0])655 c_1 = constant_op.constant([3.0])656 assign_list = pv_1.assign([c_0, c_1])657 assign_part_value = pv_1.assign_add(assign_ones)658 assign_part_var = pv_1.assign_sub(pv_0)659 self.evaluate(variables.global_variables_initializer())660 self.assertEqual([1.0], self.evaluate(plus_delta[0]))661 self.assertEqual([1.0], self.evaluate(v0))662 self.assertEqual([3.0], self.evaluate(plus_delta[1]))663 self.assertEqual([3.0], self.evaluate(v1))664 self.assertEqual([-2.0], self.evaluate(minus_delta[0]))665 self.assertEqual([-2.0], self.evaluate(v0))666 self.assertEqual([-1.0], self.evaluate(minus_delta[1]))667 self.assertEqual([-1.0], self.evaluate(v1))668 self.assertEqual([1.0], self.evaluate(assign_ones[0]))669 self.assertEqual([1.0], self.evaluate(v0))670 self.assertEqual([1.0], self.evaluate(assign_ones[1]))671 self.assertEqual([1.0], self.evaluate(v1))672 self.assertEqual([2.0], self.evaluate(assign_list[0]))673 self.assertEqual([2.0], self.evaluate(v2))674 self.assertEqual([3.0], self.evaluate(assign_list[1]))675 self.assertEqual([3.0], self.evaluate(v3))676 self.assertEqual([3.0], self.evaluate(assign_part_value[0]))677 self.assertEqual([3.0], self.evaluate(v2))678 self.assertEqual([4.0], self.evaluate(assign_part_value[1]))679 self.assertEqual([4.0], self.evaluate(v3))680 self.assertEqual([2.0], self.evaluate(assign_part_var[0]))681 self.assertEqual([2.0], self.evaluate(v2))682 self.assertEqual([3.0], self.evaluate(assign_part_var[1]))683 self.assertEqual([3.0], self.evaluate(v3))684class VariableContainerTest(test.TestCase):685 def testContainer(self):686 with ops.Graph().as_default():687 v0 = variables.Variable([0])688 with ops.container("l1"):689 v1 = variables.Variable([1])690 with ops.container("l2"):691 v2 = variables.Variable([2])692 special_v = gen_state_ops.variable(693 shape=[1],694 dtype=dtypes.float32,695 name="VariableInL3",696 container="l3",697 shared_name="")...
eval.py
Source:eval.py
...25 expr = Expression('x,y')26 self.assertEqual(hash(expr), hash(Expression('x,y')))27 self.assertNotEqual(hash(expr), hash(Expression('y, x')))28 def test_name_lookup(self):29 self.assertEqual('bar', Expression('foo').evaluate({'foo': 'bar'}))30 self.assertEqual(id, Expression('id').evaluate({}))31 self.assertEqual('bar', Expression('id').evaluate({'id': 'bar'}))32 self.assertEqual(None, Expression('id').evaluate({'id': None}))33 def test_builtins(self):34 expr = Expression('Markup')35 self.assertEqual(expr.evaluate({}), Markup)36 def test_str_literal(self):37 self.assertEqual('foo', Expression('"foo"').evaluate({}))38 self.assertEqual('foo', Expression('"""foo"""').evaluate({}))39 self.assertEqual('foo', Expression("'foo'").evaluate({}))40 self.assertEqual('foo', Expression("'''foo'''").evaluate({}))41 self.assertEqual('foo', Expression("u'foo'").evaluate({}))42 self.assertEqual('foo', Expression("r'foo'").evaluate({}))43 def test_str_literal_non_ascii(self):44 expr = Expression(u"u'\xfe'")45 self.assertEqual(u'þ', expr.evaluate({}))46 expr = Expression("u'\xfe'")47 self.assertEqual(u'þ', expr.evaluate({}))48 expr = Expression("'\xc3\xbe'")49 self.assertEqual(u'þ', expr.evaluate({}))50 def test_num_literal(self):51 self.assertEqual(42, Expression("42").evaluate({}))52 self.assertEqual(42L, Expression("42L").evaluate({}))53 self.assertEqual(.42, Expression(".42").evaluate({}))54 self.assertEqual(07, Expression("07").evaluate({}))55 self.assertEqual(0xF2, Expression("0xF2").evaluate({}))56 self.assertEqual(0XF2, Expression("0XF2").evaluate({}))57 def test_dict_literal(self):58 self.assertEqual({}, Expression("{}").evaluate({}))59 self.assertEqual({'key': True},60 Expression("{'key': value}").evaluate({'value': True}))61 def test_list_literal(self):62 self.assertEqual([], Expression("[]").evaluate({}))63 self.assertEqual([1, 2, 3], Expression("[1, 2, 3]").evaluate({}))64 self.assertEqual([True],65 Expression("[value]").evaluate({'value': True}))66 def test_tuple_literal(self):67 self.assertEqual((), Expression("()").evaluate({}))68 self.assertEqual((1, 2, 3), Expression("(1, 2, 3)").evaluate({}))69 self.assertEqual((True,),70 Expression("(value,)").evaluate({'value': True}))71 def test_unaryop_pos(self):72 self.assertEqual(1, Expression("+1").evaluate({}))73 self.assertEqual(1, Expression("+x").evaluate({'x': 1}))74 def test_unaryop_neg(self):75 self.assertEqual(-1, Expression("-1").evaluate({}))76 self.assertEqual(-1, Expression("-x").evaluate({'x': 1}))77 def test_unaryop_not(self):78 self.assertEqual(False, Expression("not True").evaluate({}))79 self.assertEqual(False, Expression("not x").evaluate({'x': True}))80 def test_unaryop_inv(self):81 self.assertEqual(-2, Expression("~1").evaluate({}))82 self.assertEqual(-2, Expression("~x").evaluate({'x': 1}))83 def test_binop_add(self):84 self.assertEqual(3, Expression("2 + 1").evaluate({}))85 self.assertEqual(3, Expression("x + y").evaluate({'x': 2, 'y': 1}))86 def test_binop_sub(self):87 self.assertEqual(1, Expression("2 - 1").evaluate({}))88 self.assertEqual(1, Expression("x - y").evaluate({'x': 1, 'y': 1}))89 def test_binop_sub(self):90 self.assertEqual(1, Expression("2 - 1").evaluate({}))91 self.assertEqual(1, Expression("x - y").evaluate({'x': 2, 'y': 1}))92 def test_binop_mul(self):93 self.assertEqual(4, Expression("2 * 2").evaluate({}))94 self.assertEqual(4, Expression("x * y").evaluate({'x': 2, 'y': 2}))95 def test_binop_pow(self):96 self.assertEqual(4, Expression("2 ** 2").evaluate({}))97 self.assertEqual(4, Expression("x ** y").evaluate({'x': 2, 'y': 2}))98 def test_binop_div(self):99 self.assertEqual(2, Expression("4 / 2").evaluate({}))100 self.assertEqual(2, Expression("x / y").evaluate({'x': 4, 'y': 2}))101 def test_binop_floordiv(self):102 self.assertEqual(1, Expression("3 // 2").evaluate({}))103 self.assertEqual(1, Expression("x // y").evaluate({'x': 3, 'y': 2}))104 def test_binop_mod(self):105 self.assertEqual(1, Expression("3 % 2").evaluate({}))106 self.assertEqual(1, Expression("x % y").evaluate({'x': 3, 'y': 2}))107 def test_binop_and(self):108 self.assertEqual(0, Expression("1 & 0").evaluate({}))109 self.assertEqual(0, Expression("x & y").evaluate({'x': 1, 'y': 0}))110 def test_binop_or(self):111 self.assertEqual(1, Expression("1 | 0").evaluate({}))112 self.assertEqual(1, Expression("x | y").evaluate({'x': 1, 'y': 0}))113 def test_binop_xor(self):114 self.assertEqual(1, Expression("1 ^ 0").evaluate({}))115 self.assertEqual(1, Expression("x ^ y").evaluate({'x': 1, 'y': 0}))116 def test_binop_contains(self):117 self.assertEqual(True, Expression("1 in (1, 2, 3)").evaluate({}))118 self.assertEqual(True, Expression("x in y").evaluate({'x': 1,119 'y': (1, 2, 3)}))120 def test_binop_not_contains(self):121 self.assertEqual(True, Expression("4 not in (1, 2, 3)").evaluate({}))122 self.assertEqual(True, Expression("x not in y").evaluate({'x': 4,123 'y': (1, 2, 3)}))124 def test_binop_is(self):125 self.assertEqual(True, Expression("1 is 1").evaluate({}))126 self.assertEqual(True, Expression("x is y").evaluate({'x': 1, 'y': 1}))127 self.assertEqual(False, Expression("1 is 2").evaluate({}))128 self.assertEqual(False, Expression("x is y").evaluate({'x': 1, 'y': 2}))129 def test_binop_is_not(self):130 self.assertEqual(True, Expression("1 is not 2").evaluate({}))131 self.assertEqual(True, Expression("x is not y").evaluate({'x': 1,132 'y': 2}))133 self.assertEqual(False, Expression("1 is not 1").evaluate({}))134 self.assertEqual(False, Expression("x is not y").evaluate({'x': 1,135 'y': 1}))136 def test_boolop_and(self):137 self.assertEqual(False, Expression("True and False").evaluate({}))138 self.assertEqual(False, Expression("x and y").evaluate({'x': True,139 'y': False}))140 def test_boolop_or(self):141 self.assertEqual(True, Expression("True or False").evaluate({}))142 self.assertEqual(True, Expression("x or y").evaluate({'x': True,143 'y': False}))144 def test_compare_eq(self):145 self.assertEqual(True, Expression("1 == 1").evaluate({}))146 self.assertEqual(True, Expression("x == y").evaluate({'x': 1, 'y': 1}))147 def test_compare_ne(self):148 self.assertEqual(False, Expression("1 != 1").evaluate({}))149 self.assertEqual(False, Expression("x != y").evaluate({'x': 1, 'y': 1}))150 self.assertEqual(False, Expression("1 <> 1").evaluate({}))151 self.assertEqual(False, Expression("x <> y").evaluate({'x': 1, 'y': 1}))152 def test_compare_lt(self):153 self.assertEqual(True, Expression("1 < 2").evaluate({}))154 self.assertEqual(True, Expression("x < y").evaluate({'x': 1, 'y': 2}))155 def test_compare_le(self):156 self.assertEqual(True, Expression("1 <= 1").evaluate({}))157 self.assertEqual(True, Expression("x <= y").evaluate({'x': 1, 'y': 1}))158 def test_compare_gt(self):159 self.assertEqual(True, Expression("2 > 1").evaluate({}))160 self.assertEqual(True, Expression("x > y").evaluate({'x': 2, 'y': 1}))161 def test_compare_ge(self):162 self.assertEqual(True, Expression("1 >= 1").evaluate({}))163 self.assertEqual(True, Expression("x >= y").evaluate({'x': 1, 'y': 1}))164 def test_compare_multi(self):165 self.assertEqual(True, Expression("1 != 3 == 3").evaluate({}))166 self.assertEqual(True, Expression("x != y == y").evaluate({'x': 1,167 'y': 3}))168 def test_call_function(self):169 self.assertEqual(42, Expression("foo()").evaluate({'foo': lambda: 42}))170 data = {'foo': 'bar'}171 self.assertEqual('BAR', Expression("foo.upper()").evaluate(data))172 data = {'foo': {'bar': range(42)}}173 self.assertEqual(42, Expression("len(foo.bar)").evaluate(data))174 def test_call_keywords(self):175 self.assertEqual(42, Expression("foo(x=bar)").evaluate({'foo': lambda x: x,176 'bar': 42}))177 def test_call_star_args(self):178 self.assertEqual(42, Expression("foo(*bar)").evaluate({'foo': lambda x: x,179 'bar': [42]}))180 def test_call_dstar_args(self):181 def foo(x):182 return x183 expr = Expression("foo(**bar)")184 self.assertEqual(42, expr.evaluate({'foo': foo, 'bar': {"x": 42}}))185 def test_lambda(self):186 # Define a custom `sorted` function cause the builtin isn't available187 # on Python 2.3188 def sorted(items, compfunc):189 items.sort(compfunc)190 return items191 data = {'items': [{'name': 'b', 'value': 0}, {'name': 'a', 'value': 1}],192 'sorted': sorted}193 expr = Expression("sorted(items, lambda a, b: cmp(a.name, b.name))")194 self.assertEqual([{'name': 'a', 'value': 1}, {'name': 'b', 'value': 0}],195 expr.evaluate(data))196 def test_list_comprehension(self):197 expr = Expression("[n for n in numbers if n < 2]")198 self.assertEqual([0, 1], expr.evaluate({'numbers': range(5)}))199 expr = Expression("[(i, n + 1) for i, n in enumerate(numbers)]")200 self.assertEqual([(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)],201 expr.evaluate({'numbers': range(5)}))202 expr = Expression("[offset + n for n in numbers]")203 self.assertEqual([2, 3, 4, 5, 6],204 expr.evaluate({'numbers': range(5), 'offset': 2}))205 def test_list_comprehension_with_getattr(self):206 items = [{'name': 'a', 'value': 1}, {'name': 'b', 'value': 2}]207 expr = Expression("[i.name for i in items if i.value > 1]")208 self.assertEqual(['b'], expr.evaluate({'items': items}))209 def test_list_comprehension_with_getitem(self):210 items = [{'name': 'a', 'value': 1}, {'name': 'b', 'value': 2}]211 expr = Expression("[i['name'] for i in items if i['value'] > 1]")212 self.assertEqual(['b'], expr.evaluate({'items': items}))213 if sys.version_info >= (2, 4):214 # Generator expressions only supported in Python 2.4 and up215 def test_generator_expression(self):216 expr = Expression("list(n for n in numbers if n < 2)")217 self.assertEqual([0, 1], expr.evaluate({'numbers': range(5)}))218 expr = Expression("list((i, n + 1) for i, n in enumerate(numbers))")219 self.assertEqual([(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)],220 expr.evaluate({'numbers': range(5)}))221 expr = Expression("list(offset + n for n in numbers)")222 self.assertEqual([2, 3, 4, 5, 6],223 expr.evaluate({'numbers': range(5), 'offset': 2}))224 def test_generator_expression_with_getattr(self):225 items = [{'name': 'a', 'value': 1}, {'name': 'b', 'value': 2}]226 expr = Expression("list(i.name for i in items if i.value > 1)")227 self.assertEqual(['b'], expr.evaluate({'items': items}))228 def test_generator_expression_with_getitem(self):229 items = [{'name': 'a', 'value': 1}, {'name': 'b', 'value': 2}]230 expr = Expression("list(i['name'] for i in items if i['value'] > 1)")231 self.assertEqual(['b'], expr.evaluate({'items': items}))232 if sys.version_info >= (2, 5):233 def test_conditional_expression(self):234 expr = Expression("'T' if foo else 'F'")235 self.assertEqual('T', expr.evaluate({'foo': True}))236 self.assertEqual('F', expr.evaluate({'foo': False}))237 def test_slice(self):238 expr = Expression("numbers[0:2]")239 self.assertEqual([0, 1], expr.evaluate({'numbers': range(5)}))240 def test_slice_with_vars(self):241 expr = Expression("numbers[start:end]")242 self.assertEqual([0, 1], expr.evaluate({'numbers': range(5), 'start': 0,243 'end': 2}))244 def test_slice_copy(self):245 expr = Expression("numbers[:]")246 self.assertEqual([0, 1, 2, 3, 4], expr.evaluate({'numbers': range(5)}))247 def test_slice_stride(self):248 expr = Expression("numbers[::stride]")249 self.assertEqual([0, 2, 4], expr.evaluate({'numbers': range(5),250 'stride': 2}))251 def test_slice_negative_start(self):252 expr = Expression("numbers[-1:]")253 self.assertEqual([4], expr.evaluate({'numbers': range(5)}))254 def test_slice_negative_end(self):255 expr = Expression("numbers[:-1]")256 self.assertEqual([0, 1, 2, 3], expr.evaluate({'numbers': range(5)}))257 def test_access_undefined(self):258 expr = Expression("nothing", filename='index.html', lineno=50)259 retval = expr.evaluate({})260 assert isinstance(retval, Undefined)261 self.assertEqual('nothing', retval._name)262 assert retval._owner is UNDEFINED263 def test_getattr_undefined(self):264 class Something(object):265 def __repr__(self):266 return '<Something>'267 something = Something()268 expr = Expression('something.nil', filename='index.html', lineno=50)269 retval = expr.evaluate({'something': something})270 assert isinstance(retval, Undefined)271 self.assertEqual('nil', retval._name)272 assert retval._owner is something273 def test_getattr_exception(self):274 class Something(object):275 def prop(self):276 raise NotImplementedError277 prop = property(prop)278 self.assertRaises(NotImplementedError,279 Expression('s.prop').evaluate, {'s': Something()})280 def test_getitem_undefined_string(self):281 class Something(object):282 def __repr__(self):283 return '<Something>'284 something = Something()285 expr = Expression('something["nil"]', filename='index.html', lineno=50)286 retval = expr.evaluate({'something': something})287 assert isinstance(retval, Undefined)288 self.assertEqual('nil', retval._name)289 assert retval._owner is something290 def test_getitem_exception(self):291 class Something(object):292 def __getitem__(self, key):293 raise NotImplementedError294 self.assertRaises(NotImplementedError,295 Expression('s["foo"]').evaluate, {'s': Something()})296 def test_error_access_undefined(self):297 expr = Expression("nothing", filename='index.html', lineno=50,298 lookup='strict')299 try:300 expr.evaluate({})301 self.fail('Expected UndefinedError')302 except UndefinedError, e:303 exc_type, exc_value, exc_traceback = sys.exc_info()304 frame = exc_traceback.tb_next305 frames = []306 while frame.tb_next:307 frame = frame.tb_next308 frames.append(frame)309 self.assertEqual('"nothing" not defined', str(e))310 self.assertEqual("<Expression 'nothing'>",311 frames[-3].tb_frame.f_code.co_name)312 self.assertEqual('index.html',313 frames[-3].tb_frame.f_code.co_filename)314 self.assertEqual(50, frames[-3].tb_lineno)315 def test_error_getattr_undefined(self):316 class Something(object):317 def __repr__(self):318 return '<Something>'319 expr = Expression('something.nil', filename='index.html', lineno=50,320 lookup='strict')321 try:322 expr.evaluate({'something': Something()})323 self.fail('Expected UndefinedError')324 except UndefinedError, e:325 self.assertEqual('<Something> has no member named "nil"', str(e))326 exc_type, exc_value, exc_traceback = sys.exc_info()327 search_string = "<Expression 'something.nil'>"328 frame = exc_traceback.tb_next329 while frame.tb_next:330 frame = frame.tb_next331 code = frame.tb_frame.f_code332 if code.co_name == search_string:333 break334 else:335 self.fail("never found the frame I was looking for")336 self.assertEqual('index.html', code.co_filename)337 self.assertEqual(50, frame.tb_lineno)338 def test_error_getitem_undefined_string(self):339 class Something(object):340 def __repr__(self):341 return '<Something>'342 expr = Expression('something["nil"]', filename='index.html', lineno=50,343 lookup='strict')344 try:345 expr.evaluate({'something': Something()})346 self.fail('Expected UndefinedError')347 except UndefinedError, e:348 self.assertEqual('<Something> has no member named "nil"', str(e))349 exc_type, exc_value, exc_traceback = sys.exc_info()350 search_string = '''<Expression 'something["nil"]'>'''351 frame = exc_traceback.tb_next352 while frame.tb_next:353 frame = frame.tb_next354 code = frame.tb_frame.f_code355 if code.co_name == search_string:356 break357 else:358 self.fail("never found the frame I was looking for")359 self.assertEqual('index.html', code.co_filename)...
unittests.py
Source:unittests.py
...7 return get_coref_infos('tests/%s' % key, 'tests/%s' % response,8 False, False, True)9def test_A1():10 doc = read('TC-A.key', 'TC-A-1.response')11 assert evaluate(doc, muc) == (1, 1, 1)12 assert evaluate(doc, b_cubed) == (1, 1, 1)13 assert evaluate(doc, ceafe) == (1, 1, 1)14 assert evaluate(doc, ceafm) == (1, 1, 1)15 assert evaluate(doc, lea) == (1, 1, 1)16 assert evaluate(doc, [blancc,blancn]) == (1,1,1)17def test_A2():18 doc = read('TC-A.key', 'TC-A-2.response')19 assert evaluate(doc, muc) == approx([1 / 3, 1 / 1, 1 / 2])20 assert evaluate(doc, b_cubed) == approx([(7 / 3) / 6, 3 / 3, 14 / 25])21 assert evaluate(doc, ceafe) == approx([0.6, 0.9, 0.72])22 assert evaluate(doc, ceafm) == approx([0.5, 1, 0.66667], abs=TOL)23 assert evaluate(doc, lea) == approx([(1 + 3 * (1 / 3)) / 6, 1, 0.5])24 assert evaluate(doc, [blancc,blancn]) == approx([0.21591, 1, 0.35385], abs=TOL)25def test_A3():26 doc = read('TC-A.key', 'TC-A-3.response')27 assert evaluate(doc, muc) == approx([3 / 3, 3 / 5, 0.75])28 assert evaluate(doc,29 b_cubed) == approx([6 / 6, (4 + 7 / 12) / 9, 110 / 163])30 assert evaluate(doc, ceafe) == approx([0.88571, 0.66429, 0.75918], abs=TOL)31 assert evaluate(doc, lea) == approx([32 1, (1 + 3 * (1 / 3) + 4 * (3 / 6)) / 9,33 2 * (1 + 3 * (1 / 3) + 434 * (3 / 6)) / 9 / (1 + (1 + 3 * (1 / 3) + 4 * (3 / 6)) / 9)35 ])36 assert evaluate(doc, ceafm) == approx([1, 0.66667, 0.8], abs=TOL)37 assert evaluate(doc, [blancc, blancn]) == approx([1, 0.42593, 0.59717], abs=TOL)38def test_A4():39 doc = read('TC-A.key', 'TC-A-4.response')40 assert evaluate(doc, muc) == approx([1 / 3, 1 / 3, 1 / 3])41 assert evaluate(doc, b_cubed) == approx([42 (3 + 1 / 3) / 6, (1 + 4 / 3 + 1 / 2) / 7,43 2 * (5 / 9) * (17 / 42) / ((5 / 9) + (17 / 42))44 ])45 assert evaluate(doc, ceafe) == approx([0.73333, 0.55, 0.62857], abs=TOL)46 assert evaluate(doc, lea) == approx([(1 + 2 + 0) / 6,47 (1 + 3 * (1 / 3) + 2 * 0 + 0) / 7,48 2 * 0.5 * 2 / 7 / (0.5 + 2 / 7)])49 assert evaluate(doc, ceafm) == approx([0.66667, 0.57143, 0.61538], abs=TOL)50 assert evaluate(doc, [blancc, blancn]) == approx([0.35227, 0.27206, 0.30357], abs=TOL)51def test_A5():52 doc = read('TC-A.key', 'TC-A-5.response')53 assert evaluate(doc, muc) == approx([1 / 3, 1 / 4, 2 / 7])54 assert evaluate(doc, b_cubed) == approx([55 (3 + 1 / 3) / 6, 2.5 / 8,56 2 * (5 / 9) * (5 / 16) / ((5 / 9) + (5 / 16))57 ])58 assert evaluate(doc, ceafe) == approx([0.68889, 0.51667, 0.59048], abs=TOL)59 assert evaluate(doc,60 lea) == approx([(1 + 2 + 3 * 0) / 6,61 (1 + 4 * (1 / 6) + 2 * 0 + 1 * 0) / 8,62 2 * 0.5 * (5 / 24) / (0.5 + (5 / 24))])63 assert evaluate(doc, ceafm) == approx([0.66667, 0.5, 0.57143], abs=TOL)64 assert evaluate(doc, [blancc, blancn]) == approx([0.35227, 0.19048, 0.24716], abs=TOL)65def test_A6():66 doc = read('TC-A.key', 'TC-A-6.response')67 assert evaluate(doc, muc) == approx([1 / 3, 1 / 4, 2 / 7])68 assert evaluate(doc, b_cubed) == approx([69 (10 / 3) / 6, (1 + 4 / 3 + 1 / 2) / 8,70 2 * (5 / 9) * (17 / 48) / ((5 / 9) + (17 / 48))71 ])72 assert evaluate(doc, ceafe) == approx([0.73333, 0.55, 0.62857], abs=TOL)73 assert evaluate(doc, lea) == approx([(1 + 2 + 3 * 0) / 6,74 (1 + 3 / 3 + 2 * 0 + 2 * 0) / 8,75 2 * 0.5 * 1 / 4 / (0.5 + 1 / 4)])76 assert evaluate(doc, ceafm) == approx([0.66667, 0.5, 0.57143], abs=TOL)77 assert evaluate(doc, [blancc, blancn]) == approx([0.35227, 0.20870, 0.25817], abs=TOL)78def test_A7():79 doc = read('TC-A.key', 'TC-A-7.response')80 assert evaluate(doc, muc) == approx([1 / 3, 1 / 3, 1 / 3])81 assert evaluate(doc, b_cubed) == approx([82 (10 / 3) / 6, (1 + 4 / 3 + 1 / 2) / 7,83 2 * (5 / 9) * (17 / 42) / ((5 / 9) + (17 / 42))84 ])85 assert evaluate(doc, ceafe) == approx([0.73333, 0.55, 0.62857], abs=TOL)86 assert evaluate(doc, lea) == approx([(1 + 2 + 3 * 0) / 6,87 (1 + 3 / 3 + 2 * 0 + 1 * 0) / 7,88 2 * 0.5 * 2 / 7 / (0.5 + 2 / 7)])89 assert evaluate(doc, ceafm) == approx([0.66667, 0.57143, 0.61538], abs=TOL)90 assert evaluate(doc, [blancc, blancn]) == approx([0.35227, 0.27206, 0.30357], abs=TOL)91def test_A10():92 doc = read('TC-A.key', 'TC-A-10.response')93 assert evaluate(doc, muc) == approx([0, 0, 0])94 assert evaluate(doc, b_cubed) == approx([3 / 6, 6 / 6, 2 / 3])95 assert evaluate(doc, lea) == approx(96 [1 / 6, 1 / 6, 2 * 1 / 6 * 1 / 6 / (1 / 6 + 1 / 6)])97 assert evaluate(doc, [blancc, blancn]) == approx([0.5, 0.36667, 0.42308], abs=TOL)98def test_A11():99 doc = read('TC-A.key', 'TC-A-11.response')100 assert evaluate(doc, muc) == approx([3 / 3, 3 / 5, 6 / 8])101 assert evaluate(doc, b_cubed) == approx(102 [6 / 6, (1 / 6 + 2 * 2 / 6 + 3 * 3 / 6) / 6, 14 / 25])103 assert evaluate(doc,104 lea) == approx([(0 + 2 + 3) / 6, 4 / 15,105 2 * 5 / 6 * 4 / 15 / (5 / 6 + 4 / 15)])106 assert evaluate(doc, [blancc, blancn]) == approx([0.5, 0.13333, 0.21053], abs=TOL)107def test_A12():108 doc = read('TC-A.key', 'TC-A-12.response')109 assert evaluate(doc, muc) == approx([0, 0, 0])110 assert evaluate(doc, b_cubed) == approx([111 (1 + 1 / 2 + 2 / 3) / 6, 4 / 7,112 2 * (13 / 36) * (4 / 7) / ((13 / 36) + (4 / 7))113 ])114 assert evaluate(doc, lea) == approx(115 [1 / 6, 1 / 7, 2 * 1 / 6 * 1 / 7 / (1 / 6 + 1 / 7)])116 assert evaluate(doc, [blancc, blancn]) == approx([0.22727, 0.11905, 0.15625], abs=TOL)117def test_A13():118 doc = read('TC-A.key', 'TC-A-13.response')119 assert evaluate(doc, muc) == approx([1 / 3, 1 / 6, 2 / 9])120 assert evaluate(doc, b_cubed) == approx([121 (1 + 1 / 2 + 2 * 2 / 3) / 6, (1 / 7 + 1 / 7 + 2 * 2 / 7) / 7,122 2 * (17 / 36) * (6 / 49) / ((17 / 36) + (6 / 49))123 ])124 assert evaluate(doc,125 lea) == approx([(1 * 0 + 2 * 0 + 3 / 3) / 6, 1 / 21,126 2 * 1 / 6 * 1 / 21 / (1 / 6 + 1 / 21)])127 assert evaluate(doc, [blancc, blancn]) == approx([0.125, 0.02381, 0.04], abs=TOL)128def test_B1():129 doc = read('TC-B.key', 'TC-B-1.response')130 assert evaluate(doc, lea) == approx([(2 * 0 + 3 / 3) / 5, (3 * 0 + 2) / 5,131 2 * 1 / 5 * 2 / 5 / (1 / 5 + 2 / 5)])132 assert evaluate(doc, [blancc, blancn]) == approx([1/2 * (1/4 + 1/3), 1/2 * (1/4 + 1/3), 1/2 * (1/4 + 1/3)])133def test_C1():134 doc = read('TC-C.key', 'TC-C-1.response')135 assert evaluate(doc, lea) == approx([(2 * 0 + 3 / 3 + 2) / 7,136 (3 * 0 + 2 + 2) / 7,137 2 * 3 / 7 * 4 / 7 / (3 / 7 + 4 / 7)])138 assert evaluate(doc, [blancc, blancn]) == approx([1/2 * (2/5 + 10/16), 1/2 * (2/5 + 10/16), 1/2 * (2/5 + 10/16)])139def test_D1():140 doc = read('TC-D.key', 'TC-D-1.response')141 assert evaluate(doc, muc) == approx(142 [9 / 9, 9 / 10, 2 * (9 / 9) * (9 / 10) / (9 / 9 + 9 / 10)])143 assert evaluate(doc, b_cubed) == approx([144 12 / 12, 16 / 21, 2 * (12 / 12) * (16 / 21) / (12 / 12 + 16 / 21)145 ])146 assert evaluate(doc, lea) == approx([147 (5 + 2 + 5) / 12, (5 + 7 * (11 / 21)) / 12,148 2 * 1 * (5 + 77 / 21) / 12 / (1 + ((5 + 77 / 21) / 12))149 ])150def test_E1():151 doc = read('TC-E.key', 'TC-E-1.response')152 assert evaluate(doc, muc) == approx(153 [9 / 9, 9 / 10, 2 * (9 / 9) * (9 / 10) / (9 / 9 + 9 / 10)])154 assert evaluate(doc, b_cubed) == approx(155 [1, 7 / 12, 2 * 1 * (7 / 12) / (1 + 7 / 12)])156 assert evaluate(doc, lea) == approx([(5 + 2 + 5) / 12,157 (10 * (20 / 45) + 2) / 12,158 2 * 1 * ((10 * (20 / 45) + 2) / 12)159 / (1 + ((10 * (20 / 45) + 2) / 12))])160def test_F1():161 doc = read('TC-F.key', 'TC-F-1.response')162 assert evaluate(doc, muc) == approx(163 [2 / 3, 2 / 2, 2 * (2 / 3) * (2 / 2) / (2 / 3 + 2 / 2)])164 assert evaluate(doc, lea) == approx(165 [4 * (2 / 6) / 4, (2 + 2) / 4, 2 * 2 / 6 * 1 / (1 + 2 / 6)])166def test_G1():167 doc = read('TC-G.key', 'TC-G-1.response')168 assert evaluate(doc, muc) == approx(169 [2 / 2, 2 / 3, 2 * (2 / 2) * (2 / 3) / (2 / 2 + 2 / 3)])170 assert evaluate(doc, lea) == approx(171 [1, (4 * 2 / 6) / 4, 2 * 1 * 2 / 6 / (1 + 2 / 6)])172def test_H1():173 doc = read('TC-H.key', 'TC-H-1.response')174 assert evaluate(doc, muc) == approx([1, 1, 1])175 assert evaluate(doc, lea) == approx([1, 1, 1])176def test_I1():177 doc = read('TC-I.key', 'TC-I-1.response')178 assert evaluate(doc, muc) == approx(179 [2 / 3, 2 / 2, 2 * (2 / 3) * (2 / 2) / (2 / 3 + 2 / 2)])180 assert evaluate(doc, lea) == approx(181 [4 * (2 / 6) / 4, (2 + 2) / 4, 2 * 2 / 6 * 1 / (2 / 6 + 1)])182def test_J1():183 doc = read('TC-J.key', 'TC-J-1.response')184 assert evaluate(doc, muc) == approx(185 [1 / 2, 1 / 1, 2 * (1 / 2) * (1 / 1) / (1 / 2 + 1 / 1)])186 assert evaluate(doc, lea) == approx([(3 * 1 / 3) / 3, 1,187 2 * 1 / 3 / (1 + 1 / 3)])188def test_K1():189 doc = read('TC-K.key', 'TC-K-1.response')190 assert evaluate(doc, muc) == approx([3 / 6, 3 / 6, 3 / 6])191 assert evaluate(doc,192 lea) == approx([(7 * (1 + 1 + 1) / 21) / 7,193 (3 / 3 + 3 / 3 + 3 / 3) / 9,194 2 * 3 / 21 * 3 / 9 / (3 / 21 + 3 / 9)])195def test_L1():196 doc = read('TC-L.key', 'TC-L-1.response')197 assert evaluate(doc, muc) == approx(198 [2 / 5, 2 / 4, 2 * (2 / 5) * (2 / 4) / (2 / 5 + 2 / 4)])199 assert evaluate(doc, lea) == approx([200 (3 * 1 / 3 + 4 * 1 / 6) / 7, (2 + 2 * 0 + 3 / 3) / 7,201 2 * (1 + 2 / 3) / 7 * 3 / 7 / (3 / 7 + (1 + 2 / 3) / 7)202 ])203def test_M1():204 doc = read('TC-M.key', 'TC-M-1.response')205 assert evaluate(doc, muc) == approx([1, 1, 1])206 assert evaluate(doc, b_cubed) == approx([1, 1, 1])207 assert evaluate(doc, ceafe) == approx([1, 1, 1])208 assert evaluate(doc, lea) == approx([1, 1, 1])209 assert evaluate(doc, ceafm) == approx([1, 1, 1])210 assert evaluate(doc, [blancc, blancn]) == approx([1, 1, 1])211def test_M2():212 doc = read('TC-M.key', 'TC-M-2.response')213 assert evaluate(doc, muc) == approx([0, 0, 0])214 assert evaluate(doc, lea) == approx([0, 0, 0])215 assert evaluate(doc, [blancc, blancn]) == approx([0, 0, 0])216def test_M3():217 doc = read('TC-M.key', 'TC-M-3.response')218 assert evaluate(doc, lea) == approx([219 6 * (4 / 15) / 6, (2 + 3 + 0) / 6,220 2 * 4 / 15 * 5 / 6 / (4 / 15 + 5 / 6)221 ])222 #the original is wrong as the |N_r| != 0223 #assert evaluate(doc, [blancc, blancn]) == approx([0.26667, 1, 0.42105], abs=TOL)224 assert evaluate(doc, [blancc, blancn]) == approx([0.26667/2, 1/2, 0.42105/2], abs=TOL)225def test_M4():226 doc = read('TC-M.key', 'TC-M-4.response')227 assert evaluate(doc, lea) == approx([228 6 * (3 / 15) / 6, 6 * (3 / 15) / 6,229 2 * 3 / 15 * 3 / 15 / (3 / 15 + 3 / 15)230 ])231 assert evaluate(doc, [blancc, blancn]) == approx([0.2, 0.2, 0.2])232def test_M5():233 doc = read('TC-M.key', 'TC-M-5.response')234 assert evaluate(doc, muc) == approx([0, 0, 0])235 assert evaluate(doc, lea) == approx([0, 0, 0])236 assert evaluate(doc, [blancc, blancn]) == approx([0, 0, 0])237def test_M6():238 doc = read('TC-M.key', 'TC-M-6.response')239 assert evaluate(doc, lea) == approx([240 6 * (1 / 15) / 6, (2 + 3 * 0 + 1 * 0) / 6,241 2 * 1 / 15 * 2 / 6 / (1 / 15 + 2 / 6)242 ])243 # the original is wrong as the |N_r| != 0244 # assert evaluate(doc, [blancc, blancn]) == approx([0.06667, 0.25, 0.10526], abs=TOL)245 assert evaluate(doc, [blancc, blancn]) == approx([0.06667/2, 0.25/2, 0.10526/2], abs=TOL)246def test_N1():247 doc = read('TC-N.key', 'TC-N-1.response')248 assert evaluate(doc, muc) == approx([0, 0, 0])249 assert evaluate(doc, lea) == approx([1, 1, 1])250 assert evaluate(doc, [blancc, blancn]) == approx([1, 1, 1])251def test_N2():252 doc = read('TC-N.key', 'TC-N-2.response')253 assert evaluate(doc, muc) == approx([0, 0, 0])254 assert evaluate(doc, lea) == approx([0, 0, 0])255 assert evaluate(doc, [blancc, blancn]) == approx([0, 0, 0])256def test_N3():257 doc = read('TC-N.key', 'TC-N-3.response')258 assert evaluate(doc, lea) == approx([1 / 6, 1 / 6, 1 / 6])259 # the original is wrong as the |C_r| != 0260 # assert evaluate(doc, [blancc, blancn]) == approx([0.73333, 1, 0.84615], abs=TOL)261 assert evaluate(doc, [blancc, blancn]) == approx([0.73333/2, 1/2, 0.84615/2], abs=TOL)262def test_N4():263 doc = read('TC-N.key', 'TC-N-4.response')264 assert evaluate(doc, muc) == approx([0, 0, 0])265 assert evaluate(doc, lea) == approx([3 / 6, 3 / 6, 3 / 6])266 assert evaluate(doc, [blancc, blancn]) == approx([0.2, 0.2, 0.2])267def test_N5():268 doc = read('TC-N.key', 'TC-N-5.response')269 assert evaluate(doc, lea) == approx([0, 0, 0])270 assert evaluate(doc, [blancc, blancn]) == approx([0, 0, 0])271def test_N6():272 doc = read('TC-N.key', 'TC-N-6.response')273 assert evaluate(doc, lea) == approx([0, 0, 0])274 # the original is wrong as the |C_r| != 0275 # assert evaluate(doc, [blancc, blancn]) == approx([0.13333, 0.18182, 0.15385], abs=TOL)...
clean_string.py
Source:clean_string.py
1from __future__ import unicode_literals2from __future__ import division3import re4import math5import random6import simple_math7def parse_string(input):8 """Checks for special case of to fraction and otherwise sends9 the input to be cleaned in this module and processed with Pyparsing.10 """11 to_fraction = False12 if '>Frac' in input:13 to_fraction = True14 input = clean_string(input)15 simple_math.BNF().parseString(input)16 output = clean_output(to_fraction)17 return output18def clean_output(to_fraction):19 """Checks for special cases of returning a fraction or needing20 to return scientific notation and returns utf-8 encoded string21 in any case. Returns a float when a float and an integer when22 an integer.23 """24 output = simple_math.evaluateStack()25 if type(output) == float:26 if float.is_integer(output):27 output = int(output)28 if to_fraction:29 output = simple_math.decimal_to_fraction(output)30 else:31 output = simple_math.sci_notation(output)32 output = unicode(output).encode('utf-8')33 return output34def clean_string(input):35 """36 Replaces a number of known inputs with values37 that can later be evaluated from within the stack.38 adds a multiplier to any input that doesn't have one39 outside off a parens.40 """41 input = checkParens(input)42 if re.search(r'[+\-*/=]{2,}', input):43 raise SyntaxError44 input = parse_function(input)45 for operator in operators:46 if operator in input:47 index = input.index(operator)48 try:49 input[index+1]50 except IndexError:51 raise SyntaxError("ERR: SYNTAX")52 for unic, byte in [(u'\u02c9', '-'),53 (u'\u00B2', '^2'),54 (u'3\u221a', 'cube_root'),55 (u'x\u221a', 'x_root'),56 (u'\u221a', 'sqrt'),57 (u'\u00B3', '^3'),58 (u'sin^-1', 'asin'),59 (u'cos^-1', 'acos'),60 (u'tan^-1', 'atan'),61 (u'\u03c0', 'PI'),62 (u'rand', random.random())]:63 input = input.replace(unic, str(byte))64 for reg_ex in [r'(\d+)(X)', r'(X)(\d+)', r'(\d+)(\()', r'(\))(\d+)']:65 input = re.sub(reg_ex, r'\1 * \2', input)66 input = x_root(input)67 input = fix_decimals(input)68 input = factorial(input)69 return input70def factorial(input):71 """Accepts either one argument or any number of arguments72 within a set of parens and returns the factorial of each.73 If a number of arguments within parens are passed then they74 are returned in parens.75 """76 if "!" in input:77 index = input.index("!")78 if index > 0 and input[index-1] in operators:79 raise SyntaxError("ERR: SYNTAX")80 left = index - 181 right = index82 while left > 0:83 if input[left] in operators:84 left += 185 break86 else:87 left -= 188 to_evaluate = input[left:right]89 if "(" in to_evaluate:90 output = evaluate_parens_factorial(to_evaluate)91 else:92 try:93 output = str(math.factorial(int(to_evaluate)))94 except ValueError:95 raise SyntaxError("ERR: DOMAIN")96 input = input.replace(input[left:index+1], output)97 return input98def evaluate_parens_factorial(to_evaluate):99 """helper function to evaluate the special case of each100 item within a set of parens.101 """102 to_evaluate = to_evaluate.strip("(")103 to_evaluate = to_evaluate.strip(")")104 to_evaluate = to_evaluate.split(",")105 output = ""106 for x in to_evaluate:107 try:108 output += str(math.factorial(int(x))) + ","109 except ValueError:110 raise SyntaxError("ERR: DOMAIN")111 output = output[:-1]112 output = "({})".format(output)113 return output114def checkParens(input):115 """Pyparsing will generally catch errors in parens except116 in the case of too many close parens. This function closes that117 gap and also allows for future functionality surrounding118 accepting input that's meant to be converted into a list.119 """120 input = input.replace("{", "[")121 input = input.replace("}", "]")122 count = 0123 for x in input:124 if x == "(":125 count += 1126 elif x == ")":127 count -= 1128 if count == -1:129 raise SyntaxError130 if count != 0:131 raise SyntaxError132 return input133def fix_decimals(input):134 """Pyparsing will not accept floats that do not135 have a placeholder in the ones spot so we add one.136 """137 if input[0] == '.':138 input = '0' + input139 for item in [('+.', '+0.'),140 ('*.', '*0.'),141 ('/.', '/0.'),142 ('-.', '-0.'),143 ('(.', '(0.'),144 (').', ')0.')]:145 input = input.replace(item[0], item[1])146 return input147operators = ['*', '+', '-', '/', '!']148def parse_function(input):149 """If the function that's passed is in the functions dictionary this150 will pull the function from the string, evaluate it and return151 a string where the original function is replaced with a string that152 represents the appropriate numeric output.153 """154 for function in functions:155 if function in input:156 index = input.index(function)157 if index > 0 and input[index - 1] not in operators:158 raise SyntaxError(b"ERR: SYNTAX")159 beginning = index + len(function)160 end = beginning + 1161 while end < (len(input) - 1):162 if input[end] == ")":163 break164 else:165 end += 1166 to_evaluate = input[beginning:end]167 evaluated = functions[function](to_evaluate)168 input = input.replace(input[index:end+1], evaluated)169 return input170def min_val(to_evaluate):171 """Accepts any number of inputs and returns the one number that172 has the lowest value.173 """174 if '[' in to_evaluate or ']' in to_evaluate:175 # come back to fix this to accept lists if time176 raise SyntaxError(b'ERR: SYNTAX')177 to_evaluate = to_evaluate.split(',')178 return str(min([float(x) for x in to_evaluate]))179def max_val(to_evaluate):180 """Accepts any number of inputs and returns the one number that181 has the highest value.182 """183 if '[' in to_evaluate or ']' in to_evaluate:184 # come back to fix this to accept lists if time185 raise SyntaxError(b'ERR: SYNTAX')186 to_evaluate = to_evaluate.split(',')187 return str(max([float(x) for x in to_evaluate]))188def ipart(to_evaluate):189 """Takes in one float as argument and returns just190 the integer part.191 """192 if "," in to_evaluate:193 raise SyntaxError(b"ERR: ARGUMENT")194 try:195 return str(float(to_evaluate) // 1)196 except ValueError:197 raise SyntaxError(b"ERR: SYNTAX")198def fpart(to_evaluate):199 """Takes in one float as argument and returns just the fractional200 part.201 """202 if "," in to_evaluate:203 raise SyntaxError(b"ERR: ARGUMENT")204 try:205 return str(float(to_evaluate) % 1)206 except ValueError:207 raise SyntaxError(b"ERR: SYNTAX")208def gcd(to_evaluate):209 """Takes in two integers and returns the greatest common210 denominator.211 """212 x, y = two_integers(to_evaluate)213 while(y):214 x, y = y, x % y215 return str(x)216def lcm(to_evaluate):217 """Takes in two integers and returns the lowest common multiplier.218 """219 x, y = two_integers(to_evaluate)220 lcm = (x*y)//int(gcd(to_evaluate))221 return str(lcm)222def randint(to_evaluate):223 """Takes in two integers and returns a random integer in between224 or equal to the integers passed.225 """226 x, y = two_integers(to_evaluate)227 rand = random.randint(x, y)228 return str(rand)229functions = {230 "iPart(": ipart,231 "fPart(": fpart,232 "min(": min_val,233 "max(": max_val,234 "lcm(": lcm,235 "gcd(": gcd,236 "randInt(": randint,237}238def two_integers(to_evaluate):239 """takes in string to evaluate and throws appropriate errors240 if any value passed is not an integer and if more than two values241 are passed.242 """243 to_evaluate = to_evaluate.split(',')244 try:245 x, y = to_evaluate246 try:247 x = float(x)248 y = float(y)249 if x.is_integer() and y.is_integer():250 x = int(x)251 y = int(y)252 else:253 raise ValueError254 except ValueError:255 raise SyntaxError(b"ERR: DOMAIN")256 except ValueError:257 raise SyntaxError(b"ERR: ARGUMENT")258 return x, y259def x_root(input):260 """Takes in the entire user input, parses out the arguments261 immediately before and after x_root and returns the result of that262 evaluation. For instance 3x_root8 will evaluate as 8**(1/3).263 """264 if 'x_root' in input:265 index = input.index('x_root')266 try:267 if (input[index-1] in operators268 or input[index+6] in operators269 or index == 0):270 raise SyntaxError(b"ERR: SYNTAX")271 except IndexError:272 raise SyntaxError("ERR: SYNTAX")273 left = index-1274 right = index + 6275 while left > 0:276 if input[left] in operators:277 left += 1278 break279 else:280 left -= 1281 while right < (len(input) - 1):282 if input[right] in operators:283 break284 else:285 right += 1286 if left == 0:287 replacement = (math.pow(float(input[index+6:right+1]),288 (1/float(input[left:index]))))289 input = input.replace(input[left:right+1], str(replacement))290 else:291 replacement = (math.pow(float(input[index+6:right]),292 (1/float(input[left:index]))))293 input = input.replace(input[left:right], str(replacement))...
TestVSCode_evaluate.py
Source:TestVSCode_evaluate.py
...10class TestVSCode_variables(lldbvscode_testcase.VSCodeTestCaseBase):11 mydir = TestBase.compute_mydir(__file__)12 def assertEvaluate(self, expression, regex):13 self.assertRegexpMatches(14 self.vscode.request_evaluate(expression, context=self.context)['body']['result'],15 regex)16 def assertEvaluateFailure(self, expression):17 self.assertNotIn('result',18 self.vscode.request_evaluate(expression, context=self.context)['body'])19 def isExpressionParsedExpected(self):20 return self.context != "hover"21 def run_test_evaluate_expressions(self, context=None):22 """23 Tests the evaluate expression request at different breakpoints24 """25 self.context = context26 program = self.getBuildArtifact("a.out")27 self.build_and_launch(program)28 source = "main.cpp"29 self.set_source_breakpoints(30 source,31 [32 line_number(source, "// breakpoint 1"),...
plural_unittests.py
Source:plural_unittests.py
...9 True, True, False,False,False,False)10 return doc_coref_infos11def test_PA1():12 doc = read('TC-PA.key', 'TC-PA-1.sys')13 assert evaluate(doc, muc) == (1, 1, 1)14 assert evaluate(doc, b_cubed) == (1, 1, 1)15 assert evaluate(doc, ceafe) == (1, 1, 1)16 assert evaluate(doc, ceafm) == (1, 1, 1)17 assert evaluate(doc, lea) == (1, 1, 1)18 assert evaluate(doc, [blancc,blancn]) == (1,1,1)19def test_PA2():20 doc = read('TC-PA.key', 'TC-PA-2.sys')21 assert evaluate(doc, muc) == (1, 1, 1)22 assert evaluate(doc, b_cubed) == (1, 1, 1)23 assert evaluate(doc, ceafe) == (1, 1, 1)24 assert evaluate(doc, ceafm) == (1, 1, 1)25 assert evaluate(doc, lea) == (1, 1, 1)26 assert evaluate(doc, [blancc,blancn]) == (1,1,1)27def test_PA3():28 doc = read('TC-PA.key', 'TC-PA-3.sys')29 assert evaluate(doc, muc) == (1, 1, 1)30 assert evaluate(doc, b_cubed) == (1, 1, 1)31 assert evaluate(doc, ceafe) == (1, 1, 1)32 assert evaluate(doc, ceafm) == (1, 1, 1)33 assert evaluate(doc, lea) == (1, 1, 1)34 assert evaluate(doc, [blancc,blancn]) == (1,1,1)35def test_PA4():36 doc = read('TC-PA.key', 'TC-PA-4.sys')37 assert evaluate(doc, muc) == approx([3/4, 1, 6/7])38 assert evaluate(doc, b_cubed) == approx([19/24, 1, 38/43])39 assert evaluate(doc, ceafe) == approx([3.8/4, 3.8/4, 3.8/4],abs=TOL)40 assert evaluate(doc, ceafm) == approx([7/8, 1, 0.93333],abs=TOL)41 assert evaluate(doc, lea) == approx([6/8, 1, 6/7])42 #assert evaluate(doc, [blancc,blancn]) == (1,1,1)43def test_PA5():44 doc = read('TC-PA.key', 'TC-PA-5.sys')45 assert evaluate(doc, muc) == approx([1, 11/12, 22/23])46 assert evaluate(doc, b_cubed) == approx([1, 0.95167, 0.97523],abs=TOL)47 assert evaluate(doc, ceafe) == approx([0.98333, 0.98333, 0.98333],abs=TOL)48 assert evaluate(doc, ceafm) == approx([0.98611, 0.98611, 0.98611],abs=TOL)49 assert evaluate(doc, lea) == approx([1, 7.6/8, 38/39])50 #assert evaluate(doc, [blancc,blancn]) == (1,1,1)51def test_PA6():52 doc = read('TC-PA.key', 'TC-PA-6.sys')53 assert evaluate(doc, muc) == approx([7/8, 7/8, 7/8])54 assert evaluate(doc, b_cubed) == approx([0.88542, 0.92130, 0.903],abs=TOL)55 assert evaluate(doc, ceafe) == approx([0.95833, 0.95833, 0.95833], abs=TOL)56 assert evaluate(doc, ceafm) == approx([0.94643, 0.94643, 0.94643], abs=TOL)57 assert evaluate(doc, lea) == approx([7/8, 11/12, 0.89535], abs=TOL)58 #assert evaluate(doc, [blancc,blancn]) == (1,1,1)59def test_PA7():60 doc = read('TC-PA.key', 'TC-PA-7.sys')61 assert evaluate(doc, muc) == approx([5/8, 5/6, 5/7])62 assert evaluate(doc, b_cubed) == approx([0.72461, 1, 0.84032],abs=TOL)63 assert evaluate(doc, ceafe) == approx([0.90278, 0.90278, 0.90278],abs=TOL)64 assert evaluate(doc, ceafm) == approx([0.85714, 0.97959, 0.91429],abs=TOL)65 assert evaluate(doc, lea) == approx([5/8, 16/21, 0.68670], abs=TOL)66 #assert evaluate(doc, [blancc,blancn]) == (1,1,1)67def test_PA8():68 doc = read('TC-PA.key', 'TC-PA-8.sys')69 assert evaluate(doc, muc) == (1, 1, 1)70 assert evaluate(doc, b_cubed) == (1, 1, 1)71 assert evaluate(doc, ceafe) == (1, 1, 1)72 assert evaluate(doc, ceafm) == (1, 1, 1)73 assert evaluate(doc, lea) == (1, 1, 1)74 assert evaluate(doc, [blancc, blancn]) == (1, 1, 1)75def test_PA9():76 doc = read('TC-PA.key', 'TC-PA-9.sys')77 assert evaluate(doc, muc) == approx([1, 11/15, 11/13])78 assert evaluate(doc, b_cubed) == approx([1, 0.75463, 0.86016],abs=TOL)79 assert evaluate(doc, ceafe) == approx([0.69167, 0.92222, 0.79048],abs=TOL)80 assert evaluate(doc, ceafm) == approx([0.86111, 0.86111, 0.86111],abs=TOL)81 assert evaluate(doc, lea) == approx([7/8, 5.2/8, 0.74590], abs=TOL)82 #assert evaluate(doc, [blancc,blancn]) == (1,1,1)83def test_PA10():84 doc = read('TC-PA.key', 'TC-PA-10.sys')85 assert evaluate(doc, muc) == approx([3/4, 3/4, 3/4])86 assert evaluate(doc, b_cubed) == approx([0.83333, 0.83333, 0.83333],abs=TOL)87 assert evaluate(doc, ceafe) == approx([3.8/4, 3.8/5, 0.84444],abs=TOL)88 assert evaluate(doc, ceafm) == approx([7/8, 7/9, 0.82353],abs=TOL)89 assert evaluate(doc, lea) == approx([6/8, 7/9, 0.76364], abs=TOL)90 #assert evaluate(doc, [blancc,blancn]) == (1,1,1)91def test_PA11():92 doc = read('TC-PA.key', 'TC-PA-11.sys')93 assert evaluate(doc, muc) == approx([7/8, 1, 14/15])94 assert evaluate(doc, b_cubed) == approx([0.88542, 1, 0.93923],abs=TOL)95 assert evaluate(doc, ceafe) == approx([0.97222, 0.97222, 0.97222],abs=TOL)96 assert evaluate(doc, ceafm) == approx([0.95833, 0.95833, 0.95833],abs=TOL)97 assert evaluate(doc, lea) == approx([7/8, 1, 14/15])98 #assert evaluate(doc, [blancc,blancn]) == (1,1,1)99def test_PB1():100 doc = read('TC-PB.key', 'TC-PB-1.sys')101 assert evaluate(doc, muc) == (1, 1, 1)102 assert evaluate(doc, b_cubed) == (1, 1, 1)103 assert evaluate(doc, ceafe) == (1, 1, 1)104 assert evaluate(doc, ceafm) == (1, 1, 1)105 assert evaluate(doc, lea) == (1, 1, 1)106 assert evaluate(doc, [blancc,blancn]) == (1,1,1)107def test_PB2():108 doc = read('TC-PB.key', 'TC-PB-2.sys')109 assert evaluate(doc, muc) == (1, 1, 1)110 assert evaluate(doc, b_cubed) == (1, 1, 1)111 assert evaluate(doc, ceafe) == (1, 1, 1)112 assert evaluate(doc, ceafm) == (1, 1, 1)113 assert evaluate(doc, lea) == (1, 1, 1)114 assert evaluate(doc, [blancc,blancn]) == (1,1,1)115def test_PB3():116 doc = read('TC-PB.key', 'TC-PB-3.sys')117 assert evaluate(doc, muc) == approx([1, 14/15, 28/29])118 assert evaluate(doc, b_cubed) == approx([1, 0.928, 0.96266],abs=TOL)119 assert evaluate(doc, ceafe) == approx([4.9/5, 4.9/5, 4.9/5])120 assert evaluate(doc, ceafm) == approx([0.975, 0.975, 0.975])121 assert evaluate(doc, lea) == approx([1, 0.92, 0.95833],abs=TOL)122 # assert evaluate(doc, [blancc,blancn]) == (1,1,1)123def test_PB4():124 doc = read('TC-PB.key', 'TC-PB-4.sys')125 assert evaluate(doc, muc) == approx([0.8, 0.8, 0.8])126 assert evaluate(doc, b_cubed) == approx([0.85, 7.5/9, 0.84158],abs=TOL)127 assert evaluate(doc, ceafe) == approx([0.77143, 0.96429, 0.85714],abs=TOL)128 assert evaluate(doc, ceafm) == approx([0.8, 8/9, 0.84211],abs=TOL)129 assert evaluate(doc, lea) == approx([0.8, 7/9, 0.78873],abs=TOL)...
Using AI Code Generation
1var page = require('webpage').create();2 page.evaluate(function() {3 document.getElementById('lst-ib').value = 'Hello World!';4 });5 page.render('google.png');6 phantom.exit();7});8var page = require('webpage').create();9 var title = page.evaluate(function() {10 return document.title;11 });12 console.log('Page title is ' + title);13 phantom.exit();14});15var page = require('webpage').create();16 var title = page.evaluate(function() {17 return document.title;18 });19 console.log('Page title is ' + title);20 page.evaluate(function() {21 document.getElementById('lst-ib').value = 'Hello World!';22 });23 page.render('google.png');24 phantom.exit();25});26var page = require('webpage').create();27 var title = page.evaluate(function() {28 return document.title;29 });30 console.log('Page title is ' + title);31 page.evaluate(function() {32 document.getElementById('lst-ib').value = 'Hello World!';33 });34 page.render('google.png');35 var result = page.evaluate(function() {36 return document.getElementById('lst-ib').value;37 });38 console.log('Search field contains ' + result);39 phantom.exit();40});41var page = require('webpage').create();42 var title = page.evaluate(function() {43 return document.title;44 });45 console.log('Page title is ' + title);46 page.evaluate(function() {47 document.getElementById('lst-ib').value = 'Hello World!';48 });49 page.render('google.png');50 var result = page.evaluate(function() {51 return document.getElementById('lst-ib').value;52 });53 console.log('Search field contains ' + result);54 page.evaluate(function() {55 document.getElementById('lst-ib').value = '';56 });57 page.render('google.png');58 var result = page.evaluate(function() {
Using AI Code Generation
1var page = require('webpage').create();2var wpt = require('webpagetest');3var wpt = new WebPageTest('www.webpagetest.org', 'A.12345678901234567890123456789012');4var location = 'Dulles:Chrome';5var runs = 3;6var firstViewOnly = true;7wpt.runTest(url, {location: location, runs: runs, fvonly: firstViewOnly}, function(err, data) {8 if (err) {9 console.log(err);10 } else {11 console.log(data);12 }13});14var page = require('webpage').create();15var wpt = require('webpagetest');16var wpt = new WebPageTest('www.webpagetest.org', 'A.12345678901234567890123456789012');17var location = 'Dulles:Chrome';18var runs = 3;19var firstViewOnly = true;20wpt.runTest(url, {location: location, runs: runs, fvonly: firstViewOnly}, function(err, data) {21 if (err) {22 console.log(err);23 } else {24 console.log(data);25 }26});27var page = require('webpage').create();28var wpt = require('webpagetest');29var wpt = new WebPageTest('www.webpagetest.org', 'A.12345678901234567890123456789012');30var location = 'Dulles:Chrome';31var runs = 3;32var firstViewOnly = true;33wpt.runTest(url, {location: location, runs: runs, fvonly: firstViewOnly}, function(err, data) {34 if (err) {35 console.log(err);36 } else {37 console.log(data);38 }39});40var page = require('webpage').create();41var wpt = require('webpagetest');42var wpt = new WebPageTest('www.webpagetest.org', 'A.12345678901234567890123456789012');
Using AI Code Generation
1var wpt = require('wpt.js');2var options = {3};4wpt.runTest(options, function(err, data) {5 if (err) {6 console.log(err);7 } else {8 console.log(data);9 }10});
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var options = {4};5wpt.runTest(url, options, function(err, data) {6 if (err) return console.error(err);7 wpt.getTestResults(data.data.testId, function(err, data) {8 if (err) return console.error(err);9 console.log(data);10 });11});
Using AI Code Generation
1var page = require('webpage').create();2 page.render('google.png');3 phantom.exit();4});5var page = require('webpage').create();6 page.render('yahoo.png');7 phantom.exit();8});9var page = require('webpage').create();10 page.render('bing.png');11 phantom.exit();12});13var page = require('webpage').create();14 page.render('facebook.png');15 phantom.exit();16});17var page = require('webpage').create();18 page.render('twitter.png');19 phantom.exit();20});21var page = require('webpage').create();22 page.render('linkedin.png');23 phantom.exit();24});25var page = require('webpage').create();26 page.render('youtube.png');27 phantom.exit();28});29var page = require('webpage').create();30 page.render('wikipedia.png');31 phantom.exit();32});33var page = require('webpage').create();34 page.render('wordpress.png');35 phantom.exit();36});37var page = require('webpage').create();38 page.render('amazon.png');39 phantom.exit();40});
Using AI Code Generation
1var wptdriver = require('wptdriver');2wptdriver.evaluate('return document.title;', function(result) {3 console.log('Title is: ' + result);4});5wptdriver.evaluate('return document.body.innerHTML;', function(result) {6 console.log('Body is: ' + result);7});8wptdriver.evaluate('return document.body.innerHTML;', function(result) {9 console.log('Body is: ' + result);10});11wptdriver.evaluate('return document.body.innerHTML;', function(result) {12 console.log('Body is: ' + result);13});14wptdriver.evaluate('return document.body.innerHTML;', function(result) {15 console.log('Body is: ' + result);16});17wptdriver.evaluate('return document.body.innerHTML;', function(result) {18 console.log('Body is: ' + result);19});20wptdriver.evaluate('return document.body.innerHTML;', function(result) {21 console.log('Body is: ' + result);22});23wptdriver.evaluate('return document.body.innerHTML;', function(result) {24 console.log('Body is: ' + result);25});26wptdriver.evaluate('return document.body.innerHTML;', function(result) {27 console.log('Body is: ' + result);28});29wptdriver.evaluate('return document.body.innerHTML;', function(result) {30 console.log('Body is: ' + result);31});32wptdriver.evaluate('return document.body.innerHTML;', function(result) {33 console.log('Body is: ' + result);34});35wptdriver.evaluate('return document.body.innerHTML;', function(result) {36 console.log('Body is: ' + result);37});38wptdriver.evaluate('return document.body.innerHTML;', function(result) {39 console.log('Body is: ' + result);40});41wptdriver.evaluate('return
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!!