示例#1
0
def create_population(self, count):
    population = []
    for _ in range(0, count):
        # Create randon network
        network = NeuralNetwork(self.nn_params_choices)
        network.create_random()

        population.append(network)

    return population
示例#2
0
    def __init__(self, x, y, id, coefs=0, intercepts=0):
        super(Player, self).__init__()
        self.surf = pygame.Surface((25, 25))
        self.surf.fill((255, 255, 255))
        self.isJump = False
        self.jumpCount = 8
        self.rect = self.surf.get_rect(
            center=(
                x,
                y,
            )
        )

        self.command = 5
        self.speed = 3
        self.x = x
        self.y = y
        self.score = 0
        if coefs == 0 and intercepts == 0:
            self.coefs, self.intercepts = NeuralNetwork.generate_weights_intercepts()
        else:
            self.coefs = coefs
            self.intercepts = intercepts
        self.intercept = 0
        self.alive = True
        self.winner = False
        self.id = id
示例#3
0
def demo():
    
    # make it reproducible by using a random seed
    np.random.seed(5135134)
    
    test_test = load_xor()
    # load the neural network, (input, [hidden], output)+
    # the hidden layers has to be an array of at least 2 layers.
    neural = nn.MLP_NeuralNetwork(2, ([4, 6]), 1)
    neural.train(test_test)
    print(neural.test(test_test))
    t0 = time()

    x = np.linspace(0, 1, 200)
    y = np.linspace(0, 1, 200)

    intensity = []
    for i in x:
        temp = []
        for j in y:
            temp.append(neural.give_input([i, j]))
        intensity.append(temp)

    #setup the 2D grid with Numpy
    x, y = np.meshgrid(x, y)

    # now just plug the data into pcolormesh, it's that easy!
    plt.pcolormesh(x, y, intensity)
    plt.colorbar()  # need a colorbar to show the intensity scale
    plt.show()  # boom
示例#4
0
def kFold(k,
          input,
          output,
          numhidden,
          learningRate,
          alfa,
          lamb,
          maxiter=1000,
          type="MSE"):
    db = dbmongo.Database()
    input, output = shuffle(input, output, random_state=0)
    nn = nnBatch.NeuralNetwork(input,
                               output,
                               numhidden=numhidden,
                               learningRate=learningRate,
                               lamb=lamb,
                               alfa=alfa,
                               type=type)

    # divido il dataset in k parti e segno il numero di pattern per ogni partizione
    sizePart = nn.numExamples // k

    errorTrain = np.empty(k)
    errorValidation = np.empty(k)

    for i in range(0, k):
        ##aggiungo una riga di zeri se no non posso concatenere con vstack
        setPass = np.zeros(shape=(1, nn.dimInput))
        setOutPass = np.zeros(shape=(1, nn.dimOutput))
        for j in range(0, k):
            if (i != j):
                # aggiungo la partizione al training set
                setPass = np.vstack(
                    (setPass, input[(j * sizePart):(sizePart * (j + 1))][:]))
                setOutPass = np.vstack(
                    (setOutPass,
                     output[(j * sizePart):(sizePart * (j + 1))][:]))
            else:
                # passo la partizione alla rete neurale come validation set
                nn.setValidation(
                    input[(i * sizePart):(sizePart * (i + 1))][:],
                    output[(i * sizePart):(sizePart * (i + 1))][:])

        # rimuovo la linea di zeri iniziali quando passo il training set
        nn.setInput(setPass[1:][:])
        nn.setOutput(setOutPass[1:][:])
        # reinizializzo i pesi
        nn.setWeights()
        nn.train(maxiter)

        errorTrain[i] = nn.errorShow[-1]
        errorValidation[i] = nn.errorValidShow[-1]

    db.insert(np.mean(errorTrain), np.mean(errorValidation), int(numhidden),
              float(learningRate), float(alfa), float(lamb))
示例#5
0
def breed(self, mother, father):
    """Make two children as parts of their parents.
    Args:
        mother (dict): Network parameters
        father (dict): Network parameters
    """
    children = []
    for _ in range(2):
        child = {}

        #Loop through the parameters and pick params for the kid
        for param in self.nn_params_choices:
            child[param] = random.choice([mother.network[param], father.network[param]])


        #create a new network object
        network = NeuralNetwork(self.nn_params_choices)
        network.create_set(child)
        children.append(child)

    return children
示例#6
0
        'n_neurons': 12,
        'activation': 'ReLu',
    },
    'C': {
        'n_inputs': 12,
        'n_neurons': 16,
        'activation': 'ReLu',
    },
    'output': {
        'n_inputs': 16,
        'n_neurons': 3,
        'activation': 'Softmax',
    },
}
# instantiate my model
mynet = NeuralNetwork(architecture=arc, loss='categorical crossentropy', optimizer='sgd')

# instantiate keras similar keras model
kerasnet = Sequential()
kerasnet.add(Dense(16, input_dim = 2, activation = 'relu'))
kerasnet.add(Dense(12, activation = 'relu'))
kerasnet.add(Dense(16, activation = 'relu'))
kerasnet.add(Dense(1, activation = 'softmax'))
## compile model
kerasnet.compile(loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy'])

# train my model
mynet.train(X_train, y_train, iterations = 1000)

# train keras model
kerasnet.fit(X_train, y_train, epochs = 1000)
示例#7
0
    plt.scatter(param1, param2, c=y)
    plt.show()

# load in architecutre from .json file
with open('data/architecture.json', 'r') as outfile:
    arc = json.load(outfile)

# establish loss function and optimizer
loss = 'categorical crossentropy'
optimizer = 'SGD'

# split data for training and testing
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# create NN instance
net = NeuralNetwork(architecture=arc, loss=loss, optimizer=optimizer)

# train network weights and biases
net.train(X_train,
          y_train,
          iterations=10000,
          learning_rate=1.0,
          decay=0.0,
          momentum=None)

# measure accuracy
acc = accuracy_score(y_test, net.predict(X_test))
print('Test accuracy: {:.2f}'.format(acc))

# see network architecture
net.visualize_network()
示例#8
0
def run_game():
    pygame.init()
    fps_clock = pygame.time.Clock()
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption("Jumpy Block")
    display = DisplayInfo()

    ADDOBSTACLE = pygame.USEREVENT + 1
    pygame.time.set_timer(ADDOBSTACLE, INTERVAL)
    obstacles = pygame.sprite.Group()
    all_sprites = pygame.sprite.Group()

    players = []
    obstacles_array = []
    for i in range(POPULATION):
        player = Player(start_pos[0], start_pos[1], i)
        players.append(player)
        all_sprites.add(player)
        if isHuman:
            break

    # can be an arbitrary number I just set it as the same as the population for simplicity's sake
    for i in range(POPULATION):
        new_obstacle = Obstacle()
        obstacles_array.append(new_obstacle)
        obstacles.add(new_obstacle)
        all_sprites.add(new_obstacle)

    game_over = False
    generation = 1
    obstacle_init = 0
    players[-1].winner = True
    high_score_index = -1
    high_score = -100

    # main loop
    while not game_over:
        screen.fill((0, 0, 0))
        alive = 0

        for event in pygame.event.get():

            if event.type == KEYDOWN and event.key == K_ESCAPE:
                game_over = True
            elif event.type == pygame.QUIT:
                game_over = True
            elif event.type == ADDOBSTACLE:
                obstacles_array[obstacle_init].start()
                obstacle_init += 1
                obstacle_init %= POPULATION

        for i, player in enumerate(players):
            obstacles_array[obstacle_init-1].pass_player(player)

            if isHuman:
                pressed_keys = pygame.key.get_pressed()
                player.human_update(pressed_keys)
                high_score = player.score
            else:
                input = NeuralNetwork.generate_input(player, obstacles_array, i)
                player.command = NeuralNetwork.produce_output(input, player.coefs, player.intercepts)
                # print(player.command)
                if player.score > high_score:
                    high_score = player.score
                    high_score_index = i
                    winner = players[i]
                    winner.winner = True

                player.computer_update(player.command)

            if pygame.sprite.spritecollideany(player, obstacles):
                if isHuman:
                    player.kill()
                    game_over = True
                else:
                    player.alive = False
                    player.kill()

        for player in players:
            if player.alive:
                alive += 1
        display.update(screen, high_score, alive, generation)
        obstacles.update()
        for cur_object in all_sprites:
            screen.blit(cur_object.surf, cur_object.rect)
        pygame.display.flip()

        # reset game when all players are dead
        if alive == 0:
            for cur_object in all_sprites:
                cur_object.kill()
            for obstacle in obstacles:
                obstacle.kill()
            screen.fill((0, 0, 0))
            print("starting next generation")
            pygame.time.delay(2000)
            winner = players[high_score_index]
            print(high_score_index)
            generation += 1
            winner.reset(start_pos[0], start_pos[1])
            players = []
            obstacles_array = []

            high_score_index = -1
            high_score = -100
            obstacle_init = 0

            for i in range(POPULATION - 1):
                new_player = Player(start_pos[0], start_pos[1], i, GeneticAlgo.mutateCoefs(winner.coefs),
                                    GeneticAlgo.mutateIntercepts(winner.intercepts))
                players.append(new_player)
                all_sprites.add(new_player)

            for i in range(POPULATION):
                new_obstacle = Obstacle()
                obstacles_array.append(new_obstacle)
                obstacles.add(new_obstacle)
                all_sprites.add(new_obstacle)

            players.append(winner)
            all_sprites.add(winner)
        fps_clock.tick(60)
示例#9
0
from src import NeuralNetwork
from testdata import create_spiral_data
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import json

# load in architecutre from .json file
with open('data/architecture.json', 'r') as outfile:
    arc = json.load(outfile)

# create class instance
net = NeuralNetwork(architecture=arc)

# visualize network as reminder
net.visualize_network()

# load in weights and biases from previously saved trained weights and biases
net.load('data/savetest.json')

# create test data
X, y = create_spiral_data(1000, 3)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# make predictions
pred = net.predict(X_test)

# measure accuracy of predictions
acc = accuracy_score(y_test, pred)
print(f'Accuracy of loaded model: {acc}')
示例#10
0
import src.NeuralNetwork as NN
import numpy as np

if __name__ == "__main__":
    X = np.array([[0,0,1],
                  [0,1,1],
                  [1,0,1],
                  [1,1,1]])
    y = np.array([[0], [1], [1], [0]])
    nn = NN.NeuralNetwork(X,y)

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

    print(nn.output)