예제 #1
0
    def test_max_pooling_1d_array(self):
        # arrange
        input = np.array([[[1, 2, -3, 4]]])
        expected = np.array([[[2, 4]]])
        pooling = MaxPoolingStep(2)

        # act
        output = pooling.forward_propagation(input)

        # assert
        self.assertEqual(expected.shape, output.shape)
        self.assertTrue(
            all(
                np.equal(expected.reshape(expected.size),
                         output.reshape(output.size))))
예제 #2
0
    def test_3_steps_with_sigmoid_output_with_bias_2d_input(self):
        # arrange
        num_classes = 3
        steps = [
            ConvolutionalStep(filter_size=(3, 3),
                              padding=1,
                              num_of_kernels=1,
                              x0='ones',
                              stride=2),
            MaxPoolingStep(2),
            OutputStep(x0='ones', activation=Sigmoid)
        ]
        network = CnnNetwork(steps)
        network.fit([], list(range(num_classes)))
        expected_output = np.array([1, 1, 1])

        inputs = [
            [1, 2, 3, 4, 5],
            [5, 6, 7, 8, 5],
            [9, 10, 11, 12, 5],
            [1, 2, 3, 4, 5],
            [5, 6, 7, 8, 5],
        ]

        # act
        output = network.forward_propagation(inputs)

        # assert
        self.assertEqual(expected_output.shape, output.shape)
        self.assertTrue(
            all(
                np.equal(expected_output.reshape(expected_output.size),
                         output.reshape(output.size))))
예제 #3
0
    def test_max_pooling_2d_array(self):
        input = np.array([[[4, 5, 7, 2, 8, 5], [3, 5, 7, 82, 35, 4],
                           [-3, -7, 23, 6, 2, 0], [-98, 23, 59, -23, 0, 2],
                           [1, 2, 3, 4, 5, 6], [-7, -6, -2, 7, 0.45, 2]]])
        expected = np.array([[[5, 82, 35], [23, 59, 2], [2, 7, 6]]])
        pooling = MaxPoolingStep(2)

        # act
        output = pooling.forward_propagation(input)

        # assert
        self.assertEqual(expected.shape, output.shape)
        self.assertTrue(
            all(
                np.equal(expected.reshape(expected.size),
                         output.reshape(output.size))))
예제 #4
0
    def test_pooling_saves_z_with_0_for_unpooled(self):
        input = np.array([[[4, 5, 7, 2, 8, 5], [3, 5, 7, 82, 35, 4],
                           [-3, -7, 23, 6, 2, 0], [-98, 23, 59, -23, 0, 2],
                           [1, 2, 3, 4, 5, 6], [-7, -6, -2, 7, 0.45, 2]]])
        expected_z = [
            0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0,
            0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0
        ]

        pooling = MaxPoolingStep(2)

        # act
        output = pooling.forward_propagation(input)

        # assert
        self.assertEqual(len(expected_z), len(pooling.z))
        self.assertEquals(expected_z, pooling.z)
예제 #5
0
    def test_pooling_back_drop(self):
        # arrange
        error = 6
        inputs = np.array([[[7, 8], [0, 2]]])
        expected = np.array([[[0, error], [0, 0]]])

        pooling = MaxPoolingStep(2)
        output = pooling.forward_propagation(inputs)

        # act
        backdrop = pooling.back_prop(np.array([error]), None)

        # assert
        self.assertEqual(expected.shape, backdrop.shape)
        self.assertTrue(
            all(
                np.equal(expected.reshape(expected.size),
                         output.reshape(backdrop.size))))
예제 #6
0
def fit():
    X = []
    y = []

    get_cards(X, y)

    filter_size = (6, 6)
    padding = 0
    stride = 1
    x0 = 'random'

    steps = [
        ConvolutionalStep(filter_size=filter_size,
                          padding=padding,
                          stride=stride,
                          num_of_kernels=4,
                          x0=x0,
                          activation=Relu),
        MaxPoolingStep(3),
        ConvolutionalStep(filter_size=filter_size,
                          padding=padding,
                          stride=stride,
                          num_of_kernels=7,
                          x0=x0,
                          activation=Relu),
        MaxPoolingStep(3),
        Flatten(),
        OutputStep(x0=x0, activation=Softmax)
    ]

    network = CnnNetwork(steps)

    network.fit(
        X,
        y,
        CrossEntropy,
        iterations=100,
        batch_size=len(X),
        learning_rate=1e-5,
        verbose=1,
    )
예제 #7
0
    def test_max_pooling_3d_array(self):
        input = np.array([[
            [1, 2, 3],
            [
                4,
                3,
                2,
            ],
            [7, -4, 7],
        ], [[0, -23, 100], [2, 5, 6], [-23.9, 4, 20.7]]])
        expected = np.array([[[7]], [[100]]])
        pooling = MaxPoolingStep(3)

        # act
        output = pooling.forward_propagation(input)

        # assert
        self.assertEqual(expected.shape, output.shape)
        self.assertTrue(
            all(
                np.equal(expected.reshape(expected.size),
                         output.reshape(output.size))))
예제 #8
0
    def test_3_steps_flow_1d_input(self):
        # arrange
        steps = [
            ConvolutionalStep(filter_size=(3, 3), num_of_kernels=1, x0='ones'),
            ReluActivation(),
            MaxPoolingStep(2)
        ]
        network = CnnNetwork(steps)
        network.fit([], [])
        expected_output = np.array([[[9, 5]]])

        input = [1, 2, 3, 4, -2, -9]

        # act
        output = network.forward_propagation(input)

        # assert
        self.assertEqual(expected_output.shape, output.shape)
        self.assertTrue(
            all(
                np.equal(expected_output.reshape(expected_output.size),
                         output.reshape(output.size))))