google-research
97 строк · 3.0 Кб
1# coding=utf-8
2# Copyright 2024 The Google Research Authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""Base test classes for graph API."""
17from absl.testing import absltest
18
19import jax
20import jax.numpy as jnp
21
22from jaxsel import agents
23from jaxsel import graph_models
24from jaxsel import subgraph_extractors
25from jaxsel import synthetic_data
26
27
28class BaseGraphTest(absltest.TestCase):
29"""Tests basic functionnality of our Graph and Agent APIs."""
30
31def setUp(self):
32"""Sets up an example image and associated graph and agent."""
33super().setUp()
34
35grid_size = 20
36num_paths = 2
37num_classes = 3
38
39agent_hidden_dim = 4
40max_graph_size = 75
41max_subgraph_size = 11
42num_steps_extractor = 50
43rho = 1e-6
44alpha = 1e-3
45ridge = 1e-7
46num_heads = 2
47n_encoder_layers = 2
48qkv_dim = 32
49mlp_dim = 64
50embedding_dim = 4
51hidden_dim = 16
52
53graph, start_node_id, label = synthetic_data.generate(
54grid_size, num_paths, num_classes)
55
56self.graph = graph
57self.start_node_id = start_node_id
58self.label = label
59
60agent_config = agents.AgentConfig(graph.graph_parameters(),
61agent_hidden_dim, agent_hidden_dim)
62
63extractor_config = subgraph_extractors.ExtractorConfig(
64max_graph_size, max_subgraph_size, rho, alpha, num_steps_extractor,
65ridge, agent_config)
66
67graph_classifier_config = graph_models.TransformerConfig(
68graph.graph_parameters(),
69num_heads=num_heads,
70num_layers=n_encoder_layers,
71qkv_dim=qkv_dim,
72mlp_dim=mlp_dim,
73image_size=grid_size**2,
74embedding_dim=embedding_dim,
75hidden_dim=hidden_dim,
76num_classes=num_classes)
77
78extractor = subgraph_extractors.SparseISTAExtractor(extractor_config)
79agent = agents.SimpleFiLMedAgentModel(agent_config)
80
81self.rng = jax.random.PRNGKey(2)
82self.agent = agent
83self.extractor = extractor
84self.graph_classifier = graph_models.TransformerClassifier(
85graph_classifier_config)
86
87def test_random_walk_on_graph(self):
88"""Tests ability to perform a random walk on a graph built from an image."""
89
90rng_agent, self.rng = jax.random.split(self.rng)
91self.agent.init_with_output(rng_agent, self.graph, method=self.agent.walk)
92
93def test_out_of_bounds_pixel_neighbors(self):
94"""The out of bounds pixel should only be linked to the start node."""
95relation_ids, neighbor_node_ids = self.graph.outgoing_edges(-1)
96del relation_ids
97assert jnp.all(neighbor_node_ids == self.graph._start_node_id)
98