Пример #1
0
def sine_training():
    n = NeuralNet([1, 5, 1])
    sines = get_sines(10)
    testing_data = next(sines)

    for i in range(1000):
        x, y = next(sines)
        n.train(x, y, 10, 1, testing_data=testing_data)

    # validation
    x, y = next(sines)
    hx = n.predict(x)

    errors = []
    print('doing validation---------------------------')
    for x, y, y_predicted in zip(x, y, hx):
        errors.append(abs(y[0] - y_predicted[0]) / y[0])
        print(x[0], y[0], y_predicted[0])

    print(sum(errors) / len(errors))

    x = np.reshape([2 * math.pi * i/1000 for i in range(1000)], (1000, 1))
    y = [(i[0] - 0.5)*2 for i in n.predict(x)]
    y_actual = [i[0] for i in np.sin(x)]

    plt.plot(x, y_actual)
    plt.hold(True)
    plt.plot(x, y)
    plt.show()
Пример #2
0
    def test_random_initialize_weights(self):
        for i, layers in enumerate(self.valid_layers):
            net = NeuralNet(layers)
            net.random_initialize_weights()

            # check shape of weights
            self.assertEqual([weight.shape for weight in net.weights], self.weights_for_valid_layers[i])
Пример #3
0
    def test_instance_creation(self):
        for layers in self.valid_layers:
            self.assertIsInstance(NeuralNet(layers), NeuralNet)

        for layers in self.invalid_layers:
            with self.assertRaises(NeuralNetError):
                NeuralNet(layers)
    def run_training(self, metadata, predict_test_data=False):
        network = NeuralNet(layers=metadata['layers'],
                            lmda=metadata['lambda'])

        generator = self.reader.get(metadata['batch_size'], 'training')
        for i in range(metadata['epochs']):
            try:
                x, y = next(generator)
            except StopIteration:
                return network

            network.train(x, y, iterations=metadata['iterations'],
                          learning_rate=metadata['learning_rate'])

        return network
def validate(reader, weights):
    data_stream = reader.get(30, mode='validation')

    n = NeuralNet(metadata['layers'])
    n.weights = weights

    counter = 0
    matches = 0

    for x, y in data_stream:
        counter += x.shape[0]
        predicted = n.predict(x)
        p_digits = np.argmax(predicted, axis=1)
        y_digits = np.argmax(y, axis=1)
        accuracy = sum([1 if i == j else 0 for i, j in zip(p_digits, y_digits)]) / len(p_digits)

        print 'predicted-\n', p_digits
        print 'actual-\n', y_digits
        print('accuracy-{0}%'.format(round(accuracy * 100, 2)))
        return None     # Let's just run only one loop for the time being
Пример #6
0
def sine_training():
    n = NeuralNet([1, 5, 1])
    sines = get_sines(10)
    testing_data = next(sines)

    for i in range(1000):
        x, y = next(sines)
        n.train(x, y, 10, 1, testing_data=testing_data)

    # validation
    x, y = next(sines)
    hx = n.predict(x)

    errors = []
    print('doing validation---------------------------')
    for x, y, y_predicted in zip(x, y, hx):
        errors.append(abs(y[0] - y_predicted[0]) / y[0])
        print(x[0], y[0], y_predicted[0])

    print(sum(errors) / len(errors))

    x = np.reshape([2 * math.pi * i / 1000 for i in range(1000)], (1000, 1))
    y = [(i[0] - 0.5) * 2 for i in n.predict(x)]
    y_actual = [i[0] for i in np.sin(x)]

    plt.plot(x, y_actual)
    plt.hold(True)
    plt.plot(x, y)
    plt.show()