Esempio n. 1
0
    def test_create_proto_index_directly_reroot_at_action(self):
        sessions = [
            """
        event {
          action {}
          action {}
        }
        event {}
        event { action {} }
        """, "", """
        event {}
        event {
          action {}
          action {}
        }
        event {  }
        """
        ]
        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)
        proto_index_directly_reroot_at_action = (
            reroot_expr.create_proto_index(
                "proto_index_directly_reroot_at_action").get_child_or_error(
                    "proto_index_directly_reroot_at_action"))

        self.assertFalse(proto_index_directly_reroot_at_action.is_repeated)
        result = expression_test_util.calculate_value_slowly(
            proto_index_directly_reroot_at_action)
        self.assertAllEqual(result.parent_index, [0, 1, 2, 3, 4])
        self.assertAllEqual(result.values, [0, 0, 0, 2, 2])
Esempio n. 2
0
  def test_project_proto_map(self, use_string_view):
    examples = [
        """
        features {
          feature {
            key: "feature1"
            value { bytes_list { value: ["hello", "world"] } }
          }
          feature {
            key: "feature2"
            value { float_list { value: 8.0 } }
          }
        }
        """, """
        features {
          feature {
            key: "feature1"
            value { bytes_list { value: "deadbeef" } }
          }
          feature {
            key: "feature3"
            value { int64_list { value: [123, 456] } }
          }
        }
        """
    ]
    expr = proto_test_util.text_to_expression(examples, tf.train.Example)
    result = expression_test_util.calculate_list_map(
        expr.project([
            "features.feature[feature1].bytes_list.value",
            "features.feature[feature2].float_list.value",
            "features.feature[feature3].int64_list.value",
        ]),
        self,
        options=self._get_calculate_options(use_string_view))

    feature1 = result["features.feature[feature1].bytes_list.value"]
    feature2 = result["features.feature[feature2].float_list.value"]
    feature3 = result["features.feature[feature3].int64_list.value"]
    self.assertAllEqual(feature1,
                        [[[[[b"hello", b"world"]]]], [[[[b"deadbeef"]]]]])
    self.assertAllEqual(feature2, [[[[[8.0]]]], [[]]])
    self.assertAllEqual(feature3, [[[]], [[[[123, 456]]]]])
    if use_string_view:
      self._check_string_view()
Esempio n. 3
0
    def test_project_proto_map_leaf_value(self):
        protos = [
            """
            int32_string_map {
              key: 222
              value: "2"
            }
            """
        ]

        expr = proto_test_util.text_to_expression(protos,
                                                  test_map_pb2.MessageWithMap)
        result = expression_test_util.calculate_list_map(
            expr.project([
                "int32_string_map[222]",
                "int32_string_map[223]",
            ]), self)
        self.assertLen(result, 2)
        self.assertAllEqual(result["int32_string_map[222]"], [[b"2"]])
        self.assertAllEqual(result["int32_string_map[223]"], [[]])
Esempio n. 4
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])
Esempio n. 5
0
def _create_slice_and_project_example():
    r"""Creates an example for test_slice_and_project.

  Returns:

               ------*-----------------
              /                        \
        ---session0----             session1
       /      \        \             /      \
    event0  event1   event2      event3   event4
    /    \    |  \     |  \       /       /  |  \
  act0 act1 act2 act3 act4 act5 act6   act7 act8 act9
    |   |     |        |    |     |      |    |   |
    a   b     c        e    f     g      h    i   j

  This also adds an action_mask that is false for the zeroeth action.
  """
    return proto_test_util.text_to_expression([
        """
        event:{
          action_mask: [false, true]
          action:{
            doc_id:"a"
          }
          action:{
            doc_id:"b"
          }
        }
        event:{
          action_mask: [false, true]
          action:{
            doc_id:"c"
          }
          action:{
          }
        }
        event:{
          action_mask: [false, true]
          action:{
            doc_id:"e"
          }
          action:{
            doc_id:"f"
          }
        }""", """
        event:{
          action_mask: [false]
          action:{
            doc_id:"g"
          }
        }
        event:{
          action_mask: [false, true, true]
          action:{
            doc_id:"h"
          }
          action:{
            doc_id:"i"
          }
          action:{
            doc_id:"j"
          }
        }"""
    ], test_pb2.Session)