本文整理汇总了Python中neuralnet.NeuralNet.saveToFile方法的典型用法代码示例。如果您正苦于以下问题:Python NeuralNet.saveToFile方法的具体用法?Python NeuralNet.saveToFile怎么用?Python NeuralNet.saveToFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类neuralnet.NeuralNet
的用法示例。
在下文中一共展示了NeuralNet.saveToFile方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: NNPlayer
# 需要导入模块: from neuralnet import NeuralNet [as 别名]
# 或者: from neuralnet.NeuralNet import saveToFile [as 别名]
#.........这里部分代码省略.........
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)
def setNumRecent(self, recent):
""" changes number of results to store as recent """
self.numRecent = recent
def setNumIterations(self, iters):
self.numIterations = iters
def saveToFile(self, filename, mode):
self.Q.saveToFile(filename, mode)
f = open(filename, mode)
f.write(str(self.id)+"\n")
f.write(str(self.rowSize)+"\n")
f.write(str(self.stones)+"\n")
f.write(str(self.inputSize)+"\n")
f.write(self.strategy+"\n")
f.write(str(self.learn)+"\n")
f.write(str(self.alpha)+"\n")
f.write(str(self.discount)+"\n")
f.write(str(self.numIterations)+"\n")
f.write(str(self.numRecent)+"\n")
f.flush()
f.close()
def loadFromFile(self, filename):
self.Q.loadFromFile(filename)
f = open(filename, 'r')
self.id = int(f.readline())
self.rowSize = int(f.readline())
self.stones = int(f.readline())
self.strategy = f.readline().trim()
self.learn = f.readline()
self.alpha = float(f.readline())
self.discount = float(f.readline())
self.numIterations = int(f.readline())
self.numRecent = int(f.readline())