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()
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()
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