def testTensorMapHasKey(self): m = map_ops.empty_tensor_map() k = constant_op.constant(1.0) k2 = constant_op.constant(2.0) v = constant_op.constant(2.0) m = map_ops.tensor_map_insert(m, k, v) # Check has key. b = map_ops.tensor_map_has_key(m, k) b2 = map_ops.tensor_map_has_key(m, k2) self.assertAllEqual(b, True) self.assertAllEqual(b2, False)
def testStringKeyGrad(self): with backprop.GradientTape(persistent=True) as tape: m = map_ops.empty_tensor_map() k = constant_op.constant("key") k2 = constant_op.constant("key2") v = constant_op.constant(2.0) v2 = constant_op.constant(22.0) tape.watch(v) tape.watch(v2) m = map_ops.tensor_map_insert(m, k, v) m = map_ops.tensor_map_insert(m, k2, v2) s = map_ops.tensor_map_size(m) self.assertAllEqual(s, 2) # Test lookup and gradient. l = map_ops.tensor_map_lookup(m, k, v.dtype) self.assertAllClose(l, v) self.assertAllClose(tape.gradient(l * 5, v), 5) # Test replace and gradient. m = map_ops.tensor_map_insert(m, k, v2) l2 = map_ops.tensor_map_lookup(m, k, v2.dtype) self.assertAllClose(l2, v2) g = tape.gradient(l2 * 6, v2) self.assertAllEqual(g, 6) # Test erase, has key, and gradient. m = map_ops.tensor_map_erase(m, k, v2.dtype) s = map_ops.tensor_map_size(m) self.assertAllEqual(s, 1) h = map_ops.tensor_map_has_key(m, k) self.assertAllEqual(h, False) l = map_ops.tensor_map_lookup(m, k2, v2.dtype) g2 = tape.gradient(l * 6, v2) self.assertAllEqual(g2, 6) del tape
def testIfHasKeyLookup(self): m = map_ops.empty_tensor_map() k = constant_op.constant(1.0) k2 = constant_op.constant(2.0) v = constant_op.constant(2.0) m = map_ops.tensor_map_insert(m, k, v) default_value = array_ops.zeros_like(v) l = control_flow_ops.cond( map_ops.tensor_map_has_key(m, k), lambda: map_ops.tensor_map_lookup(m, k, dtypes.float32), lambda: default_value) l2 = control_flow_ops.cond( map_ops.tensor_map_has_key(m, k2), lambda: map_ops.tensor_map_lookup(m, k, dtypes.float32), lambda: default_value) self.assertAllClose(l, v) self.assertAllClose(l2, default_value)
def testStringKeyValue(self): m = map_ops.empty_tensor_map() k = constant_op.constant("key") v = constant_op.constant("value") k2 = constant_op.constant(1.0) v2 = constant_op.constant(2.0) # Test insert and lookup on string key-value pair. m = map_ops.tensor_map_insert(m, k, v) m = map_ops.tensor_map_insert(m, k2, v2) l = map_ops.tensor_map_lookup(m, k, v.dtype) self.assertAllEqual(l, v) # Test lookup on float key-value pair. l2 = map_ops.tensor_map_lookup(m, k2, v2.dtype) self.assertAllClose(l2, v2) # Test erase and has key. self.assertAllEqual(map_ops.tensor_map_has_key(m, k), True) m = map_ops.tensor_map_erase(m, k, v.dtype) self.assertAllEqual(map_ops.tensor_map_has_key(m, k), False) self.assertAllEqual(map_ops.tensor_map_has_key(m, k2), True)
def testVectorValue(self): m = map_ops.empty_tensor_map() k = constant_op.constant([1.0, 2.0]) v = constant_op.constant([11.0, 22.0]) # Test insert and lookup. m = map_ops.tensor_map_insert(m, k, v) s = map_ops.tensor_map_size(m) self.assertAllEqual(s, 1) l = map_ops.tensor_map_lookup(m, k, v.dtype) self.assertAllEqual(l, v) # Test erase and has key. m = map_ops.tensor_map_erase(m, k, v.dtype) s = map_ops.tensor_map_size(m) self.assertAllEqual(s, 0) self.assertAllEqual(map_ops.tensor_map_has_key(m, k), False)