modify AlphaGo

This commit is contained in:
Tongzheng Ren 2017-11-08 08:32:07 +08:00
parent a38ecabc59
commit b382bd8d31
2 changed files with 108 additions and 84 deletions

View File

@ -1,6 +1,7 @@
import tensorflow as tf
import numpy as np
import time
import os
import multi_gpu
import tensorflow.contrib.layers as layers
@ -55,16 +56,11 @@ train_op = tf.train.RMSPropOptimizer(1e-4).minimize(total_loss)
var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
saver = tf.train.Saver(max_to_keep=10, var_list=var_list)
def train():
data = np.load("data.npz")
boards = data["boards"]
wins = data["wins"]
ps = data["ps"]
print (boards.shape)
print (wins.shape)
print (ps.shape)
data_path = "/home/tongzheng/data/"
data_name = os.listdir("/home/tongzheng/data/")
epochs = 100
batch_size = 32
batch_num = boards.shape[0] // batch_size
result_path = "./results/"
with multi_gpu.create_session() as sess:
sess.run(tf.global_variables_initializer())
@ -73,11 +69,20 @@ def train():
print('Restoring model from {}...'.format(ckpt_file))
saver.restore(sess, ckpt_file)
for epoch in range(epochs):
time_train = -time.time()
for name in data_name:
data = np.load(data_path + name)
boards = data["boards"]
wins = data["wins"]
ps = data["ps"]
print (boards.shape)
print (wins.shape)
print (ps.shape)
batch_num = boards.shape[0] // batch_size
index = np.arange(boards.shape[0])
np.random.shuffle(index)
losses = []
regs = []
time_train = -time.time()
for iter in range(batch_num):
_, l, r, value, prob = sess.run([train_op, loss, reg, v, p], feed_dict={x:boards[index[iter*batch_size:(iter+1)*batch_size]],
z:wins[index[iter*batch_size:(iter+1)*batch_size]],
@ -86,13 +91,14 @@ def train():
losses.append(l)
regs.append(r)
if iter % 1 == 0:
print("Epoch: {}, Iteration: {}, Time: {}, Loss: {}, Reg: {}".format(epoch, iter, time.time()+time_train, np.mean(np.array(losses)), np.mean(np.array(regs))))
print("Epoch: {}, Part {}, Iteration: {}, Time: {}, Loss: {}, Reg: {}".format(epoch, name, iter, time.time()+time_train, np.mean(np.array(losses)), np.mean(np.array(regs))))
time_train=-time.time()
losses = []
regs = []
if iter % 20 == 0:
save_path = "Epoch{}.Iteration{}.ckpt".format(epoch, iter)
save_path = "Epoch{}.Part{}.Iteration{}.ckpt".format(epoch, name, iter)
saver.save(sess, result_path + save_path)
del data, boards, wins, ps
def forward(board):
result_path = "./results/"
@ -106,5 +112,5 @@ def forward(board):
raise ValueError("No model loaded")
return sess.run([p,v], feed_dict={x:board})
if __name__='main':
if __name__=="__main__":
train()

View File

@ -1,13 +1,17 @@
import os
import threading
import numpy as np
path = "/raid/tongzheng/AG/self_play_204/"
path = "/home/yama/leela-zero/data/npz-files/"
name = os.listdir(path)
print(len(name))
thread_num = 17
batch_num = len(name) // thread_num
def integrate(name, index):
boards = np.zeros([0, 19, 19, 17])
wins = np.zeros([0, 1])
ps = np.zeros([0, 362])
for n in name:
data = np.load(path + n)
board = data["boards"]
@ -23,21 +27,23 @@ for n in name:
boards = np.concatenate([boards, board], axis=0)
wins = np.concatenate([wins, win], axis=0)
ps = np.concatenate([ps, p], axis=0)
print("Finish " + n)
# print("Finish " + n)
print ("Integration {} Finished!".format(index))
board_ori = boards
win_ori = wins
p_ori = ps
for i in range(1, 3):
board = np.rot90(board_ori, i, (1, 2))
p = np.concatenate(
[np.rot90(p_ori[:, :-1].reshape(-1, 19, 19), i, (1, 2)).reshape(-1, 361), p_ori[:, -1].reshape(-1, 1)], axis=1)
[np.rot90(p_ori[:, :-1].reshape(-1, 19, 19), i, (1, 2)).reshape(-1, 361), p_ori[:, -1].reshape(-1, 1)],
axis=1)
boards = np.concatenate([boards, board], axis=0)
wins = np.concatenate([wins, win_ori], axis=0)
ps = np.concatenate([ps, p], axis=0)
board = board_ori[:, ::-1]
p = np.concatenate([p_ori[:, :-1].reshape(-1, 19, 19)[:, ::-1].reshape(-1, 361), p_ori[:, -1].reshape(-1, 1)], axis=1)
p = np.concatenate([p_ori[:, :-1].reshape(-1, 19, 19)[:, ::-1].reshape(-1, 361), p_ori[:, -1].reshape(-1, 1)],
axis=1)
boards = np.concatenate([boards, board], axis=0)
wins = np.concatenate([wins, win_ori], axis=0)
ps = np.concatenate([ps, p], axis=0)
@ -50,16 +56,28 @@ wins = np.concatenate([wins, win_ori], axis=0)
ps = np.concatenate([ps, p], axis=0)
board = board_ori[:, ::-1]
p = np.concatenate([np.rot90(p_ori[:, :-1].reshape(-1, 19, 19)[:, ::-1], 1, (1,2)).reshape(-1, 361), p_ori[:, -1].reshape(-1, 1)], axis=1)
boards = np.concatenate([boards, np.rot90(board, 1, (1,2))], axis=0)
wins = np.concatenate([wins, win_ori], axis=0)
ps = np.concatenate([ps, p], axis=0)
board = board_ori[:, :, ::-1]
p = np.concatenate([np.rot90(p_ori[:, :-1].reshape(-1, 19, 19)[:, :, ::-1], 1, (1,2)).reshape(-1, 361), p_ori[:, -1].reshape(-1, 1)],
p = np.concatenate(
[np.rot90(p_ori[:, :-1].reshape(-1, 19, 19)[:, ::-1], 1, (1, 2)).reshape(-1, 361), p_ori[:, -1].reshape(-1, 1)],
axis=1)
boards = np.concatenate([boards, np.rot90(board, 1, (1, 2))], axis=0)
wins = np.concatenate([wins, win_ori], axis=0)
ps = np.concatenate([ps, p], axis=0)
np.savez("data", boards=boards, wins=wins, ps=ps)
board = board_ori[:, :, ::-1]
p = np.concatenate(
[np.rot90(p_ori[:, :-1].reshape(-1, 19, 19)[:, :, ::-1], 1, (1, 2)).reshape(-1, 361),
p_ori[:, -1].reshape(-1, 1)],
axis=1)
boards = np.concatenate([boards, np.rot90(board, 1, (1, 2))], axis=0)
wins = np.concatenate([wins, win_ori], axis=0)
ps = np.concatenate([ps, p], axis=0)
np.savez("/home/tongzheng/data/data-" + str(index), boards=boards, wins=wins, ps=ps)
print ("Thread {} has finished.".format(index))
thread_list = list()
for i in range(thread_num):
thread_list.append(threading.Thread(target=integrate, args=(name[batch_num * i:batch_num * (i + 1)], i,)))
for thread in thread_list:
thread.start()
for thread in thread_list:
thread.join()