Ejemplo n.º 1
0
class TestSigmoid(unittest.TestCase):
    def setUp(self):
        self.sigmoid = Sigmoid()

    def test_forward(self):
        x = np.array([[1.0, -0.5], [-2.0, 3.0]])
        assert_almost_equal(
            ([[0.73105858, 0.37754067], [0.11920292, 0.95257413]]),
            self.sigmoid.forward(x))

    def test_backward(self):
        x = np.array([[1.0, -0.5], [-2.0, 3.0]])
        self.sigmoid.forward(x)
        dout = 1
        assert_almost_equal(
            np.array([[0.0386563, 0.0552267], [0.0110237, 0.0020409]]),
            self.sigmoid.backward(self.sigmoid.backward(dout)))
Ejemplo n.º 2
0
class TestSigmoid(unittest.TestCase):
    def setUp(self):
        self.sigmoid = Sigmoid()
        self.x = np.random.randn(10, 4)

    def test_forward(self):
        out = self.sigmoid.forward(self.x)
        self.assertEqual((10, 4), out.shape)

    def test_backward(self):
        self.sigmoid.forward(self.x)
        dout = np.random.randn(10, 4)
        dx = self.sigmoid.backward(dout)
        self.assertEqual((10, 4), dx.shape)
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
class NeuralNetwork:
    def __init__(self,
                 input_width,
                 output_width,
                 hidden_width=None,
                 depth=3,
                 learning_rate=.1,
                 activation=Sigmoid):
        self.network = []
        self.learning_rate = learning_rate
        self.activation = Sigmoid()
        last_width = input_width
        for layer_idx in range(depth - 2):
            if isinstance(hidden_width,
                          (collections.Sequence, tuple, np.ndarray)):
                width = hidden_width[layer_idx]
            elif isinstance(hidden_width, (int, float)):
                width = hidden_width
            else:
                width = np.abs(input_width - output_width) / (depth - 1)
            scale = last_width**-.5
            layer = np.random.normal(scale=scale, size=(width, last_width))
            self.network.append(layer)
            last_width = width
        scale = last_width**-.5
        self.network.append(
            np.random.normal(scale=scale, size=(output_width, last_width)))

    def predict(self, inputs):
        for layer in self.network:
            inputs = self.activation.forward(np.dot(layer, inputs))
        return inputs

    def cost_f(self, predictions, targets):
        return targets - predictions

    def train(self, features, labels):
        outputs = [np.array(features, ndmin=2).T]
        for layer in self.network:
            outputs.append(self.activation.forward(np.dot(layer, outputs[-1])))

        labels = np.array(labels, ndmin=2).T
        errors = self.cost_f(outputs[-1], labels)
        for l_idx, layer in enumerate(self.network[::-1]):
            p_deltas = errors * self.activation.backward(outputs[-l_idx - 1])
            errors = np.dot(layer.T, errors)
            layer += self.learning_rate * np.dot(p_deltas,
                                                 outputs[-l_idx - 2].T)
Ejemplo n.º 5
0
            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)

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

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