예제 #1
0
class DenseTest(unittest.TestCase):
    # When a setUp() method is defined, the test runner will run that method
    # prior to each test. Likewise, if a tearDown() method is defined, the
    # test runner will invoke that method after each test.
    def setUp(self):
        self.N = 2 # number of inputs
        self.D = 3 # input dimension
        self.H = 4 # output/hidden dimension
        self.layer = Dense() # defines the layer we are testing

    def tearDown(self):
        pass

    def test_one_plus_one(self):
        self.assertEqual(1 + 1, 2)

    def test_one_plus_two(self):
        self.assertEqual(1 + 2, 3)

    def test_forward(self):
        # `numpy.linspace(start, stop, num=50...)` returns an array of evenly
        # spaced numbers over a specified interval.
        # `num=` Number of samples to generate. Default is 50. Must be non-negative.
        x = np.linspace(-1, 1, num=self.N * self.D).reshape(self.N, self.D)
        w = np.linspace(-0.5, 0.5, num=self.D * self.H).reshape(self.D, self.H)
        b = np.linspace(-0.5, 0.5, num=self.H)

        output = self.layer.forward(x, w, b)
        expected_output = np.dot(x, w) + b

        np.testing.assert_array_almost_equal(expected_output, output, decimal=9)

# TODO: line 28-30 why those ranges?
예제 #2
0
        random.shuffle(index_list)

        for i in range(image_number // batch_size):
            train_image = np.zeros((784, batch_size))
            train_label = np.zeros((10, batch_size))

            for j in range(batch_size):
                index = index_list[i * batch_size + j]
                inputx = (img[index] - 128) / 256.0
                inputx = inputx.reshape((784, 1))
                label = np.zeros([10, 1])
                label[labels[index]] = 1
                train_image[:, j:j + 1] = inputx
                train_label[:, j:j + 1] = label

            dense.forward(train_image)
            sigmoid.forward(dense.end)
            loss.forward(sigmoid.end)
            loss.backward(train_label)
            sigmoid.backward(loss.grad)
            dense.backward(sigmoid.grad)

        print("--------------")
        count = 0
        for i in range(10000):
            inputx = (test_imgs[i] - 128) / 256.0
            inputx = inputx.reshape((784, 1))
            dense.forward(inputx)
            sigmoid.forward(dense.end)
            loss.forward(sigmoid.end)
예제 #3
0
from softmax import Softmax
from data_loader import get_images_and_labels, get_test_images_and_labels

if __name__ == '__main__':

    dense = Dense(10, 784)
    dense.load_model("./model/w.npy", "./model/b.npy")
    dense1 = Dense(10, 100)
    sigmoid = Sigmoid()
    loss = Softmax()

    img, labels = get_images_and_labels()
    test_imgs, test_label = get_test_images_and_labels()

    train_label = np.zeros([10, 1])
    train_label[labels[0]] = 1
    inputx = (img[0] - 128) / 256.0

    count = 0
    for i in range(10000):
        inputx = (test_imgs[i] - 128) / 256.0
        inputx = inputx.reshape((784, 1))
        dense.forward(inputx)
        sigmoid.forward(dense.end)
        loss.forward(sigmoid.end)

        if (loss.end.argmax() == test_label[i]):
            count += 1

    print("epoch = ", "   ", count / 10000)