def train(): # create neural network global nnet nnet = get_model() # SIMULATION: playing and updating Monte Carlo Tree if not bool(const.mct): # start with a simulation const.mct[0] = Node(const.game.board2array()) simulation(const.game, const.N_ROLLOUTS) for _ in range(const.N_ITERATION_MAIN): data, p_labels, v_labels = extract_data(const.game, nnet, const.MIN_VISITS) # train the network train_model(data, p_labels, v_labels, nnet, const.EPOCHS) # clear the monte carlo tree if (const.CLEAR_TREE_ON_ITERATION): const.game.restart() const.mcd = {0: Node(const.game.board2array())} if (const.EVALUATE_ON_ITERATION): w, l = evaluation(const.game, const.EVALUATION_EPISODES) t = const.EVALUATION_EPISODES - (w + l) print("won: ", w, " ties: ", t, " lost: ", l) simulation(const.game, const.N_ROLLOUTS) # save the Monte Carlo Tree # save model save_model(nnet) # save latest monte carlo tree save_mct(const.mct, const.WEIGHTS_PATH, const.GAME)
def restart(): global model, modelHistory, modelTrained, trainingCount, best, q_agent, q_values_saved, deep_agent, \ deep_values_saved, white_turn, player_turn, player_first slim_restart() best = None # used for playing against the minimax AI white_turn = True player_turn = bool(random.getrandbits(1)) player_first = player_turn if gameState == "aiGame": if aiType == "qagent": q_agent = rl.QAgent(not player_first) q_values_saved = False if aiType == "deeprl": deep_agent = rl.DeepAgent(not player_first) deep_values_saved = False if aiType == "nn" and (modelTrained or not modelTrained and trainingCount >= 50): model, modelHistory = nn.train_model(model, 100) modelTrained = True
# Finetune all parameters criterion = nn.CrossEntropyLoss() # loss function (+softmax) optimizer_ft = optim.SGD(model_ft.parameters(), lr=lr, momentum=0.9) # LR Schedules # for no policy, use : exp_lr_scheduler = None # Decay LR by a factor of 0.1 every 7 epochs exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=5*len(datasets['train']), gamma=0.1) # Prepare dataloaders data_loaders = {'train' : DataLoader(datasets['train'], batch_size=bsize_train, shuffle=True, num_workers=0), 'val' : DataLoader(datasets['val'], batch_size=bsize_val, shuffle=False, num_workers=0)} # for submission num_epochs > 60 epochs print('\nTraining the model:') best_model, curve_data = train_model(model_ft, optimizer_ft, data_loaders, num_epochs=num_epochs, loss_func=criterion, scheduler=exp_lr_scheduler, device=device, return_best=True) # ------------------- MODEL CHECKPOINT ------------------ # # Save (current) best model: # torch.save(best_model.state_dict(), f'./{model_save_path}.pkl') # # Save training/val losses and accuracies for plotting # torch.save(curve_data,f'./{model_save_path}_plot.pkl') # ------------------- Evaluate Model ------------------- # model_ft=best_model # set model mode for prediction: model_ft.eval(); if CHECK_VAL_PRED: print('\nModel Evaluation\n'+'-'*10)
import getpass import createDicts import nn import csv # Read password to connect to DB #pw = getpass.getpass() # Create dictionaries for player and team names names, teams = createDicts.createDicts() w = csv.writer(open("players.csv", "w")) for key, val in names.items(): w.writerow([key, val]) #Start tensorflow model nn.train_model(names, teams)
import nn model = nn.create_model() nn.train_model(model)
def main(train, generate, orchestrate): print('') if (train): # generate targets and store metadata if (generate): targetsJSON = generate_targets() else: try: # load targets metadata targetsJSON = json.load( open(os.path.join(os.getcwd(), 'targets/metadata.json'), 'r')) except: targetsJSON = generate_targets() # if the project settings and data settings don't align, regenerate # this only happens if the import works if (targetsJSON['SAMPLES_PER_TARGET'] != SAMPLES_PER_TARGET or targetsJSON['NUM_OF_TARGETS'] < NUM_OF_TARGETS or targetsJSON['SAMPLE_RATE'] != SAMPLE_RATE): targetsJSON = generate_targets() # make targets metadata the same length as NUM_OF_TARGETS if (targetsJSON['NUM_OF_TARGETS'] != NUM_OF_TARGETS): targetsJSON['targets'] = targetsJSON[ 'targets'][:NUM_OF_TARGETS] # format to torch datasets print('Preprocessing dataset... 📝') size_of_training_set = round(NUM_OF_TARGETS * 0.7) train_dataset = TargetsDataset( targetsJSON['targets'][:size_of_training_set], len(targetsJSON['all_labels'])) test_dataset = TargetsDataset( targetsJSON['targets'][size_of_training_set:], len(targetsJSON['all_labels'])) print('Dataset loaded! 🗄') # train the model final_model, accuracy = train_model(train_dataset, test_dataset, len(targetsJSON['all_labels'])) # save model and settings export_path = os.path.join( os.getcwd(), f'models/model_{datetime.now().strftime("%d%m%y_%H%M")}') torch.save(final_model.state_dict(), f'{export_path}.pth') train_settings = export_settings() train_settings['Final Accuracy'] = accuracy train_settings['all_labels'] = targetsJSON['all_labels'] with open(f'{export_path}.json', 'w') as json_file: json.dump(train_settings, json_file) print('Model saved! 📁') # orchestrate a user-defined sample if (orchestrate): try: # use the model just trained eval_model = final_model.eval() eval_settings = train_settings except: # or load an existing one eval_model, eval_settings = load_model() # get filepath and evaluate custom_target = click.prompt( 'What is the filepath to the target sound?', type=str)[1:-1] orchestrate_target(eval_model, eval_settings, custom_target) print('')
if __name__ == '__main__': pg.init() model = nn.create_model() trainingCount = 0 # number of training samples for the neural network modelTrained = False logging = True # check if the files containing the training samples exist if os.path.isfile("datasets/xvalues.txt") and os.path.isfile( "datasets/yvalues.txt"): with open('datasets/xvalues.txt') as file: trainingCount = sum(1 for line in file) # a minimum of 50 training samples is required to begin training the network if trainingCount >= 50: model, modelHistory = nn.train_model(model, 200) modelTrained = True size = width, height = 1024, 600 # size of the screen screen = pg.display.set_mode(size) pg.display.set_caption('tictAItoe') # title of the game window ''' The possible gameStates are the following: gameState = "title" -> Main menu gameState = "twoPlayerGame" -> Two player game gameState = "aiGame" -> Game against the AI ''' gameState = "title" ''' The possible aiTypes are the following: