示例#1
0
    def test_ZeropadLayer(self):
        inputs = rs.ConnectionLayer(depth=2, height=3, width=3)
        inputs.values = [[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                         [[10, 11, 12], [13, 14, 15], [16, 17, 18]]]

        outputs = rs.ConnectionLayer(depth=2, height=8, width=8)
        rs.utils.ZeropadLayer(inputs, outputs, 3, 2, 4, 1)

        assert outputs.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],
                                   [0, 0, 0, 0, 0, 0, 0, 0],
                                   [0, 0, 0, 1, 2, 3, 0, 0],
                                   [0, 0, 0, 4, 5, 6, 0, 0],
                                   [0, 0, 0, 7, 8, 9, 0, 0],
                                   [0, 0, 0, 0, 0, 0, 0, 0]],
                                  [[0, 0, 0, 0, 0, 0, 0, 0],
                                   [0, 0, 0, 0, 0, 0, 0, 0],
                                   [0, 0, 0, 0, 0, 0, 0, 0],
                                   [0, 0, 0, 0, 0, 0, 0, 0],
                                   [0, 0, 0, 10, 11, 12, 0, 0],
                                   [0, 0, 0, 13, 14, 15, 0, 0],
                                   [0, 0, 0, 16, 17, 18, 0, 0],
                                   [0, 0, 0, 0, 0, 0, 0, 0]]]

        outputs = rs.ConnectionLayer(depth=2, height=5, width=5)
        rs.utils.ZeropadLayer(inputs, outputs, 1, 1, 1, 1)

        assert outputs.values == [
            [[0, 0, 0, 0, 0], [0, 1, 2, 3, 0], [0, 4, 5, 6, 0],
             [0, 7, 8, 9, 0], [0, 0, 0, 0, 0]],
            [[0, 0, 0, 0, 0], [0, 10, 11, 12, 0], [0, 13, 14, 15, 0],
             [0, 16, 17, 18, 0], [0, 0, 0, 0, 0]],
        ]
    def test_IdentityActivationLayer(self):
        inputs = rs.ConnectionLayer(depth=1, height=2, width=2)
        outputs = rs.ConnectionLayer(depth=1, height=2, width=2)

        rs.activation.IdentityActivationLayer(inputs, outputs)

        assert inputs.values == [[[0, 0], [0, 0]]]
        assert outputs.values == [[[0, 0], [0, 0]]]

        inputs.values = [[[-1, -2], [1, 2]]]
        assert outputs.values == [[[-1, -2], [1, 2]]]
示例#3
0
    def test_SigmoidActivationLayer(self):
        inputs = rs.ConnectionLayer(depth=1, height=2, width=2)
        outputs = rs.ConnectionLayer(depth=1, height=2, width=2)

        rs.activation.SigmoidActivationLayer(inputs, outputs)

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

        inputs.values = [[[-1, -2], [1, 2]]]
        assert [[[round(value, 2) for value in row] for row in depth_slice]
                for depth_slice in outputs.values] == [[[0.27, 0.12],
                                                        [0.73, 0.88]]]
    def test_MaxPoolingLayer(self):
        inputs = rs.ConnectionLayer(depth=1, height=12, width=12)
        inputs.values = [
            [[0.23, -0.41, -0.1, -0.19, -1.26, -0.07, 1.53, -1.11, 0.03, 1.66, 1.88, -0.62],
             [-1.63, 0.26, 0.21, 0.29, -1.56, -1.79, -0.08, -0.74, -1.52, -0.82, -1.5, 1.41],
             [-0.32, -1.99, 0.74, -1.27, -1.59, -1.7, 1.28, -1.51, 0.11, 0.14, 0.03, 1.53],
             [0.18, 0.78, -0.06, -0.46, -0.05, 0.67, -0.12, 0.96, -1.5, -0.16, 1.94, -1.26],
             [1.65, -0.23, 1.86, 1.79, 0.99, -0.09, 0.11, 1.63, -1.37, -0.75, 0.54, -0.25],
             [1.47, -0.08, 0.89, 1.79, -1.55, 0.16, -0.55, -1.12, 0.95, -0.18, -0.05, -1.07],
             [1.59, -1.59, 1.53, 0.43, 0.07, 0.71, 0.75, 1.92, 0.07, 0.13, -1.28, -0.69],
             [-1.24, -1.15, 0.89, 1.9, 0.2, 1.73, -1.64, -1.6, -1.78, -1.1, -0.27, 0.5],
             [1.03, 1.84, 1.3, -1.59, 1.91, -1.31, -0.58, -1.95, 1.26, 1.57, 0.68, -0.99],
             [-1.75, -1.63, 0.36, 1.68, 1.55, 0.67, 0.12, -1.84, -1.55, -1.91, -1.26, -0.56],
             [-1.31, -1.57, -1.74, 0.21, -0.18, 1.22, -0.74, 0.92, 1.27, -0.44, -0.47, -1.04],
             [0.96, -1.0, -0.46, 0.32, -1.59, -1.08, 1.81, 1.27, -0.38, 1.48, -1.51, -1.07]]
        ]

        outputs = rs.ConnectionLayer(depth=1, height=6, width=6)
        rs.pooling.MaxPoolingLayer(inputs, outputs, 2, 2, 2, 2)

        assert [[[round(value, 2) for value in row]
                 for row in depth_slice]
                for depth_slice in outputs.values] == [
            [[0.26, 0.29, -0.07, 1.53, 1.66, 1.88],
             [0.78, 0.74, 0.67, 1.28, 0.14, 1.94],
             [1.65, 1.86, 0.99, 1.63, 0.95, 0.54],
             [1.59, 1.90, 1.73, 1.92, 0.13, 0.50],
             [1.84, 1.68, 1.91, 0.12, 1.57, 0.68],
             [0.96, 0.32, 1.22, 1.81, 1.48, -0.47]]
        ]

        outputs = rs.ConnectionLayer(depth=1, height=10, width=10)
        rs.pooling.MaxPoolingLayer(inputs, outputs, 3, 3, 1, 1)

        assert [[[round(value, 2) for value in row]
                 for row in depth_slice]
                for depth_slice in outputs.values] == [
            [[0.74, 0.74, 0.74, 0.29, 1.53, 1.53, 1.53, 1.66, 1.88, 1.88],
             [0.78, 0.78, 0.74, 0.67, 1.28, 1.28, 1.28, 0.96, 1.94, 1.94],
             [1.86, 1.86, 1.86, 1.79, 1.28, 1.63, 1.63, 1.63, 1.94, 1.94],
             [1.86, 1.86, 1.86, 1.79, 0.99, 1.63, 1.63, 1.63, 1.94, 1.94],
             [1.86, 1.86, 1.86, 1.79, 0.99, 1.92, 1.92, 1.92, 0.95, 0.54],
             [1.59, 1.90, 1.90, 1.90, 1.73, 1.92, 1.92, 1.92, 0.95, 0.50],
             [1.84, 1.90, 1.91, 1.91, 1.91, 1.92, 1.92, 1.92, 1.57, 1.57],
             [1.84, 1.90, 1.91, 1.91, 1.91, 1.73, 1.26, 1.57, 1.57, 1.57],
             [1.84, 1.84, 1.91, 1.91, 1.91, 1.22, 1.27, 1.57, 1.57, 1.57],
             [0.96, 1.68, 1.68, 1.68, 1.81, 1.81, 1.81, 1.48, 1.48, 1.48]]
        ]
    def test_L2NormPoolingLayer(self):
        inputs = rs.ConnectionLayer(depth=1, height=12, width=12)
        inputs.values = [[[
            0.23, -0.41, -0.1, -0.19, -1.26, -0.07, 1.53, -1.11, 0.03, 1.66,
            1.88, -0.62
        ],
                          [
                              -1.63, 0.26, 0.21, 0.29, -1.56, -1.79, -0.08,
                              -0.74, -1.52, -0.82, -1.5, 1.41
                          ],
                          [
                              -0.32, -1.99, 0.74, -1.27, -1.59, -1.7, 1.28,
                              -1.51, 0.11, 0.14, 0.03, 1.53
                          ],
                          [
                              0.18, 0.78, -0.06, -0.46, -0.05, 0.67, -0.12,
                              0.96, -1.5, -0.16, 1.94, -1.26
                          ],
                          [
                              1.65, -0.23, 1.86, 1.79, 0.99, -0.09, 0.11, 1.63,
                              -1.37, -0.75, 0.54, -0.25
                          ],
                          [
                              1.47, -0.08, 0.89, 1.79, -1.55, 0.16, -0.55,
                              -1.12, 0.95, -0.18, -0.05, -1.07
                          ],
                          [
                              1.59, -1.59, 1.53, 0.43, 0.07, 0.71, 0.75, 1.92,
                              0.07, 0.13, -1.28, -0.69
                          ],
                          [
                              -1.24, -1.15, 0.89, 1.9, 0.2, 1.73, -1.64, -1.6,
                              -1.78, -1.1, -0.27, 0.5
                          ],
                          [
                              1.03, 1.84, 1.3, -1.59, 1.91, -1.31, -0.58,
                              -1.95, 1.26, 1.57, 0.68, -0.99
                          ],
                          [
                              -1.75, -1.63, 0.36, 1.68, 1.55, 0.67, 0.12,
                              -1.84, -1.55, -1.91, -1.26, -0.56
                          ],
                          [
                              -1.31, -1.57, -1.74, 0.21, -0.18, 1.22, -0.74,
                              0.92, 1.27, -0.44, -0.47, -1.04
                          ],
                          [
                              0.96, -1.0, -0.46, 0.32, -1.59, -1.08, 1.81,
                              1.27, -0.38, 1.48, -1.51, -1.07
                          ]]]

        outputs = rs.ConnectionLayer(depth=1, height=6, width=6)
        rs.pooling.L2NormPoolingLayer(inputs, outputs, 2, 2, 2, 2)

        assert [[[round(value, 2) for value in row] for row in depth_slice]
                for depth_slice in outputs.values
                ] == [[[1.72, 0.42, 2.69, 2.03, 2.40, 2.86],
                       [2.17, 1.54, 2.42, 2.20, 1.52, 2.77],
                       [2.22, 3.26, 1.85, 2.06, 1.84, 1.23],
                       [2.81, 2.63, 1.88, 3.08, 2.10, 1.56],
                       [3.19, 2.68, 2.87, 2.75, 3.18, 1.83],
                       [2.47, 1.84, 2.28, 2.51, 2.04, 2.17]]]

        outputs = rs.ConnectionLayer(depth=1, height=10, width=10)
        rs.pooling.L2NormPoolingLayer(inputs, outputs, 3, 3, 1, 1)

        assert [
            [[round(value, 2) for value in row] for row in depth_slice]
            for depth_slice in outputs.values
        ] == [[[2.76, 2.56, 2.98, 3.79, 4.08, 3.76, 3.22, 3.14, 3.40, 3.73],
               [2.83, 2.67, 2.73, 3.66, 3.63, 3.46, 3.16, 3.01, 3.36, 3.56],
               [3.39, 3.70, 3.54, 3.45, 2.92, 3.30, 3.42, 3.26, 2.97, 2.94],
               [3.14, 3.40, 3.78, 3.24, 2.05, 2.38, 3.19, 3.24, 3.12, 2.73],
               [4.07, 3.97, 4.07, 3.24, 2.19, 3.00, 3.36, 3.32, 2.31, 2.06],
               [3.74, 3.84, 3.66, 3.60, 3.09, 3.82, 3.89, 3.58, 2.65, 2.20],
               [4.15, 4.29, 3.85, 3.90, 3.53, 4.34, 4.29, 4.30, 3.26, 2.75],
               [3.95, 4.35, 4.20, 4.50, 3.78, 4.24, 4.47, 4.92, 4.07, 3.32],
               [4.38, 4.33, 4.04, 3.89, 3.26, 3.55, 3.81, 4.47, 3.77, 3.31],
               [3.90, 3.53, 3.36, 3.32, 3.45, 3.58, 3.72, 4.00, 3.78, 3.56]]]
    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