Exemplo n.º 1
0
def test_validation():
    graph = Topology('cpu')

    class ValidationNodeStub(WorkerNodeBase):
        def __init__(self, fails_validation):
            super().__init__()
            self.fails_validation = fails_validation

        def _create_unit(self, creator: TensorCreator) -> Unit:
            return RandomUnitStub(creator)

        def _step(self):
            pass

        def validate(self):
            if self.fails_validation:
                raise NodeValidationException('Node failed to validate')

    node = ValidationNodeStub(fails_validation=True)
    graph.add_node(node)

    with pytest.raises(NodeValidationException):
        graph.prepare()

    node.fails_validation = False

    graph.prepare()
Exemplo n.º 2
0
    def test_expert_dimensions(self):
        """Tests multi-dimensional expert indexes."""
        device = 'cpu'
        parent_rf_size_x = parent_rf_size_y = 4
        n_channels = 4
        image_grid_size_x = image_grid_size_y = 16
        input_dimensions = (image_grid_size_y, image_grid_size_x, n_channels)
        parent_rf_dims = Size2D(parent_rf_size_x, parent_rf_size_y)
        parent_grid_dimensions = (4, 4)

        graph = Topology(device)

        node = ReceptiveFieldNode(input_dimensions, parent_rf_dims)

        graph.add_node(node)

        memory_block = MemoryBlock()
        memory_block.tensor = torch.zeros(image_grid_size_y,
                                          image_grid_size_x,
                                          n_channels,
                                          device=device)
        memory_block.tensor[0, parent_rf_size_x, 0] = 1

        Connector.connect(memory_block, node.inputs.input)

        graph.prepare()

        graph.step()

        node_output = node.outputs.output.tensor

        assert node_output.shape == torch.Size(parent_grid_dimensions +
                                               (parent_rf_size_y,
                                                parent_rf_size_x, n_channels))
        assert node_output[0, 1, 0, 0, 0] == 1
Exemplo n.º 3
0
def test_is_initialized():
    graph = Topology('cpu')
    assert not graph.is_initialized()

    graph.add_node(NodeStub())
    assert not graph.is_initialized()

    graph.prepare()
    assert graph.is_initialized()

    with raises(IllegalStateException):
        graph.add_node(NodeStub())
Exemplo n.º 4
0
    def test_inverse_projection(self, device):
        dtype = get_float(device)
        params = ExpertParams()
        params.flock_size = 2
        params.n_cluster_centers = 4

        params.spatial.input_size = 6
        params.spatial.buffer_size = 7
        params.spatial.batch_size = 3
        params.temporal.n_frequent_seqs = 2
        params.temporal.seq_length = 3
        input_size = (3, 2)

        graph = Topology(device)
        node = ExpertFlockNode(params=params)

        graph.add_node(node)

        input_block = MemoryBlock()
        input_block.tensor = torch.rand((params.flock_size, ) + input_size,
                                        dtype=dtype,
                                        device=device)
        Connector.connect(input_block, node.inputs.sp.data_input)

        graph.prepare()

        node._unit.flock.sp_flock.cluster_centers = torch.tensor(
            [[[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 0.5, 0.5, 0, 0],
              [0, 0, 0.5, 0, 0.5, 0]],
             [[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0],
              [0, 0, 0, 1, 0, 0]]],
            dtype=dtype,
            device=device)

        # Just SP inverse projection
        data = torch.tensor([[0, 0, 1, 0], [0.2, 0.3, 0.4, 0.1]],
                            dtype=dtype,
                            device=device)

        packet = InversePassOutputPacket(data,
                                         node.outputs.tp.projection_outputs)
        projected = node.recursive_inverse_projection_from_output(packet)

        # The result of the projection itself would be [[0, 0, 0.5, 0.5, 0, 0], ...], and it should be viewed as (2, 3, 2).
        expected_projection = torch.tensor(
            [[[0, 0], [0.5, 0.5], [0, 0]], [[0.2, 0.3], [0.4, 0.1], [0, 0]]],
            dtype=dtype,
            device=device)

        assert same(expected_projection, projected[0].tensor)
Exemplo n.º 5
0
    def test_rf_node(self, device):
        float_dtype = get_float(device)
        parent_rf_size_x = parent_rf_size_y = 4
        n_channels = 4
        image_grid_size_x = image_grid_size_y = 16
        dimensions = (image_grid_size_y, image_grid_size_x, n_channels)
        parent_rf_dims = Size2D(parent_rf_size_y, parent_rf_size_x)

        graph = Topology(device)

        node = ReceptiveFieldNode(dimensions,
                                  parent_rf_dims,
                                  flatten_output_grid_dimensions=True)

        graph.add_node(node)

        memory_block = MemoryBlock()
        memory_block.tensor = torch.zeros(image_grid_size_y,
                                          image_grid_size_x,
                                          n_channels,
                                          dtype=float_dtype,
                                          device=device)
        memory_block.tensor[0, parent_rf_size_x, 0] = 1

        Connector.connect(memory_block, node.inputs.input)

        graph.prepare()

        graph.step()

        node_output = node.outputs.output.tensor

        n_parent_rfs = (image_grid_size_y // parent_rf_size_y) * (
            image_grid_size_x // parent_rf_size_x)
        assert node_output.shape == torch.Size(
            [n_parent_rfs, parent_rf_size_y, parent_rf_size_x, n_channels])
        assert node_output[1, 0, 0, 0] == 1

        back_projection = node.recursive_inverse_projection_from_output(
            InversePassOutputPacket(node_output, node.outputs.output))
        # assert back_projection.interpret_shape == input_image.shape
        assert same(back_projection[0].tensor, memory_block.tensor)