def test_serialize(self):
        random = np.random.RandomState(seed=42)
        node_features = generate_random_node_features(random, 1, 3, 1)

        input = TensorGraph(
            node_features,
            np.array([[
                [[0, 0, 1], [0, 0, 1], [1, 1, 0]],
                [[1, 2, 2], [2, 3, 5], [2, 5, 3]],
            ]],
                     dtype=float))
        expected_output = TensorGraph(
            node_features,
            np.array([[
                [[0, 0, 0.71], [0, 0, 0.71], [0.71, 0.71, 0]],
                [[0.2, 0.28, 0.28], [0.28, 0.3, 0.5], [0.28, 0.5, 0.3]],
            ]],
                     dtype=float))

        model_input = TensorGraphInput(
            TensorGraphShape(num_nodes=3, edge_dims=2))
        x = NormaliseGraph(method='spectral')(model_input)
        model = tf.keras.Model(model_input, x)
        config = model.get_config()

        reinitialized_model = tf.keras.Model.from_config(config)
        output = reinitialized_model(input)

        assert_graph_equal(self,
                           expected_output,
                           output,
                           allow_nodes_close=True,
                           allow_edges_close=True)
    def test_serialize(self):
        random = np.random.RandomState(seed=42)
        node_features = generate_random_node_features(random, 1, 3, 1)

        input = TensorGraph(
            node_features,
            np.array([[
                [[0, 1, 0], [1, 0, 0], [0, 1, 0]],
                [[0, 2, 3], [2, 0, 4], [3, 4, 0]],
            ]],
                     dtype=float))
        expected_output = TensorGraph(
            node_features,
            np.array([[
                [[1, 1, 0], [1, 1, 0], [0, 1, 1]],
                [[1, 2, 3], [2, 1, 4], [3, 4, 1]],
            ]],
                     dtype=float))

        model_input = TensorGraphInput(
            TensorGraphShape(num_nodes=3, edge_dims=2))
        x = AddSelfLoops()(model_input)
        model = tf.keras.Model(model_input, x)
        config = model.get_config()

        reinitialized_model = tf.keras.Model.from_config(config)
        output = reinitialized_model(input)

        assert_graph_equal(self,
                           expected_output,
                           output,
                           allow_nodes_close=True)
    def _test_call(self, input, expected_output, **kwargs):
        graph_input = TensorGraph(None, input)
        graph_expected = TensorGraph(None, expected_output)

        shiftTensor = AddSelfLoops(**kwargs)
        input_shape = graph_tensor_shape(graph_input)
        computed_shape = shiftTensor.compute_output_shape(input_shape)
        graph_output = shiftTensor(graph_input)

        assert_graph_equal(self, graph_expected, graph_output)
        assert_graph_shape(self, graph_output, computed_shape)
    def _test_call(self, input, expected_output, **kwargs):
        graph_input = TensorGraph(None, input)
        graph_expected = TensorGraph(None, expected_output)

        shiftTensor = NormaliseGraph(**kwargs)
        input_shape = graph_tensor_shape(graph_input)
        computed_shape = shiftTensor.compute_output_shape(input_shape)
        graph_output = shiftTensor(graph_input)

        assert_graph_equal(self,
                           graph_expected,
                           graph_output,
                           allow_edges_close=True)
        assert_graph_shape(self, graph_output, computed_shape)
Example #5
0
def tfrecord_to_tensorgraph(example: dict, name: str = 'graph'):
    num_nodes = example[name + ':num_nodes']

    node_features = tf.sparse.to_dense(example[name + ':node_features'])
    node_features = tf.reshape(node_features, (num_nodes, -1))

    edge_features = tf.sparse.to_dense(example[name + ':edge_features'])
    edge_features = tf.reshape(edge_features, (-1, num_nodes, num_nodes))

    return TensorGraph(node_features, edge_features)
    def test_node_and_edge_features(self):
        random = np.random.RandomState(seed=42)
        node_features = generate_random_node_features(random, 3, 10, 42)
        edge_features = generate_random_adjacency(random, 3, 10, 7)

        node_tensor = tf.convert_to_tensor(node_features, dtype=tf.float32)
        edge_tensor = tf.convert_to_tensor(edge_features, dtype=tf.float32)

        graph = TensorGraph(node_tensor, edge_tensor)

        self.assertAllEqual(node_tensor, graph[NODE_FEATURES])
        self.assertAllEqual(edge_tensor, graph[EDGE_FEATURES])