当前位置: 首页>>代码示例>>Python>>正文


Python NeuralNetwork.set_learning_rate方法代码示例

本文整理汇总了Python中NeuralNetwork.NeuralNetwork.set_learning_rate方法的典型用法代码示例。如果您正苦于以下问题:Python NeuralNetwork.set_learning_rate方法的具体用法?Python NeuralNetwork.set_learning_rate怎么用?Python NeuralNetwork.set_learning_rate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NeuralNetwork.NeuralNetwork的用法示例。


在下文中一共展示了NeuralNetwork.set_learning_rate方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from NeuralNetwork import NeuralNetwork [as 别名]
# 或者: from NeuralNetwork.NeuralNetwork import set_learning_rate [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()
开发者ID:dnav6987,项目名称:TicTacToe,代码行数:93,代码来源:Players.py


注:本文中的NeuralNetwork.NeuralNetwork.set_learning_rate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。