コード例 #1
0
    def generate_initial_population(self):
        new_population = []

        for i in range(self.population_size):
            new_population.append(NeuralNetwork(self.n_layers))

        return new_population
コード例 #2
0
    def test_forward_pass(self):
        input_dim = 2
        number_labels = 2
        intermediate_neurons = 3
        intermediate_layers = 1

        neural_net = NeuralNetwork(input_dim=input_dim,
                                   number_labels=number_labels,
                                   intermediate_neurons=intermediate_neurons,
                                   intermediate_layers=intermediate_layers)

        # the layers in the neural net are randomly initialized
        # modify them to fixed numbers in order to test
        first_layer = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])

        intermediate_layer = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
        intermediate_layer = np.vstack((intermediate_layer, np.array([1, 1,
                                                                      1]).T))

        last_layer = np.array([[0.5, 1], [0.5, 1], [0.5, 1], [1, 1]])

        neural_net.layers = [first_layer, intermediate_layer, last_layer]

        expected_result = [92.5, 184]

        actual_result = neural_net.forward_pass(np.array([1, 3]))

        for actual, expected in zip(actual_result, expected_result):
            self.assertEqual(expected, actual)
コード例 #3
0
 def crossover(self):
     #Introduce crossover
     i = 0
     while len(self.new_generation) < self.number_of_agents:
         
         new_agent = NeuralNetwork()
         
         #Copy half of all weights from parent i and parent i+1
         new_agent.weights_left = np.concatenate((self.list_of_neural_nets[i].weights_left[:int(len(self.list_of_neural_nets[i].weights_left)/2)], \
                                                  self.list_of_neural_nets[i+1].weights_left[int(len(self.list_of_neural_nets[i+1].weights_left)/2):]))
     
         new_agent.weights_down = np.concatenate((self.list_of_neural_nets[i].weights_down[:int(len(self.list_of_neural_nets[i].weights_down)/2)], \
                                                  self.list_of_neural_nets[i+1].weights_down[int(len(self.list_of_neural_nets[i+1].weights_down)/2):]))
     
         new_agent.weights_right = np.concatenate((self.list_of_neural_nets[i].weights_right[:int(len(self.list_of_neural_nets[i].weights_right)/2)], \
                                                   self.list_of_neural_nets[i+1].weights_right[int(len(self.list_of_neural_nets[i+1].weights_right)/2):]))
     
         new_agent.weights_rotate = np.concatenate((self.list_of_neural_nets[i].weights_rotate[:int(len(self.list_of_neural_nets[i].weights_rotate)/2)], \
                                                    self.list_of_neural_nets[i+1].weights_rotate[int(len(self.list_of_neural_nets[i+1].weights_rotate)/2):]))
         
         self.new_generation.append(new_agent)
         
         i += 1
     
     print(f"Number of agents for next generation: {len(self.list_of_neural_nets)}")
コード例 #4
0
ファイル: classifier.py プロジェクト: EoinM95/FYP
 def load_model_from_file(self, classifier_type, sentence_features,
                          filename):
     """Restore classifier from file"""
     if classifier_type is NEURAL_NET:
         self.classifier = NeuralNetwork(sentence_features, filename)
     else:
         self.classifier = NBClassifier(filename)  #pylint: disable = R0204
コード例 #5
0
ファイル: neural_net_tests.py プロジェクト: EoinM95/FYP
 def test_xor_gate(self):
     """Simulate XOR gate and ensure working"""
     inputs = [[1.0, 1.0],
               [1.0, 0.0],
               [0.0, 1.0],
               [0.0, 0.0]]
     output_vector = [[0.0],
                      [1.0],
                      [1.0],
                      [0.0]]
     inputs = np.array(inputs, dtype='float32')
     output_vector = np.array(output_vector)
     net = NeuralNetwork(inputs, output_vector)
     net.train()
     output = net.feed(np.array([[0, 1]], dtype='float32'))[0][0]
     output = round(output, 3)
     self.assertAlmostEqual(output, 1)
     output = net.feed(np.array([[1, 0]], dtype='float32'))[0][0]
     output = round(output, 3)
     self.assertAlmostEqual(output, 1)
     output = net.feed(np.array([[0, 0]], dtype='float32'))[0][0]
     output = round(output, 3)
     self.assertAlmostEqual(output, 0)
     output = net.feed(np.array([[1, 1]], dtype='float32'))[0][0]
     output = round(output, 3)
     self.assertAlmostEqual(output, 0)
コード例 #6
0
ファイル: main.py プロジェクト: justinjk007/digit-recognition
def main():
    # load data
    _input = torch.tensor(data.training_input, dtype=torch.float)
    _output = torch.tensor(data.training_expected_output, dtype=torch.float)

    # This section is for plotting ##############################
    gene_array = []
    loss_array = []
    fig, ax = plt.subplots()
    ax.set(xlabel='generation',
           ylabel='mean sum squared error',
           title='Neural network, error loss after each generation')
    # This section is for plotting ##############################

    ANN = NeuralNetwork(i=45, o=10, h=5)  # input,output,hidden layer size
    # weight training
    for i in range(15000):
        # mean sum squared error
        mean_error = torch.mean((_output - ANN(_input))**2).detach().item()
        print("Generation: " + str(i) + " error: " + str(mean_error))
        gene_array.append(i)
        loss_array.append(mean_error)
        ANN.train(_input, _output)

    torch.save(ANN, "algo1.weights")
    ANN = torch.load("14_good.weights")
    test_trained_network(ANN)
    ax.plot(gene_array, loss_array)
    plt.show()
コード例 #7
0
ファイル: paramServer.py プロジェクト: for-l00p/Spark
def resetParamServer():
	global params, accruedGradients, history_S, history_Y, rho,batches_processed
	
	nn = NeuralNetwork(NNlayers)
	params = nn.get_weights()
	accruedGradients = np.zeros(sum(nn.sizes))
	history_S = []
	history_Y = []
	rho = []
	batches_processed = 0
コード例 #8
0
    def test_sigmoid_activation_fct_expected(self):
        test_vector = np.array([1.1, 0.4, 0.9, -0.7])

        expected_vector = np.array([0.75, 0.6, 0.711, 0.33])

        neural_net = NeuralNetwork(1000, 10)

        actual_vector = neural_net.sigmoid_activation_fct(test_vector)

        for expected, actual in zip(expected_vector, actual_vector):
            self.assertAlmostEqual(expected, actual, 2)
コード例 #9
0
    def __init__(self, board_size, brain=None):
        if brain == None:
            self.brain = NeuralNetwork(
                [NeuralLayer(16, 24), NeuralLayer(4, 16)])
        else:
            self.brain = brain
        self.direction = "up"

        # TODO: Grow the snake by this value each time it picks up food
        self.growcountDefault = 3
        self.growcount = self.growcountDefault

        self.death_cause = "None"

        self.board_size = board_size
        self.pos_x = self.board_size // 2
        self.pos_y = self.board_size // 2

        self.alive = True

        self.length = 5
        self.food = Food(self.board_size)

        self.lifetime = 0
        self.fitness = 0

        self.tail = [[self.pos_x,
                      self.pos_y - 4], [self.pos_x, self.pos_y - 3],
                     [self.pos_x, self.pos_y - 2],
                     [self.pos_x, self.pos_y - 1]]

        self.vector = []

        self.left_to_live_start = 200
        self.left_to_live = self.left_to_live_start

        #Make moves equal tail and then append current head position to it
        self.parts = [[self.pos_x,
                       self.pos_y - 4], [self.pos_x, self.pos_y - 3],
                      [self.pos_x, self.pos_y - 2],
                      [self.pos_x, self.pos_y - 1], [self.pos_x, self.pos_y]]

        # Add food positions and lengths for each move
        self.move_history = {
            "fitness":
            self.fitness,
            "moves":
            self.parts,
            "food_position": [[self.food.pos_x, self.food.pos_y]
                              for i in range(len(self.parts))],
            "length": [self.length for i in range(len(self.parts))]
        }
コード例 #10
0
ファイル: xor.py プロジェクト: gholmes829/Neural-Network
def main():
    inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
    labels = np.array([[0], [1], [1], [0]])

    architecture = (2, 3, 1)
    nn = NeuralNetwork(architecture, activation="sigmoid", cost="mse")

    costs = nn.train(inputs, labels, alpha=1, iterations=5000)
    print("\nCost always decreases:",
          all([costs[i + 1] < costs[i] for i in range(len(costs) - 1)]))
    print("Lowest cost:", min(costs))
    print("\nResults:", "\n", np.round(nn.evaluate(inputs), 0))
    plotCosts(costs)

    # GRAPHING
    plt.style.use(["dark_background"])
    #plt.rc("grid", alpha=0.25)

    start, end = -0.5, 1.5

    fidelity = 0.01
    n = int((end - start) / fidelity)
    points = np.meshgrid(np.linspace(start, end, n + 1),
                         np.linspace(start, end, n + 1))
    values = np.zeros((n + 1, n + 1))

    for i in range(n + 1):
        for j in range(n + 1):
            values[i, j] = nn.evaluate(
                np.array([points[0][i, j], points[1][i, j]]))

    x, y = points
    # RdYlGn
    plt.contourf(x, y, values, np.linspace(0, 1, 51), cmap="jet_r")
    plt.colorbar()
    plt.contour(x,
                y,
                values,
                0.5,
                linewidths=2,
                linestyles="dashed",
                colors="black")

    plt.grid(color="black", alpha=0.25)
    plt.axhline(y=0, color="k", lw=2)
    plt.axvline(x=0, color="k", lw=2)
    plt.title("Neural Network XOR Boundary")
    plt.xlabel("X Axis")
    plt.ylabel("Y Axis")
    plt.show()
コード例 #11
0
    def __init__(self, board_size, brain=None):
        if brain == None:
            _input_nodes = 5
            _output_nodes = 4
            self.brain = NeuralNetwork([_input_nodes] + hidden_layers +
                                       [_output_nodes])
        else:
            self.brain = brain

        self.direction = "up"

        self.growcount = 1

        self.death_cause = "None"

        self.board_size = board_size
        self.x = self.board_size // 2
        self.y = self.board_size // 2

        self.alive = True

        self.length = 5
        self.food = Food(self.board_size)

        self.lifetime = 0
        self.fitness = 0

        self.tail = [[self.x, self.y - 4], [self.x, self.y - 3],
                     [self.x, self.y - 2], [self.x, self.y - 1]]

        self.vector = []

        self.left_to_live_start = 200
        self.left_to_live = self.left_to_live_start

        self.parts = [[self.x, self.y - 4], [self.x, self.y - 3],
                      [self.x, self.y - 2], [self.x, self.y - 1],
                      [self.x, self.y]]

        # Add food positions and lengths for each move
        self.move_history = {
            "fitness":
            self.fitness,
            "moves":
            self.parts,
            "food_position":
            [[self.food.x, self.food.y] for i in range(len(self.parts))],
            "length": [self.length for i in range(len(self.parts))]
        }
コード例 #12
0
ファイル: tictactoe.py プロジェクト: oskvaj/Gyarte-AI-Gaming
    def __init__(self, brain=None):
        self.marker = None

        self.fitness = 0
        if brain == None:
            inputNodes = 9
            hiddenNodes = 7
            outputNodes = 9
            self.brain = NeuralNetwork([inputNodes, hiddenNodes, outputNodes])
        else:
            self.brain = brain

        self.wins = 0
        self.draws = 0
        self.total_matches = 0
コード例 #13
0
    def test_forward_pass_with_weights_randomly_initialized(self):
        input_dim = 32 * 32
        number_labels = 10
        intermediate_neurons = 4000
        intermediate_layers = 2

        neural_net = NeuralNetwork(input_dim=input_dim,
                                   number_labels=number_labels,
                                   intermediate_neurons=intermediate_neurons,
                                   intermediate_layers=intermediate_layers)

        input_vector = np.random.uniform(0, 1, input_dim)

        forward_res = neural_net.forward_pass(input_vector)

        self.assertEqual(number_labels, len(forward_res))
コード例 #14
0
 def populate_missing_agents(self):
     #To fulfill the capacity of list, duplicate current best agent
     #and add as new agents
     while len(self.new_generation) < int(self.number_of_agents/2):
         
         #Create new agent and mutate its weights
         try:
             new_agent = copy.deepcopy(self.new_generation[0])
         except Exception as exception:
             new_agent = NeuralNetwork()
             print(f"Exception occured: {exception}")
             print(f"New agent had to be created.")
         
         self.new_generation.append(new_agent)
     
     self.list_of_neural_nets = []
     self.list_of_neural_nets = self.new_generation
コード例 #15
0
 def __init__(self, directors=None):
     super(NeuralNetWorkflow, self).__init__(workflow_name='neural_net_workflow')
     self.image_path = self.config.path.get('source_data', None)
     self.file_name = 'neural_net_workflow_scores.csv'
     self.directors = directors
     self.encoding = None
     self.base_dir = str()
     self.file = str()
     self.compressed_file = str()
     self.set_up()
     self.neural_net = NeuralNetwork(num_inputs=256,
                                     num_ouputs=5,
                                     classes=5,
                                     class_lables=['christopher_nolan',
                                                   'coen_brothers',
                                                   'david_lynch',
                                                   'spike_jonze',
                                                   'wes_anderson'])
コード例 #16
0
    def test_layer_shape_expected(self):
        input_dim = 32 * 32
        number_labels = 10
        intermediate_layers = 1
        intermediate_neurons = 1000

        neural_net = NeuralNetwork(input_dim=input_dim,
                                   number_labels=number_labels,
                                   intermediate_layers=intermediate_layers,
                                   intermediate_neurons=intermediate_neurons)

        self.assertEqual(len(neural_net.layers), 3)

        self.assertEqual(neural_net.layers[0].shape,
                         (input_dim + 1, intermediate_neurons))

        self.assertEqual(neural_net.layers[1].shape,
                         (intermediate_neurons + 1, intermediate_neurons))

        self.assertEqual(neural_net.layers[2].shape,
                         (intermediate_neurons + 1, number_labels))
コード例 #17
0
    def test_errors_computed_correctly(self):
        # mock the layers just as in the forward pass case
        input_dim = 2
        number_labels = 2
        intermediate_neurons = 3
        intermediate_layers = 1

        neural_net = NeuralNetwork(input_dim=input_dim,
                                   number_labels=number_labels,
                                   intermediate_neurons=intermediate_neurons,
                                   intermediate_layers=intermediate_layers)

        # the layers in the neural net are randomly initialized
        # modify them to fixed numbers in order to test
        first_layer = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])

        intermediate_layer = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
        intermediate_layer = np.vstack((intermediate_layer, np.array([1, 1,
                                                                      1]).T))

        last_layer = np.array([[0.5, 1], [0.5, 1], [0.5, 1], [1, 1]])

        neural_net.layers = [first_layer, intermediate_layer, last_layer]
コード例 #18
0
def main_menu():
    run = True

    list_of_neural_nets = []
    number_of_games = 2

    while number_of_games > 0:
        win.fill((0, 0, 0))
        draw_text_middle('Press whatever button', 60, (255, 255, 255), win)
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

            if event.type == pygame.KEYDOWN:

                net = NeuralNetwork()
                net = main(win, net)
                list_of_neural_nets.append(net)

                number_of_games -= 1

    return list_of_neural_nets
コード例 #19
0
def main(mnist_path, output_path, activation, hl_sizes):
    f = gzip.open(mnist_path, 'rb')
    training_set, test_set = pickle.load(f, encoding='latin1')

    training_examples = transform_examples(training_set)
    test_examples = transform_examples(test_set)

    if activation == "sigmoid":
        activation = neural_net.sigmoid
        d_activation = neural_net.d_sigmoid
    elif activation == "relu":
        activation = neural_net.relu
        d_activation = neural_net.d_relu
    elif activation == "elu":
        activation = neural_net.elu
        d_activation = neural_net.d_elu

    network = NeuralNetwork([PIXEL_COUNT] + hl_sizes + [NUM_CHARACTERS],
                            output_path,
                            act_func=activation,
                            act_func_deriv=d_activation)
    network.train(training_generator(training_examples), test_examples)

    pickle.dump(network, open(output_path, 'wb'))
コード例 #20
0
	def __init__(self, neuralNetLayers):
		self.neuralNet = NeuralNetwork(neuralNetLayers)
		self.currentParamsStep = None
		self.accruedGradients = np.zeros(sum(self.neuralNet.sizes))
		self.isAvailable = True
コード例 #21
0
def main():
    # load the dataset from file
    dataset = pd.read_csv('WDBC_imbalanced.dat')

    # change M and B labels to numbers
    dataset.loc[dataset.diagnosis == 'M', 'diagnosis'] = 0
    dataset.loc[dataset.diagnosis == 'B', 'diagnosis'] = 1

    # Split the dataset into test and train datasets, 30% of datasets for testing
    train_X, test_X, train_Y, test_Y = train_test_split(
        dataset[dataset.columns[2:12]].values,
        dataset.diagnosis.values,
        test_size=0.3,
    )
    # convert output data for making into tensors
    train_Y = train_Y.astype(np.float32)
    test_Y = train_Y.astype(np.float32)

    # scale the data
    sc = StandardScaler()
    train_X = sc.fit_transform(train_X)
    test_X = sc.fit_transform(test_X)

    # load data into tensor
    _input = torch.tensor(train_X, dtype=torch.float)
    _output = torch.tensor(train_Y, dtype=torch.long)

    # input,output,hidden layer size
    model = NeuralNetwork(i=10, h1=6, h2=2, o=2)

    # loss function and optimizer
    lossFunction = nn.CrossEntropyLoss()
    # learning rate and momentum
    optimizer = optim.SGD(model.parameters(), lr=0.002, momentum=0.9)

    # This section is for plotting ##############################
    gene_array = []
    loss_array = []
    fig, ax = plt.subplots()
    ax.set(xlabel='generation',
           ylabel='mean sum squared error',
           title='Neural network, error loss after each generation')
    # This section is for plotting ##############################

    for epoch in range(15000):
        # Forward Pass
        output = model(_input)
        # Loss at each oteration by comparing to target
        loss = lossFunction(output, _output)

        # Backpropogating gradient of loss
        optimizer.zero_grad()
        loss.backward()

        # Updating parameters(weights and bias)
        optimizer.step()
        _loss = loss.item()

        gene_array.append(epoch)
        loss_array.append(_loss)
        print("Epoch {}, Training loss: {}".format(epoch, _loss / len(_input)))

    torch.save(model, "algo1.weights")
    # model = torch.load("68_error.weights")
    test_trained_network(model, test_X, test_Y)
    ax.plot(gene_array, loss_array)
    plt.show()
コード例 #22
0
def run():
    df = pd.read_csv('spambase_data\\spambase.data', header=None)
    df = df.sample(frac=1).reset_index(drop=True)
    print(f'No. missing values: {df.isnull().sum().sum()}')

    X = df.drop(57, axis=1)
    y = df.loc[:, 57]

    # Feature selection

    abs_corr_w_target = X.apply(
        lambda col: col.corr(y)).abs().sort_values().to_frame()
    abs_corr = X.corr().abs()
    plotting_tools.plot_heatmap(abs_corr_w_target,
                                title='Correlation with target',
                                size=(8, 16),
                                one_dim=True)
    plotting_tools.plot_heatmap(abs_corr,
                                title='Correlation before feature selection',
                                size=(10, 16))

    to_drop = set()

    # Amount of variation
    variance = X.var(axis=0, ddof=1)
    to_drop.update(variance[variance < 0.01].index.values)

    # Correlation with target
    to_drop.update(abs_corr_w_target[abs_corr_w_target[0] < 0.01].index.values)

    # Pairwise correlation
    to_drop.update(preprocessing.find_correlated(abs_corr, abs_corr_w_target))

    to_drop = list(to_drop)
    nr_dropped = len(to_drop)
    X.drop(to_drop, axis=1, inplace=True)
    abs_corr = X.corr().abs()
    plotting_tools.plot_heatmap(abs_corr,
                                title='Correlation after feature selection',
                                size=(10, 16))
    print(f'Dropped features: {to_drop}')

    # Data standardization works better, use normalization only for tests
    # X = preprocessing.normalize_data(X)
    X = preprocessing.standardize_data(X)

    X = X.values
    y = y.values
    train_inputs, cv_inputs, test_inputs = np.split(
        X, [int(0.6 * len(df)), int(0.8 * len(df))])
    train_outputs, cv_outputs, test_outputs = np.split(
        y, [int(0.6 * len(df)), int(0.8 * len(df))])

    print(f'Training set size: {train_outputs.shape[0]}\n'
          f'Cross validation set size: {cv_outputs.shape[0]}\n'
          f'Test set size: {test_outputs.shape[0]}')

    model = NeuralNetwork([57 - nr_dropped, 32, 1],
                          activation_function='sigmoid')

    # Only use this part for tuning hyperparameters, slows down the program significantly
    # lambdas = list(np.arange(0.5, 1.5, 0.1))
    # model.plot_learning_curves(train_inputs, train_outputs, cv_inputs, cv_outputs,
    #                           learning_rate=1.5, epochs=500, lambda_=0.6)
    # model.plot_validation_curves(train_inputs, train_outputs, cv_inputs, cv_outputs,
    #                             learning_rate=1.5, epochs=1000, lambdas=lambdas)

    model.gradient_descent(train_inputs,
                           train_outputs,
                           1.5,
                           4000,
                           0.6,
                           gradient_check=False,
                           plot_cost=False)

    train_predictions = np.where(model.predict(train_inputs) > 0.5, 1, 0)
    test_predictions = np.where(model.predict(test_inputs) > 0.5, 1, 0)

    train_columns = {
        'Train predictions': train_predictions[:, 0],
        'Train outputs': train_outputs
    }
    test_columns = {
        'Test predictions': test_predictions[:, 0],
        'Test outputs': test_outputs
    }

    train_results = pd.DataFrame(train_columns)
    test_results = pd.DataFrame(test_columns)

    train_correct = pd.value_counts(train_results['Train predictions'] ==
                                    train_results['Train outputs'])[True]
    test_correct = pd.value_counts(
        test_results['Test predictions'] == test_results['Test outputs'])[True]

    test_positive_predictions = test_results[test_results['Test predictions']
                                             == 1]
    test_negative_predictions = test_results[test_results['Test predictions']
                                             == 0]

    test_is_positive_correct = pd.value_counts(
        test_positive_predictions['Test predictions'] ==
        test_positive_predictions['Test outputs'])
    test_is_negative_correct = pd.value_counts(
        test_negative_predictions['Test predictions'] ==
        test_negative_predictions['Test outputs'])

    test_true_positives = test_is_positive_correct[True]
    test_false_positives = test_is_positive_correct[False]
    test_true_negatives = test_is_negative_correct[True]
    test_false_negatives = test_is_negative_correct[False]

    test_precision = test_true_positives / (test_true_positives +
                                            test_false_positives)
    test_recall = test_true_positives / (test_true_positives +
                                         test_false_negatives)
    test_confusion_matrix = pd.DataFrame(
        [[test_true_positives, test_false_positives],
         [test_false_negatives, test_true_negatives]],
        columns=[1, 0],
        index=[1, 0])

    train_acc = train_correct / len(train_outputs)
    test_acc = test_correct / len(test_outputs)
    print(f'train_acc = {train_acc}')
    print(f'test_acc = {test_acc}')
    print(f'test_precision = {test_precision}')
    print(f'test_recall = {test_recall}')

    plotting_tools.plot_cm(test_confusion_matrix, title='Confusion matrix')
コード例 #23
0
 def populate_list_of_neural_nets(self):
     for _ in range(self.number_of_agents):
         self.list_of_neural_nets.append(NeuralNetwork())
コード例 #24
0
def main():

    print("Fit pong screen into the window")
    print("Press 'up' or 'down' to start infering actions.")
    print("Press 'q' for quit.")

    infering = False

    #Load neural network
    nn = NeuralNetwork()
    nn.load()

    #Call function to clear buffer of pressed keys
    get_key_pressed()

    last_pos_h = 0
    last_pos_v = 0

    #Keeping getting track of the object locations and keys pressed
    while True:

        screen, obj_locations = get_screen_features()

        key_pressed = get_key_pressed()

        cv2.imshow("PilotoRobo - PythonJogaPong", screen)

        #Pass next frame every 10ms
        #Exit when 'q' is pressed
        if cv2.waitKey(1) == ord('q') or key_pressed == -1:
            cv2.destroyAllWindows()
            break

        #Calculate speed
        h_speed = obj_locations[0] - last_pos_h
        v_speed = obj_locations[1] - last_pos_v

        last_pos_h = obj_locations[0]
        last_pos_v = obj_locations[1]

        screen_features = np.insert(obj_locations, 2, [h_speed, v_speed])

        #Check whether we are already saving data
        if infering:

            prediction_probs = nn.predict([screen_features])[0]
            prediction = np.argmax(prediction_probs)

            if prediction == 0:
                print(prediction_probs, "Nothing")
                ReleaseKey(0x48)
                ReleaseKey(0x50)
            elif prediction == 1:
                print(prediction_probs, "Up")
                ReleaseKey(0x50)
                PressKey(0x48)
            elif prediction == 2:
                print(prediction_probs, "Down")
                ReleaseKey(0x48)
                PressKey(0x50)

        elif key_pressed > 0:
            print("Infering")
            infering = True
コード例 #25
0
ファイル: paramServer.py プロジェクト: for-l00p/Spark
		temp_y[int(l)] = 1 # we can cast this because we know labels are ints and not a weird float
		ys.append(temp_y)
	y = np.asarray(ys)
	return x,y

#data = np.array([[0,0,0],[0,1,1],[1,0,1],[1,1,0]],dtype=np.float64)
rawData = np.loadtxt("iris.data",delimiter=",") # Labels must be floats
np.random.shuffle(rawData)
label_count = len(set(rawData[:,-1]))
feature_count = len(rawData[0])-1
X, y = sliceData(rawData)
dataSetSize = len(X)

#######neural network #######################
NNlayers = [feature_count, 10, label_count]	#
nn = NeuralNetwork(NNlayers)				#
costFunction = nn.cost	 					#
#############################################

params = nn.get_weights() #weights
accruedGradients = np.zeros(sum(nn.sizes))
old_gradients = None
old_params = None
maxHistory = 10
history_S = [] #s_k = x_kp1 - x_k
history_Y = [] #y_k = gf_kp1 - gf_k
rho = []  #rho_k = 1.0 / (s_k * y_k)

batches_processed = 0
batch_size = 10
コード例 #26
0
ファイル: pathnet.py プロジェクト: c-phillips/pathnet
    def __init__(self, config, N):
        # with tf.variable_scope("PathNet", reuse=tf.AUTO_REUSE) as self.var_scope:
        # first, we will instantiate the networks for each pathnet layer
        self.network_structure = []
        self.x_train = None
        self.y_train = None
        self.x_test = None
        self.y_test = None

        data_dims = list(config.pop("datashape", None))
        if data_dims is None:
            raise ValueError(
                "You must provide a datashape parameter to configure PathNet!")
        data_dims.insert(0, None)
        print(data_dims)

        self.M = 0
        self.L = 0
        self.N = N

        self.first_layer = None
        self.sums = []
        for l, (layer_name, layer_structure) in enumerate(config.items()):
            new_layer = []

            if 'conditioning' not in layer_structure:
                print(layer_name)
                if self.M == 0:
                    self.M = layer_structure['num_modules']
                    self.first_layer = l
                    self.L = len(config.items()) - l
                    print("FIRST LAYER: {}".format(layer_name))

                    self.Pmat = tf.placeholder(tf.float32, [self.L, self.M],
                                               name="PathMatrix")
                    print(self.Pmat.get_shape().as_list())
            else:
                print("CONDITIONING: {}".format(layer_name))

            with tf.variable_scope(layer_name):
                for i in range(layer_structure['num_modules']):
                    # if it is the first module of the layer, we set the x_input to None for
                    # assignment later; otherwise, we set it to the input of the first module
                    temp_struct = copy.deepcopy(
                        layer_structure['module_structure'])
                    # for j in range(len(temp_struct)):
                    #     if 'name' in temp_struct[j]:
                    #         temp_struct[j]['name'] += "_{}_{}".format(layer_name, i+1)
                    #         # print(temp_struct[j]['name'])

                    # We need to select the proper input for this network provided the other
                    # networks/layers have been created.
                    input_ref = None
                    if i == 0 and self.first_layer is not None and l > self.first_layer:
                        input_ref = self.sums[-1]
                    elif i == 0 and self.first_layer is not None and l == self.first_layer:
                        input_ref = self.network_structure[self.first_layer -
                                                           1][0].yhat
                    elif i == 0 and self.first_layer is None and l > 0:
                        input_ref = self.network_structure[l - 1].yhat
                    elif i > 0 and self.first_layer is not None:
                        input_ref = new_layer[0].x_input

                    with tf.variable_scope("M" + str(i)):
                        new_layer.append(
                            NeuralNetwork(
                                temp_struct,
                                x_in=input_ref,
                                x_in_shape=data_dims
                                if l == 0 else self.network_structure[
                                    l - 1][0].yhat.get_shape().as_list(),
                                make_dataset=True if l == 0 else False,
                                name="M" + str(i)))
                        new_layer[-1].build_network()

                    del temp_struct
                    if 'conditioning' not in layer_structure and i == 0:
                        sum_shape = new_layer[-1].yhat.get_shape().as_list(
                        )  # self.sums[-1].get_shape().as_list() if l > self.first_layer else
                        # with tf.variable_scope("sums_{}".format(layer_name), reuse=tf.AUTO_REUSE):
                        s = tf.get_variable("sum", [*sum_shape[1:]],
                                            initializer=tf.zeros_initializer()
                                            )  #, validate_shape=False)
                        # s = tf.get_variable("sum", initializer=tf.truncated_normal([*sum_shape[1:]], mean=0.0, stddev=0.0))
                        self.sums.append(s)

                    if self.first_layer is not None:
                        self.sums[-1] = self.sums[-1] + self.Pmat[
                            l - self.first_layer, i] * new_layer[-1].yhat

                    # tf.get_variable_scope().reuse_variables()
            self.network_structure.append(new_layer)

        self.output = self.sums[
            -1]  # the main network output is the last sum layer
        self.data_layer = self.network_structure[0][
            0]  # this makes it easy to access our datapipeline
コード例 #27
0
    # Step-size
    h = 0.4
    # Numerical differentiation factor
    epsilon = 0.0001
    # Learning rate
    tau = 2
    # Tolerance for objective function
    TOL = 1
    # Shrinking factor
    rho = 0.5
    # Satisfactory descent rate
    beta = 0.95
    # Time allowed for the algorithm to run (seconds)
    running_time = 1.1

    x = []
    correct = []
    for i in range(0, 10):
        net = NeuralNetwork(M, h, epsilon, tau, TOL, running_time, beta, rho)
        net.learn(500)
        y = net.see(1000)
        x.append(i)
        correct.append(y)

    plt.plot(x, correct, 'K')
    plt.plot([0, 9], [98, 98], 'r', label="98 % correct")
    plt.plot([0, 9], [99, 99], 'b', label="99 % correct")
    plt.legend(fontsize=13)
    plt.title("Correct points after 1.1 second", fontsize=15)
    plt.xlabel("Try nr.")
    plt.show()
コード例 #28
0
win = pygame.display.set_mode((s_width, s_height))
pygame.display.set_caption('Tetris')

# neural_nets = main_menu()  # start game

#Number of agents in population
number_of_agents = 5

#Number of generations
number_of_generations = 5

#Generate neural networks
list_of_neural_nets = []

for _ in range(number_of_agents):
    list_of_neural_nets.append(NeuralNetwork())

for generation in range(number_of_generations):

    print(f"\n---------- Current generation: {generation+1} -----------")

    #Let them play one by one
    for index, net in enumerate(list_of_neural_nets):
        print(f"\nCurrent agent: {index+1}")
        play_tetris(net)

    #Create new generation according to score
    new_generation = []

    sorted_by_level = sorted(list_of_neural_nets,
                             key=lambda x: x.level_score,
コード例 #29
0
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
from sklearn.preprocessing import MinMaxScaler
from neural_net import NeuralNetwork

#1D artificial data
X = np.arange(0, 20).reshape(20, 1) + np.random.randn(20, 1)
y = (np.arange(0, 20) + np.random.randn(20)).reshape(20, 1)
scaler = MinMaxScaler()
X = scaler.fit_transform(X)
y = scaler.fit_transform(y)
X_train, X_test, y_train, y_test = train_test_split(X,
                                                    y,
                                                    test_size=0.3,
                                                    random_state=101)
plt.plot(X, y, '*', label='train data')
plt.xlabel('features')
plt.ylabel('labels')
plt.title('DNN Regressor')

nn = NeuralNetwork(mode='regression')
nn.train(X_train, y_train, 1000)
y_pred = nn.predict(X_test)
print()
r2 = r2_score(y_test, y_pred)
print('The R^2 score is:', r2)

plt.plot(X_test, y_pred, 'r*', label='test data')
plt.legend()
コード例 #30
0
import matplotlib.pyplot as plt


# functions
def n():
    return input('Ingrese un numero: ')


# input data
inputs = np.array([[0, 1, 0], [0, 0, 0], [0, 1, 1], [0, 0, 0], [1, 0, 0],
                   [1, 1, 1], [1, 0, 1]])
# output data
outputs = np.array([[0], [0], [0], [0], [1], [1], [1]])

# create a neural network
NeuralN = NeuralNetwork(inputs, outputs)
NeuralN.train()

# create two new examples to test and predict
example1 = np.array([[1, 1, 0]])
example2 = np.array([[0, 1, 1]])
exampleUser = np.array([n(), n(), n()])

# print and predict the examples
print(NeuralN.predict(example1), '- Correct: ', example1[0][0])
print(NeuralN.predict(example2), '- Correct: ', example2[0][0])
print(NeuralN.predict(exampleUser), '- Correct: ', exampleUser[0][0])

# plot the error over the entire training duration
plt.figure(figsize=(15, 5))
plt.plot(NeuralN.iters_hist, NeuralN.costerror_hist)