Exemple #1
0
def importCatDogANN(fileName = root.path()+"/res/recCatDogANN"):
    n = FeedForwardNetwork()
    n.addInputModule(LinearLayer(7500, name='in'))
    n.addModule(SigmoidLayer(9000, name='hidden'))
    n.addOutputModule(LinearLayer(2, name='out'))
    n.addConnection(FullConnection(n['in'], n['hidden'], name='c1'))
    n.addConnection(FullConnection(n['hidden'], n['out'], name='c2'))

    n.sortModules()
    params = np.load(root.path()+'/res/cat_dog_params.txt.npy')
    n._setParameters(params)
    return n
Exemple #2
0
def importCatDogANN(fileName=root.path() + "/res/recCatDogANN"):
    n = FeedForwardNetwork()
    n.addInputModule(LinearLayer(7500, name='in'))
    n.addModule(SigmoidLayer(9000, name='hidden'))
    n.addOutputModule(LinearLayer(2, name='out'))
    n.addConnection(FullConnection(n['in'], n['hidden'], name='c1'))
    n.addConnection(FullConnection(n['hidden'], n['out'], name='c2'))

    n.sortModules()
    params = np.load(root.path() + '/res/cat_dog_params.txt.npy')
    n._setParameters(params)
    return n
def StateToActionNetwork(genome=None):
	#initial a network [12,12,4] and initial weights are baseline policy versions
	
	from pybrain.structure import FeedForwardNetwork,LinearLayer,TanhLayer,FullConnection
	network = FeedForwardNetwork()
	inLayer= LinearLayer(12) 
	outLayer = LinearLayer(4)
	network.addInputModule(inLayer) 
	network.addOutputModule(outLayer)
	
	weights = [] 	
	if(genome == None):
		import pickle
		weights = pickle.load(open("seed2"))
	else:
		weights = genome
	 
	in_to_out = FullConnection(inLayer,outLayer)   		
	network.addConnection(in_to_out)
	network.sortModules()
	network._setParameters(weights)
	return network 		
	network.addConnection(in_hidden)
	network.addConnection(hidden_out)
	network.sortModules()
 	x = network.params 


	for h in labels:
		j = [0,0,0,0,0,0,0,0,0,0]
		j[h] = 1
		targets +=  [j]

	newParams = lmsTrain(network, dataSet, targets, 20)
	newParams = newParams.flatten()
	x[(len(x) - (784 * 10)):] = newParams
	network._setParameters(p=x)
	activations = np.zeros(10)
	results = []

	for x in dataSet:
		activations = np.zeros(10)
		r = network.activate(x)
		activations[np.argmax(r)] = 1
		results += [1]
	
	testTargets = []
	for x in testLabels:
		h = np.zeros(10)
		h[x] = 1
		testTargets += [h]
	
Exemple #5
0
class NET():
    def __init__(self, arg):
        self.inputsize = arg[0]
        self.outputsize = arg[-1]
        self.hiden = arg[1:-1]
        self.err = 1
        self.old_err = 1
        b = []
        b.append(self.inputsize)
        b += self.hiden
        b.append(self.outputsize)
        #print b#"%s, %s, %s, hiddenclass=TanhLayer"%(self.inputsize, self.hiden, self.outputsize)
        self.net = FeedForwardNetwork()
        self.inputlayer = LinearLayer(self.inputsize, "Input")
        self.net.addInputModule(self.inputlayer)
        self.outputlayer = LinearLayer(self.outputsize, "Output")
        self.net.addOutputModule(self.outputlayer)
        self.hidenlayers = []
        for i in xrange(len(self.hiden)):
            self.hidenlayers.append(SigmoidLayer(self.hiden[i], "hiden%s" % i))
            self.net.addModule(self.hidenlayers[-1])
        self.net.addConnection(
            FullConnection(self.inputlayer, self.outputlayer))
        for i in xrange(len(self.hidenlayers)):
            self.net.addConnection(
                FullConnection(self.inputlayer, self.hidenlayers[i]))
            self.net.addConnection(
                FullConnection(self.hidenlayers[i], self.outputlayer))
        for i in xrange(len(self.hidenlayers)):
            for j in xrange(i + 1, len(self.hidenlayers)):
                self.net.addConnection(
                    FullConnection(self.hidenlayers[i], self.hidenlayers[j]))
                #self.print_conections(self.net)
        self.net.sortModules()
        self.ds = SupervisedDataSet(self.inputsize, self.outputsize)

    def Update(self, hiden, h):
        self.net = FeedForwardNetwork()
        self.inputlayer = LinearLayer(self.inputsize, "Input")
        self.net.addInputModule(self.inputlayer)
        self.outputlayer = LinearLayer(self.outputsize, "Output")
        self.net.addOutputModule(self.outputlayer)
        self.hidenlayers = []
        for i in xrange(len(hiden)):
            self.hidenlayers.append(SigmoidLayer(hiden[i], "hiden%s" % i))
            self.net.addModule(self.hidenlayers[-1])
        self.net.addConnection(
            FullConnection(self.inputlayer, self.outputlayer))
        for i in xrange(len(self.hidenlayers)):
            self.net.addConnection(
                FullConnection(self.inputlayer, self.hidenlayers[i]))
            self.net.addConnection(
                FullConnection(self.hidenlayers[i], self.outputlayer))
        for i in xrange(len(self.hidenlayers)):
            for j in xrange(i + 1, len(self.hidenlayers)):
                if i < h:
                    self.net.addConnection(
                        FullConnection(self.hidenlayers[i],
                                       self.hidenlayers[j]))
                elif i == h:
                    self.net.addConnection(
                        FullConnection(self.hidenlayers[i],
                                       self.hidenlayers[j],
                                       inSliceTo=hiden[i] - 1))
                else:
                    self.net.addConnection(
                        FullConnection(self.hidenlayers[i],
                                       self.hidenlayers[j]))
                #self.print_conections(self.net)
        self.net.sortModules()
        self.hiden = hiden

    def print_conections(self, n):
        print("BEGIN")
        for mod in n.modules:
            print(mod)
            for conn in n.connections[mod]:
                print(conn)
                for cc in range(len(conn.params)):
                    print(conn.whichBuffers(cc), conn.params[cc])
        print("END")

    def AddData(self, datainput, dataoutput, learningrate):
        if len(dataoutput) != len(datainput):
            print("Not equals data", len(dataoutput), len(datainput))
            return 1
        self.ds = SupervisedDataSet(self.inputsize, self.outputsize)
        for i in xrange(len(dataoutput)):
            self.ds.appendLinked(datainput[i], dataoutput[i])
        self.trainer = BackpropTrainer(self.net,
                                       dataset=self.ds,
                                       learningrate=learningrate)
        return 0

    def TrainNet(self, epoch, error):

        if epoch <= 5:
            epoch = 5
        i = 0
        count = 0
        while i < epoch:
            if error == self.err:
                break
            self.err = self.trainer.train()
            if self.err == self.old_err:
                count += 1
            else:
                count = 0
            if count == 3:
                self.err = self.old_err
                return (self.err, 1)
            self.old_err = self.err
            i += 1
        #self.SaveNet('%s  %s_%s_%s.work'%(self.err, self.inputsize, self.hiden, self.outputsize))
        return [self.err, 0]

    def TrainNetOnce(self):

        self.err = self.trainer.train()

        return self.err

    def SaveNet(self, filename=None):
        if filename == None:
            NetworkWriter.writeToFile(
                self.net, '%s  %s_%s_%s.xml' %
                (self.err, self.inputsize, self.hiden, self.outputsize))
        else:
            NetworkWriter.writeToFile(self.net, filename)

    def LoadNet(self, fname):
        self.net = NetworkReader.readFrom(fname)
        tree = ET.parse(fname)
        x = tree.getroot()
        l = []
        for modules in x.findall('Network/Modules/SigmoidLayer/dim'):
            l.append(int(modules.get("val")))
        self.hiden = l[:]
        self.inputsize = self.net.indim
        self.outputsize = self.net.outdim

    def TestNet(self, inp):
        if len(inp) != self.inputsize:
            return 0
        return self.net.activate(inp[:])

    def UpdateWeights(self, f1, f2=None):
        n = NetworkReader.readFrom(f1)
        if f2 != None:
            n2 = NetworkReader.readFrom(f2)

        def DictParams(n):
            l1 = []
            for mod in n.modules:
                l = []
                for conn in n.connections[mod]:

                    if conn.paramdim > 0:

                        l.append([conn.outmod.name, conn.params])
                d = dict(l)
                l1.append([mod.name, d])
            d1 = dict(l1)
            return d1

        d1 = DictParams(n)
        if f2 != None:
            d2 = DictParams(n2)
        d3 = DictParams(self.net)

        params = np.array([])
        if f2 != None:
            for i in d2:
                for j in d2[i]:
                    try:
                        b = d3[i][j][:]
                        b[:d2[i][j].size] = d2[i][j][:]
                        d3[i].update({j: b})
                    except:
                        pass
        for i in d1:
            for j in d1[i]:
                try:
                    b = d3[i][j][:]
                    b[:d1[i][j].size] = d1[i][j][:]
                    d3[i].update({j: b})
                except:
                    pass
        for i in d3["Input"]:
            params = np.hstack((params, d3["Input"][i]))
        for i in xrange(len(self.hiden)):
            for j in d3["hiden%s" % i]:
                params = np.hstack((params, d3["hiden%s" % i][j]))
        self.net._setParameters(params)
    N_GAUSSIANS = 3
    n.addOutputModule(MixtureDensityLayer(dim=1, name='out', mix=N_GAUSSIANS))
    # add bias module and connection to out module
    n.addModule(BiasUnit(name = 'bias'))
    n.addConnection(FullConnection(n['bias'], n['out']))

    # arbitrary number of hidden layers of type 'hiddenclass'
    n.addModule(SigmoidLayer(5, name='hidden'))
    n.addConnection(FullConnection(n['bias'], n['hidden']))
    
    # network with hidden layer(s), connections 
    # from in to first hidden and last hidden to out
    n.addConnection(FullConnection(n['in'], n['hidden']))
    n.addConnection(FullConnection(n['hidden'], n['out']))   
    n.sortModules()
    n._setParameters(np.random.uniform(-0.1, 0.1, size=n.paramdim))
    
    # build some data
    y = np.arange(0.0, 1.0, 0.005).reshape(200,1)
    x = (
        y + 
        0.3 * np.sin(2 * np.pi * y) + 
        np.random.uniform(-0.1, 0.1, y.size).reshape(y.size, 1)
    )
    dataset = SupervisedDataSet(1, 1)
    dataset.setField('input', x)
    dataset.setField('target', y)
    
    # train the network
    trainer = RPropMinusTrainerMix(n, dataset=dataset, verbose=True, 
                                   weightdecay=0.05)
n.addConnection(in_to_hidden)
n.addConnection(bias_to_hidden)
n.addConnection(bias_to_out)
n.addConnection(hidden_to_out)

n.sortModules()
n.reset()

#read the initail weight values from myparam2.txt
filetoopen = os.path.join(os.getcwd(),'myparam2.txt')
if os.path.isfile(filetoopen):
  myfile = open('myparam2.txt','r')
  c=[]
  for line in myfile:
    c.append(float(line))
  n._setParameters(c)
else:
  myfile = open('myparam2.txt','w')
  for i in n.params:
    myfile.write(str(i)+'\n')
myfile.close()

#activate the neural networks
act = SupervisedDataSet(1,1)
act.addSample((0.2,),(0.880422606518061,))
n.activateOnDataset(act)
#create the test DataSet
x = numpy.arange(0.0, 1.0+0.01, 0.01)
s = 0.5+0.4*numpy.sin(2*numpy.pi*x)
tsts = SupervisedDataSet(1,1)
tsts.setField('input',x.reshape(len(x),1))
Exemple #8
0
class NeuralNet(regression):
    
    
    '''
    #deprecated
    def __init__(self, inputDim, outputDim):
        \'''
	Initializes class parameters
	
	Input:   

        \'''
        regression.__init__(self,inputDim, outputDim)
        #self.net = buildNetwork(inputDim, outputDim)
        self.net = FeedForwardNetwork()
        inLayer = LinearLayer(inputDim)
        hiddenLayer1 = TanhLayer(10)
        hiddenLayer2 = TanhLayer(10)
        outLayer = SigmoidLayer(outputDim)
        self.net.addInputModule(inLayer)
        self.net.addModule(hiddenLayer1)
        self.net.addModule(hiddenLayer2)
        self.net.addOutputModule(outLayer)

        in_to_hidden1 = FullConnection(inLayer, hiddenLayer1)
        hidden1_to_hidden2=FullConnection(hiddenLayer1,  hiddenLayer2)
        hidden2_to_out = FullConnection(hiddenLayer2, outLayer)
        self.net.addConnection(in_to_hidden1)
        self.net.addConnection(hidden1_to_hidden2)
        self.net.addConnection(hidden2_to_out)

        self.net.sortModules()
        self.shape=self.net.params.shape
        self.ds = SupervisedDataSet(self.inputDimension, self.outputDimension)
    
    '''
 
    def __init__(self, rs):
        regression.__init__(self,rs)
        self.learningRate=rs.learningRate
        self.momentum=rs.momentum
        
        self.net = FeedForwardNetwork()
        
        #input Layer
        inLayer = layersDict[rs.inputLayer](rs.inputDim)
        self.net.addInputModule(inLayer)
        
        #outputLayer
        outLayer = layersDict[rs.outputLayer](rs.outputDim)
        self.net.addOutputModule(outLayer)
        
        #no hidden Layer
        if(len(rs.hiddenLayers)==0):
            #connection between input and output Layer
            in_to_out = FullConnection(inLayer, outLayer)
            self.net.addConnection(in_to_out)
            if(rs.bias==True):
                bias= BiasUnit('bias')
                self.net.addModule(bias)
                bias_to_out = FullConnection(bias, outLayer)
                self.net.addConnection(bias_to_out)
        else :
            #hidden Layers
            hiddenLayers=[]
            for layer in rs.hiddenLayers:
                tmp=layersDict[layer[0]](layer[1])
                self.net.addModule(tmp)
                hiddenLayers.append(tmp)
             
            #connection between input and first hidden Layer  
            in_to_hidden=FullConnection(inLayer,hiddenLayers[0])
            self.net.addConnection(in_to_hidden)
            
            #connection between hidden Layers
            i=0
            for i in range(1,len(hiddenLayers)):
                hidden_to_hidden=FullConnection(hiddenLayers[i-1],hiddenLayers[i])
                self.net.addConnection(hidden_to_hidden)
            
            #connection between last hidden Layer and output Layer   
            hidden_to_out= FullConnection(hiddenLayers[i],outLayer)
            self.net.addConnection(hidden_to_out)     
            
            if(rs.bias==True):
                bias=BiasUnit('bias')
                self.net.addModule(bias)
                for layer in hiddenLayers :
                    bias_to_hidden = FullConnection(bias, layer)
                    self.net.addConnection(bias_to_hidden)
                
                bias_to_out = FullConnection(bias, outLayer)
                self.net.addConnection(bias_to_out)
                

        
        #initilisation of weight
        self.net.sortModules()
        self.shape=self.net.params.shape
        self.net._setParameters(np.random.normal(0.0,0.1,self.shape))
            
        
        self.ds = SupervisedDataSet(self.inputDimension, self.outputDimension)
        #print(self.net)
            
    def setTheta(self, theta):
        self.net._setParameters(theta.reshape(self.shape))

    def getTheta(self):
        return self.net.params

    def load(self,thetaFile):
        '''
        load wheight of the neural network from the thetafile
        '''
        self.net._setParameters(np.loadtxt(thetaFile+".theta"))
        #print ("theta LOAD : ", self.net.params)
        return self.net.params

    def getTrainingData(self, inputData, outputData):
        '''
        Verifies the validity of the given input and output data
        Data should be organized by columns
        
        Input:      -inputdata, numpy N-D array
                    -outputData, numpy N-D array
        '''
        regression.getTrainingData(self,inputData, outputData)

        for i in range(self.numberOfSamples):
            self.ds.addSample(inputData[i],outputData[i])

    def train(self):
        '''
        Perform batch regression
        '''
        trainer = BackpropTrainer(self.net, self.ds, learningrate=self.learningRate, momentum=self.momentum)

        minError=10
        while(True):
            error=trainer.train()
            print(self.meanSquareError())
            if(error<minError):
                minError=error
                self.saveTheta(self.rs.path+self.rs.thetaFile+".theta")

        
        #trainer.trainUntilConvergence(maxEpochs=10, verbose=True)
        #trainer.trainEpochs(10)
    def computeOutput(self, inputVal):
        '''
        Returns the output depending on the given input and theta
        
        Input:      -inputVal: numpy N-D array
                    -theta: numpy N-D array
        
        Output:     -fa_out: numpy N-D array, output approximated
        '''
        assert(inputVal.shape[0]==self.inputDimension), "NeuralNet: Bad input format : " + str(inputVal.shape[0])+"/"+str(self.inputDimension)
        output=self.net.activate(inputVal)
        #print(output)
        return output

    def meanSquareError(self):
        output=self.net.activateOnDataset(self.ds)
        return np.mean((self.outputData - output)**2)
class NNW:
  def __init__(self, num_input, num_hidden, num_output):
      # self.net = buildNetwork(num_input, num_hidden, num_output, bias = True)
    self.net = FeedForwardNetwork()

    self.num_input = num_input
    self.num_hidden = num_hidden
    self.num_output = num_output

    inLayer = LinearLayer(num_input, name='in')
    hiddenLayer1 = SigmoidLayer(num_hidden, name='hidden1')
    outLayer = LinearLayer(num_output, name='out')

    self.net.addInputModule(inLayer)
    self.net.addModule(hiddenLayer1)
    self.net.addOutputModule(outLayer)

    self.in_to_hidden = FullConnection(inLayer, hiddenLayer1)
    self.hidden_to_out = FullConnection(hiddenLayer1, outLayer)

    self.net.addConnection(self.in_to_hidden)
    self.net.addConnection(self.hidden_to_out)

    self.net.sortModules()

    self.dataset = None

  def trainData(self, learningRate = 0.01, batch = True, maxEpochs = 100, continueEpochs = 10):
    # http://pybrain.org/docs/api/supervised/trainers.html?highlight=backproptrainer#pybrain.supervised.trainers.BackpropTrainer
    # BackpropTrainer(module, dataset=None, learningrate=0.01, lrdecay=1.0, momentum=0.0, verbose=False, batchlearning=False, weightdecay=0.0)
    # things for setting:
    # 1. dataset
    # 2. learningrate: 0.01 ~ 0.25
    # 3. batchlearning: True or False
    trainer = BackpropTrainer(self.net, dataset = self.dataset, learningrate = learningRate, batchlearning = batch)

    # trainUntilConvergence(dataset=None, maxEpochs=None, verbose=None, continueEpochs=10, validationProportion=0.1)
    # things for setting:
    # 1. maxEpochs: at most that many epochs are trained. 
    # 2. continueEpochs: Each time validation error hits a minimum, try for continueEpochs epochs to find a better one.
    # 3. validationProportion: ratio of the dataset for validation dataset.
    trainer.trainUntilConvergence(maxEpochs = 10000, continueEpochs = 10, validationProportion=0.2)
    # print error

  def trainOnce(self, learningRate = 0.01, batch = True, maxEpochs = 100, continueEpochs = 10):
    # http://pybrain.org/docs/api/supervised/trainers.html?highlight=backproptrainer#pybrain.supervised.trainers.BackpropTrainer
    # BackpropTrainer(module, dataset=None, learningrate=0.01, lrdecay=1.0, momentum=0.0, verbose=False, batchlearning=False, weightdecay=0.0)
    # things for setting:
    # 1. dataset
    # 2. learningrate: 0.01 ~ 0.25
    # 3. batchlearning: True or False
    trainer = BackpropTrainer(self.net, dataset = self.dataset, learningrate = learningRate, batchlearning = batch)

    error = trainer.train()
    print error


  def setTrainData(self, train, target):
    ds = SupervisedDataSet(self.num_input, self.num_output)
    dataSize = len(train) # should be same as len(target)
    for i in range(dataSize):
      ds.addSample(train[i], target[i])
    self.dataset = ds


  def activate(self, inputData):
      # self.net.sortModules()
      decision = self.net.activate(inputData)
      return decision

  def getParameter(self, laynumber = 0):
    if laynumber == 0:
          return self.net.params
    elif laynumber == 1:
          return self.in_to_hidden.params
    elif laynumber == 2:
          return self.hidden_to_out.params

  def setParameters(self, para):
      self.net._setParameters(para)
    N_GAUSSIANS = 3
    n.addOutputModule(MixtureDensityLayer(dim=1, name='out', mix=N_GAUSSIANS))
    # add bias module and connection to out module
    n.addModule(BiasUnit(name='bias'))
    n.addConnection(FullConnection(n['bias'], n['out']))

    # arbitrary number of hidden layers of type 'hiddenclass'
    n.addModule(SigmoidLayer(5, name='hidden'))
    n.addConnection(FullConnection(n['bias'], n['hidden']))

    # network with hidden layer(s), connections
    # from in to first hidden and last hidden to out
    n.addConnection(FullConnection(n['in'], n['hidden']))
    n.addConnection(FullConnection(n['hidden'], n['out']))
    n.sortModules()
    n._setParameters(np.random.uniform(-0.1, 0.1, size=n.paramdim))

    # build some data
    y = np.arange(0.0, 1.0, 0.005).reshape(200, 1)
    x = (y + 0.3 * np.sin(2 * np.pi * y) +
         np.random.uniform(-0.1, 0.1, y.size).reshape(y.size, 1))
    dataset = SupervisedDataSet(1, 1)
    dataset.setField('input', x)
    dataset.setField('target', y)

    # train the network
    trainer = RPropMinusTrainerMix(n,
                                   dataset=dataset,
                                   verbose=True,
                                   weightdecay=0.05)
    trainer.trainEpochs(200)
Exemple #11
0
n.addConnection(in_to_hidden)
n.addConnection(bias_to_hidden)
n.addConnection(bias_to_out)
n.addConnection(hidden_to_out)

n.sortModules()
n.reset()

#read the initail weight values from myparam2.txt
filetoopen = os.path.join(os.getcwd(), 'myparam2.txt')
if os.path.isfile(filetoopen):
    myfile = open('myparam2.txt', 'r')
    c = []
    for line in myfile:
        c.append(float(line))
    n._setParameters(c)
else:
    myfile = open('myparam2.txt', 'w')
    for i in n.params:
        myfile.write(str(i) + '\n')
myfile.close()

#activate the neural networks
act = SupervisedDataSet(1, 1)
act.addSample((0.2, ), (0.880422606518061, ))
n.activateOnDataset(act)
#create the test DataSet
x = numpy.arange(0.0, 1.0 + 0.01, 0.01)
s = 0.5 + 0.4 * numpy.sin(2 * numpy.pi * x)
tsts = SupervisedDataSet(1, 1)
tsts.setField('input', x.reshape(len(x), 1))
class Slave(object):
    def __init__(self):
        self.net = FeedForwardNetwork()

    def createNetwork(self, inLayer, inLType, outLayer, outLType, hLayerNum, hiddenLayers, hLayersType, bias=True, outPutBias=True):

        del self.net
        self.net = FeedForwardNetwork()

        if bias:
            # Definição da camada de entrada
            if inLType == 0:
                self.net.addInputModule(LinearLayer(inLayer,name='in'))
            elif inLType == 1:
                self.net.addInputModule(SigmoidLayer(inLayer,name='in'))
            elif inLType == 2:
                self.net.addInputModule(TanhLayer(inLayer,name='in'))
            elif inLType == 3:
                self.net.addInputModule(SoftmaxLayer(inLayer,name='in'))
            elif inLType == 4:
                self.net.addInputModule(GaussianLayer(inLayer,name='in'))

            # Definição das camadas escondidas
            self.hiddenLayers = []
            if hLayersType == 0:
                for i in range(0, hLayerNum):
                    self.hiddenLayers.append(LinearLayer(hiddenLayers[i]))
                    self.net.addModule(self.hiddenLayers[i])
            elif hLayersType == 1:
                for i in range(0, hLayerNum):
                    self.hiddenLayers.append(SigmoidLayer(hiddenLayers[i]))
                    self.net.addModule(self.hiddenLayers[i])
            elif hLayersType == 2:
                for i in range(0, hLayerNum):
                    self.hiddenLayers.append(TanhLayer(hiddenLayers[i]))
                    self.net.addModule(self.hiddenLayers[i])
            elif hLayersType == 3:
                for i in range(0, hLayerNum):
                    self.hiddenLayers.append(SoftmaxLayer(hiddenLayers[i]))
                    self.net.addModule(self.hiddenLayers[i])
            elif hLayersType == 4:
                for i in range(0, hLayerNum):
                    self.hiddenLayers.append(GaussianLayer(hiddenLayers[i]))
                    self.net.addModule(self.hiddenLayers[i])

            # Definição da camada de saída
            if outLType == 0:
                self.net.addOutputModule(LinearLayer(outLayer,name='out'))
            elif outLType == 1:
                self.net.addOutputModule(SigmoidLayer(outLayer,name='out'))
            elif outLType == 2:
                self.net.addOutputModule(TanhLayer(outLayer,name='out'))
            elif outLType == 3:
                self.net.addOutputModule(SoftmaxLayer(inLayer,name='out'))
            elif outLType == 4:
                self.net.addOutputModule(GaussianLayer(outLayer,name='out'))

            # Criação do Bias
            self.net.addModule(BiasUnit(name='networkBias'))

            # Conexão entre as diversas camadas
            if self.hiddenLayers:
                self.net.addConnection(FullConnection(self.net['in'], self.hiddenLayers[0]))
                for h1, h2 in zip(self.hiddenLayers[:-1], self.hiddenLayers[1:]):
                    self.net.addConnection(FullConnection(self.net['networkBias'],h1))
                    self.net.addConnection(FullConnection(h1,h2))
                if outPutBias:
                    self.net.addConnection(FullConnection(self.net['networkBias'],self.net['out']))
                self.net.addConnection(FullConnection(self.hiddenLayers[-1],self.net['out']))
            else:
                if outPutBias:
                    self.net.addConnection(FullConnection(self.net['networkBias'],self.net['out']))
                self.net.addConnection(FullConnection(self.net['in'],self.net['out']))
        else:
            # Definição da camada de entrada
            if inLType == 0:
                self.net.addInputModule(LinearLayer(inLayer,name='in'))
            elif inLType == 1:
                self.net.addInputModule(SigmoidLayer(inLayer,name='in'))
            elif inLType == 2:
                self.net.addInputModule(TanhLayer(inLayer,name='in'))
            elif inLType == 3:
                self.net.addInputModule(SoftmaxLayer(inLayer,name='in'))
            elif inLType == 4:
                self.net.addInputModule(GaussianLayer(inLayer,name='in'))

            # Definição das camadas escondidas
            self.hiddenLayers = []
            if hLayersType == 0:
                for i in range(0, hLayerNum):
                    self.hiddenLayers.append(LinearLayer(hiddenLayers[i]))
                    self.net.addModule(self.hiddenLayers[i])
            elif hLayersType == 1:
                for i in range(0, hLayerNum):
                    self.hiddenLayers.append(SigmoidLayer(hiddenLayers[i]))
                    self.net.addModule(self.hiddenLayers[i])
            elif hLayersType == 2:
                for i in range(0, hLayerNum):
                    self.hiddenLayers.append(TanhLayer(hiddenLayers[i]))
                    self.net.addModule(self.hiddenLayers[i])
            elif hLayersType == 3:
                for i in range(0, hLayerNum):
                    self.hiddenLayers.append(SoftmaxLayer(hiddenLayers[i]))
                    self.net.addModule(self.hiddenLayers[i])
            elif hLayersType == 4:
                for i in range(0, hLayerNum):
                    self.hiddenLayers.append(GaussianLayer(hiddenLayers[i]))
                    self.net.addModule(self.hiddenLayers[i])

            # Definição da camada de saída
            if outLType == 0:
                self.net.addOutputModule(LinearLayer(outLayer,name='out'))
            elif outLType == 1:
                self.net.addOutputModule(SigmoidLayer(outLayer,name='out'))
            elif outLType == 2:
                self.net.addOutputModule(TanhLayer(outLayer,name='out'))
            elif outLType == 3:
                self.net.addOutputModule(SoftmaxLayer(inLayer,name='out'))
            elif outLType == 4:
                self.net.addOutputModule(GaussianLayer(outLayer,name='out'))

            if self.hiddenLayers:
                self.net.addConnection(FullConnection(self.net['in'], self.hiddenLayers[:1]))
                for h1, h2 in zip(self.hiddenLayers[:-1], self.hiddenLayers[1:]):
                    self.net.addConnection(FullConnection(h1,h2))
                self.net.addConnection(FullConnection(self.hiddenLayers[-1:],self.net['out']))
            else:
                self.net.addConnection(FullConnection(self.net['in'],self.net['out']))

        # Termina de construir a rede e a monta corretamente
        self.net.sortModules()

    def setParameters(self, parameters):
        self.net._setParameters(parameters)

    def getParameters(self):
        return self.net.params.tolist()

    def createDataSet(self, ds):
        inp = ds.indim
        targ = ds.outdim

        self.ds = SupervisedDataSet(inp, targ)

        for i,t in ds:
            self.ds.addSample(i,t)

    def updateDataSet(self, ds):
        self.ds.clear(True)
        for i,t in ds:
            self.ds.addSample(i,t)
        self.trainer.setData(self.ds)

    def createTrainer(self, learnrate=0.01, ldecay=1.0, momentum=0.0, batchlearn=False, wdecay=0.0):
        self.trainer = BackpropTrainer(self.net, self.ds, learningrate=learnrate, lrdecay=ldecay, momentum=momentum, batchlearning=batchlearn, weightdecay=wdecay)

    def trainNetwork(self):
        self.trainer.train()

    def loadNetwork(self, net):
        del self.net
        self.net = net
Exemple #13
0
net.addOutputModule(outLayer)

net.addConnection(FullConnection(inLayer, hiddenLayer))
#net.addConnection(FullConnection(hiddenLayer,hiddenLayer2))
#net.addConnection(FullConnection(hiddenLayer2, outLayer))
net.addConnection(FullConnection(hiddenLayer, outLayer))
net.sortModules()
#net = buildNetwork(ds_train.indim, HIDDEN_NEURONS_NUM, ds_train.outdim, bias=True,outclass=SoftmaxLayer)
# ds.indim -- количество нейронов входного слоя, равне количеству признаков
# ds.outdim -- количество нейронов выходного слоя, равное количеству меток классов
# SoftmaxLayer -- функция активации, пригодная для решения задачи многоклассовой классификации

init_params = np.random.random(
    (len(net.params)
     ))  # Инициализируем веса сети для получения воспроизводимого результата
net._setParameters(init_params)
#%%
np.random.seed(0)
# Модуль настройки параметров pybrain использует модуль random; зафиксируем seed для получения воспроизводимого результата
trainer = BackpropTrainer(
    net, dataset=ds_train)  # Инициализируем модуль оптимизации
err_train, err_val = trainer.trainUntilConvergence(maxEpochs=MAX_EPOCHS)
line_train = plt.plot(err_train, 'b', err_val, 'r')  # Построение графика
xlab = plt.xlabel('Iterations')
ylab = plt.ylabel('Error')

#%%
#ROC - кривые - порог
grance = 0.5
res_train = net.activateOnDataset(
    ds_train)  # Подсчет результата на обучающей выборке
Exemple #14
0
class Panda(object):
    """
	This class is in charge of manipulating the pandas, the neural network, the collisions, etc
	
	Attributes:
		pandaIds (dict): Maps an int to a Panda object
	    pandaList (list): List of all the pandas
	    pandaActorIdle (Actor): Panda3d Actor class
	    pandaActorWalking (Actor): Panda3d Actor class
		livingPandas (int): counter of living pandas

		health (float): Amount of health
	    viewDistance (float): Maximum view distance
	    baseSpeed (float): Speed multiplier
	    baseTurnSpeed (float): Turn speed multiplier (in degrees)
	    isAlive (bool): True if alive
	    isDying (bool): True if health == 0
	    carrotsEaten (int): Number of carrots eaten by the panda (score)
	    brainWeights (list): List of weights for the net
	    network (FeedForwardNetwork): PyBrain Neural Network object
		inputNumber (int): Number of view frustums
	    inputDistanceList (list): float between 1.0 and 0.0; 1.0 -> farther, 0.0 -> closer
	    inputTypeList (list): -1 spike, 0 nothing, 1 carrot
	    lensNodeList (list): list of the nodes of the frustums
	    handleLifeBar (NodePath): Node for the life bar
	    pandaHandle (NodePath): Node for the panda
	    pandaActorIdleHandle (NodePath): Node for the idle panda animation
	    pandaActorWalkingHandle (NodePath): Node for the walking panda animation
	    
	"""
    pandaList = []
    pandaIds = {}
    pandaActorIdle = None
    pandaActorWalking = None
    livingPandas = 0

    def __init__(self, game, x, y, genome):
        """
		Initialize
		
		Args:
		    game (game (Game): A reference to the Game object
		    x (int): x coordinate
		    y (int): y coordinate
		    genome (G1DList): PyEvolve's individual container
		"""
        if not Panda.pandaActorIdle:
            Panda.pandaActorIdle = Actor("models/panda-model",
                                         {"walk": "models/panda-walk4"})
            Panda.pandaActorIdle.setPos(0, 0, 0)
            Panda.pandaActorIdle.setScale(0.005, 0.005, 0.005)

        if not Panda.pandaActorWalking:
            Panda.pandaActorWalking = Actor("models/panda-model",
                                            {"walk": "models/panda-walk4"})
            Panda.pandaActorWalking.setPos(0, 0, 0)
            Panda.pandaActorWalking.setScale(0.005, 0.005, 0.005)
            Panda.pandaActorWalking.loop("walk")

        self.health = 100.0
        self.viewDistance = 50.0
        self.baseSpeed = 1.0
        self.baseTurnSpeed = 10.0
        self.isAlive = True
        self.isDying = False
        self.carrotsEaten = 0

        self.lensNodeList = []
        self.inputNumber = 7  #number of view frustums
        self.inputDistanceList = [
            0
        ] * self.inputNumber  # float between 1.0 and 0.0; 1.0 -> farther, 0.0 -> closer
        self.inputTypeList = [
            0
        ] * self.inputNumber  #-1 spike, 0 nothing, 1 carrot

        h = randrange(0, 360)
        self.pandaHandle = game.render.attachNewNode("pandaHandle")
        self.pandaHandle.setPos(x, y, 0)
        self.pandaHandle.setH(h)
        #self.pandaHandle.showTightBounds()

        self.pandaActorWalkingHandle = self.pandaHandle.attachNewNode(
            "pandaActorWalkingHandle")
        self.pandaActorWalkingHandle.setPos(0, 0, 0)

        self.pandaActorIdleHandle = self.pandaHandle.attachNewNode(
            "pandaActorIdleHandle")
        self.pandaActorIdleHandle.setPos(0, 0, 0)
        Panda.pandaActorWalking.instanceTo(self.pandaActorWalkingHandle)

        Panda.pandaList.append(self)
        Panda.livingPandas += 1

        Panda.pandaIds[genome.getParam("pandaId")] = self
        self.brainWeights = genome.genomeList

        self.__setUpLifeBar()
        self.__setUpLens()
        self.__setUpBrain(genome)

    def __delete(self, task=None):
        """
		Free memory allocated by this panda
		
		Args:
		    task (task): Panda3D requires this param
		"""
        self.isAlive = False

        for node in self.lensNodeList:
            node.removeNode()
        del self.lensNodeList[:]

        self.bgHandle.removeNode()
        self.fgHandle.removeNode()
        self.handleLifeBar.removeNode()

        self.pandaActorIdleHandle.removeNode()
        self.pandaActorWalkingHandle.removeNode()
        self.pandaHandle.removeNode()
        Panda.livingPandas -= 1

    def __die(self, game):
        """
		Prepare to die
		
		Args:
		    game (Game): A reference to the Game object
		"""

        if not self.isDying:
            self.health = 0.0  #maybe it is < 0
            self.isDying = True
            Panda.pandaActorIdle.instanceTo(self.pandaActorIdleHandle)
            self.pandaActorWalkingHandle.hide()
            game.taskMgr.doMethodLater(3, self.__delete, 'delete panda')

    def __setUpLens(self):
        """
		Set up the view frustums
		"""
        hfov = 180.0 / self.inputNumber
        vfov = 10.0
        for i in range(1, self.inputNumber * 2, 2):
            delta = 180.0 - (90 + vfov / 2)
            delta *= (pi / 180.0)
            hfovRad = hfov * (pi / 180.0)

            pointTo = Vec3(cos(hfovRad * i / 2), -sin(hfovRad * i / 2),
                           cos(delta))

            lens = PerspectiveLens()
            lens.setFov(hfov, vfov)
            lens.setNear(0.01)
            lens.setFar(self.viewDistance)
            lens.setViewVector(pointTo, pointTo.up())

            lNode = LensNode("lensNode", lens)
            #lNode.showFrustum()
            handleLens = self.pandaHandle.attachNewNode(lNode)
            handleLens.setPos(0, 0, 0)

            self.lensNodeList.append(handleLens)

    def __setUpLifeBar(self):
        """
		Set up the life bar
		"""
        self.handleLifeBar = self.pandaHandle.attachNewNode("lifeBarNode")
        self.handleLifeBar.setBillboardPointEye()
        self.handleLifeBar.setPos(0, 0, 3)

        self.bgCard = CardMaker("bg")
        self.bgCard.setColor(1, 0, 0, 1)
        self.bgCard.setFrame(-1, 1, -0.2, 0.2)
        self.bgHandle = self.handleLifeBar.attachNewNode(
            self.bgCard.generate())
        self.bgHandle.setPos(0, 0, 0)

        self.fgCard = CardMaker("fg")
        self.fgCard.setColor(0, 1, 0, 1)
        self.fgCard.setFrame(-1, 1, -0.2, 0.2)
        self.fgHandle = self.handleLifeBar.attachNewNode(
            self.fgCard.generate())
        self.fgHandle.setPos(0, 0, 0)

        self.__updateHealthBar()

    def update(self, game, carrots, spikes):
        """

		
		Args:
		    game (Game): A reference to the Game object
		    carrots (list): list of carrots
		    spikes (list): list of spikes
		
		Returns:
		    None: if is dying
		"""
        if self.isDying:
            return

        self.__updateInputs(carrots, spikes)
        brainInput = self.inputDistanceList + self.inputTypeList
        brainOutput = self.network.activate(brainInput).tolist()

        self.pandaHandle.setH(self.pandaHandle,
                              (brainOutput[0]) * self.baseTurnSpeed)
        self.pandaHandle.setPos(self.pandaHandle, 0,
                                -((brainOutput[1] + 1.0) * self.baseSpeed), 0)

        self.__handleCollisions(game, carrots, spikes)
        self.__decrementHealth()
        self.__updateHealthBar()

        if self.health <= 0.0:
            self.__die(game)

    def __decrementHealth(self):
        """
		Decrement the health of the panda each frame
		"""
        self.health -= 0.2

    def __updateInputs(self, carrots, spikes):
        """
		Update the inputs of the neural network
		
		Args:
		    carrots (list): list of carrots
		    spikes (list): list of spikes
		"""
        self.inputDistanceList = [
            self.viewDistance
        ] * self.inputNumber  # float between 1.0 and 0.0; 1.0 -> farther, 0.0 -> closer
        self.inputTypeList = [
            0
        ] * self.inputNumber  #-1 spike, 0 nothing, 1 carrot

        for i in range(self.inputNumber):
            carrotDistances = []
            spikeDistances = []

            for carrot in carrots:
                if not carrot.isActive:
                    continue

                point = carrot.carrotHandle.getPos(self.lensNodeList[i])

                if self.lensNodeList[i].node().isInView(
                        Point3(point.getX(), point.getY(), 0.01)):
                    dist = (point.getXy() -
                            self.lensNodeList[i].getPos().getXy()).length()
                    carrotDistances.append(dist)

            for spike in spikes:
                point = spike.spikeHandle.getPos(self.lensNodeList[i])

                if self.lensNodeList[i].node().isInView(
                        Point3(point.getX(), point.getY(), 0.01)):
                    dist = (point.getXy() -
                            self.lensNodeList[i].getPos().getXy()).length()
                    spikeDistances.append(dist)

            self.inputTypeList[i] = 0
            minDist = None

            if carrotDistances:
                minDist = min(carrotDistances)
                self.inputTypeList[i] = 1

            if spikeDistances:
                minS = min(spikeDistances)

                if minDist != None:  #minDist can be 0, which is evaluted false
                    if minS < minDist:
                        minDist = minS
                        self.inputTypeList[i] = -1
                else:
                    minDist = minS
                    self.inputTypeList[i] = -1

            if minDist != None:
                self.inputDistanceList[i] = minDist

            if self.inputDistanceList[i] > self.viewDistance:
                self.inputDistanceList[i] = self.viewDistance

        self.inputDistanceList = [
            float(i) / self.viewDistance for i in self.inputDistanceList
        ]  #normalize the distances
        #print([round(i, 2) for i in self.inputDistanceList])

    def __updateHealthBar(self):
        """
		Update health bar
		"""
        self.bgHandle.setScale(1.0 - self.health / 100.0, 1, 1)
        self.bgHandle.setPos((self.health / 100.0), 0, 0)

        self.fgHandle.setScale(self.health / 100.0, 1, 1)
        self.fgHandle.setPos(-(1.0 - (self.health / 100.0)), 0, 0)

    def __handleCollisions(self, game, carrots, spikes):
        """
		If there is a collision with a carrot, increment health
		If there is a collision with a spike, kill the panda (NOOOO!!!)
		
		Args:
		    game (Game): A reference to the Game object
		    carrots (list): list of carrots
		    spikes (list): list of spikes
		"""
        for carrot in carrots:
            if not carrot.isActive:
                continue

            point = carrot.carrotHandle.getPos()
            dist = (point.getXy() -
                    self.pandaHandle.getPos().getXy()).lengthSquared()
            if dist < 10:
                self.__eatCarrot()
                carrot.goToHeaven(game, Panda.pandaList, spikes)
                break
        for spike in spikes:
            point = spike.spikeHandle.getPos()
            dist = (point.getXy() -
                    self.pandaHandle.getPos().getXy()).lengthSquared()
            if dist < 20:
                self.__die(game)
                break

    def __eatCarrot(self):
        """
		Increment score and life points
		"""
        self.carrotsEaten += 1
        self.health += 40.0
        if self.health > 100.0:
            self.health = 100.0

    def __setUpBrain(self, genome):
        """
		Set up PyBrain's neural network
		
		Args:
		    genome (G1DList): PyEvolve's individual container
		"""
        self.network = FeedForwardNetwork()

        inLayer = TanhLayer(14)
        hiddenLayer = TanhLayer(12)
        hiddenLayer2 = TanhLayer(6)
        outLayer = TanhLayer(2)

        self.network.addInputModule(inLayer)
        self.network.addModule(hiddenLayer)
        self.network.addModule(hiddenLayer2)
        self.network.addOutputModule(outLayer)

        in_to_hidden = FullConnection(inLayer, hiddenLayer)
        hidden_to_hidden2 = FullConnection(hiddenLayer, hiddenLayer2)
        hidden2_to_out = FullConnection(hiddenLayer2, outLayer)

        self.network.addConnection(in_to_hidden)
        self.network.addConnection(hidden_to_hidden2)
        self.network.addConnection(hidden2_to_out)

        self.network.sortModules()

        new_params = numpy.array(genome.genomeList)
        self.network._setParameters(new_params)

    @staticmethod
    def getBestPanda():
        """
		Returns the panda with the highest score
		
		Returns:
		    Panda: panda with the highest score
		"""
        best = Panda.pandaList[0]
        for panda in Panda.pandaList:
            if best.carrotsEaten < panda.carrotsEaten:
                best = panda
        return best

    @staticmethod
    def getAvgScore():
        """
		Returns the average population score
		
		Returns:
		    float: Average score
		"""
        avg = 0.0
        for panda in Panda.pandaList:
            avg += panda.carrotsEaten
        avg = avg / len(Panda.pandaList)
        return round(avg, 3)

    @staticmethod
    def clearPandas(game):
        """
		Remove all the pandas
		
		Args:
		    game (Game): A reference to the Game object
		"""
        game.taskMgr.remove('delete panda')

        for panda in Panda.pandaList[:]:
            if panda.isAlive:
                panda.__delete()
        del Panda.pandaList[:]
        Panda.pandaIds.clear()

    @staticmethod
    def getScoreById(pandaId):
        """
		Given the Id of a panda return its score
		
		Args:
		    pandaId (int): Id of panda
		
		Returns:
		    int: panda score
		"""
        score = Panda.pandaIds[pandaId].carrotsEaten
        if not Panda.pandaIds[pandaId].isDying:
            score += 3

        return score

    def drawDebugLines(self, game, carrots, spikes):
        """
		Debug info
		
		Args:
		    game (Game): A reference to the Game object
		    carrots (list): list of carrots
		    spikes (list): list of spikes
		"""
        for carrot in carrots:
            if not carrot.isActive:
                continue

            point = carrot.carrotHandle.getPos()

            linesegs = LineSegs("lines")
            linesegs.setColor(1, 1, 1, 1)

            linesegs.drawTo(point)
            linesegs.drawTo(self.pandaHandle.getPos())

            node = linesegs.create(False)
            game.render.attachNewNode(node)

        for spike in spikes:
            point = spike.spikeHandle.getPos(self.pandaHandle)

            linesegs = LineSegs("lines")
            linesegs.setColor(1, 1, 1, 1)

            linesegs.drawTo(point)
            linesegs.drawTo(self.pandaHandle.getPos())

            node = linesegs.create(False)
            game.render.attachNewNode(node)
Exemple #15
0
class NeuralNetwork:

    def __init__(self, genes=None):

        self.net = FeedForwardNetwork()
        self.inLayer = TanhLayer(16)
        self.hiddenLayer = TanhLayer(20)
        self.hiddenLayer2 = TanhLayer(20)
        self.outLayer = SoftmaxLayer(4)

        self.net.addInputModule(self.inLayer)
        self.net.addModule(self.hiddenLayer)
        self.net.addModule(self.hiddenLayer2)
        self.net.addOutputModule(self.outLayer)

        self.in_to_hidden = FullConnection(self.inLayer, self.hiddenLayer)
        self.hidden1_to_hidden2 = FullConnection(self.hiddenLayer, self.hiddenLayer2)
        self.hidden2_to_out = FullConnection(self.hiddenLayer2, self.outLayer)

        self.net.addConnection(self.in_to_hidden)
        self.net.addConnection(self.hidden1_to_hidden2)
        self.net.addConnection(self.hidden2_to_out)

        self.net.sortModules()

        # Set the params to the provided params
        if genes is not None:
            self.net._setParameters(genes)

    def makemove(self, board):
        inputs = []
        inputs.append(np.reshape(board, 16).tolist())
        # print(inputs)

        for i in range(len(inputs[0])):

            if inputs[0][i] != 0:
                inputs[0][i] = math.log(inputs[0][i], 2)

        largest = max(inputs[0])

        for i in range(len(inputs[0])):
            inputs[0][i] /= largest
            inputs[0][i] = round(inputs[0][i], 2)

        # print(inputs)
        #print(inputs[0])
        #print(self.net.params)

        output = self.net.activate(inputs[0])
        largest = max(output)
        index = [i for i, j in enumerate(output) if j == largest]
        return index[0] + 1

    def printParams(self):
        print(self.net.params)

    def getParams(self):
        return self.net.params

    def setParams(self, genes):
        self.net._setParameters(genes)