Exemple #1
0
def main():

    train_data = np.array([[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5],
                           [0, 6], [0, 7], [1, 0], [1, 1], [1, 2], [1, 3],
                           [1, 4], [1, 5], [1, 6], [1, 7], [2, 0], [2, 1],
                           [2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7],
                           [3, 0], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5],
                           [3, 6], [3, 7], [4, 0], [4, 1], [4, 2], [4, 3],
                           [4, 4], [4, 5], [4, 6], [4, 7], [5, 0], [5, 1],
                           [5, 2], [5, 3], [5, 4], [5, 5], [5, 6], [5, 7],
                           [6, 0], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5],
                           [6, 6], [6, 7], [7, 0], [7, 1], [7, 2], [7, 3],
                           [7, 4], [7, 5], [7, 6], [7, 7]])
    label_data = np.array(
        [[0], [1], [2], [3], [4], [5], [6], [7], [1], [0], [3], [2], [5], [4],
         [7], [6], [2], [3], [0], [1], [6], [7], [4], [5], [3], [2], [1], [0],
         [7], [6], [5], [4], [4], [5], [6], [7], [0], [1], [2], [3], [5], [4],
         [7], [6], [1], [0], [3], [2], [6], [7], [4], [5], [2], [3], [0], [1],
         [7], [6], [5], [4], [3], [2], [1], [0]])
    learn_rate = 0.15
    epoch = 600000
    loss_list = []
    w1, b1, w2, b2 = init(input_size=2, hidden_size=16, output_size=1)
    #print(w1, w2, b1, b2)

    for i in range(epoch):
        affine1 = Affine(w1, b1)
        affine2 = Affine(w2, b2)
        sigmoid = Sigmoid()
        loss = MSE()
        x1 = affine1.forward(train_data)
        y1 = sigmoid.forward(x1)
        x2 = affine2.forward(y1)
        ls = loss.mean_square_error(x2, label_data)
        print(ls)
        loss_list.append(ls)
        dout = loss.backward(x2, label_data)
        dx = affine2.backward(dout)
        w2 = w2 - learn_rate * affine2.dw
        b2 = b2 - learn_rate * affine2.db
        dy1 = sigmoid.backward(dx)
        dx = affine1.backward(dy1)
        b1 = b1 - learn_rate * affine1.db
        w1 = w1 - learn_rate * affine1.dw
    #print(w1,w2,b1,b2)

    plt.plot(loss_list)
    plt.show()
    acc(w1, b1, w2, b2, train_data, label_data)
class TestAffine(unittest.TestCase):
    def setUp(self):
        W = np.random.randn(2, 4)
        b = np.random.randn(4)
        self.affine = Affine(W, b)

    def test_forward(self):
        out = self.affine.forward(np.random.randn(4, 2))
        self.assertEqual((4, 4), out.shape)

    def test_backward(self):
        self.affine.forward(np.random.randn(4, 2))
        dx = self.affine.backward(np.random.randn(4, 4))
        self.assertEqual((4, 2), dx.shape)
Exemple #3
0
class TestAffine(unittest.TestCase):
    def setUp(self):
        W = np.array([[-0.22472106, -0.42868683, 0.21713442],
                      [-0.13635294, 0.45327181, -1.31839392]])
        b = np.array([1.55270156, 1.44441689, -1.69451485])
        self.affine = Affine(W, b)

    def test_forward(self):
        x = np.array([1.52949391, -0.81788271])
        assert_almost_equal(([1.32051278, 0.41801982, -0.28411748]),
                            self.affine.forward(x))

    def test_backward(self):
        x = np.array([1.52949391, -0.81788271])
        self.affine.forward(x)
        dout = 1
        assert_almost_equal(
            np.array([[-0.2247211, -0.1363529], [-0.4286868, 0.4532718],
                      [0.2171344, -1.3183939]]), self.affine.backward(dout))