예제 #1
0
파일: test_nn.py 프로젝트: sumesh1/numpyCNN
    def test_dropout(self):
        np.random.seed(1)
        x = np.random.randn(3, 5)
        w1 = np.random.randn(2, 3)
        b1 = np.random.randn(2, 1).reshape(1, 2)
        w2 = np.random.randn(3, 2)
        b2 = np.random.randn(3, 1).reshape(1, 3)
        w3 = np.random.randn(1, 3)
        b3 = np.random.randn(1, 1)

        nn = NeuralNetwork(
            input_dim=3,
            layers=[
                FullyConnected(2, relu),
                Dropout(keep_prob=0.7),
                FullyConnected(3, relu),
                Dropout(keep_prob=0.7),
                FullyConnected(1, sigmoid)
            ],
            cost_function=sigmoid_cross_entropy,
        )
        nn.layers[0].w = w1
        nn.layers[0].b = b1
        nn.layers[2].w = w2
        nn.layers[2].b = b2
        nn.layers[4].w = w3
        nn.layers[4].b = b3
        np.random.seed(1)
        a_last = nn.forward_prop(x.T)
        np.testing.assert_array_almost_equal(
            a_last,
            np.array([[0.369747, 0.496834, 0.045651, 0.014469, 0.369747]]).T)
예제 #2
0
파일: test_nn.py 프로젝트: sumesh1/numpyCNN
    def test_forward(self):
        np.random.seed(6)
        x = np.random.randn(5, 4)
        x_num = 5
        h1_num = 4
        h2_num = 3
        y_num = 1

        w1 = np.random.randn(h1_num, x_num)
        b1 = np.random.randn(1, h1_num)
        w2 = np.random.randn(h2_num, h1_num)
        b2 = np.random.randn(1, h2_num)
        w3 = np.random.randn(y_num, h2_num)
        b3 = np.random.randn(1, y_num)

        nn = NeuralNetwork(input_dim=x_num,
                           layers=[
                               FullyConnected(h1_num, relu),
                               FullyConnected(h2_num, relu),
                               FullyConnected(y_num, sigmoid)
                           ],
                           cost_function=sigmoid_cross_entropy)

        nn.layers[0].w = w1
        nn.layers[0].b = b1
        nn.layers[1].w = w2
        nn.layers[1].b = b2
        nn.layers[2].w = w3
        nn.layers[2].b = b3

        a_last = nn.forward_prop(x.T)
        np.testing.assert_array_almost_equal(
            a_last,
            np.array([[0.03921668, 0.70498921, 0.19734387, 0.04728177]]).T)