class Individual:
    dimensions = (5, 5, 5, 1)
    feature_limits = None

    def __init__(self, feature_limits=None, weights=None, mutate_prob=0.03):
        if weights is None:
            self.nn = NeuralNetwork(Individual.dimensions)
        else:
            self.nn = NeuralNetwork(Individual.dimensions,
                                    weights=weights,
                                    mutate_prob=mutate_prob)

        self.bird = attrib.Bird()
        Individual.feature_limits = feature_limits

    def find_fitness(self, X):
        # step-1 : feature scaling
        X = X / self.feature_limits
        # step-2 : feed forward
        self.nn.feed_forward(X)
        y = self.nn.y
        if y > 0.5:
            self.bird.fly()

        return self.bird.score
    def play(self, board):
        nn = NeuralNetwork(n_inputs = nn_inputs,
                           n_hidden = nn_hidden,
                           size_hidden = nn_hidden_size,
                           n_output = nn_output,
                           weights = self.genome)

        flattened_board = board[0] + board[1] + board[2]
        floats = [{self.another: 0.0,
                   ' ': 0.5,
                   self.symbol: 1.0}[char]
                  for char in flattened_board]
        possibilities = nn.evaluate(floats)

        coords = {}
        for i in range(0,3):
            for j in range(0,3):
                coords[(i, j)] = possibilities[i * 3 + j]

        best_moves = sorted(list(coords.items()), key=lambda x: x[-1])
        # print(best_moves)
        for coord, score in best_moves:
            x, y = coord
            if board[x][y] == ' ':
                return (x, y)

        print(possibilities)

        def randpos():
            return (randint(0, 2), randint(0,2))

        while True:
            x, y = randpos()
            if board[x][y] == ' ':
                return (x, y)
示例#3
0
    def __init__(self, screen):
        self.screen = screen

        # Create two paddles
        self.paddleA = Paddle(WHITE, 10, 100)
        self.paddleA.rect.x = 20
        self.paddleA.rect.y = 200

        self.paddleB = Paddle(WHITE, 10, 100)
        self.paddleB.rect.x = 670
        self.paddleB.rect.y = 200

        self.ball = Ball(WHITE, 10, 10)
        self.ball.rect.x = 345
        self.ball.rect.y = 195

        # Add all sprites to list
        self.all_sprites_list = pygame.sprite.Group()
        self.all_sprites_list.add(self.paddleA)
        self.all_sprites_list.add(self.paddleB)
        self.all_sprites_list.add(self.ball)

        self.X = np.array([])
        self.Y = np.array([])
        self.lasthit = LASTHIT_PADDLE_A
        self.laststart_left = False

        # self.nnA = NeuralNetwork([4, 32, 4, 4, 1])
        # self.nnB = NeuralNetwork([4, 8, 4, 4, 1])
        # self.nnA = NeuralNetwork([4, 32, 4, 4, 1])
        # self.nnB = NeuralNetwork([4, 32, 8, 1])
        # self.nnA = NeuralNetwork([4, 8, 4, 1])
        # self.nnB = NeuralNetwork([4, 16, 4, 1])
        self.nnA = NeuralNetwork([4, 16, 4, 4, 1])
        self.nnB = NeuralNetwork([4, 16, 8, 1])
示例#4
0
def newGeneration(fromZero=True, snakes=None):
    newSnakes = []

    if fromZero == True:
        for i in range(0, POPULATION):
            newSnakes.append(Snake())
        return newSnakes
    else:
        normalizeFitness(snakes)
        best = selectBest(snakes)

        for snake in best:
            newSnakes.append(Snake(snake.brain))  # add copies
        snakes.reverse()

        for i in range(0, POPULATION - BEST_N):  # add mixed snakes
            parent1 = pickParent(snakes)
            parent2 = pickParent(snakes)
            brain = NeuralNetwork([], parent1.brain)
            brain.combine(parent2.brain)
            newSnakes.append(Snake(brain))

        for i in range(BEST_N, POPULATION):  # mutate snakes
            newSnakes[i].brain.mutate(MUTATION_CHANCE)

        return newSnakes
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()
示例#6
0
 def __init__(self, layers=None, weights=None):
     self.score = 0
     # creating new random object
     if weights is None:
         self.nn = NeuralNetwork(layers=layers)
     # mutation
     else:
         self.nn = NeuralNetwork(layers=layers, weights=weights)
示例#7
0
 def __init__(self, feature_limits=None, weights=None, mutate_prob=0.03):
     if weights is None:
         self.nn = NeuralNetwork(Individual.dimensions)
     else:
         self.nn = NeuralNetwork(Individual.dimensions, weights=weights, mutate_prob=mutate_prob)
     
     self.bird = attrib.Bird()
     Individual.feature_limits = feature_limits
示例#8
0
 def __init__(self, brain=None):
     super().__init__()
     xRandom = random.uniform(0.09, 0.14)
     self.moveTo(Vector2(display.width * xRandom, display.height // 2))
     if brain is None:
         self.brain = NeuralNetwork(6, 10, 1)
     else:
         self.brain = brain
     self.alive = True
     self.vision = []
     self.fitness = 0
     self.seed = random.randint(0, 7)
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()
示例#11
0
 def loadBest(self):
     self.livingBirds = []
     with open("birdbrain.pickle", "rb") as f:
         trainedNet = pickle.load(f)
         chickBrain = NeuralNetwork(weights=trainedNet)
         chick = Bird(brain=chickBrain)
         self.livingBirds.append(chick)
示例#12
0
 def repopulate(self):
     from copy import deepcopy
     COPY_PER = 0.2
     MUTATE_PER = 0.2
     CROSS_PER = 0.4
     sort_fit(self)
     new_networks = []
     self.cars = []
     self.time = 0
     for i in xrange(self.size):
         self.cars.append(Car(self.world))
     for i in xrange(int(self.size * COPY_PER)):
         new_networks.append(self.networks[i])
     for i in xrange(int(self.size * MUTATE_PER)):
         new_networks.append(mutate(deepcopy(self.networks[i]), 0.4))
     f = self.size * CROSS_PER
     for i in xrange(int(self.size * CROSS_PER)):
         net = crossover(self.networks[randint(0, f - 1)],
                         self.networks[randint(0, f - 1)])
         mutate(net, 0.2)
         new_networks.append(net)
     for i in xrange(self.size - len(new_networks)):
         net = NeuralNetwork([5, 6, 6, 2])
         new_networks.append(net)
     self.networks = new_networks
     if self.generation % 5 == 0:
         self.networks[0].print_network("Net 0 Gen " + str(self.generation))
         self.networks[1].print_network("Net 1 Gen " + str(self.generation))
     self.time = 0
     self.generation += 1
示例#13
0
    def hatchBirds(self):
        display.generation += 1
        display.frame = 0
        for _ in range(500):
            if len(self.deadBirds) == 0:
                chick = Bird()
            else:
                parent = random.choices(
                    self.deadBirds,
                    weights=[b.fitness for b in self.deadBirds]).pop()
                parentBrain = parent.getBrain()
                chickBrain = NeuralNetwork(weights=parentBrain)
                chickBrain.mutate()
                chick = Bird(brain=chickBrain)

            self.livingBirds.append(chick)

        self.deadBirds = []
示例#14
0
文件: engine.py 项目: airsick/PokeBot
	def main(self):
		# enable browser logging
		d = DesiredCapabilities.CHROME
		d['loggingPrefs'] = { 'browser':'ALL' }
		driver = webdriver.Chrome(desired_capabilities=d)
		self.login(driver)
		if mode == 1:
			while True:
				ai = NeuralNetwork()
				self.acceptMatch(driver, ai)
		elif mode == 2:
			while True:
				ai = NeuralNetwork()
				self.challengePlayer(driver, player, ai)
		else:
			while True:
				ai = NeuralNetwork()
				self.randomMatch(driver, ai)
示例#15
0
 def __init__(self, brain=None):
     if brain == None:
         self.brain = NeuralNetwork(NETWORK)  # new brain
     else:
         self.brain = NeuralNetwork([], brain)  # copy brain
     self.active = True
     self.ticks_alive = 0
     self.body = []
     self.body.append(
         Element(
             (BOARD_LEFT + BOARD_SIZE // 2, BOARD_TOP + BOARD_SIZE // 2),
             Direction.UP))
     self.turns = []
     self.think_lock = 0
     # every snake has his own apple to collect
     self.apple = Element((0, 0))
     self.placeApple()
     self.apples_eaten = 0
     self.steps_without_apple = 0
示例#16
0
 def test_strategy2(self):
     neural = NeuralNetwork(**stratify_parts(['AAPL', 'MSFT'], [0.25] * 3,
                                             '2017-01-01', '2018-01-01'),
                            options_list=get_options_list(['sma', 'ema']),
                            days=7,
                            tolerance=0.01)
     Strategy(neural=neural,
              start='2018-04-01',
              end='2018-05-01',
              symbol='MSFT',
              threshold=0.7)
示例#17
0
 def mutate(self, network, intensity, mutate_all=False):
     network_weights = network.weights
     child_weights = []
     for weights in network_weights:
         copy_gene = weights.copy()
         mutated_gene = self.mutator(copy_gene, intensity, mutate_all)
         child_weights.append(mutated_gene)
     child_network = NeuralNetwork(network.shape_v,
                                   child_weights,
                                   symmetry_mat=network.symmetry_mat)
     return child_network
示例#18
0
 def test_strategy1(self):
     neural = NeuralNetwork(**stratify_parts(['AAPL', 'MSFT'],
                                             [0.5, 0.1, 0.3], '2015-01-01',
                                             '2018-01-01'),
                            options_list=get_options_list(
                                ['sma', 'ema', 'macd']),
                            days=3,
                            tolerance=0.05)
     Strategy(neural=neural,
              start='2018-01-01',
              end='2018-03-01',
              symbol='AAPL')
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()
示例#20
0
def createNeuralNetworkForVerificationFrom(filenames):
    # Read input data
    networkFile = readNetworkFile(filenames[0])
    regulation = float(networkFile[0])
    configuration = [int(numOfNeurons) for numOfNeurons in networkFile[1:]]

    weights = readWeightsFile(filenames[1])

    instances, className = readDatasetFile(filenames[2])

    # Print input data
    print('Regulation: {}\n'.format(regulation))
    print('Configuration: {}\n'.format(configuration))
    printWeights(weights)
    printTrainingSet(instances)

    # Initialize and train neural network
    print('Training neural network...\n')
    neuralNetwork = NeuralNetwork(configuration, regulation)
    neuralNetwork.weights = weights

    return neuralNetwork, instances, className
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()
示例#22
0
def sample():
    X = np.array([[0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1]])
    y = np.array([[0], [1], [1], [0]])
    nn = NeuralNetwork(X, y)

    for i in range(1500):
        nn.feedforward()
        nn.backprop()

    print(nn.output)
示例#23
0
def test_learning_rate():

    learning_rates = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
    training_errors = []
    training_accuracy = []

    # hardcoded for 'big'

    epoch = 150000
    hidden_size = 38

    i = 0
    for curr_rate in learning_rates:

        print '\n======================================='
        print i + 1, '. LEARNING RATE:', curr_rate
        print '=======================================\n'

        nn = NeuralNetwork(num_inputs=input_size,
                           num_hidden=hidden_size,
                           num_outputs=output_size,
                           learning_rate=curr_rate,
                           hidden_layer_bias=1,
                           output_layer_bias=1)

        train(nn, epoch)
        error, accuracy = validate(nn, 1000)

        training_errors.append(error)
        training_accuracy.append(accuracy)

        i += 1

    fig, ax1 = plt.subplots()

    ax2 = ax1.twinx()
    ax1.plot(learning_rates, training_errors, 'b-o')
    ax2.plot(learning_rates, training_accuracy, 'r-o')

    plt.title('Learning rate vs error\n(' + str(SRC) + ', epoch = ' +
              str(epoch) + ')')
    ax1.set_ylabel('Error')
    ax2.set_ylabel('Accuracy')
    ax1.set_xlabel('Learning rate')
    plt.show()
示例#24
0
def test_num_hidden():

    # hardcoded for 'big'

    epoch = 150000

    training_hidden = []
    training_errors = []
    training_accuracy = []

    i = 0
    for x in range(10, 50, 2):

        print '\n======================================='
        print i + 1, '. # HIDDEN:', x
        print '=======================================\n'

        nn = NeuralNetwork(num_inputs=input_size,
                           num_hidden=x,
                           num_outputs=output_size,
                           learning_rate=0.8,
                           hidden_layer_bias=1,
                           output_layer_bias=1)

        train(nn, epoch)
        error, accuracy = validate(nn, 1000)

        training_errors.append(error)
        training_hidden.append(x)
        training_accuracy.append(accuracy)

        i += 1

    fig, ax1 = plt.subplots()

    ax2 = ax1.twinx()
    ax1.plot(training_hidden, training_errors, 'b-o')
    ax2.plot(training_hidden, training_accuracy, 'r-o')

    plt.title('# hidden neurons vs error\n(' + str(SRC) + ', epoch = ' +
              str(epoch) + ')')
    ax1.set_ylabel('Error')
    ax2.set_ylabel('Accuracy')
    ax1.set_xlabel('# hidden neurons')
    plt.show()
示例#25
0
def createNeuralNetworkForTrainingFrom(filenames):
    # Read input data
    networkFile = readNetworkFile(filenames[0])
    regulation = float(networkFile[0])
    configuration = [int(numOfNeurons) for numOfNeurons in networkFile[1:]]

    instances, className, classValues = readTrainingDatasetFile(filenames[1])

    # Print input data
    print('Regulation: {}\n'.format(regulation))
    print('Configuration: {}\n'.format(configuration))
    print('Class name: {}\n'.format(className))
    print('Class values: {}\n'.format(classValues))

    # Initialize neural network
    neuralNetwork = NeuralNetwork(configuration, regulation, classValues)

    return neuralNetwork, instances, className
示例#26
0
 def cross_breed(self, network_a, network_b):
     network_weights_a = network_a.weights
     network_weights_b = network_b.weights
     child_weights = []
     for a_layer, b_layer in zip(network_weights_a, network_weights_b):
         assert a_layer.shape == b_layer.shape
         offspring_layer = []
         weights_a_flat = a_layer.flatten()
         weights_b_flat = b_layer.flatten()
         self.joiner(weights_a_flat, weights_b_flat, offspring_layer)
         offspring_layer = np.array(offspring_layer,
                                    dtype=np.float).reshape(a_layer.shape)
         offspring_layer.reshape(a_layer.shape)
         child_weights.append(offspring_layer)
     child_network = NeuralNetwork(network_a.shape_v,
                                   child_weights,
                                   symmetry_mat=network_a.symmetry_mat)
     return child_network
示例#27
0
def load():

    # load network and play game
    filename = 'saved/' + SRC + '.json'

    print '\nloading network from', filename
    nn = NeuralNetwork(loadfile=filename)  # load network

    print '\n======================================='
    print 'NETWORK SUMMARY'
    print '=======================================\n'

    print '#input =', nn.num_inputs
    print '#output =', nn.num_outputs
    print '#hidden =', nn.num_hidden

    # validate(nn,50) # validate loaded network

    test(nn)  # play
示例#28
0
def training(num_epochs, data, g_truth, training_rate = 0.01):
    nn = NeuralNetwork()
    losses = []
    for i in range(num_epochs):
        # create loss
        loss_list = [loss(nn.forward(data_i), gt_i) for data_i, gt_i in zip(data, g_truth)]
        l = sum(loss_list) * (1.0/len(loss_list))
        losses.append(l.value)
        
        # generate gradients
        nn.zero_grad()
        l.backward()

        #update gradients
        for p in nn.params():
            p.value -= training_rate * p.grad
        print(f"loss: {l.value}")
示例#29
0
class Bird(Particle):
    def __init__(self, brain=None):
        super().__init__()
        xRandom = random.uniform(0.09, 0.14)
        self.moveTo(Vector2(display.width * xRandom, display.height // 2))
        if brain is None:
            self.brain = NeuralNetwork(6, 10, 1)
        else:
            self.brain = brain
        self.alive = True
        self.vision = []
        self.fitness = 0
        self.seed = random.randint(0, 7)

    def getBrain(self):
        return self.brain.copyNetwork()

    def getPosition(self):
        return self.pos

    def update(self, pipes):
        self.applyForce(gravity)
        self.think(pipes)
        self.doPhysics()
        if self.alive:
            self.fitness += 1

    def think(self, pipes):
        inputs = [self.pos.y / display.height, self.vel.y / self.maxVelocity]

        self.vision = self.perceive(pipes)
        for v in self.vision:
            inputs.append(v.x / display.width)
            inputs.append(v.y / display.height)
            # inputs.append(self.pos.angle_to(v) / 360)

        while len(inputs) < 4:
            inputs.append(0)

        predictions = self.brain.predict(inputs)
        prediction = predictions.pop()
        if prediction > 0.50:
            self.jump()

    def perceive(self, pipescollection):
        # for now, I'll just look at the bottom left of pipe[0] and top left of pipe[1]
        # TODO: look at the next pipe in the future
        seen = []
        pipes = pipescollection.getPipes()
        if len(pipes) > 0:
            seen.append(Vector2(pipes[0].getRect().bottomleft))
        if len(pipes) > 1:
            seen.append(Vector2(pipes[1].getRect().topleft))
        return seen

    def draw(self):
        display.drawBird(self.pos, self.vision, seed=self.seed)

    def kill(self):
        self.alive = False

    def isDead(self):
        return not self.alive

    def offScreen(self):
        return self.pos.y < 0 or self.pos.y > display.height

    def collide(self, pipeRects):
        birdRect = display.birdRect
        birdRect.center = self.pos
        return birdRect.collidelist(pipeRects) > -1

    def jump(self):
        self.applyForce(Vector2(0, -30))
示例#30
0
文件: test.py 项目: nchhaj189/deeplib
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