def normal_sequence_example(): input_size = 1 hidden_sizes = [25] output_size = 1 for i in range(0, 4): X = np.array([range(0, 100)]).T y = [1] values = [1] for j in range(1, 100): y.append(y[-1] + np.random.normal(0, 1)) y = np.array([y]).T X_scale = np.power(10, len(str(int(np.amax(X))))) y_scale = np.power(10, len(str(int(np.amax(y))))) X = X / X_scale y = y / y_scale NN = NeuralNetwork(input_size, hidden_sizes, output_size) NN.train(X, y, 10000) plt.subplot(221 + i) plot_predictions(lambda x: NN.predict(x), X, y, X_scale, y_scale) plt.show()
def random_decision_boundary_example(): input_size = 2 hidden_sizes = [10, 20, 30, 20, 10] output_size = 1 np.random.seed(0) X = np.random.randn(100, 2) y = np.random.randint(0, 2, (100, 1)) NN = NeuralNetwork(input_size, hidden_sizes, output_size) NN.train(X, y, 10000) plot_decision_boundary(lambda x: NN.predict(x), X, y) plt.show()
def moons_decision_boundary_example(): input_size = 2 hidden_sizes = [5, 10, 5] output_size = 1 np.random.seed(0) X, y = make_moons(200, noise=0.20) y = np.array([y]).T NN = NeuralNetwork(input_size, hidden_sizes, output_size) NN.train(X, y, 10000) plot_decision_boundary(lambda x: NN.predict(x), X, y) plt.show()
def exponential_sequence_example(): input_size = 1 hidden_sizes = [100, 200, 100] output_size = 1 sequence = np.arange(0, 10, 0.1) X = np.array([sequence]).T y = np.array([np.exp(sequence)]).T X_scale = np.power(10, len(str(int(np.amax(X))))) y_scale = np.power(10, len(str(int(np.amax(y))))) X = X / X_scale y = y / y_scale NN = NeuralNetwork(input_size, hidden_sizes, output_size) NN.train(X, y, 10000) plot_predictions(lambda x: NN.predict(x), X, y, X_scale, y_scale) plt.show()
def random_sequence_example(): input_size = 1 hidden_sizes = [25] output_size = 1 for i in range(0, 4): X = np.sort(np.random.uniform(0, 100, (25, input_size)), axis=0) X = np.sort(np.concatenate((X, X * 0.75)), axis=0) y = np.sort(np.random.uniform(0, 100, (25, output_size)), axis=1) y = np.sort(np.concatenate((y, y * 0.75)), axis=1) X_scale = np.power(10, len(str(int(np.amax(X))))) y_scale = np.power(10, len(str(int(np.amax(y))))) X = X / X_scale y = y / y_scale NN = NeuralNetwork(input_size, hidden_sizes, output_size) NN.train(X, y, 10000) plt.subplot(221 + i) plot_predictions(lambda x: NN.predict(x), X, y, X_scale, y_scale) plt.show()
import neural from numpy import random from neural import Layers, NeuralNetwork if __name__ == "__main__": random.seed(1) layer1 = Layers(1, 3) network = NeuralNetwork() network.addLayer(layer1) print(network.printweights) train_in = array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]]) train_out = array([[0, 1, 1, 0]]).T network.train(train_in, train_out, 70000) print(network.printweights) print "Stage 3) Considering a new situation [1, 1, 0] -> ?: " output = network.calc(array([1, 1, 0])) print output
direction = 0 elif event.key == K_RIGHT: direction = 1 ''' if collision(bar.position, bar.size[0], ball.position): ball.new_position_on_grid_random() score += 1 else: error = ball.move() error_count += error if error_count == 1: network.train(data, resp) generation += 1 score = 0 error_count = 0 ball_pos = ball.position[0] - SCREEN_SIZE[0] / 2 bar_pos = bar.position[0] - SCREEN_SIZE[0] / 2 bar_pos += bar.size[0] / 2 ball_pos = ball_pos / 100 bar_pos = bar_pos / 100 direction = network.feedforward(np.array([ball_pos, bar_pos])) direction = 1 if direction > 0.5 else 0 ''' df.loc[index] = [ball_pos, bar_pos, direction]
images = utils.read_images(base_dir) images = utils.setup_images(images, image_size) uniq_classes = set([cl for _, cl in images]) classes = {cl: i for i, cl in enumerate(uniq_classes)} im_size = len(images) input_array = numpy.zeros((im_size, in_layer_size), "float") output_array = numpy.zeros((im_size, out_layer_size), "float") - 1.0 print "Setup network arrays..." for i in range(im_size): (image, fl) = images[i] a = numpy.array(list(image), "float") print "#%d image from dir(class): " % i + fl input_array[i, :] = a.flatten() output_array[i, classes[fl]] = 1.0 print input_array[i, :] print output_array[i, :] layers = (in_layer_size, hidden_layer_size, out_layer_size) network = NeuralNetwork(layers) network.train(input_array, output_array, True) print images[0][0] a = numpy.array(list(images[0][0]), "float") cc = numpy.zeros((im_size, in_layer_size), "float") cc[0, :] = a.flatten() res = network.classify(cc) print res[0].shape