def main(): dataset = pbh.getSupervisedDataSet() network = pbh.loadNetwork() trainer = BackpropTrainer(network, dataset) """ print("Training network until convergence...") trainer.trainUntilConvergence() """ try: while(True): startTime = time.time() error = trainer.train() pbh.saveNetwork(network) duration = time.time() - startTime print("Epoch error: %f after time: %fs" %(error, duration)) except KeyboardInterrupt: print "User stopped training"
import sys sys.path.append("./data/") import time, profile, itertools import ModifiedTTTBoard as MTT import minmax import pybrainHelpers as PBH network = PBH.loadNetwork() def neural_network_board_value(board): net_input = [unicode(elem, "utf-8") for elem in board.board] val = network.activate(net_input) return val[0] def neural_network_minmax(board, depth, alpha=-9999999, beta=9999999): neural_network_minmax.nodes_visited += 1 if board.isLeafNode() or depth == 0: return (board.boardScore(), board) is_max_board = True if board.getActivePlayer() == 1 else False best_child = None result = None child_ranks = [(child, neural_network_board_value(child)) for child in board.generateChildBoards()] if is_max_board == True: child_ranks = sorted(child_ranks, key=lambda x: x[1], reverse=True) result = alpha for child in child_ranks: old_result = result result = max(result, neural_network_minmax(child[0], depth-1, result, beta)[0]) if not result == old_result: