Skip to content

Instantly share code, notes, and snippets.

View ikbendewilliam's full-sized avatar
🤷‍♂️
coding I guess

William Verhaeghe ikbendewilliam

🤷‍♂️
coding I guess
View GitHub Profile
@ikbendewilliam
ikbendewilliam / training.py
Last active June 14, 2018 09:18
Code for the tutorial on medium: Reinforcement learning on Reversing Stones
NUMBER_OF_GAMES = 5000
begintime = time.time()
for game in range(NUMBER_OF_GAMES):
board_log, action_log, probs_log, result = play_game()
all_boards.append(board_log[-1][-1])
all_actions.append(action_log)
all_probs.append(probs_log)
if (game + 1) % math.floor(NUMBER_OF_GAMES / 100) == 0 and game > 0:
time_passed = math.floor(time.time() - begintime)
print(str(game) + " / " + str(NUMBER_OF_GAMES) + \
@ikbendewilliam
ikbendewilliam / AIgaming.py
Created June 14, 2018 09:14
Code for the tutorial on medium: Reinforcement learning on Reversing Stones
botName = 'yourbotname'
from random import randint
from random import choice
import tensorflow as tf
import numpy as np
import time
import datetime
import math
import requests
@ikbendewilliam
ikbendewilliam / benchmark.py
Created June 14, 2018 09:14
Code for the tutorial on medium: Reinforcement learning on Reversing Stones
def benchmark(games = 100):
wins = draws = losses = 0
for i in range(games):
player_index = 0
player_ids = ["p0", "p1"]
result = initialise(player_index, player_ids, BOARD_SIZE)
# Initialise logs for game
board_log = []
action_log = [[], []]
@ikbendewilliam
ikbendewilliam / benchmarkExecution.py
Created June 14, 2018 09:14
Code for the tutorial on medium: Reinforcement learning on Reversing Stones
print(benchmark(1000))
@ikbendewilliam
ikbendewilliam / guessMove.py
Created June 14, 2018 09:15
Code for the tutorial on medium: Reinforcement learning on Reversing Stones
def guess_move(current_board, possible_moves, training):
probs = session.run(probabilities, feed_dict={input_positions:[[cell for row in current_board for cell in row]]})[0]
possible_moves = [move[0] * BOARD_SIZE[1] + move[1] for move in possible_moves]
probs = [p if index in possible_moves else 0 for index, p in enumerate(probs)]
probs = [round(p, 6) for p in probs]
if sum(probs) > 0:
probs = [p / sum(probs) for p in probs]
else:
@ikbendewilliam
ikbendewilliam / import.py
Created June 14, 2018 09:15
Code for the tutorial on medium: Reinforcement learning on Reversing Stones
import aigamingReversingStones
import tensorflow as tf
import numpy as np
import time
import datetime
import math
from random import randint
from random import choice
@ikbendewilliam
ikbendewilliam / init.py
Created June 14, 2018 09:15
Code for the tutorial on medium: Reinforcement learning on Reversing Stones
CONFIGURATIONS = [[8, 8], [14, 8], [4, 4]]
USE_CONFIGURATION = 0
BOARD_SIZE = CONFIGURATIONS[USE_CONFIGURATION]
print("Using configuration: \nBoard size: " + str(BOARD_SIZE[0]) + " by " + str(BOARD_SIZE[1]))
@ikbendewilliam
ikbendewilliam / initialise.py
Created June 14, 2018 09:16
Code for the tutorial on medium: Reinforcement learning on Reversing Stones
inputs_units = BOARD_SIZE[0] * BOARD_SIZE[1]
hidden_units = BOARD_SIZE[0] * BOARD_SIZE[1]
output_units = BOARD_SIZE[0] * BOARD_SIZE[1]
def initialise_tf():
global input_positions, labels, learning_rate, W1, b1, h1, W2, b2, logits, probabilities, cross_entropy, train_step
input_positions = tf.placeholder(tf.float32, shape=(1, inputs_units))
labels = tf.placeholder(tf.int64)
learning_rate = tf.placeholder(tf.float32, shape=[])
@ikbendewilliam
ikbendewilliam / initialiseGame.py
Created June 14, 2018 09:16
Code for the tutorial on medium: Reinforcement learning on Reversing Stones
def initialise(player_index, player_ids, board_dimensions):
return aigamingReversingStones.initialise(player_index=player_index, player_ids=player_ids, board_dimensions=board_dimensions)
@ikbendewilliam
ikbendewilliam / load.py
Created June 14, 2018 09:16
Code for the tutorial on medium: Reinforcement learning on Reversing Stones
def load_model(name='model.ckpt', games = 100):
global session
tf.reset_default_graph()
initialise_tf()
session = tf.Session()
tf.train.Saver().restore(session, "./models/" + name)
benchmark(games)