def test_get_sparse_tensors(self): """Tests get_sparse_tensors on a deep expression.""" for options in options_to_test: with self.session(use_gpu=False) as sess: expression = prensor_test_util.create_nested_prensor() sparse_tensor_map = prensor_util.get_sparse_tensors( expression, options) string_tensor_map = { str(k): v for k, v in sparse_tensor_map.items() } string_np_map = sess.run(string_tensor_map) self.assertAllEqual( string_np_map["doc.bar"].indices, [[0, 0, 0], [1, 0, 0], [1, 0, 1], [1, 1, 0]]) self.assertAllEqual(string_np_map["doc.bar"].values, [b"a", b"b", b"c", b"d"]) self.assertAllEqual(string_np_map["doc.keep_me"].indices, [[0, 0], [1, 0]]) self.assertAllEqual(string_np_map["doc.keep_me"].values, [False, True]) self.assertAllEqual( string_np_map["user.friends"].indices, [[0, 0, 0], [1, 0, 0], [1, 0, 1], [1, 1, 0], [2, 0, 0]]) self.assertAllEqual(string_np_map["user.friends"].values, [b"a", b"b", b"c", b"d", b"e"])
def new_op(pren, options): """Op for mapping prensor using the operation.""" sparse_tensor_map = prensor_util.get_sparse_tensors(pren, options) sparse_tensors = [sparse_tensor_map[p] for p in paths] result_as_tensor = operation(*sparse_tensors) result = _as_leaf_node(result_as_tensor, is_repeated, sparse_tensors[0].dense_shape[0], options) if result.values.dtype != dtype: raise ValueError( "Type unmatched: actual ({})!= expected ({})".format( str(result.values.dtype), str(dtype))) return result
def test_get_sparse_tensors_simple_dense(self): """Tests get_sparse_tensors on a deep expression.""" for options in options_to_test: expression = prensor_test_util.create_simple_prensor() sparse_tensor_map = prensor_util.get_sparse_tensors( expression, options) string_tensor_map = { str(k): tf.sparse.to_dense(v) for k, v in sparse_tensor_map.items() } self.assertAllEqual(string_tensor_map["foo"], [9, 8, 7]) self.assertAllEqual(string_tensor_map["foorepeated"], [[9, 0], [8, 7], [6, 0]])
def test_create_proto_index_directly_reroot_at_action_sparse_dense(self): sessions = [ """ event { action {} action {} } event {} event { action {} } """, "", """ event {} event { action {} action {} } event { } """ ] with self.session(use_gpu=False) as sess: expr = proto_test_util.text_to_expression(sessions, test_pb2.Session) reroot_expr = expr.reroot("event.action") # Reroot with a depth > 1 (all the other cases are depth == 1) [prensor_tree] = calculate.calculate_prensors([ reroot_expr.create_proto_index( "proto_index_directly_reroot_at_action") ]) proto_index_node = prensor_tree.get_child_or_error( "proto_index_directly_reroot_at_action").node self.assertFalse(proto_index_node.is_repeated) sparse_tensors = prensor_util.get_sparse_tensors( prensor_tree, calculate_options.get_default_options()) proto_index_directly_reroot_at_action = sparse_tensors[path.Path( ["proto_index_directly_reroot_at_action"])] [sparse_value, dense_value] = sess.run([ proto_index_directly_reroot_at_action, tf.sparse_tensor_to_dense(proto_index_directly_reroot_at_action) ]) self.assertAllEqual(sparse_value.values, [0, 0, 0, 2, 2]) self.assertAllEqual(sparse_value.indices, [[0], [1], [2], [3], [4]]) self.assertAllEqual(sparse_value.dense_shape, [5]) self.assertAllEqual(dense_value, [0, 0, 0, 2, 2])
def test_get_sparse_tensors_simple(self): """Tests get_sparse_tensors on a deep expression.""" for options in options_to_test: expression = prensor_test_util.create_simple_prensor() sparse_tensor_map = prensor_util.get_sparse_tensors( expression, options) string_tensor_map = { str(k): v for k, v in sparse_tensor_map.items() } self.assertAllEqual(string_tensor_map["foo"].indices, [[0], [1], [2]]) self.assertAllEqual(string_tensor_map["foo"].dense_shape, [3]) self.assertAllEqual(string_tensor_map["foo"].values, [9, 8, 7]) self.assertAllEqual(string_tensor_map["foorepeated"].indices, [[0, 0], [1, 0], [1, 1], [2, 0]]) self.assertAllEqual(string_tensor_map["foorepeated"].values, [9, 8, 7, 6]) self.assertAllEqual(string_tensor_map["foorepeated"].dense_shape, [3, 2])