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


Python NeuralNet.learnFromExample方法代码示例

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


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

示例1: NNPlayer

# 需要导入模块: from neuralnet import NeuralNet [as 别名]
# 或者: from neuralnet.NeuralNet import learnFromExample [as 别名]

#.........这里部分代码省略.........
        for op in oppSide:
            state.append(float(op))
        return state
    
    def gameOver(self, myScore, oppScore):
        """ notifies learner that the game is over,
        update the Q function based on win or loss and the move list """
        if not self.learn:
            return
        
        reward = float(myScore) - float(oppScore)
        self.movelist[self.id].append(reward)
        self._updateGameRecord(self.movelist[self.id])
        self.movelist[self.id] = []
        self._learnFromGameRecord()
        
    def _learnFromGameRecord(self):
        for i in self.recentGames:
            self._learnFromGame(self.recentGames[i])
            
    def _learnFromGame(self, movelist):
        reward = float(movelist[-1])
        # update Q function
        sap = movelist[-2]
        state = sap.state
        action = sap.action
        
        example = []
        example.append(float(action))
        example.extend(state[:self.inputSize-1])
        
        oldQ = self.Q.calculate(example)
        newQ = float((1.0 - self.alpha) * oldQ + self.alpha * reward)
        self.Q.learnFromExample(example, newQ)
        
        nextState = state[:]
        nextAction = action
        nextExample = example[:]
        
        for i in range(3, len(movelist)):
            sap = movelist[len(movelist)-i]
            reward = sap.reward
            state = sap.state
            action = sap.action
            
            example = []
            example.append(float(action))
            example.extend(state[:self.inputSize-1])
        
            # find expected rewards
            qVals = []
            for i in range(self.rowSize):
                nextExample[0] = float(i)
                qVals[i] = self.Q.calculate(nextExample)
            
            maxVal = max(qVals)
            oldQ = self.Q.calculate(example)
            newQ = float((1.0 - self.alpha) * oldQ + self.alpha * (reward + self.discount * maxVal))
            self.Q.learnFromExample(example, newQ)
            
    def _updateGameRecord(self, moves):
        """ updates statistics """
        while len(self.recentGames) > self.numRecent:
            del self.recentGames[0]
        self.recentGames.extend(moves)
        
开发者ID:changwang,项目名称:Moncala,代码行数:69,代码来源:player.py


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