def main(num_epochs=NUM_EPOCHS): print("Loading data...") dataset = load_data() print("Building model and compiling functions...") output_layer = build_model( input_height=dataset['input_height'], input_width=dataset['input_width'], output_dim=dataset['output_dim'], ) iter_funcs = create_iter_functions( dataset, output_layer, X_tensor_type=T.tensor4, ) print("Starting training...") now = time.time() try: for epoch in train(iter_funcs, dataset): print("Epoch {} of {} took {:.3f}s".format( epoch['number'], num_epochs, time.time() - now)) now = time.time() print(" training loss:\t\t{:.6f}".format(epoch['train_loss'])) print(" validation loss:\t\t{:.6f}".format(epoch['valid_loss'])) print(" validation accuracy:\t\t{:.2f} %%".format( epoch['valid_accuracy'] * 100)) if epoch['number'] >= num_epochs: break except KeyboardInterrupt: pass # Can we do something with final output model? Like show a couple of moves... #test_batch = dataset['X_test'] # Test cases -- for Deuce. Can it keep good hands, break pairs, etc? # NOTE: These cases, and entire training... do *not* include number of draw rounds left. Add that! test_cases = ['As,Ad,4d,3s,2c', 'As,Ks,Qs,Js,Ts', '3h,3s,3d,5c,6d', '3h,4s,3d,5c,6d', '2h,3s,4d,6c,5s', '8s,Ad,Kd,8c,Jd', '8s,Ad,2d,7c,Jd', '2d,7d,8d,9d,4d', '7c,8c,Tc,Js,Qh', '2c,8s,5h,8d,2s', '[8s,9c,8c,Kd,7h]', '[Qh,3h,6c,5s,4s]', '[Jh,Td,9s,Ks,5s]', '[6c,4d,Ts,Jc,6s]', '[4h,8h,2c,7d,3h]', '[2c,Ac,Tc,6d,3d]', '[Ad,3c,Tc,4d,5d]', '3d,Jc,7d,Ac,6s', '7h,Kc,5s,2d,Tc', '5c,6h,Jc,7h,2d', 'Ts,As,3s,2d,4h'] print('looking at some test cases: %s' % test_cases) # Fill in test cases to get to batch size? for i in range(BATCH_SIZE - len(test_cases)): test_cases.append(test_cases[1]) test_batch = np.array([cards_input_from_string(case) for case in test_cases], np.int32) predict_model(output_layer=output_layer, test_batch=test_batch) print('again, the test cases: \n%s' % test_cases) #print(predict(output_layer[x=test_batch])) return output_layer
def evaluate_batch_hands(output_layer, test_cases): now = time.time() for i in range(BATCH_SIZE - len(test_cases)): test_cases.append(test_cases[0]) test_batch = np.array([cards_input_from_string(case) for case in test_cases], np.int32) print('%.2fs to create BATCH_SIZE input' % (time.time() - now)) now = time.time() pred = output_layer.get_output(lasagne.utils.floatX(test_batch), deterministic=True) print('%.2fs to get_output' % (time.time() - now)) now = time.time() #print('Prediciton: %s' % pred) #print(tp.pprint(pred)) softmax_values = pred.eval() print('%.2fs to eval() output' % (time.time() - now)) now = time.time() return softmax_values
def play(sample_size, output_file_name, model_filename=None): # Compute rewards with payout table cashier = JacksOrBetter() # "976-9-6" Jacks or Better -- with 100% long-term payout. #func = get_model_from_pickle('model.pickle') # If model file provided, unpack model, and create intelligent agent. if model_filename and os.path.isfile(model_filename): print('\nExisting model in file %s. Attempt to load it!\n' % model_filename) all_param_values_from_file = np.load(model_filename) # Size must match exactly! if USE_FAT_MODEL: output_layer, input_layer, layers = build_fat_model( HAND_TO_MATRIX_PAD_SIZE, HAND_TO_MATRIX_PAD_SIZE, 32, ) elif USE_FULLY_CONNECTED_MODEL: output_layer, input_layer, layers = build_fully_connected_model( HAND_TO_MATRIX_PAD_SIZE, HAND_TO_MATRIX_PAD_SIZE, 32, ) else: output_layer, input_layer, layers = build_model( HAND_TO_MATRIX_PAD_SIZE, HAND_TO_MATRIX_PAD_SIZE, 32, ) #print('filling model with shape %s, with %d params' % (str(output_layer.get_output_shape()), len(all_param_values_from_file))) lasagne.layers.set_all_param_values(output_layer, all_param_values_from_file) # Test the model, by giving it dummy inputs # Test cases -- it keeps the two aces. But can it recognize a straight? A flush? Trips? Draw?? Two pair?? test_cases = ['As,Ad,4d,3s,2c', 'As,Ks,Qs,Js,Ts', '3h,3s,3d,5c,6d', '3h,4s,3d,5c,6d', '2h,3s,4d,6c,5s', '8s,Ad,Kd,8c,Jd', '8s,Ad,2d,7c,Jd', '2d,7d,8d,9d,4d', '7c,8c,Tc,Js,Qh', '2c,8s,5h,8d,2s', '[8s,9c,8c,Kd,7h]', '[Qh,3h,6c,5s,4s]', '[Jh,Td,9s,Ks,5s]', '[6c,4d,Ts,Jc,6s]', '[4h,8h,2c,7d,3h]', '[2c,Ac,Tc,6d,3d]', '[Ad,3c,Tc,4d,5d]'] for i in range(BATCH_SIZE - len(test_cases)): test_cases.append(test_cases[1]) test_batch = np.array([cards_input_from_string(case) for case in test_cases], np.int32) predict_model(output_layer=output_layer, input_layer=input_layer , test_batch=test_batch) print('Cases again %s' % str(test_cases)) print('Creating player, based on this pickled model...') player = TrainedModelPlayer(output_layer, test_batch, input_layer=input_layer) else: player = OneRuleNaivePlayer() # RandomPlayer() # random draw # Run it for each game... round = 0 results = [] expected_results = [] # if player supplies us internal estimate of his hand value... if output_file_name: output_file = open(output_file_name, 'w') csv_writer = csv.writer(output_file) csv_writer.writerow(POKER_GAME_HEADER) csv_header_map = CreateMapFromCSVKey(POKER_GAME_HEADER) else: csv_writer = None now = time.time() try: while round < sample_size: sys.stdout.flush() # If AI model, then batch it. Otherwise, just go hand at a time. if isinstance(player, TrainedModelPlayer): # hand, payout, expected_payout = game(round, cashier, player=player) # Need to batch games. Since network evaluation so expensive! hand_batch, payout_batch, expected_payout_batch = game_batch(round, cashier, player=player) # Iterate over the batch... for i in range(BATCH_SIZE): hand = hand_batch[i] payout = payout_batch[i] expected_payout = expected_payout_batch[i] results.append(payout) expected_results.append(expected_payout) # Save hand to CSV, if output supplied. if csv_writer: hand_csv_row = output_hand_csv(poker_hand=hand, header_map=csv_header_map) csv_writer.writerow(hand_csv_row) # Hack, to show matrix for final hand. # print hand_to_matrix(hand.final_hand) # print(hand_string(hand.final_hand)) # pretty_print_hand_matrix(hand.final_hand) round += 1 else: for i in range(BATCH_SIZE): hand, payout, expected_payout = game(round, cashier, player=player) results.append(payout) expected_results.append(expected_payout) # Save hand to CSV, if output supplied. if csv_writer: hand_csv_row = output_hand_csv(poker_hand=hand, header_map=csv_header_map) csv_writer.writerow(hand_csv_row) round += 1 # Print the best 100 results. Why? Shows skew at the top. print '\npaid %s' % sorted(results, reverse=True)[0:100] print('\n%d hands took %.2fs. Running return: %.5f. Expected return: %.5f' % (len(results), time.time() - now, np.mean(results), np.mean(expected_results))) # sys.exit(-3) except KeyboardInterrupt: pass if csv_writer: print '\nwrote %d rows' % len(results) output_file.close() print '\npaid %s' % sorted(results, reverse=True)[0:100] print '\nexpected %s' % sorted(expected_results, reverse=True)[0:100] # What do we expect with these hands? And more importantly, how did we make out? print '\nexpected:\tave: %.5f\tstdev: %.5f\tskew: %.5f' % (np.mean(expected_results), np.std(expected_results), ss.skew(expected_results)) print '\nstats(%d):\tave: %.5f\tstdev: %.5f\tskew: %.5f' % (len(results), np.mean(results), np.std(results), ss.skew(results)) sys.stdout.flush()
def main(num_epochs=NUM_EPOCHS, out_file=None): print("Loading data...") dataset = load_data() print("Building model and compiling functions...") sys.stdout.flush() # Helps keep track of output live in re-directed out output_layer = build_model( input_height=dataset['input_height'], input_width=dataset['input_width'], output_dim=dataset['output_dim'], ) # Optionally, load in previous model parameters, from pickle! # NOTE: Network shape must match *exactly* if os.path.isfile(out_file): print('Existing model in file %s. Attempt to load it!' % out_file) all_param_values_from_file = np.load(out_file) print('Loaded values %d' % len(all_param_values_from_file)) lasagne.layers.set_all_param_values(output_layer, all_param_values_from_file) print('Successfully initialized model with previous saved params!') else: print('No existing model file (or skipping intialization) %s' % out_file) #iter_funcs = create_iter_functions( # dataset, # output_layer, # X_tensor_type=T.tensor4, # ) # Define iter functions differently. Since we now want to predict the entire vector. iter_funcs = create_iter_functions_full_output( dataset, output_layer, X_tensor_type=T.tensor4, ) print("Starting training...") now = time.time() try: for epoch in train(iter_funcs, dataset, epoch_switch_adapt=EPOCH_SWITCH_ADAPT): sys.stdout.flush() # Helps keep track of output live in re-directed out print("Epoch {} of {} took {:.3f}s".format( epoch['number'], num_epochs, time.time() - now)) now = time.time() print(" training loss:\t\t{:.6f}".format(epoch['train_loss'])) print(" validation loss:\t\t{:.6f}".format(epoch['valid_loss'])) print(" validation accuracy:\t\t{:.2f} %%".format( epoch['valid_accuracy'] * 100)) # Save model, after every epoch. save_model(out_file=out_file, output_layer=output_layer) if epoch['number'] >= num_epochs: break except KeyboardInterrupt: pass # Can we do something with final output model? Like show a couple of moves... #test_batch = dataset['X_test'] # Test cases -- it keeps the two aces. But can it recognize a straight? A flush? Trips? Draw?? Two pair?? test_cases = ['As,Ad,4d,3s,2c', '6d,7d,8d,9d,Kh', 'Jh,Td,9s,8s,5s', 'As,Ks,Qs,Js,Ts', '3h,3s,3d,5c,6d', '3h,4s,3d,5c,6d', '2h,3s,4d,6c,5s', '8s,Ad,Kd,8c,Jd', '8s,Ad,2d,7c,Jd', '2d,7d,8d,9d,4d', '7c,8c,Tc,Js,Qh', '2c,8s,5h,8d,2s', '6s,5s,Kd,3c,4h', 'As,Ks,Qs,Js,Qc', '[8s,9c,8c,Kd,7h]', '[Qh,3h,6c,5s,4s]', '[Jh,Td,9s,Ks,5s]', '[6c,4d,Ts,Jc,6s]', '[4h,8h,2c,7d,3h]', '[2c,Ac,Tc,6d,3d]', '[Ad,3c,Tc,4d,5d]'] print('looking at some test cases: %s' % test_cases) # Fill in test cases to get to batch size? for i in range(BATCH_SIZE - len(test_cases)): test_cases.append(test_cases[1]) test_batch = np.array([cards_input_from_string(case) for case in test_cases], np.int32) predict_model(output_layer=output_layer, test_batch=test_batch) print('again, the test cases: \n%s' % test_cases) return output_layer
def main(num_epochs=NUM_EPOCHS): print("Loading data...") dataset = load_data() print("Building model and compiling functions...") output_layer = build_model( input_height=dataset["input_height"], input_width=dataset["input_width"], output_dim=dataset["output_dim"] ) iter_funcs = create_iter_functions(dataset, output_layer, X_tensor_type=T.tensor4) print("Starting training...") now = time.time() try: for epoch in train(iter_funcs, dataset): print("Epoch {} of {} took {:.3f}s".format(epoch["number"], num_epochs, time.time() - now)) now = time.time() print(" training loss:\t\t{:.6f}".format(epoch["train_loss"])) print(" validation loss:\t\t{:.6f}".format(epoch["valid_loss"])) print(" validation accuracy:\t\t{:.2f} %%".format(epoch["valid_accuracy"] * 100)) if epoch["number"] >= num_epochs: break except KeyboardInterrupt: pass # Can we do something with final output model? Like show a couple of moves... # test_batch = dataset['X_test'] # Test cases -- it keeps the two aces. But can it recognize a straight? A flush? Trips? Draw?? Two pair?? test_cases = [ "As,Ad,4d,3s,2c", "As,Ks,Qs,Js,Ts", "3h,3s,3d,5c,6d", "3h,4s,3d,5c,6d", "2h,3s,4d,6c,5s", "8s,Ad,Kd,8c,Jd", "8s,Ad,2d,7c,Jd", "2d,7d,8d,9d,4d", "7c,8c,Tc,Js,Qh", "2c,8s,5h,8d,2s", "[8s,9c,8c,Kd,7h]", "[Qh,3h,6c,5s,4s]", "[Jh,Td,9s,Ks,5s]", "[6c,4d,Ts,Jc,6s]", "[4h,8h,2c,7d,3h]", "[2c,Ac,Tc,6d,3d]", "[Ad,3c,Tc,4d,5d]", ] print("looking at some test cases: %s" % test_cases) # Fill in test cases to get to batch size? for i in range(BATCH_SIZE - len(test_cases)): test_cases.append(test_cases[1]) test_batch = np.array([cards_input_from_string(case) for case in test_cases], np.int32) predict_model(output_layer=output_layer, test_batch=test_batch) print("again, the test cases: \n%s" % test_cases) # print(predict(output_layer[x=test_batch])) return output_layer