nn = Network([n, 20, N], [None, 'sigmoid', 'softmax'], [True, True, False], layer_weight_means_and_stds=[(0, 0.1), (0, 0.1)]) # Learning rate eta = 0.001 # Number of iterations to complete N_iterations = 10000 # Perform gradient descent for i in range(N_iterations): # For stochastic gradient descent, take random samples of X and T # Run the features through the neural net (to compute a and z) y_pred = nn.feed_forward(X) # Compute the gradient grad_w = nn._gradient_fun(X, T) # Update the neural network weight matrices for w, gw in zip(nn.weights, grad_w): w -= eta * gw # Print some statistics every thousandth iteration if i % 1000 == 0: misclassified = sum(np.argmax(y_pred, axis=1) != y.ravel()) print( "Iteration: {0}, Objective Function Value: {1:3f}, Misclassified: {2}" .format(i, nn._J_fun(X, T), misclassified))
layer_weight_means_and_stds=[(0, 0.1), (0, 0.1)]) eta = 0.005 # Number of iterations to complete N_iterations = 500000 batch_size = int(m / 10) # Perform gradient descent for i in range(N_iterations): # For stochastic gradient descent, take random samples of X and T batch = np.random.randint(0, m, size=batch_size) # Run the features through the neural net (to compute a and z) y_pred = nn.feed_forward(X) # Compute the gradient grad_w = nn._gradient_fun(X, y) # Update the neural network weight matrices for w, gw in zip(nn.weights, grad_w): w -= eta * gw # Print some statistics every thousandth iteration if i % 10000 == 0: print('Iteration: {0}, Objective Function Value: {1:3f}' .format(i, nn._J_fun(X, y_pred))) print('Total error: {}'.format( np.abs(np.sum(y_pred - y) / np.sum(y))))
from neural_network import Network # define net = Network([2, 3, 3]) dataset = [ ((0, 0), (0, 0, 0)), ((0, 1), (0, 1, 1)), ((1, 0), (0, 1, 1)), ((1, 1), (1, 1, 0)), ] # teach for net_error in net.teach_loop(dataset): print(net_error) # evaluate results while True: values = input("Inputs: ") while True: try: inputs = tuple(int(v) for v in values.split()) except ValueError: pass else: break # eval outputs = net.feed_forward(inputs) print(outputs) print([round(o) for o in outputs])
class AutonomousSnake: directions = {0: np.array([0, -1]), 1: np.array([1, -1]), 2: np.array([1, 0]), 3: np.array([1, 1]), 4: np.array([0, 1]), 5: np.array([-1, 1]), 6: np.array([-1, 0]), 7: np.array([-1, -1])} def __init__(self, pos=[13, 13], length=5, base_max_moves=100, field_width=25, field_height=25): self.field_width = field_width self.field_height = field_height self.position = np.array([pos[0], pos[1]]) self.velocity = self.directions[np.random.choice([0, 2, 4, 6])] self.length = length self.score = 0 self.brain = Network([24, 18, 4]) self.tail = np.empty(shape=[0, 2]) for i in range(self.length - 1, 0, -1): self.tail = np.append(self.tail, [self.position - i * self.velocity], axis=0) self.alive = True self.time_alive = 0 self.max_moves = base_max_moves self.moves_left = self.max_moves self.grow_count = 0 self.lastMoveDir = np.array(self.velocity) self.food = self.place_food() self.vision = np.zeros(24) self.fitness = 0 def think(self): self.see() decision = self.brain.feed_forward(self.vision.reshape((24, 1))) # decision_arg = np.argmax(decision) chosen_direction = self.directions[np.argmax(decision) * 2] if not np.array_equal(chosen_direction, -self.velocity): self.velocity = chosen_direction # self.velocity = self.directions[decision_arg * 2] # if not np.array_equal(self.velocity, -self.directions[ # decision_arg * 2]) else self.velocity def move(self): if self.will_collide(): self.alive = False if not self.alive: return self.tail = np.append(self.tail, [self.position], axis=0) self.position += self.velocity if self.grow_count == 0: self.tail = np.delete(self.tail, 0, 0) else: self.grow_count -= 1 self.length += 1 self.lastMoveDir = self.velocity if np.array_equal(self.position, self.food): self.eat() self.time_alive += 1 self.moves_left -= 1 if self.moves_left <= 0: self.alive = False def eat(self): self.food = self.place_food() self.grow_count += 1 self.score += 1 self.moves_left = self.max_moves def place_food(self): pos = np.array([np.random.randint(0, self.field_width), np.random.randint(0, self.field_height)]) while self.occupied(pos): pos = np.array([np.random.randint(0, self.field_width), np.random.randint(0, self.field_height)]) return pos def calc_fitness(self): if self.score < 10: self.fitness = math.floor(self.time_alive ** 2 * 2 ** self.score) else: self.fitness = self.time_alive ** 2 * 2 ** 10 * (self.score - 9) # Check if the position is occupied by the body of the snake def occupied(self, pos): for cell in self.tail: if np.array_equal(cell, pos): return True return np.array_equal(self.position, pos) def will_collide(self): next_position = self.position + self.velocity if self.is_on_tail(next_position): return True return (next_position[0] < 0 or next_position[0] >= self.field_width or next_position[1] < 0 or next_position[ 1] >= self.field_height) def is_on_tail(self, pos): for cell in self.tail: if np.array_equal(pos, cell): return True return False def see(self): self.vision = np.array([]) for d in self.directions: d_vision = self.look_in_direction(self.directions[d]) self.vision = np.append(self.vision, d_vision) def look_in_direction(self, direction): cur_pos = self.position + direction vision = np.zeros(3) food_found = False tail_found = False distance = 1.0 while not ( cur_pos[0] < 0 or cur_pos[0] >= self.field_width or cur_pos[1] < 0 or cur_pos[1] >= self.field_height): if not food_found and np.array_equal(self.food, cur_pos): vision[0] = 1.0 food_found = True if not tail_found and self.is_on_tail(cur_pos): vision[1] = 1.0 / distance tail_found = True cur_pos = cur_pos + direction distance += 1.0 vision[2] = 1.0 / distance return vision def crossover_brain(self, other): new_snake = AutonomousSnake() if self.fitness > other.fitness: new_snake.brain = self.brain.crossover(other.brain) else: new_snake.brain = other.brain.crossover(self.brain) return new_snake def mutate(self, rate, mag): self.brain.mutate(rate, mag) def reincarnate(self): self.position = np.array((13, 13)) self.velocity = self.directions[np.random.choice([0, 2, 4, 6])] self.length = 5 self.alive = True self.time_alive = 0 self.grow_count = 0 self.tail = np.empty(shape=[0, 2]) for i in range(self.length - 1, 0, -1): self.tail = np.append(self.tail, [self.position - i * self.velocity], axis=0) return self def set_training_config(self, config): self.max_moves = config.max_moves self.moves_left = self.max_moves def save(self, population, generation): brain_data = {"sizes": self.brain.sizes, "weights": self.brain.weights, "biases": self.brain.biases} data = {"generation": generation, # "id": self.sid, "fitness": self.fitness, "brain": brain_data} try: pickle_out = open("populations/pop{}/gen{}.pickle".format(population, generation), "wb") except Exception: os.mkdir("populations/pop{}".format(population)) pickle_out = open("populations/pop{}/gen{}.pickle".format(population, generation), "wb") pickle_out = open("populations/pop{}/gen{}.pickle".format(population, generation), "wb") pickle.dump(data, pickle_out) pickle_out.close() @classmethod def load_snake(cls, filename): pickle_in = open(filename, 'rb') data = pickle.load(pickle_in) pickle_in.close() brain = Network(data["brain"]["sizes"]) brain.weights = [np.array(w) for w in data["brain"]["weights"]] brain.biases = [np.array(b) for b in data["brain"]["biases"]] snake = AutonomousSnake() snake.fitness = data["fitness"] snake.brain = brain return snake def __copy__(self): snake_copy = AutonomousSnake() snake_copy.base_max_moves = self.max_moves snake_copy.brain = copy.copy(self.brain) return snake_copy