def __init__(self, games_count=50, nodes=[100], preprocess=False): self.is_preprocess = preprocess # Load training data data_2048 = np.loadtxt('data' + system_divider + 'log-2048-demo.txt', dtype=float, usecols=range(17)) # Get the labels raw_labels_2048 = data_2048[:, 16] labels_2048 = [[0 for y in range(4)] for x in range(len(raw_labels_2048))] for i in range(len(raw_labels_2048)): labels_2048[i][int(raw_labels_2048[i])] = 1.0 labels_2048 = np.array(labels_2048) # Get the states states_2048 = np.delete(data_2048, np.s_[-1:], 1) # Preprocess training data if self.is_preprocess: states_2048 = self.preprocess(states_2048) # Wrap the data data = [states_2048, labels_2048, states_2048, labels_2048] # Get the imput size input_size = np.array(states_2048).shape[1] if nodes != [100]: print('Layers ' + str(nodes)) # Initialize neural network self.neural_net = ANN(nodes=[24] + nodes + [4], data=data) # Train self.train() # Call super super(Neural, self).__init__(games_count=games_count)
from network import ANN from compare import * from train import Trainer import numpy as np x = np.array([[4, 5.5], [4.5, 1], [9, 2.5], [6,2], [10, 10]], dtype = float) y = np.array([[70], [89], [85], [75], [10]], dtype = float) N = ANN(2, 5, 1, 0.000001) x = x / np.amax(x) y = y / np.amax(y) print "y -> net(y)" print y, "\n------------\n", N.forward(x) ag = analyticalGrad(N, x, y) ng = numericalGrad (N, x, y) print "Analytical grad: ", sum(ag), ", ", ag.shape print "Numerical grad: ", sum(ng), ", ", ng.shape T = Trainer(N) T.train(x,y) ag = analyticalGrad(N, x, y) ng = numericalGrad (N, x, y) print "Analytical grad: ", sum(ag), ", ", ag.shape print "Numerical grad: ", sum(ng), ", ", ng.shape
class Neural(Player): neural_net = None def __init__(self, games_count=50, nodes=[100], preprocess=False): self.is_preprocess = preprocess # Load training data data_2048 = np.loadtxt('data' + system_divider + 'log-2048-demo.txt', dtype=float, usecols=range(17)) # Get the labels raw_labels_2048 = data_2048[:, 16] labels_2048 = [[0 for y in range(4)] for x in range(len(raw_labels_2048))] for i in range(len(raw_labels_2048)): labels_2048[i][int(raw_labels_2048[i])] = 1.0 labels_2048 = np.array(labels_2048) # Get the states states_2048 = np.delete(data_2048, np.s_[-1:], 1) # Preprocess training data if self.is_preprocess: states_2048 = self.preprocess(states_2048) # Wrap the data data = [states_2048, labels_2048, states_2048, labels_2048] # Get the imput size input_size = np.array(states_2048).shape[1] if nodes != [100]: print('Layers ' + str(nodes)) # Initialize neural network self.neural_net = ANN(nodes=[24] + nodes + [4], data=data) # Train self.train() # Call super super(Neural, self).__init__(games_count=games_count) def preprocess(self, data): shape = np.array(data).shape # If list of flat lists if shape[0] > shape[1]: copy = [] for i in range(shape[0]): copy.append( Process.multiple_methods(argument=data[i], methods=[Process.logarithm, Process.moveable_lines])) return copy # If one single else: return Process.multiple_methods(argument=data, methods=[Process.logarithm, Process.moveable_lines]) def train(self, batch=20, verbose_level=0, epochs=10): """ Method for training the network """ print('Training ...') self.neural_net.train(batch=batch, verbose_level=verbose_level, epochs=epochs) def do_move(self, game): prediction = None # Should preprocess ? if self.is_preprocess: # Preprocess to match training n = self.preprocess(game.grid) # Do move prediction = self.neural_net.blind_test([n])[0] else: flat = [np.array(game.grid).flatten()] prediction = self.neural_net.blind_test(flat)[0] # print(prediction) for pred in prediction: if game.move(self.directions[pred]): return True # print('Game over') # print(game.grid) # No lleagal moves, game over return False
training = np.array([ ['10000000', '10000000'], ['01000000', '01000000'], ['00100000', '00100000'], ['00010000', '00010000'], ['00001000', '00001000'], ['00000100', '00000100'], ['00000010', '00000010'], ['00000001', '00000001']]) #training = np.array([ ['10000000', '10000000']]) nn = ANN(num_features, num_labels, num_hidden) # Build training features training_features = [] for i in range (len(training)): training_features.append( np.asarray( list(training[i][0]), dtype = int )) training_features = np.asarray(training_features) # Build training labels training_labels = [] for i in range (len(training)): training_labels.append( list(np.asarray( list(training[i][1]), dtype = int ))) training_labels = np.asarray(training_labels)