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


Python Brain.getBins方法代码示例

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


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

示例1: __init__

# 需要导入模块: from brain import Brain [as 别名]
# 或者: from brain.Brain import getBins [as 别名]
class AI:
    def __init__(self, inputFile=False, trainMode=False, realtimeMode=False, loadNeuralNetwork=False):
        # Object variables for initialisation
        self.inputFile = inputFile
        self.trainMode = trainMode
        self.realtimeMode = realtimeMode
        self.loadNeuralNetwork = loadNeuralNetwork

        self.neuralNet = FeedForwardNetwork()
        inLayer = LinearLayer(7)
        hiddenLayer = SigmoidLayer(5)
        outLayer = LinearLayer(1)

        self.neuralNet.addInputModule(inLayer)
        self.neuralNet.addModule(hiddenLayer)
        self.neuralNet.addOutputModule(outLayer)

        self.neuralNet.addConnection(FullConnection(inLayer, hiddenLayer))
        self.neuralNet.addConnection(FullConnection(hiddenLayer, outLayer))

        self.neuralNet.sortModules()

        DEBUG("Neural Network:")
        DEBUG(self.neuralNet)
        DEBUG("--------")

        if self.loadNeuralNetwork:
            self.neuralNet = NetworkReader.readFrom("lastNetwork.xml")

        if self.inputFile:
            if not os.path.exists(inputFile):
                print "File for CSV parsing does not exist!"
                return
            savedData = self.readCSVData(self.inputFile)

            # train upon the given data
            if self.trainMode:
                ds = self.makeTrainingSet(savedData)
                for inpt, target in ds:
                    DEBUG("Input Values from training:")
                    DEBUG(inpt)
                    DEBUG("Correction factor from training:")
                    DEBUG(target)
                trainer = BackpropTrainer(self.neuralNet, ds, verbose=True)
                trainer.trainUntilConvergence(maxEpochs=10)
                self.printGraph(ds)
                NetworkWriter.writeToFile(self.neuralNet, "lastNetwork.xml")

            # read the saved data and use it in the network
            for line in savedData:
                DEBUG("Used Data: ")
                DEBUG(line)
                result = self.neuralNet.activate(line)
                DEBUG(result)
            DEBUG("Final weigths:")
            DEBUG(self.neuralNet.params)

        # now fun begins. this is the standard mode this module runs in.
        if self.realtimeMode:
            print "We should start now!"
            self.brain = Brain()
            if os.path.isfile("lastNetwork.xml"):
                while True:
                    lines = self.brain.getBins()
                    print (lines)
            else:
                # we do a realtime training of the neural network
                print "No trained network exists!"
                time.sleep(1)
                print "Will do on-line training!"
                time.sleep(1)
                print "Hold tight for 10 seconds"

                tempBinWriter = csv.writer(open("temp.csv", "ab"))

                for i in range(1, 10):
                    time.sleep(1)
                    bins = self.brain.getBins()
                    print bins
                    tempBinWriter.writerow(bins)

                tempSavedData = self.readCSVData("temp.csv")

                ds = self.makeTrainingSet(tempSavedData)
                trainer = BackpropTrainer(self.neuralNet, ds, verbose=True)
                trainer.trainUntilConvergence(maxEpochs=10)

    def getRandomness(self):
        if self.realtimeMode:
            bins = self.brain.getBins()
            rand = 1
            for i in range(7):
                rand *= bins[i] * 100
        return rand

    def getNeuralResult(self):
        return self.neuralNet.activate(self.brain.getBins())

    def makeTrainingSet(self, savedData):
        # init a dataset with seven input and one output value
#.........这里部分代码省略.........
开发者ID:barde,项目名称:efhagame,代码行数:103,代码来源:ai.py


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