Exemplo n.º 1
0
    def test_DenseLayer(self):
        inputs = rs.Spectrum(width=3)
        outputs = rs.Spectrum(width=4)
        dense_layer = rs.dense.DenseLayer(inputs, outputs)

        assert inputs.values == [0, 0, 0]
        assert [round(value, 2) for value in outputs.values] == [0, 0, 0, 0]

        inputs.values = [0.1, 0.2, 0.3]
        assert [round(value, 2)
                for value in outputs.values] == [0.6, 0.6, 0.6, 0.6]

        dense_layer.neurons[0].weights = [1, 0, 0]
        assert [round(value, 2)
                for value in outputs.values] == [0.1, 0.6, 0.6, 0.6]

        dense_layer.neurons[1].weights = [0, 1, 0]
        assert [round(value, 2)
                for value in outputs.values] == [0.1, 0.2, 0.6, 0.6]

        dense_layer.neurons[2].weights = [0, 0, 1]
        assert [round(value, 2)
                for value in outputs.values] == [0.1, 0.2, 0.3, 0.6]

        dense_layer.neurons[3].weights = [0, 0, 0]
        assert [round(value, 2)
                for value in outputs.values] == [0.1, 0.2, 0.3, 0]

        inputs.values = [3, 2, 1]
        assert [round(value, 2) for value in outputs.values] == [3, 2, 1, 0]
Exemplo n.º 2
0
    def test_Spectrum(self):
        spectrum = rs.Spectrum(width=2)
        assert spectrum.values == [0, 0]

        with pytest.raises(TypeError) as e:
            spectrum = rs.Spectrum()
        assert "The width of the spectrum must be specified." in str(e)

        connection1 = rs.Connection(0)
        connection2 = rs.Connection(1)
        connection3 = rs.Connection(0)

        spectrum = rs.Spectrum(
            connections=[connection1, connection2, connection3])

        assert spectrum.connections == [connection1, connection2, connection3]
        assert spectrum.values == [0, 1, 0]

        spectrum.values = [0.5, 1, 1.5]
        assert spectrum.values == [0.5, 1, 1.5]

        assert spectrum[0] == connection1
        assert spectrum[1:] == [connection2, connection3]
        with pytest.raises(IndexError) as e:
            connection = spectrum[3]
        assert "Spectrum width exceeded." in str(e)

        test_list = []
        for connection in spectrum:
            test_list.append(connection)
        assert test_list == [connection1, connection2, connection3]

        assert len(spectrum) == 3
Exemplo n.º 3
0
    def test_Image(self):
        image = rs.Image(height=3, width=2)
        assert image.values == [
            [0, 0],
            [0, 0],
            [0, 0]
        ]

        with pytest.raises(TypeError) as e:
            image = rs.Image()
        assert "The height and width of the image must be specified." in str(e)

        connection1 = rs.Connection(0)
        connection2 = rs.Connection(1)
        connection3 = rs.Connection(0)
        connection4 = rs.Connection(1)
        
        spectrum1 = rs.Spectrum([connection1, connection2])
        spectrum2 = rs.Spectrum([connection3, connection4])

        image = rs.Image(spectra=[spectrum1, spectrum2])

        assert image.spectra == [spectrum1, spectrum2]
        assert image.values == [
            [0, 1],
            [0, 1]
        ]

        image.values = [
            [0.5, 1],
            [1.5, 2]
        ]
        assert image.values == [
            [0.5, 1],
            [1.5, 2]
        ]

        assert image[0][0] == connection1
        assert image[1:][0][1] == connection4
        with pytest.raises(IndexError) as e:
            connection = image[2][0]
        assert "Image height exceeded." in str(e)

        test_list = []
        for spectrum in image:
            for connection in spectrum:
                test_list.append(connection)
        assert test_list == [
            connection1, connection2, connection3, connection4]

        assert len(image) == 2
Exemplo n.º 4
0
    def test_DenseNeuron(self):
        inputs = rs.Spectrum(width=4)
        output = rs.Connection()
        neuron = rs.dense.DenseNeuron(inputs, output)

        assert inputs.values == [0, 0, 0, 0]
        assert neuron.weights == [1, 1, 1, 1]
        assert output.value == 0

        inputs.values = [0.1, 0.2, 0.3, 0.4]
        assert round(output.value, 2) == 1.0

        neuron.weights = [4, 3, 2, 1]
        assert round(output.value, 2) == 2.0

        neuron.weights = [0.5, 1, 1.5, 2]
        assert round(output.value, 2) == 1.5

        inputs.values = [2, 2, 1, 1]
        assert round(output.value, 2) == 6.5
Exemplo n.º 5
0
    def test_ConnectionLayer(self):
        layer = rs.ConnectionLayer(height=3, width=4, depth=2)
        assert layer.values == [
            [[0, 0, 0, 0],
             [0, 0, 0, 0],
             [0, 0, 0, 0]],

            [[0, 0, 0, 0],
             [0, 0, 0, 0],
             [0, 0, 0, 0]],
        ]

        with pytest.raises(TypeError) as e:
            layer = rs.ConnectionLayer()
        assert "The depth, height, and width of the layer must be specified."\
            in str(e)

        connection1 = rs.Connection(0)
        connection2 = rs.Connection(1)
        connection3 = rs.Connection(0)
        connection4 = rs.Connection(1)
        connection5 = rs.Connection(0)
        connection6 = rs.Connection(1)

        spectrum1 = rs.Spectrum([connection1])
        spectrum2 = rs.Spectrum([connection2])
        spectrum3 = rs.Spectrum([connection3])
        spectrum4 = rs.Spectrum([connection4])
        spectrum5 = rs.Spectrum([connection5])
        spectrum6 = rs.Spectrum([connection6])

        image1 = rs.Image([spectrum1, spectrum2, spectrum3])
        image2 = rs.Image([spectrum4, spectrum5, spectrum6])

        layer = rs.ConnectionLayer(images=[image1, image2])

        assert layer.images == [image1, image2]
        assert layer.values == [
            [[0],
             [1],
             [0]],

            [[1],
             [0],
             [1]]
        ]

        layer.values = [
            [[0.5],
             [1],
             [1.5]],

            [[2],
             [2.5],
             [3]]
        ]
        assert layer.values == [
            [[0.5],
             [1],
             [1.5]],

            [[2],
             [2.5],
             [3]]
        ]

        assert layer[0][1][0] == connection2
        assert layer[1:][0][1][0] == connection5
        with pytest.raises(IndexError) as e:
            connection = layer[2][0][0]
        assert "Layer depth exceeded." in str(e)

        test_list = []
        for image in layer:
            for spectrum in image:
                for connection in spectrum:
                    test_list.append(connection)
        assert test_list == [
            connection1,
            connection2,
            connection3,
            connection4,
            connection5,
            connection6
        ]

        assert len(layer) == 2