本文整理汇总了Python中NeuralNetwork.NeuralNetwork.get_output方法的典型用法代码示例。如果您正苦于以下问题:Python NeuralNetwork.get_output方法的具体用法?Python NeuralNetwork.get_output怎么用?Python NeuralNetwork.get_output使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NeuralNetwork.NeuralNetwork
的用法示例。
在下文中一共展示了NeuralNetwork.get_output方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from NeuralNetwork import NeuralNetwork [as 别名]
# 或者: from NeuralNetwork.NeuralNetwork import get_output [as 别名]
class LearningPlayer:
def __init__(self, player):
self.player = player
self.new_neural_net()
self.memories = Memories()
# make a new neural net. The number of hidden nodes, learning rate and momentum
# were found experimentally.
def new_neural_net(self):
input_size = output_size = SIZE**2
self.net = NeuralNetwork([input_size, 20, output_size])
self.net.set_learning_rate(.002)
self.net.set_momentum(.8)
def get_player(self):
return self.player
def set_player(self, player):
self.player = player
# get the move from the neural network
def make_move(self, board, learning = False):
# flatten the board from a grid to 1D for the neural network
if learning:
inputs = Game.flatten(board)
else:
inputs = Game.flatten(board, self.player)
self.memories.observe(inputs)
# get the neural networks movs
self.net.set_input(inputs)
self.net.forward_propagate()
output = self.net.get_output()
# if we are learning, we want to take the neural networks top choice,
# but if we are in a game, it needs to pick a valid move
while True:
move = output.index(max(output)) # highest rated move by NN
# Convert to x, y
y = move/SIZE
x = move%SIZE
if learning: return move
if board[x][y] == EMPTY:
return move
else:
output[move] = -1
def learn_all_known_boards(self):
self.passed_moves = self.failed_moves = 0
# solve from the reference frame of the X player
perfect_player = PerfectPlayer(X)
for board in self.memories.get_memories():
# build a grid out of a flattened board
grid_board = Game.unflatten(board)
# don't recompute move, if it's already be calculated
if self.memories.remember_move(board) >= 0:
correct_move = self.memories.remember_move(board)
# get the move from the perfect player and store it in memory
else:
correct_move = perfect_player.make_move(grid_board)
self.memories.learn_move(board, correct_move)
self.learn_move(grid_board, correct_move) # learn the move
# have the neural network 'learn' a move
def learn_move(self, board, correct_move):
my_move = self.make_move(board, True) # the neural nets move
if my_move == correct_move: self.passed_moves += 1 # it got it right!
else:
self.failed_moves += 1
# excpeted the right move to be 100% likely and the others to be 0% likely
expected_output = [0 for i in range(SIZE) for j in range(SIZE)]
expected_output[correct_move] = 1
self.net.back_propagate(expected_output) # this is where the 'learning' is done
def forget(self):
self.new_neural_net()