def get_forwards(feature_csvs): global x_iter, predicts if not type(feature_csvs) is list: feature_csvs = [feature_csvs] x_tests = [] for feature_csv in feature_csvs: feature = [int(x) for x in feature_csv.split(',')] board, ko, turn = play_go.FromFeature((feature + [0, 0, 0])[:84]) feature = play_go.ToFeature(board, ko, turn, 0, 0, True, True) x_test, _ = train_lib.parse_row(feature, True) x_tests.append(np.asarray(x_test, dtype=np.float32)) x_iter.update(x_tests) if predicts == None: predicts = estimator.predict( x=x_iter) # Only calls once with opened iterator. #predicts = estimator.predict(x=np.asarray(x_tests, dtype=np.float32)) rets = [] for _ in range(len(x_tests)): # predict in predicts: #for predict in predicts: probabilities = predicts.next()['probabilities'] actions = [] for i in range(len(probabilities)): actions.append([ str(train_lib.UnpackAction(i)) if i > 0 and i < 82 else ACTION_CODE[i], float(probabilities[i]) ]) rets.append(sorted(actions, key=lambda x: x[1], reverse=True)[:10]) if len(rets) == 1: return rets[0] return rets
def load_dataset(filename): with gfile.Open(filename) as csv_file: data_file = csv.reader(csv_file) data, target = [], [] for row in data_file: if len(row) >= 81 * 13: x, y = row[:81 * 13], int(row[-1]) else: x, y = train_lib.parse_row(row, produce_dominion) data.append(np.asarray(x, dtype=np.float32)) target.append(y) return data, target
def load_dataset(filename): with gfile.Open(filename) as csv_file: data_file = csv.reader(csv_file) data, target = [], [] for row in data_file: if len(row) >= 81 * 13: x, y = row[:81 * 13], row[-(len(row) % 81):] else: x, y = train_lib.parse_row(row, produce_dominion) data.append(np.asarray(x, dtype=np.float32)) target.append(int(y[0])) # Uses action only on policy net. return data, target
def load_csv(filename): line_cnt = sum(1 for row in open(filename)) with gfile.Open(filename) as csv_file: data_file = csv.reader(csv_file) data, action, result = [''] * line_cnt, [0] * line_cnt, [0.0 ] * line_cnt idx = 0 for row in data_file: if len(row) >= 81 * 13: x, a, r = row[:81 * 13], row[81 * 13], row[81 * 13 + 1] else: # TODO: deprecated. x, y = train_lib.parse_row(row, produce_dominion) data[idx] = np.asarray(x, dtype=np.float32) action[idx] = int(a) result[idx] = float(r) idx += 1 return data, action, result
def get_forwards(feature_csvs): if not type(feature_csvs) is list: feature_csvs = [feature_csvs] x_tests = [] for feature_csv in feature_csvs: feature = [int(x) for x in feature_csv.split(',')] board, ko, turn = play_go.FromFeature(feature + [0]) feature = play_go.ToFeature(board, ko, turn, 0, 0, True, True) x_test, _ = train_lib.parse_row(feature, True) x_tests.append(np.asarray(x_test, dtype=np.float32)) predicts = estimator.predict(np.array(x_tests)) rets = [] for predict in predicts: probabilities = list(predict['probabilities']) rets.append([float(x) for x in probabilities]) if len(rets) == 1: return rets[0] return rets
# Generates training data from rich feature csv. # As feature getting larger, it takes 30 minutes to initialize. # # Usage: python generate_train_data.py <in_csv> import numpy as np import csv import sys from tensorflow.python.platform import gfile import train_lib in_csv = sys.argv[1] with gfile.Open(in_csv) as inf: data_file = csv.reader(inf) data, target = [], [] for row in data_file: x, y = train_lib.parse_row(row, True) print(','.join(list(map(str, x + [y]))))
STONE_CODE = ' BW' POS_CODE = ' abcdefghi' ACTION_CODE = { 0: 'P', 82: 'S' } move_sequences = [] # Load model and predict model_fn = importlib.import_module('%s.model_fn' % model_dir) estimator = model_fn.GetEstimator(model_dir) board, ko, turn = play_go.InitBoard() passed = False while True: feature = play_go.ToFeature(board, ko, turn, 0, 0, True, True) print(play_go.SPrintBoard(feature[:-1])) x_test, _ = train_lib.parse_row(feature, True) # TODO: make configuable predict = estimator.predict(np.asarray(x_test, dtype=np.float32)) probabilities = list(predict)[0]['probabilities'] actions = [] for i in range(len(probabilities)): actions.append([train_lib.UnpackAction(i) if i > 0 and i < 82 else ACTION_CODE[i], probabilities[i]]) actions[0][1] = actions[0][1] / 5 # suppress pass sorted_actions = sorted(actions, key=lambda x:x[1], reverse = True) print(sorted_actions[:5]) if sorted_actions[0][0] == 'P': move_sequences.append('%s[]' % STONE_CODE[turn]) ko = 0 if passed: