Exemplo n.º 1
0
 def __init__(self, M, c, *args, **kwargs):
     """
     Initialize an MDN with M kernels and output dimension c.
     
             """
     FeedForwardNetwork.__init__(self, *args, **kwargs)
     self.M = M
     self.c = c
     # we have to include the additional arguments in argdict in order to
     # have XML serialization work properly
     self.argdict['c'] = c
     self.argdict['M'] = M
Exemplo n.º 2
0
    def _initializeNetwork(self):
        """ build the combined network consisting of the module and
            the explorer and initializing the log likelihoods dataset.
        """
        self.network = FeedForwardNetwork()
        self.network.addInputModule(self._module)
        self.network.addOutputModule(self._explorer)
        self.network.addConnection(IdentityConnection(self._module, self._explorer))
        self.network.sortModules()

        # initialize gradient descender
        self.gd.init(self.network.params)

        # initialize loglh dataset
        self.loglh = LoglhDataSet(self.network.paramdim)
Exemplo n.º 3
0
 def _initializeNetwork(self):
     """ build the combined network consisting of the module and
         the explorer and initializing the log likelihoods dataset.
     """
     self.network = FeedForwardNetwork()
     self.network.addInputModule(self._module)
     self.network.addOutputModule(self._explorer)
     self.network.addConnection(IdentityConnection(self._module, self._explorer))
     self.network.sortModules()
     
     # initialize gradient descender
     self.gd.init(self.network.params) 
     
     # initialize loglh dataset
     self.loglh = LoglhDataSet(self.network.paramdim)    
    def RunSimulator(self):
        #pprint(self.parameters)

        self.myDataset = ClassificationDataSet(int(self.parameters['INPUT']), int(self.parameters['OUTPUT']))
        #Load float format iris dataset
        self.readMyData(self.filename)
        #create baseline network
        self.network = FeedForwardNetwork()
        #Selection of hidden layers (1 or 2)
        if int(self.parameters['HIDDEN1']) == 0 :
            #Build Architecture (One hidden layer)
            inLayer = LinearLayer(int(self.parameters['INPUT']))
            if self.parameters['HIDDEN_S/T'] == 'T':
                hiddenLayer0 = TanhLayer(int(self.parameters['HIDDEN0']))
            else:
                hiddenLayer0 = SoftmaxLayer(int(self.parameters['HIDDEN0']))
            if self.parameters['OUTPUT_S/L'] == 'S': 
                outLayer = SoftmaxLayer(int(self.parameters['OUTPUT']))
            else:
                outLayer = LinearLayer(int(self.parameters['OUTPUT']))
            self.network.addInputModule(inLayer)
            self.network.addModule(hiddenLayer0)
            self.network.addOutputModule(outLayer)
 
            #Make connections
            in_to_hidden = FullConnection(inLayer, hiddenLayer0)
            hidden_to_out = FullConnection(hiddenLayer0, outLayer)
            self.network.addConnection(in_to_hidden)
            self.network.addConnection(hidden_to_out)
            if self.parameters['BIAS'] == 'True':
                bias = BiasUnit('bias')
                self.network.addModule(bias)
                bias_to_hidden0 = FullConnection(bias, hiddenLayer0)
                bias_to_out = FullConnection(bias, outLayer)            
                self.network.addConnection(bias_to_hidden0)
                self.network.addConnection(bias_to_out)  
        elif int(self.parameters['HIDDEN0']) == 0 :
            print "Cannot delete layer 0"
            sys.exit()
        else:   #Two hidden layers
            #Build Architecture
            inLayer = LinearLayer(int(self.parameters['INPUT']))
            if self.parameters['HIDDEN_S/T'] == 'T':
                hiddenLayer0 = TanhLayer(int(self.parameters['HIDDEN0']))
                hiddenLayer1 = TanhLayer(int(self.parameters['HIDDEN1']))
            else:
                hiddenLayer0 = SoftmaxLayer(int(self.parameters['HIDDEN0']))
                hiddenLayer1 = SoftmaxLayer(int(self.parameters['HIDDEN1']))
            if self.parameters['OUTPUT_S/L'] == 'S': 
                outLayer = SoftmaxLayer(int(self.parameters['OUTPUT']))
            else:
                outLayer = LinearLayer(int(self.parameters['OUTPUT']))
            self.network.addInputModule(inLayer)
            self.network.addModule(hiddenLayer0)
            self.network.addModule(hiddenLayer1)
            self.network.addOutputModule(outLayer)
            
            #Make connections
            in_to_hidden = FullConnection(inLayer, hiddenLayer0)
            hidden_to_hidden = FullConnection(hiddenLayer0, hiddenLayer1)
            hidden_to_out = FullConnection(hiddenLayer1, outLayer)
            self.network.addConnection(in_to_hidden)
            self.network.addConnection(hidden_to_hidden)
            self.network.addConnection(hidden_to_out)
            if self.parameters['BIAS'] == 'True':
                bias = BiasUnit('bias')
                self.network.addModule(bias)
                bias_to_hidden0 = FullConnection(bias, hiddenLayer0)
                bias_to_hidden1 = FullConnection(bias, hiddenLayer1)
                bias_to_out = FullConnection(bias, outLayer)            
                self.network.addConnection(bias_to_hidden0)
                self.network.addConnection(bias_to_hidden1)
                self.network.addConnection(bias_to_out)  

        # topologically sort the units in network
        self.network.sortModules()

        # split the data randomly into 90% training, 10% test = 0.1
        testData, trainData = self.myDataset.splitWithProportion(float(self.parameters['SPLIT_DATA']))

        #create the trainer environment for backprop and  train network
        trainer = BackpropTrainer(self.network, dataset = trainData, learningrate = \
        float(self.parameters['LEARNING_RATE']), momentum=float(self.parameters['MOMENTUM']), \
        weightdecay=float(self.parameters['WEIGHT_DECAY']))

        #Data workspace
        TrainingPoints = []
        TestPoints = []
        xAxis = []
        #Run for specified number of Epochs
        for i in range(int(self.parameters['EPOCHS'])):
            trainer.trainEpochs(1)
            trnresult = self.SumSquareError(self.network.activateOnDataset(dataset=trainData), trainData['target'])
            tstresult = self.SumSquareError(self.network.activateOnDataset(dataset=testData), testData['target'])
            #Print Current Errors (comment out when not needed)
            #print "epoch: %4d" % trainer.totalepochs, \
            #" train error: %5.2f" % trnresult, \
            #" test error: %5.2f" % tstresult
            #Build Lists for plotting 
            TrainingPoints.append(trnresult)
            TestPoints.append(tstresult)
            xAxis.append(i)

        #Output Results
        return self.AnalyzeOutput(testData)
class PolicyGradientLearner(DirectSearchLearner, DataSetLearner,
                            ExploringLearner):
    """ PolicyGradientLearner is a super class for all continuous direct search
        algorithms that use the log likelihood of the executed action to update
        the weights. Subclasses are ENAC, GPOMDP, or REINFORCE.
    """

    _module = None

    def __init__(self):
        # gradient descender
        self.gd = GradientDescent()

        # create default explorer
        self._explorer = None

        # loglh dataset
        self.loglh = None

        # network to tie module and explorer together
        self.network = None

    def _setLearningRate(self, alpha):
        """ pass the alpha value through to the gradient descent object """
        self.gd.alpha = alpha

    def _getLearningRate(self):
        return self.gd.alpha

    learningRate = property(_getLearningRate, _setLearningRate)

    def _setModule(self, moduleParam):
        """ initialize gradient descender with module parameters and
            the loglh dataset with the outdim of the module. """
        self._module = moduleParam

        # initialize explorer
        self._explorer = NormalExplorer(moduleParam.outdim)

        # build network
        self._initializeNetwork()

    def _getModule(self):
        return self._module

    module = property(_getModule, _setModule)

    def _setExplorer(self, explorer):
        """ assign non-standard explorer to the policy gradient learner.
            requires the module to be set beforehand.
        """
        assert self._module

        self._explorer = explorer

        # build network
        self._initializeNetwork()

    def _getExplorer(self):
        return self._explorer

    explorer = property(_getExplorer, _setExplorer)

    def _initializeNetwork(self):
        """ build the combined network consisting of the module and
            the explorer and initializing the log likelihoods dataset.
        """
        self.network = FeedForwardNetwork()
        self.network.addInputModule(self._module)
        self.network.addOutputModule(self._explorer)
        self.network.addConnection(
            IdentityConnection(self._module, self._explorer))
        self.network.sortModules()

        # initialize gradient descender
        self.gd.init(self.network.params)

        # initialize loglh dataset
        self.loglh = LoglhDataSet(self.network.paramdim)

    def learn(self):
        """ calls the gradient calculation function and executes a step in direction
            of the gradient, scaled with a small learning rate alpha. """
        if self.dataset is None:
            raise Exception("Dataset must be not None!")

        if self.module is None:
            raise Exception("Module must be not None!")

        # calculate the gradient with the specific function from subclass
        gradient = self.calculateGradient()

        # scale gradient if it has too large values
        if max(gradient) > 1000:
            gradient = gradient / max(gradient) * 1000

        # update the parameters of the module
        p = self.gd(gradient.flatten())
        self.network._setParameters(p)
        self.network.reset()

    def explore(self, state, action):
        # forward pass of exploration
        explorative = ExploringLearner.explore(self, state, action)

        # backward pass through network and store derivs
        self.network.backward()
        self.loglh.appendLinked(self.network.derivs.copy())

        return explorative

    def reset(self):
        self.loglh.clear()

    def calculateGradient(self):
        abstractMethod()
    def RunSimulator(self):
        self.myDataset = ClassificationDataSet(int(self.parameters["INPUT"]), int(self.parameters["OUTPUT"]))
        # Load float format iris dataset
        self.readMyData(filename)
        # create baseline network
        self.network = FeedForwardNetwork()
        # Selection of hidden layers (1 or 2)
        if int(self.parameters["HIDDEN1"]) == 0:
            # Build Architecture (One hidden layer)
            inLayer = LinearLayer(int(self.parameters["INPUT"]))
            if self.parameters["HIDDEN_S/T"] == "T":
                hiddenLayer0 = TanhLayer(int(self.parameters["HIDDEN0"]))
            else:
                hiddenLayer0 = SoftmaxLayer(int(self.parameters["HIDDEN0"]))
            if self.parameters["OUTPUT_S/L"] == "S":
                outLayer = SoftmaxLayer(int(self.parameters["OUTPUT"]))
            else:
                outLayer = LinearLayer(int(self.parameters["OUTPUT"]))
            self.network.addInputModule(inLayer)
            self.network.addModule(hiddenLayer0)
            self.network.addOutputModule(outLayer)

            # Make connections
            in_to_hidden = FullConnection(inLayer, hiddenLayer0)
            hidden_to_out = FullConnection(hiddenLayer0, outLayer)
            self.network.addConnection(in_to_hidden)
            self.network.addConnection(hidden_to_out)
            if self.parameters["BIAS"] == "True":
                bias = BiasUnit("bias")
                self.network.addModule(bias)
                bias_to_hidden0 = FullConnection(bias, hiddenLayer0)
                bias_to_out = FullConnection(bias, outLayer)
                self.network.addConnection(bias_to_hidden0)
                self.network.addConnection(bias_to_out)
        elif int(self.parameters["HIDDEN0"]) == 0:
            print "Cannot delete layer 0"
            sys.exit()
        else:  # Two hidden layers
            # Build Architecture
            inLayer = LinearLayer(int(self.parameters["INPUT"]))
            if self.parameters["HIDDEN_S/T"] == "T":
                hiddenLayer0 = TanhLayer(int(self.parameters["HIDDEN0"]))
                hiddenLayer1 = TanhLayer(int(self.parameters["HIDDEN1"]))
            else:
                hiddenLayer0 = SoftmaxLayer(int(self.parameters["HIDDEN0"]))
                hiddenLayer1 = SoftmaxLayer(int(self.parameters["HIDDEN1"]))
            if self.parameters["OUTPUT_S/L"] == "S":
                outLayer = SoftmaxLayer(int(self.parameters["OUTPUT"]))
            else:
                outLayer = LinearLayer(int(self.parameters["OUTPUT"]))
            self.network.addInputModule(inLayer)
            self.network.addModule(hiddenLayer0)
            self.network.addModule(hiddenLayer1)
            self.network.addOutputModule(outLayer)

            # Make connections
            in_to_hidden = FullConnection(inLayer, hiddenLayer0)
            hidden_to_hidden = FullConnection(hiddenLayer0, hiddenLayer1)
            hidden_to_out = FullConnection(hiddenLayer1, outLayer)
            self.network.addConnection(in_to_hidden)
            self.network.addConnection(hidden_to_hidden)
            self.network.addConnection(hidden_to_out)
            if self.parameters["BIAS"] == "True":
                bias = BiasUnit("bias")
                self.network.addModule(bias)
                bias_to_hidden0 = FullConnection(bias, hiddenLayer0)
                bias_to_hidden1 = FullConnection(bias, hiddenLayer1)
                bias_to_out = FullConnection(bias, outLayer)
                self.network.addConnection(bias_to_hidden0)
                self.network.addConnection(bias_to_hidden1)
                self.network.addConnection(bias_to_out)

        # topologically sort the units in network
        self.network.sortModules()

        # split the data randomly into 90% training, 10% test = 0.1
        testData, trainData = self.myDataset.splitWithProportion(float(self.parameters["SPLIT_DATA"]))

        # create the trainer environment for backprop and  train network
        trainer = BackpropTrainer(
            self.network,
            dataset=trainData,
            learningrate=float(self.parameters["LEARNING_RATE"]),
            momentum=float(self.parameters["MOMENTUM"]),
            weightdecay=float(self.parameters["WEIGHT_DECAY"]),
        )

        # Data workspace
        TrainingPoints = []
        TestPoints = []
        xAxis = []
        # Run for specified number of Epochs
        for i in range(int(self.parameters["EPOCHS"])):
            trainer.trainEpochs(1)
            trnresult = self.SumSquareError(self.network.activateOnDataset(dataset=trainData), trainData["target"])
            tstresult = self.SumSquareError(self.network.activateOnDataset(dataset=testData), testData["target"])
            # Print Current Errors (comment out when not needed)
            print "epoch: %4d" % trainer.totalepochs, " train error: %5.2f" % trnresult, " test error: %5.2f" % tstresult
            # Build Lists for plotting
            TrainingPoints.append(trnresult)
            TestPoints.append(tstresult)
            xAxis.append(i)

        # Output Results
        self.AnalyzeOutput(testData)
        pylab.plot(xAxis, TrainingPoints, "b-")
        pylab.plot(xAxis, TestPoints, "r-")
        pylab.xlabel("EPOCHS")
        pylab.ylabel("Sum Squared Error")
        pylab.title("Plot of Training Errors")
        pylab.show()
class BrainApp(Process):

    #Parameters (attributes) and values
    parameters = {'INPUT': '4',     #No of input dimensions
                  'OUTPUT': '2',    #No of Output Class
                  'HIDDEN0': '9',   #No of Hidden Neurons 1st layer
                  'HIDDEN1': '9',   #Second Layer
                  'HIDDEN_S/T': 'T', #Hidden layer activations (S)oftMax or (T)anh
                  'OUTPUT_S/L': 'S', #Output layer activation (S)oftMax or (L)inear
                  'LEARNING_RATE': '0.15',
                  'MOMENTUM': '0.0',
                  'BIAS': 'True',
                  'EPOCHS': '25',  #No of training cycles used
                  'WEIGHT_DECAY': '0.0',
                  'SPLIT_DATA': '0.1',
                  'UPPER_LIMIT': '0.6',  #Higher than this, taken as 1.0
                  'LOWER_LIMIT': '0.4' } #Less than this, taken as zero

    def __init__(self, output, filename, epoch=120, learningrate=0.15, hidden=9):
      self.filename = filename

      self.parameters['EPOCHS'] = epoch
      self.parameters['LEARNING_RATE'] = learningrate
      self.parameters['HIDDEN0'] = hidden
      self.parameters['HIDDEN1'] = hidden
      self.output = output
      super(BrainApp, self).__init__()

    def run(self):
      self.output.append(self.RunSimulator());

    #Configure input file (text) into input list and output list
    def readMyData(self, filename):
       print("FILE is %s" % filename)
       file = open(filename, 'r')
       for line in file.readlines():
           L = line.split(" ")
           inSample = []
           outSample = []
           for i in range(int(self.parameters['INPUT'])):
              inSample.append(float(L[i]))
           for j in range(int(self.parameters['OUTPUT'])):
              outSample.append(float(L[j+int(self.parameters['INPUT'])]))
          
           self.myDataset.addSample(inSample,outSample)

    #current Error Measure - You could add your own
    def SumSquareError(self, Actual, Desired):
       error = 0.
       for i in range(len(Desired)):
          for j in range(len(Desired[i])):
             error = error + ((Actual[i])[j] - (Desired[i])[j])*((Actual[i])[j] - (Desired[i])[j])
       return error

    def RunSimulator(self):
        #pprint(self.parameters)

        self.myDataset = ClassificationDataSet(int(self.parameters['INPUT']), int(self.parameters['OUTPUT']))
        #Load float format iris dataset
        self.readMyData(self.filename)
        #create baseline network
        self.network = FeedForwardNetwork()
        #Selection of hidden layers (1 or 2)
        if int(self.parameters['HIDDEN1']) == 0 :
            #Build Architecture (One hidden layer)
            inLayer = LinearLayer(int(self.parameters['INPUT']))
            if self.parameters['HIDDEN_S/T'] == 'T':
                hiddenLayer0 = TanhLayer(int(self.parameters['HIDDEN0']))
            else:
                hiddenLayer0 = SoftmaxLayer(int(self.parameters['HIDDEN0']))
            if self.parameters['OUTPUT_S/L'] == 'S': 
                outLayer = SoftmaxLayer(int(self.parameters['OUTPUT']))
            else:
                outLayer = LinearLayer(int(self.parameters['OUTPUT']))
            self.network.addInputModule(inLayer)
            self.network.addModule(hiddenLayer0)
            self.network.addOutputModule(outLayer)
 
            #Make connections
            in_to_hidden = FullConnection(inLayer, hiddenLayer0)
            hidden_to_out = FullConnection(hiddenLayer0, outLayer)
            self.network.addConnection(in_to_hidden)
            self.network.addConnection(hidden_to_out)
            if self.parameters['BIAS'] == 'True':
                bias = BiasUnit('bias')
                self.network.addModule(bias)
                bias_to_hidden0 = FullConnection(bias, hiddenLayer0)
                bias_to_out = FullConnection(bias, outLayer)            
                self.network.addConnection(bias_to_hidden0)
                self.network.addConnection(bias_to_out)  
        elif int(self.parameters['HIDDEN0']) == 0 :
            print "Cannot delete layer 0"
            sys.exit()
        else:   #Two hidden layers
            #Build Architecture
            inLayer = LinearLayer(int(self.parameters['INPUT']))
            if self.parameters['HIDDEN_S/T'] == 'T':
                hiddenLayer0 = TanhLayer(int(self.parameters['HIDDEN0']))
                hiddenLayer1 = TanhLayer(int(self.parameters['HIDDEN1']))
            else:
                hiddenLayer0 = SoftmaxLayer(int(self.parameters['HIDDEN0']))
                hiddenLayer1 = SoftmaxLayer(int(self.parameters['HIDDEN1']))
            if self.parameters['OUTPUT_S/L'] == 'S': 
                outLayer = SoftmaxLayer(int(self.parameters['OUTPUT']))
            else:
                outLayer = LinearLayer(int(self.parameters['OUTPUT']))
            self.network.addInputModule(inLayer)
            self.network.addModule(hiddenLayer0)
            self.network.addModule(hiddenLayer1)
            self.network.addOutputModule(outLayer)
            
            #Make connections
            in_to_hidden = FullConnection(inLayer, hiddenLayer0)
            hidden_to_hidden = FullConnection(hiddenLayer0, hiddenLayer1)
            hidden_to_out = FullConnection(hiddenLayer1, outLayer)
            self.network.addConnection(in_to_hidden)
            self.network.addConnection(hidden_to_hidden)
            self.network.addConnection(hidden_to_out)
            if self.parameters['BIAS'] == 'True':
                bias = BiasUnit('bias')
                self.network.addModule(bias)
                bias_to_hidden0 = FullConnection(bias, hiddenLayer0)
                bias_to_hidden1 = FullConnection(bias, hiddenLayer1)
                bias_to_out = FullConnection(bias, outLayer)            
                self.network.addConnection(bias_to_hidden0)
                self.network.addConnection(bias_to_hidden1)
                self.network.addConnection(bias_to_out)  

        # topologically sort the units in network
        self.network.sortModules()

        # split the data randomly into 90% training, 10% test = 0.1
        testData, trainData = self.myDataset.splitWithProportion(float(self.parameters['SPLIT_DATA']))

        #create the trainer environment for backprop and  train network
        trainer = BackpropTrainer(self.network, dataset = trainData, learningrate = \
        float(self.parameters['LEARNING_RATE']), momentum=float(self.parameters['MOMENTUM']), \
        weightdecay=float(self.parameters['WEIGHT_DECAY']))

        #Data workspace
        TrainingPoints = []
        TestPoints = []
        xAxis = []
        #Run for specified number of Epochs
        for i in range(int(self.parameters['EPOCHS'])):
            trainer.trainEpochs(1)
            trnresult = self.SumSquareError(self.network.activateOnDataset(dataset=trainData), trainData['target'])
            tstresult = self.SumSquareError(self.network.activateOnDataset(dataset=testData), testData['target'])
            #Print Current Errors (comment out when not needed)
            #print "epoch: %4d" % trainer.totalepochs, \
            #" train error: %5.2f" % trnresult, \
            #" test error: %5.2f" % tstresult
            #Build Lists for plotting 
            TrainingPoints.append(trnresult)
            TestPoints.append(tstresult)
            xAxis.append(i)

        #Output Results
        return self.AnalyzeOutput(testData)

    #Analyse the Test Data Set
    #Save the actual and desired results for tes data in 'result.dat'
    #Print the Correct, Wrong and Unknown Statistics
    def AnalyzeOutput(self, testData):
        #Compare actual test results (writes to data file 'result.dat')
        total = { 'good' : 0,
                  'bad' : 0,
                  'unknown' : 0 }        
        actualTestOutput = self.network.activateOnDataset(dataset=testData)
        desiredTestOutput = testData['target']
        resultFile = open(result, 'w')
        resultFile.write( "   Test Outputs\n")
        resultFile.write(" Actual    Desired")
        for m in range(len(actualTestOutput)):        #say, 15 for iris data (10%)
            resultFile.write('\n')
            sample = { 'good': 0,
                       'bad' : 0,
                       'unknown' : 0 }
            for n in range(int(self.parameters['OUTPUT'])):   #say, 3 classes as in iris data
               actual = float(actualTestOutput[m][n])   #class by class
               resultFile.write( "  %2.1f" % actual,)
               resultFile.write( '\t',)
               desired = float(desiredTestOutput[m][n])
               resultFile.write( "    %2.1f\n" % desired )
              
               #for classifier, calculate the errors
               upper = float(self.parameters['UPPER_LIMIT'])
               lower = float(self.parameters['LOWER_LIMIT'])

               #Check for silly values
               if upper >= 1.0 or lower >= 1.0 or lower > upper :
                   print "Illegal setting of upper or lower decision limit"
                   sys.exit()
               #My logic to built result determined by the values of upper and lower limits
               if desired > upper and actual > upper :
                   sample['good'] += 1
               elif desired < lower and actual < lower :
                   sample['good'] += 1
               elif desired > upper and actual < lower :    #corrected 25th April
                   sample['bad'] += 1
               elif desired < lower and actual > upper :    #corrected 25th April
                   sample['bad'] += 1
               else :
                   sample['unknown'] += 1
            if sample['good'] == int(self.parameters['OUTPUT']) :
                total['good'] += 1
            elif sample['bad'] > 0 :
                total['bad'] += 1
            elif sample['bad'] == 0 and sample['unknown'] > 0 :
                total['unknown'] += 1

        total_ret = {}

        #Print Test Result Analysis to Screen
        print  #New line
        percentage = 100.0*float(total['good'])/float(len(actualTestOutput))
        total_ret['correct_percentage'] = percentage
        print " Correct\t= %f" % percentage
        percentage = 100.0*float(total['bad'])/float(len(actualTestOutput))
        total_ret['bad_percentage'] = percentage
        print " Wrong\t\t= %f" % percentage
        percentage = 100.0*float(total['unknown'])/float(len(actualTestOutput))
        total_ret['unknown_percentage'] = percentage
        print " Unknown\t= %f" % percentage
        resultFile.close()

        total_ret['epoch'] = self.parameters['EPOCHS']
        total_ret['leanringrate'] = self.parameters['LEARNING_RATE']
        total_ret['hidden'] = self.parameters['HIDDEN0']

        return total_ret
Exemplo n.º 8
0
def net_shared_multi(h1dim=8, h2dim=4, h1multi=2, h2multi=4, k1=5, k2=10, bias=True):
    net = FeedForwardNetwork()
    # make modules
    inp=LinearLayer(28*28,name='input')
    h1=[SigmoidLayer(h1dim**2,name='hidden1st'+str(i)) for i in range(h1multi)]
    h2=[SigmoidLayer(h2dim**2,name='hidden2nd'+str(j)) for j in range(h2multi)]
    outp=SoftmaxLayer(10,name='output')
    net.addInputModule(inp)
    for i in range(h1multi):
        net.addModule(h1[i])
    for j in range(h2multi):
        net.addModule(h2[j])
    net.addOutputModule(outp)
    if bias:
        net.addModule(BiasUnit(name='bias'))
        net.addConnection(FullConnection(net['bias'],outp))
    # make connections
    for i in range(h1multi):
        net = shared_mesh(net, inlayer=inp, outlayer=h1[i], k=k1, mc_name='mother1st'+str(i), bias=bias)
        for j in range(h2multi):
            net = shared_mesh(net, inlayer=h1[i], outlayer=h2[j], k=k2, mc_name='mother2nd'+str(i)+'-'+str(j), bias=bias)
    for j in range(h2multi):
        net.addConnection(FullConnection(h2[j], outp))
    net.sortModules()
    return net
    def RunSimulator(self):
        self.myDataset = ClassificationDataSet(int(self.parameters['INPUT']), int(self.parameters['OUTPUT']))
        #Load float format iris dataset
        self.readMyData(filename)
        #create baseline network
        self.network = FeedForwardNetwork()
        #Selection of hidden layers (1 or 2)
        if int(self.parameters['HIDDEN1']) == 0 :
            #Build Architecture (One hidden layer)
            inLayer = LinearLayer(int(self.parameters['INPUT']))
            hiddenLayer0 = TanhLayer(int(self.parameters['HIDDEN0'])) #Sigmoid to Tanh 25Apr
            outLayer = SoftmaxLayer(int(self.parameters['OUTPUT']))
            self.network.addInputModule(inLayer)
            self.network.addModule(hiddenLayer0)
            self.network.addOutputModule(outLayer)
            #Make connections
            in_to_hidden = FullConnection(inLayer, hiddenLayer0)
            hidden_to_out = FullConnection(hiddenLayer0, outLayer)
            self.network.addConnection(in_to_hidden)
            self.network.addConnection(hidden_to_out)
        elif int(self.parameters['HIDDEN0']) == 0 :
            print "Cannot delete layer 0"
            sys.exit()
        else:   #Two hidden layers
            #Build Architecture
            inLayer = LinearLayer(int(self.parameters['INPUT']))
            hiddenLayer0 = TanhLayer(int(self.parameters['HIDDEN0']))  #Tanh
            hiddenLayer1 = TanhLayer(int(self.parameters['HIDDEN1']))  #Tanh
            outLayer = SoftmaxLayer(int(self.parameters['OUTPUT']))
            self.network.addInputModule(inLayer)
            self.network.addModule(hiddenLayer0)
            self.network.addModule(hiddenLayer1)
            self.network.addOutputModule(outLayer)
            #Make connections
            in_to_hidden = FullConnection(inLayer, hiddenLayer0)
            hidden_to_hidden = FullConnection(hiddenLayer0, hiddenLayer1)
            hidden_to_out = FullConnection(hiddenLayer1, outLayer)
            self.network.addConnection(in_to_hidden)
            self.network.addConnection(hidden_to_hidden)
            self.network.addConnection(hidden_to_out)

        #initialize dataset
        self.network.sortModules()
        # split the data randomly into 90% training, 10% test = 0.1
        testData, trainData = self.myDataset.splitWithProportion(float(self.parameters['SPLIT_DATA']))

        #create the trainer environment for backprop and  train network
        trainer = BackpropTrainer(self.network, dataset = trainData, learningrate = \
        float(self.parameters['LEARNING_RATE']), momentum=float(self.parameters['MOMENTUM']), \
        weightdecay=float(self.parameters['WEIGHT_DECAY']))

        #Data workspace
        TrainingPoints = []
        TestPoints = []
        xAxis = []
        #Run for specified number of Epochs
        for i in range(int(self.parameters['EPOCHS'])):
            trainer.trainEpochs(1)
            trnresult = self.SumSquareError(self.network.activateOnDataset(dataset=trainData), trainData['target'])
            tstresult = self.SumSquareError(self.network.activateOnDataset(dataset=testData), testData['target'])
            #Print Current Errors (comment out when not needed)
            print "epoch: %4d" % trainer.totalepochs, \
            " train error: %5.2f" % trnresult, \
            " test error: %5.2f" % tstresult
            #Build Lists for plotting 
            TrainingPoints.append(trnresult)
            TestPoints.append(tstresult)
            xAxis.append(i)

        #Output Results
        self.AnalyzeOutput(testData)
        pylab.plot(xAxis, TrainingPoints, 'b-')
        pylab.plot(xAxis,TestPoints, 'r-')
        pylab.xlabel('EPOCHS')
        pylab.ylabel('Sum Squared Error')
        pylab.title('Plot of Training Errors')
        pylab.show()
Exemplo n.º 10
0
    def neuralnetworktrain(self):
        dataset = self.getdata()

        # Constructing a multiple output neural network.
        # Other neural network architectures will also be experimented,
        # like using different single output neural networks.
        net = FeedForwardNetwork()
        inp = LinearLayer(9)
        h1 = SigmoidLayer(20)
        h2 = TanhLayer(10)
        outp = LinearLayer(3)

        # Adding the modules to the architecture
        net.addOutputModule(outp)
        net.addInputModule(inp)
        net.addModule(h1)
        net.addModule(h2)

        # Creating the connections
        net.addConnection(FullConnection(inp, h1))
        net.addConnection(FullConnection(h1, h2))
        net.addConnection(FullConnection(h2, outp))
        net.sortModules()

        # Training the neural network using Backpropagation
        t = BackpropTrainer(net, learningrate=0.01, momentum=0.5, verbose=True)
        t.trainOnDataset(dataset, 5)
        t.testOnData(verbose=False)

        # Saving the trained neural network information to file
        self.writetrainedinfo(net)
Exemplo n.º 11
0
    def neuralnetworktrain(self):
        dataset = self.getdata()

        # Constructing a multiple output neural network.
        # Other neural network architectures will also be experimented,
        # like using different single output neural networks.
        net = FeedForwardNetwork()
        inp = LinearLayer(9)
        h1 = SigmoidLayer(20)
        h2 = TanhLayer(10)
        outp = LinearLayer(3)

        # Adding the modules to the architecture
        net.addOutputModule(outp)
        net.addInputModule(inp)
        net.addModule(h1)
        net.addModule(h2)

        # Creating the connections
        net.addConnection(FullConnection(inp, h1))
        net.addConnection(FullConnection(h1, h2))
        net.addConnection(FullConnection(h2, outp))
        net.sortModules()

        # Training the neural network using Backpropagation
        t = BackpropTrainer(net, learningrate=0.01, momentum=0.5, verbose=True)
        t.trainOnDataset(dataset, 5)
        t.testOnData(verbose=False)

        # Saving the trained neural network information to file
        self.writetrainedinfo(net)
Exemplo n.º 12
0
    for x in range(k):
        mc[x] = MotherConnection(k,name=mc_name+str(x))      # reference to the connections for each ROW in the patch, becouse module slicing is easy for consequent indeces in pybrain
    for i in range(outk):      # row index
        for j in range(outk):  # column index
            out_neuron = i*outk+j
            outSlice[i][j] = ModuleSlice(outlayer,inSliceFrom=out_neuron,inSliceTo=out_neuron+1,outSliceFrom=out_neuron,outSliceTo=out_neuron+1)
            for x in range(k):
                in_neuron = (grid[i]+x)*ink+grid[j]     # first neuron in the specific row of wanted patch
                inSlice[x][i][j]=ModuleSlice(inlayer,inSliceFrom=in_neuron,inSliceTo=in_neuron+k,outSliceFrom=in_neuron,outSliceTo=in_neuron+k)
                net.addConnection(SharedFullConnection(mc[x],inSlice[x][i][j],outSlice[i][j]))
    if bias:
        net.addConnection(FullConnection(net['bias'],outlayer))
    return net

##############################
#build a small test network
if False:
    from pybrain.structure.networks import FeedForwardNetwork
    from pybrain.structure.modules import LinearLayer, SigmoidLayer, SoftmaxLayer
    net_test = FeedForwardNetwork()
    # make modules
    inp=LinearLayer(5*5,name='input')
    outp=SigmoidLayer(2*2,name='output')
    net_test.addInputModule(inp)
    net_test.addOutputModule(outp)
    net_test = shared_mesh(net_test, inp, outp, k=3, bias=False)
    net_test.sortModules()
    print net_test
    for i in net_test.connections.values()[0]:
        print ('%s (%d - %d) -> (%d - %d)' %(i.mother, i.inSliceFrom, i.inSliceTo -1 , i.outSliceFrom, i.outSliceTo-1))
Exemplo n.º 13
0
def createNetwork():
    # create network and layers
    net = FeedForwardNetwork()
    in_layer = LinearLayer(16)
    hid1_layer = SigmoidLayer(20)
    hid2_layer = SigmoidLayer(20)
    out_layer = SigmoidLayer(2)

    # add layers to network
    net.addInputModule(in_layer)
    net.addModule(hid1_layer)
    net.addModule(hid2_layer)
    net.addOutputModule(out_layer)

    # create connections between layers
    in_to_hid1 = FullConnection(in_layer, hid1_layer)
    hid1_to_hid2 = FullConnection(hid1_layer, hid2_layer)
    hid2_to_out = FullConnection(hid2_layer, out_layer)

    # add connections to network
    net.addConnection(in_to_hid1)
    net.addConnection(hid1_to_hid2)
    net.addConnection(hid2_to_out)

    # sort modules
    net.sortModules()

    return net
Exemplo n.º 14
0
def buildNetwork(InputLength=1,
                 HiddenLength=0,
                 OutputLength=1,
                 bias=True,
                 seed=None):

    network = FeedForwardNetwork()
    input_layer = LinearLayer(InputLength)
    if HiddenLength > 0:
        hidden_layer = SigmoidLayer(HiddenLength)
    output_layer = SigmoidLayer(OutputLength)

    network.addInputModule(input_layer)
    network.addOutputModule(output_layer)
    if HiddenLength > 0:
        network.addModule(hidden_layer)

    if HiddenLength > 0:
        network.addConnection(FullConnection(input_layer, hidden_layer))
        network.addConnection(FullConnection(hidden_layer, output_layer))
    else:
        network.addConnection(FullConnection(input_layer, output_layer))

    if bias:
        bias_node = BiasUnit()
        network.addModule(bias_node)
        network.addConnection(FullConnection(bias_node, input_layer))
        if HiddenLength > 0:
            network.addConnection(FullConnection(bias_node, hidden_layer))
        network.addConnection(FullConnection(bias_node, output_layer))

    network.sortModules()

    numpy.random.seed(seed)
    random.seed(seed)
    network.randomize()

    #print network.params

    return network
Exemplo n.º 15
0
    def __init__(self,
                 datatxt_app_id,
                 datatxt_app_key,
                 lang='it'
                 ):

        self.lang = lang

        #create network and modules
        self.net = FeedForwardNetwork()
        self.inp = LinearLayer(9, name='input')
        self.h1 = LinearLayer(4, name='h1')
        self.h2 = LinearLayer(4, name='h2')
        self.hrho = LinearLayer(1, name='hrho')
        self.hsig = SigmoidLayer(3, name='hsig')
        self.outp = LinearLayer(1, name='output')

        # add modules
        self.net.addOutputModule(self.outp)
        self.net.addInputModule(self.inp)
        self.net.addModule(self.h1)
        self.net.addModule(self.h2)
        self.net.addModule(self.hrho)
        self.net.addModule(self.hsig)

        # create connections
        self.net.addConnection(FullConnection(self.inp, self.h1, name='input->h1'))
        self.net.addConnection(FullConnection(self.inp, self.h2, name='input->h2'))

        # self.net.addConnection(FullConnection(self.inp, self.h1,
        #                                       name='input->h1',
        #                                       inSliceFrom=0,
        #                                       inSliceTo=4))
        # self.net.addConnection(FullConnection(self.inp, self.h2,
        #                                       name='input->h2',
        #                                       inSliceFrom=4,
        #                                       inSliceTo=8))
        self.net.addConnection(FullConnection(self.inp, self.hrho,
                                              name='input->hrho',
                                              inSliceFrom=8,
                                              inSliceTo=9))
        self.net.addConnection(FullConnection(self.h1, self.hsig))
        self.net.addConnection(FullConnection(self.h2, self.hsig))
        self.net.addConnection(FullConnection(self.hrho, self.hsig))
        self.net.addConnection(FullConnection(self.hsig, self.outp))

        # finish up
        self.net.sortModules()

        self.ds = SupervisedDataSet(9, 1)

        self.trainer = BackpropTrainer(self.net, self.ds,
                                       learningrate=self.TRAINER_LEARNINGRATE,
                                       lrdecay=self.TRAINER_LRDECAY)

        self.dq = DataTXTQuerist(app_id=datatxt_app_id,
                                 app_key=datatxt_app_key)

        self.dq.set_params(lang=lang,
                           rho=self.DATATXT_RHO,
                           dbpedia=self.DATATXT_DBPEDIA)
Exemplo n.º 16
0
class Nuts4Nuts(object):
    LOWER_THRESHOLD = 0.33
    UPPER_THRESHOLD = 0.66
    TRAINER_LEARNINGRATE = 0.02
    TRAINER_LRDECAY = 1.0
    DATATXT_RHO = 0.2
    DATATXT_DBPEDIA = True
    W_PARENT_TYPE = 1
    W_IS_PARENT = 1
    W_HAS_LOCALITY = 1
    BIAS = 0.1
    SCORE_WEIGHT_NN = 0.66
    SCORE_WEIGHT_TEMPLATES = 0.7
    SET_MATCH_THRESHOLD = 0.6

    def __init__(self,
                 datatxt_app_id,
                 datatxt_app_key,
                 lang='it'
                 ):

        self.lang = lang

        #create network and modules
        self.net = FeedForwardNetwork()
        self.inp = LinearLayer(9, name='input')
        self.h1 = LinearLayer(4, name='h1')
        self.h2 = LinearLayer(4, name='h2')
        self.hrho = LinearLayer(1, name='hrho')
        self.hsig = SigmoidLayer(3, name='hsig')
        self.outp = LinearLayer(1, name='output')

        # add modules
        self.net.addOutputModule(self.outp)
        self.net.addInputModule(self.inp)
        self.net.addModule(self.h1)
        self.net.addModule(self.h2)
        self.net.addModule(self.hrho)
        self.net.addModule(self.hsig)

        # create connections
        self.net.addConnection(FullConnection(self.inp, self.h1, name='input->h1'))
        self.net.addConnection(FullConnection(self.inp, self.h2, name='input->h2'))

        # self.net.addConnection(FullConnection(self.inp, self.h1,
        #                                       name='input->h1',
        #                                       inSliceFrom=0,
        #                                       inSliceTo=4))
        # self.net.addConnection(FullConnection(self.inp, self.h2,
        #                                       name='input->h2',
        #                                       inSliceFrom=4,
        #                                       inSliceTo=8))
        self.net.addConnection(FullConnection(self.inp, self.hrho,
                                              name='input->hrho',
                                              inSliceFrom=8,
                                              inSliceTo=9))
        self.net.addConnection(FullConnection(self.h1, self.hsig))
        self.net.addConnection(FullConnection(self.h2, self.hsig))
        self.net.addConnection(FullConnection(self.hrho, self.hsig))
        self.net.addConnection(FullConnection(self.hsig, self.outp))

        # finish up
        self.net.sortModules()

        self.ds = SupervisedDataSet(9, 1)

        self.trainer = BackpropTrainer(self.net, self.ds,
                                       learningrate=self.TRAINER_LEARNINGRATE,
                                       lrdecay=self.TRAINER_LRDECAY)

        self.dq = DataTXTQuerist(app_id=datatxt_app_id,
                                 app_key=datatxt_app_key)

        self.dq.set_params(lang=lang,
                           rho=self.DATATXT_RHO,
                           dbpedia=self.DATATXT_DBPEDIA)

    def _treat_sample(self, sample):
        newsample0 = sample[0:3]
        newsample1 = sample[4:7]
        rho0 = sample[3]
        rho1 = sample[7]

        wsample0 = (self.W_PARENT_TYPE*(newsample0[0]+self.BIAS),
                    self.W_IS_PARENT*(newsample0[1]+self.BIAS),
                    self.W_HAS_LOCALITY*(newsample0[2]+self.BIAS),
                    rho0)

        wsample1 = (self.W_PARENT_TYPE*(newsample1[0]+self.BIAS),
                    self.W_IS_PARENT*(newsample1[1]+self.BIAS),
                    self.W_HAS_LOCALITY*(newsample1[2]+self.BIAS),
                    rho1)

        return wsample0 + wsample1 + tuple([rho0-rho1])

    def add_sample(self, sample, target):
        sample = self._treat_sample(sample)
        self.ds.addSample(sample, target)

    def train(self, nsteps=None):
        if nsteps:
            for _ in range(0, nsteps):
                self.trainer.train()
        else:
            self.trainer.trainUntilConvergence()

    def activate_from_sample(self, sample):
        sample = self._treat_sample(sample)
        return self.net.activate(sample)

    def activate(self, candidate0, candidate1):
        feat0 = candidate0.features.dump_features()
        feat1 = candidate1.features.dump_features()
        return self.activate_from_sample(feat0+feat1)

    def _decide(self, nnres):
        result = None
        if nnres < self.LOWER_THRESHOLD:
            result = 0
        elif nnres > self.UPPER_THRESHOLD:
            result = 1

        return result

    def _dedup_candidates(self, candidates):
        names = [c.name for c in candidates]
        for name in names:
            dups = [c for c in enumerate(candidates) if c[1].name == name]
            if len(dups) > 1:
                max_feat = max([c[1].features.rho for c in dups])
                dedups = [c for c in dups if c[1].features.rho == max_feat][0]
                dups.reverse()
                for d in dups:
                    if d[0] != dedups[0]:
                        del candidates[d[0]]

        return candidates

    def _select_couples(self, candidates):
        logger.setLevel(logging.DEBUG)
        logger.debug('call _select_couples')

        winning_candidates = defaultdict(int)
        for c in combinations(candidates, 2):
            nnres = self.activate(c[0], c[1])
            result = self._decide(nnres)
            logger.debug('couple: (cand0: {cand0}, cand1: {cand1}), nnres: {nnres}'.format(
                         cand0=c[0],
                         cand1=c[1],
                         nnres=nnres))
            if result is not None:
                winning_candidates[c[result]] += 1

        logger.debug(winning_candidates)
        for cand in candidates:
            cand.score = winning_candidates[cand]/float(len(candidates))

        max_score = max(cand.score for cand in candidates)

        selected_places = [c for c in candidates if c.score >= max_score]

        return selected_places

    def _lau3_from_lau2(self, candidates):
        lau2 = [c for c in candidates if c.type == '/LAU2']
        lau3 = [c for c in candidates if c.type == '/LAU3']

        logger.debug(lau2)
        logger.debug(lau3)

        winning_lau3s = list()

        for cand in lau3:
            lau3_fathers = [father
                            for father in lau2
                            if father.name.lower() in [cf.lower() for cf in cand.fathers]
                            ]
            if len(lau3_fathers) == 1:
                winning_lau3s.append((cand, lau3_fathers[0]))

        # logger.debug(winning_lau3s)
        if len(winning_lau3s) == 1:
            winning_lau3s[0][0].score = 1.0
            return [winning_lau3s[0][0]]
        elif len(winning_lau3s) > 1:
            if len(frozenset(father for lau3, father in winning_lau3s)) == 1:
                winning_lau3s[0][1].score = 1.0
                return [winning_lau3s[0][1]]

        return winning_lau3s


    def from_candidates(self, candidates):
        logger.debug('candidates: %s' % candidates)
        logger.debug('len(candidates): %s' % len(candidates))

        candidates = self._dedup_candidates(candidates)

        logger.debug('(deduped) candidates: %s' % candidates)
        logger.debug('(deduped) len(candidates): %s' % len(candidates))

        if len(candidates) == 0:
            logger.debug('No candidates found')
            return candidates

        if len(candidates) == 1:
            candidates[0].score = 1.0
            return candidates

        winning_lau3s = self._lau3_from_lau2(candidates)
        if winning_lau3s:
            return winning_lau3s

        return self._select_couples(candidates)

    def find_municipality(self, page):
        ta = TemplateAnalyzer(page=page, lang=self.lang)
        candidates_from_templates = ta.analyze_templates()

        logger.debug('candidates from templates: {candidates}'.format(
                     candidates=candidates_from_templates))

        ag = AbstractGetter(page=page, lang=self.lang)
        self.abstract = ag.get_abstract()
        querytext = self.dq.query(self.abstract)

        pg = PlacesGetter(page=page,
                          queryres=querytext)


        candidates_for_nn = pg.get_candidates()

        logger.debug('candidates: {candidates}'.format(candidates=candidates_for_nn))

        common_candidates = set([c.name for c in candidates_from_templates]).intersection(
                                 set([c.name for c in candidates_for_nn])
                                 )

        logger.debug(common_candidates)
        if common_candidates:
            result = [c
                      for c in candidates_from_templates
                      for name in common_candidates
                      if c.name == name
                      ]

            if len(result) == 1:
                result[0].set_match()
                result[0].score = 1.0

            return result

        else:
            candidates_from_nn = self.from_candidates(candidates=candidates_for_nn)

            for c in candidates_from_nn:
                c.score = c.score*self.SCORE_WEIGHT_NN

            for c in candidates_from_templates:
                c.score = c.score*self.SCORE_WEIGHT_TEMPLATES

            candidates_from_templates = self._lau3_from_lau2(candidates_from_templates) or \
                                        candidates_from_templates

            logger.debug(candidates_from_templates)
            logger.debug(candidates_from_nn)
            if candidates_from_templates and candidates_from_nn:
                merge_candidates = candidates_from_nn + candidates_from_templates
                total_candidates = self._lau3_from_lau2(merge_candidates) or \
                    merge_candidates
                if len(total_candidates) == 1:
                    total_candidates[0].set_match()
                return total_candidates
            else:
                if candidates_from_templates:
                    if len(candidates_from_templates) == 1 and \
                       candidates_from_templates[0].score > self.SET_MATCH_THRESHOLD:
                       candidates_from_templates[0].set_match()
                    return candidates_from_templates

                else:
                    if len(candidates_from_nn) == 1 and \
                       candidates_from_nn[0].score > self.SET_MATCH_THRESHOLD:
                       candidates_from_nn[0].set_match()
                    return candidates_from_nn

    def show(self):
        for mod in self.net.modules:
            print "Module: {name} ({mod})".format(name=mod.name, mod=mod)
            if mod.paramdim > 0:
                print "* parameters:", mod.params
            for conn in self.net.connections[mod]:
                print conn
                try:
                    paramdim = len(conn.params)
                except:
                    paramdim = conn.paramdim
                for cc in range(paramdim):
                    print conn.whichBuffers(cc), conn.params[cc]

    def save(self, filename='nut4nutsNN.xml'):
        logger.info('Writing NN to file: {filename}'.format(filename=filename))
        NetworkWriter.writeToFile(self.net, filename)

    def load(self, filename='nut4nutsNN.xml'):
        logger.info('Loading NN from file: {filename}'.format(filename=filename))
        self.net = NetworkReader.readFrom(filename)
Exemplo n.º 17
0
class PolicyGradientLearner(DirectSearchLearner, DataSetLearner, ExploringLearner):
    """ PolicyGradientLearner is a super class for all continuous direct search
        algorithms that use the log likelihood of the executed action to update
        the weights. Subclasses are ENAC, GPOMDP, or REINFORCE.
    """
    
    _module = None
    
    def __init__(self):        
        # gradient descender
        self.gd = GradientDescent()
        
        # create default explorer
        self._explorer = None
        
        # loglh dataset
        self.loglh = None
        
        # network to tie module and explorer together
        self.network = None
        
    
    def _setLearningRate(self, alpha):
        """ pass the alpha value through to the gradient descent object """
        self.gd.alpha = alpha
    
    def _getLearningRate(self):
        return self.gd.alpha
    
    learningRate = property(_getLearningRate, _setLearningRate)
        
    def _setModule(self, module):
        """ initialize gradient descender with module parameters and
            the loglh dataset with the outdim of the module. """
        self._module = module
        
        # initialize explorer
        self._explorer = NormalExplorer(module.outdim)
        
        # build network
        self._initializeNetwork()
    
    def _getModule(self):
        return self._module
        
    module = property(_getModule, _setModule)
    
    def _setExplorer(self, explorer):
        """ assign non-standard explorer to the policy gradient learner.
            requires the module to be set beforehand.
        """
        assert self._module
        
        self._explorer = explorer
        
        # build network
        self._initializeNetwork()

    def _getExplorer(self):
        return self._explorer
    
    explorer = property(_getExplorer, _setExplorer)
        
    
    def _initializeNetwork(self):
        """ build the combined network consisting of the module and
            the explorer and initializing the log likelihoods dataset.
        """
        self.network = FeedForwardNetwork()
        self.network.addInputModule(self._module)
        self.network.addOutputModule(self._explorer)
        self.network.addConnection(IdentityConnection(self._module, self._explorer))
        self.network.sortModules()
        
        # initialize gradient descender
        self.gd.init(self.network.params) 
        
        # initialize loglh dataset
        self.loglh = LoglhDataSet(self.network.paramdim)    
        
        
    def learn(self):
        """ calls the gradient calculation function and executes a step in direction
            of the gradient, scaled with a small learning rate alpha. """
        assert self.dataset != None
        assert self.module != None
                
        # calculate the gradient with the specific function from subclass
        gradient = self.calculateGradient()

        # scale gradient if it has too large values
        if max(gradient) > 1000:
            gradient = gradient / max(gradient) * 1000
        
        # update the parameters of the module
        p = self.gd(gradient.flatten())
        self.network._setParameters(p)
        self.network.reset()
    
    def explore(self, state, action):
        # forward pass of exploration
        explorative = ExploringLearner.explore(self, state, action)
        
        # backward pass through network and store derivs
        self.network.backward()
        self.loglh.appendLinked(self.network.derivs.copy())
        
        return explorative
    
    def reset(self):
        self.loglh.clear() 
    
    def calculateGradient(self):
        abstractMethod()
Exemplo n.º 18
0
def net_shared(h1dim=8, bias=True):
    net = FeedForwardNetwork()
    # make modules
    inp=LinearLayer(28*28,name='input')
    h1=SigmoidLayer(h1dim**2,name='hidden')
    outp=SoftmaxLayer(10,name='output')
    net.addInputModule(inp)
    net.addModule(h1)
    net.addOutputModule(outp)
    if bias:
        net.addModule(BiasUnit(name='bias'))
        net.addConnection(FullConnection(net['bias'],outp))
    # make connections
    net = shared_mesh(net, inlayer=inp, outlayer=h1, k=5, bias=bias)
    net.addConnection(FullConnection(h1, outp))
    net.sortModules()
    return net
Exemplo n.º 19
0
def main():
    results = []
    args = parse_args()

    # there's a bug in the _sparseness method in sklearn's nmf module that is
    # hit in some edge cases.  The value it computes isn't actually needed in
    # this case, so we can just ignore this divide by 0 error
    np.seterr(invalid="ignore")

    mtx = np.loadtxt(args.data_file, delimiter=',', skiprows=1)
    clabels = np.loadtxt(args.class_file, delimiter=',')

    print("Matrix is %d by %d and %f sparse" %
          (len(mtx), len(mtx[0]), Matrix.get_sparsity(mtx)))
    #print("clabels is %d by %d and %f sparse" % (len(clabels), len(clabels[0]), Matrix.get_sparsity(clabels)))
    #mtx = np.matrix.transpose(mtx)  # transpose to put samples into columns, genes into rows

    # create random class labels, replace with result of NMF
    #clabels = np.zeros(len(mtx))
    #for i in range(len(mtx)):
    # clabels[i] = random.randint(0, 3)
    clabels = np.matrix.transpose(clabels)

    print '-----------Logestic Regression-----------'
    t_lacc = 0
    for i in range(10):
        t_lacc = t_lacc + logistic_regression(mtx, clabels, True)

    print 'accuracy of logistic regression ', (t_lacc * 10)

    print '-----------ANN Computation----------'
    # prepare dataset for ANN
    ds = ClassificationDataSet(len(mtx[0]), 1,
                               nb_classes=5)  # replace with result of NMF
    for k in xrange(len(mtx)):
        ds.addSample(np.ravel(mtx[k]), clabels[k])

    # 10-fold cv
    t_error = 0
    t_acc = 0
    for i in range(10):
        # divide the data into training and test sets

        tstdata_temp, trndata_temp = ds.splitWithProportion(0.10)

        tstdata = ClassificationDataSet(len(mtx[0]), 1, nb_classes=5)
        for n in xrange(0, tstdata_temp.getLength()):
            tstdata.addSample(
                tstdata_temp.getSample(n)[0],
                tstdata_temp.getSample(n)[1])

        trndata = ClassificationDataSet(len(mtx[0]), 1, nb_classes=5)
        for n in xrange(0, trndata_temp.getLength()):
            trndata.addSample(
                trndata_temp.getSample(n)[0],
                trndata_temp.getSample(n)[1])

        trndata._convertToOneOfMany()
        tstdata._convertToOneOfMany()

        fnn = FeedForwardNetwork()
        inp = LinearLayer(trndata.indim)
        h1 = SigmoidLayer(10)
        h2 = TanhLayer(10)
        h3 = TanhLayer(10)
        h4 = TanhLayer(10)
        h5 = TanhLayer(10)
        outp = LinearLayer(trndata.outdim)
        #fnn = buildNetwork( trndata.indim, 10 , trndata.outdim, outclass=SoftmaxLayer )

        # add modules
        fnn.addOutputModule(outp)
        fnn.addInputModule(inp)
        fnn.addModule(h1)
        fnn.addModule(h2)
        fnn.addModule(h3)
        fnn.addModule(h4)
        fnn.addModule(h5)
        # create connections
        fnn.addConnection(FullConnection(inp, h1))
        fnn.addConnection(FullConnection(inp, h2))
        fnn.addConnection(FullConnection(inp, h3))
        fnn.addConnection(FullConnection(inp, h4))
        fnn.addConnection(FullConnection(inp, h5))
        fnn.addConnection(FullConnection(h1, h2))
        fnn.addConnection(FullConnection(h2, h3))
        fnn.addConnection(FullConnection(h3, h4))
        fnn.addConnection(FullConnection(h4, h5))

        fnn.addConnection(FullConnection(h5, outp))

        fnn.sortModules()

        trainer = BackpropTrainer(fnn,
                                  dataset=trndata,
                                  momentum=0.1,
                                  learningrate=0.01,
                                  verbose=True,
                                  weightdecay=0.01)

        #trainer.trainUntilConvergence()
        trainer.trainEpochs(5)

        t_error = t_error + percentError(
            trainer.testOnClassData(dataset=tstdata), tstdata['class'])

    print 'avg error ', (t_error / 10)
    print 'avg acc ', (100 - (t - error / 10))
Exemplo n.º 20
0
ds = SupervisedDataSet(n_input, 1) 

min_price = 250
max_price = 1500

iman = Inputman(n_input, 1)
iman.set_interval(min_price, max_price)
iman.set_norm_range(-1,1)
iman.set(training_start)


for x in range(0, training_range): 
    xs, ys = iman.next_norm()
    ds.addSample(xs[0], ys[0])

nn = FeedForwardNetwork()
inLayer = LinearLayer(n_input)
hiddenLayer = TanhLayer(3)
outLayer = LinearLayer(1)

nn.addInputModule(inLayer)
nn.addModule(hiddenLayer)
nn.addOutputModule(outLayer)
in_to_hidden = FullConnection(inLayer, hiddenLayer)
hidden_to_out = FullConnection(hiddenLayer, outLayer)
nn.addConnection(in_to_hidden)
nn.addConnection(hidden_to_out)
nn.sortModules()

#trainer = BackpropTrainer(nn, ds, learningrate=0.01, momentum=0.1)
ga = GA(ds.evaluateModuleMSE, nn, minimize=True)
Exemplo n.º 21
0
def save_best_individual(individual):
    with open('best_individual.pkl', 'wb') as output:
        # save individual
        pickle.dump(individual, output, pickle.HIGHEST_PROTOCOL)


# loads an individual from a pickle file
def load_best_individual():
    with open('best_individual.pkl', 'rb') as inp:
        # load individual
        individual = pickle.load(inp)
    return individual


# create network and layers
net = FeedForwardNetwork()
in_layer = LinearLayer(16)
hid1_layer = SigmoidLayer(20)
hid2_layer = SigmoidLayer(20)
out_layer = SigmoidLayer(2)

# add layers to network
net.addInputModule(in_layer)
net.addModule(hid1_layer)
net.addModule(hid2_layer)
net.addOutputModule(out_layer)

# create connections between layers
in_to_hid1 = FullConnection(in_layer, hid1_layer)
hid1_to_hid2 = FullConnection(hid1_layer, hid2_layer)
hid2_to_out = FullConnection(hid2_layer, out_layer)
Exemplo n.º 22
0
def anntrain(xdata,ydata):#,epochs):
    #print len(xdata[0])
    ds=SupervisedDataSet(len(xdata[0]),1)
    #ds=ClassificationDataSet(len(xdata[0]),1, nb_classes=2)
    for i,algo in enumerate (xdata):
        ds.addSample(algo,ydata[i])
    #ds._convertToOneOfMany( ) esto no
    net= FeedForwardNetwork()
    inp=LinearLayer(len(xdata[0]))
    h1=SigmoidLayer(1)
    outp=LinearLayer(1)
    net.addOutputModule(outp) 
    net.addInputModule(inp) 
    net.addModule(h1)
    #net=buildNetwork(len(xdata[0]),1,1,hiddenclass=TanhLayer,outclass=SoftmaxLayer)
    
    net.addConnection(FullConnection(inp, h1))  
    net.addConnection(FullConnection(h1, outp))

    net.sortModules()

    trainer=BackpropTrainer(net,ds)#, verbose=True)#dataset=ds,verbose=True)
    #trainer.trainEpochs(40)
    trainer.trainOnDataset(ds,40) 
    #trainer.trainUntilConvergence(ds, 20, verbose=True, validationProportion=0.15)
    trainer.testOnData()#verbose=True)
    #print 'Final weights:',net.params
    return net
Exemplo n.º 23
0
def net_shared2(h1dim=13,h2dim=5, bias=True):  #  alternative default: h1dim=8, h2dim=4,
    net = FeedForwardNetwork()
    # make modules
    inp=LinearLayer(28*28,name='input')
    h1=SigmoidLayer(h1dim**2,name='hidden1st')
    h2=SigmoidLayer(h2dim**2,name='hidden2nd')
    outp=SoftmaxLayer(10,name='output')
    net.addInputModule(inp)
    net.addModule(h1)
    net.addModule(h2)
    net.addOutputModule(outp)
    if bias:
        net.addModule(BiasUnit(name='bias'))
        net.addConnection(FullConnection(net['bias'],outp))
    # make connections
    net = shared_mesh(net, inlayer=inp, outlayer=h1, k=5, mc_name='mother1st', bias=bias)
    net = shared_mesh(net, inlayer=h1, outlayer=h2, k=5, mc_name='mother2nd', bias=bias)
    net.addConnection(FullConnection(h2, outp))
    net.sortModules()
    return net
class BrainApp:
    TkField = []
    # Parameters (attributes) and values
    parameters = {
        "INPUT": "4",  # No of input dimensions
        "OUTPUT": "3",  # No of Output Class
        "HIDDEN0": "3",  # No of Hidden Neurons 1st layer
        "HIDDEN1": "2",  # Second Layer
        "HIDDEN_S/T": "T",  # Hidden layer activations (S)oftMax or (T)anh
        "OUTPUT_S/L": "S",  # Output layer activation (S)oftMax or (L)inear
        "LEARNING_RATE": "0.05",
        "MOMENTUM": "0.0",
        "BIAS": "True",
        "EPOCHS": "120",  # No of training cycles used
        "WEIGHT_DECAY": "0.0",
        "SPLIT_DATA": "0.1",
        "UPPER_LIMIT": "0.6",  # Higher than this, taken as 1.0
        "LOWER_LIMIT": "0.4",
    }  # Less than this, taken as zero

    # Configure input file (text) into input list and output list
    def readMyData(self, filename):
        print ("FILE is %s" % filename)
        file = open(filename, "r")
        for line in file.readlines():
            L = line.split(" ")
            inSample = []
            outSample = []
            for i in range(int(self.parameters["INPUT"])):
                inSample.append(float(L[i]))
            for j in range(int(self.parameters["OUTPUT"])):
                outSample.append(float(L[j + int(self.parameters["INPUT"])]))

            self.myDataset.addSample(inSample, outSample)

    # current Error Measure - You could add your own
    def SumSquareError(self, Actual, Desired):
        error = 0.0
        for i in range(len(Desired)):
            for j in range(len(Desired[i])):
                error = error + ((Actual[i])[j] - (Desired[i])[j]) * ((Actual[i])[j] - (Desired[i])[j])
        return error

    def RunSimulator(self):
        self.myDataset = ClassificationDataSet(int(self.parameters["INPUT"]), int(self.parameters["OUTPUT"]))
        # Load float format iris dataset
        self.readMyData(filename)
        # create baseline network
        self.network = FeedForwardNetwork()
        # Selection of hidden layers (1 or 2)
        if int(self.parameters["HIDDEN1"]) == 0:
            # Build Architecture (One hidden layer)
            inLayer = LinearLayer(int(self.parameters["INPUT"]))
            if self.parameters["HIDDEN_S/T"] == "T":
                hiddenLayer0 = TanhLayer(int(self.parameters["HIDDEN0"]))
            else:
                hiddenLayer0 = SoftmaxLayer(int(self.parameters["HIDDEN0"]))
            if self.parameters["OUTPUT_S/L"] == "S":
                outLayer = SoftmaxLayer(int(self.parameters["OUTPUT"]))
            else:
                outLayer = LinearLayer(int(self.parameters["OUTPUT"]))
            self.network.addInputModule(inLayer)
            self.network.addModule(hiddenLayer0)
            self.network.addOutputModule(outLayer)

            # Make connections
            in_to_hidden = FullConnection(inLayer, hiddenLayer0)
            hidden_to_out = FullConnection(hiddenLayer0, outLayer)
            self.network.addConnection(in_to_hidden)
            self.network.addConnection(hidden_to_out)
            if self.parameters["BIAS"] == "True":
                bias = BiasUnit("bias")
                self.network.addModule(bias)
                bias_to_hidden0 = FullConnection(bias, hiddenLayer0)
                bias_to_out = FullConnection(bias, outLayer)
                self.network.addConnection(bias_to_hidden0)
                self.network.addConnection(bias_to_out)
        elif int(self.parameters["HIDDEN0"]) == 0:
            print "Cannot delete layer 0"
            sys.exit()
        else:  # Two hidden layers
            # Build Architecture
            inLayer = LinearLayer(int(self.parameters["INPUT"]))
            if self.parameters["HIDDEN_S/T"] == "T":
                hiddenLayer0 = TanhLayer(int(self.parameters["HIDDEN0"]))
                hiddenLayer1 = TanhLayer(int(self.parameters["HIDDEN1"]))
            else:
                hiddenLayer0 = SoftmaxLayer(int(self.parameters["HIDDEN0"]))
                hiddenLayer1 = SoftmaxLayer(int(self.parameters["HIDDEN1"]))
            if self.parameters["OUTPUT_S/L"] == "S":
                outLayer = SoftmaxLayer(int(self.parameters["OUTPUT"]))
            else:
                outLayer = LinearLayer(int(self.parameters["OUTPUT"]))
            self.network.addInputModule(inLayer)
            self.network.addModule(hiddenLayer0)
            self.network.addModule(hiddenLayer1)
            self.network.addOutputModule(outLayer)

            # Make connections
            in_to_hidden = FullConnection(inLayer, hiddenLayer0)
            hidden_to_hidden = FullConnection(hiddenLayer0, hiddenLayer1)
            hidden_to_out = FullConnection(hiddenLayer1, outLayer)
            self.network.addConnection(in_to_hidden)
            self.network.addConnection(hidden_to_hidden)
            self.network.addConnection(hidden_to_out)
            if self.parameters["BIAS"] == "True":
                bias = BiasUnit("bias")
                self.network.addModule(bias)
                bias_to_hidden0 = FullConnection(bias, hiddenLayer0)
                bias_to_hidden1 = FullConnection(bias, hiddenLayer1)
                bias_to_out = FullConnection(bias, outLayer)
                self.network.addConnection(bias_to_hidden0)
                self.network.addConnection(bias_to_hidden1)
                self.network.addConnection(bias_to_out)

        # topologically sort the units in network
        self.network.sortModules()

        # split the data randomly into 90% training, 10% test = 0.1
        testData, trainData = self.myDataset.splitWithProportion(float(self.parameters["SPLIT_DATA"]))

        # create the trainer environment for backprop and  train network
        trainer = BackpropTrainer(
            self.network,
            dataset=trainData,
            learningrate=float(self.parameters["LEARNING_RATE"]),
            momentum=float(self.parameters["MOMENTUM"]),
            weightdecay=float(self.parameters["WEIGHT_DECAY"]),
        )

        # Data workspace
        TrainingPoints = []
        TestPoints = []
        xAxis = []
        # Run for specified number of Epochs
        for i in range(int(self.parameters["EPOCHS"])):
            trainer.trainEpochs(1)
            trnresult = self.SumSquareError(self.network.activateOnDataset(dataset=trainData), trainData["target"])
            tstresult = self.SumSquareError(self.network.activateOnDataset(dataset=testData), testData["target"])
            # Print Current Errors (comment out when not needed)
            print "epoch: %4d" % trainer.totalepochs, " train error: %5.2f" % trnresult, " test error: %5.2f" % tstresult
            # Build Lists for plotting
            TrainingPoints.append(trnresult)
            TestPoints.append(tstresult)
            xAxis.append(i)

        # Output Results
        self.AnalyzeOutput(testData)
        pylab.plot(xAxis, TrainingPoints, "b-")
        pylab.plot(xAxis, TestPoints, "r-")
        pylab.xlabel("EPOCHS")
        pylab.ylabel("Sum Squared Error")
        pylab.title("Plot of Training Errors")
        pylab.show()

    # Create Parameters GUI
    def __init__(self, parent):
        self.myparent = parent
        r = 0
        for c in self.parameters.keys():
            Label(parent, text=c, relief=RIDGE, width=25).grid(row=r, column=0)
            self.TkField.append(Entry(parent))
            self.TkField[r].grid(row=r, column=1)
            self.TkField[r].insert(0, "                %s" % self.parameters[c])
            self.TkField[r].bind("<Button-1>", self.ClearWidget)
            self.TkField[r].bind("<Return>", self.ok)
            r += 1
        Button(parent, text="RUN", command=self.RunSimulator).grid(row=r, column=0)
        Button(parent, text="END", command=self.terminate).grid(row=r, column=1)

    # Enter new data (called when <Return> key is pressed after data entry)
    def ok(self, event):
        position = int(event.widget.grid_info()["row"])  # get the parameter position of the <Return> event
        self.TkField[position].insert(0, "                ")
        self.parameters[self.parameters.keys()[position]] = self.TkField[position].get()  # get new data entry
        self.TkField[position].delete(0, END)
        self.TkField[position].insert(0, self.parameters[self.parameters.keys()[position]])
        self.TkField[position].icursor(0)

    # END button is pressed
    def terminate(self):
        self.myparent.destroy()

    # Clear Data Field in GUI (lefthand mouse key is pressed)
    def ClearWidget(self, event):
        position = int(event.widget.grid_info()["row"])  # get the parameter position of the <Button-1> event
        self.TkField[position].delete(0, END)

    # Analyse the Test Data Set
    # Save the actual and desired results for tes data in 'result.dat'
    # Print the Correct, Wrong and Unknown Statistics
    def AnalyzeOutput(self, testData):
        # Compare actual test results (writes to data file 'result.dat')
        total = {"good": 0, "bad": 0, "unknown": 0}
        actualTestOutput = self.network.activateOnDataset(dataset=testData)
        desiredTestOutput = testData["target"]
        resultFile = open(result, "w")
        resultFile.write("   Test Outputs\n")
        resultFile.write(" Actual    Desired")
        for m in range(len(actualTestOutput)):  # say, 15 for iris data (10%)
            resultFile.write("\n")
            sample = {"good": 0, "bad": 0, "unknown": 0}
            for n in range(int(self.parameters["OUTPUT"])):  # say, 3 classes as in iris data
                actual = float(actualTestOutput[m][n])  # class by class
                resultFile.write("  %2.1f" % actual)
                resultFile.write("\t")
                desired = float(desiredTestOutput[m][n])
                resultFile.write("    %2.1f\n" % desired)

                # for classifier, calculate the errors
                upper = float(self.parameters["UPPER_LIMIT"])
                lower = float(self.parameters["LOWER_LIMIT"])

                # Check for silly values
                if upper >= 1.0 or lower >= 1.0 or lower > upper:
                    print "Illegal setting of upper or lower decision limit"
                    sys.exit()
                # My logic to built result determined by the values of upper and lower limits
                if desired > upper and actual > upper:
                    sample["good"] += 1
                elif desired < lower and actual < lower:
                    sample["good"] += 1
                elif desired > upper and actual < lower:  # corrected 25th April
                    sample["bad"] += 1
                elif desired < lower and actual > upper:  # corrected 25th April
                    sample["bad"] += 1
                else:
                    sample["unknown"] += 1
            if sample["good"] == int(self.parameters["OUTPUT"]):
                total["good"] += 1
            elif sample["bad"] > 0:
                total["bad"] += 1
            elif sample["bad"] == 0 and sample["unknown"] > 0:
                total["unknown"] += 1

        # Print Test Result Analysis to Screen
        print  # New line
        percentage = 100.0 * float(total["good"]) / float(len(actualTestOutput))
        print " Correct\t= %f" % percentage
        percentage = 100.0 * float(total["bad"]) / float(len(actualTestOutput))
        print " Wrong\t\t= %f" % percentage
        percentage = 100.0 * float(total["unknown"]) / float(len(actualTestOutput))
        print " Unknown\t= %f" % percentage
        resultFile.close()
Exemplo n.º 25
0
from pybrain.structure.networks import FeedForwardNetwork
from pybrain.structure import FullConnection
from pybrain.structure import LinearLayer, SigmoidLayer, BiasUnit

rede = FeedForwardNetwork()

camadaEntrada = LinearLayer(2)  # quantidade de neuronios na camada de entrada
# Não serão submetidos a nenhuma função de ativação, por isso usar o LinearLayer

camadaOculta = SigmoidLayer(3)
camadaSaida = SigmoidLayer(1)
bias_oculta = BiasUnit()
bias_saida = BiasUnit()

rede.addModule(camadaEntrada)
rede.addModule(camadaOculta)
rede.addModule(camadaSaida)
rede.addModule(bias_oculta)
rede.addModule(bias_saida)

entradaOculta = FullConnection(camadaEntrada, camadaOculta)
ocultaSaida = FullConnection(camadaOculta, camadaSaida)
biasOculta = FullConnection(bias_oculta, camadaOculta)
biasSaida = FullConnection(bias_saida, camadaSaida)

rede.sortModules()
class BrainApp:
    TkField = []
    #Parameters (attributes) and values
    parameters = {'INPUT': '4',     #No of input dimensions
                  'OUTPUT': '3',    #No of Output Class
                  'HIDDEN0': '3',   #No of Hidden Neurons 1st layer
                  'HIDDEN1': '2',   #Second Layer
                  'LEARNING_RATE': '0.05',
                  'MOMENTUM': '0.0',
                  'EPOCHS': '120',  #No of training cycles used
                  'WEIGHT_DECAY': '0.0',
                  'SPLIT_DATA': '0.1',
                  'UPPER_LIMIT': '0.6',  #Higher than this, taken as 1.0
                  'LOWER_LIMIT': '0.4' } #Less than this, taken as zero

    #Configure input file (text) into input list and output list
    def readMyData(self, filename):
       print("FILE is %s" % filename)
       file = open(filename, 'r')
       for line in file.readlines():
           L = line.split(" ")
           inSample = []
           outSample = []
           for i in range(int(self.parameters['INPUT'])):
              inSample.append(float(L[i]))
           for j in range(int(self.parameters['OUTPUT'])):
              outSample.append(float(L[j+int(self.parameters['INPUT'])]))
          
           self.myDataset.addSample(inSample,outSample)

    #current Error Measure - You could add your own
    def SumSquareError(self, Actual, Desired):
       error = 0.
       for i in range(len(Desired)):
          for j in range(len(Desired[i])):
             error = error + ((Actual[i])[j] - (Desired[i])[j])*((Actual[i])[j] - (Desired[i])[j])
       return error

    def RunSimulator(self):
        self.myDataset = ClassificationDataSet(int(self.parameters['INPUT']), int(self.parameters['OUTPUT']))
        #Load float format iris dataset
        self.readMyData(filename)
        #create baseline network
        self.network = FeedForwardNetwork()
        #Selection of hidden layers (1 or 2)
        if int(self.parameters['HIDDEN1']) == 0 :
            #Build Architecture (One hidden layer)
            inLayer = LinearLayer(int(self.parameters['INPUT']))
            hiddenLayer0 = TanhLayer(int(self.parameters['HIDDEN0'])) #Sigmoid to Tanh 25Apr
            outLayer = SoftmaxLayer(int(self.parameters['OUTPUT']))
            self.network.addInputModule(inLayer)
            self.network.addModule(hiddenLayer0)
            self.network.addOutputModule(outLayer)
            #Make connections
            in_to_hidden = FullConnection(inLayer, hiddenLayer0)
            hidden_to_out = FullConnection(hiddenLayer0, outLayer)
            self.network.addConnection(in_to_hidden)
            self.network.addConnection(hidden_to_out)
        elif int(self.parameters['HIDDEN0']) == 0 :
            print "Cannot delete layer 0"
            sys.exit()
        else:   #Two hidden layers
            #Build Architecture
            inLayer = LinearLayer(int(self.parameters['INPUT']))
            hiddenLayer0 = TanhLayer(int(self.parameters['HIDDEN0']))  #Tanh
            hiddenLayer1 = TanhLayer(int(self.parameters['HIDDEN1']))  #Tanh
            outLayer = SoftmaxLayer(int(self.parameters['OUTPUT']))
            self.network.addInputModule(inLayer)
            self.network.addModule(hiddenLayer0)
            self.network.addModule(hiddenLayer1)
            self.network.addOutputModule(outLayer)
            #Make connections
            in_to_hidden = FullConnection(inLayer, hiddenLayer0)
            hidden_to_hidden = FullConnection(hiddenLayer0, hiddenLayer1)
            hidden_to_out = FullConnection(hiddenLayer1, outLayer)
            self.network.addConnection(in_to_hidden)
            self.network.addConnection(hidden_to_hidden)
            self.network.addConnection(hidden_to_out)

        #initialize dataset
        self.network.sortModules()
        # split the data randomly into 90% training, 10% test = 0.1
        testData, trainData = self.myDataset.splitWithProportion(float(self.parameters['SPLIT_DATA']))

        #create the trainer environment for backprop and  train network
        trainer = BackpropTrainer(self.network, dataset = trainData, learningrate = \
        float(self.parameters['LEARNING_RATE']), momentum=float(self.parameters['MOMENTUM']), \
        weightdecay=float(self.parameters['WEIGHT_DECAY']))

        #Data workspace
        TrainingPoints = []
        TestPoints = []
        xAxis = []
        #Run for specified number of Epochs
        for i in range(int(self.parameters['EPOCHS'])):
            trainer.trainEpochs(1)
            trnresult = self.SumSquareError(self.network.activateOnDataset(dataset=trainData), trainData['target'])
            tstresult = self.SumSquareError(self.network.activateOnDataset(dataset=testData), testData['target'])
            #Print Current Errors (comment out when not needed)
            print "epoch: %4d" % trainer.totalepochs, \
            " train error: %5.2f" % trnresult, \
            " test error: %5.2f" % tstresult
            #Build Lists for plotting 
            TrainingPoints.append(trnresult)
            TestPoints.append(tstresult)
            xAxis.append(i)

        #Output Results
        self.AnalyzeOutput(testData)
        pylab.plot(xAxis, TrainingPoints, 'b-')
        pylab.plot(xAxis,TestPoints, 'r-')
        pylab.xlabel('EPOCHS')
        pylab.ylabel('Sum Squared Error')
        pylab.title('Plot of Training Errors')
        pylab.show()

    #Create Parameters GUI
    def __init__(self, parent):
        self.myparent = parent
        r = 0
        for c in self.parameters.keys():  
            Label(parent, text=c, relief=RIDGE,  width=25).grid(row=r, column=0)
            self.TkField.append(Entry(parent))
            self.TkField[r].grid(row=r, column=1)
            self.TkField[r].insert(0,"                %s" % self.parameters[c])
            self.TkField[r].bind("<Button-1>", self.ClearWidget)
            self.TkField[r].bind("<Return>", self.ok)
            r += 1
        Button(parent,text='RUN', command=self.RunSimulator).grid(row=r, column=0)
        Button(parent,text='END', command=self.terminate).grid(row=r, column=1)

    #Enter new data (called when <Return> key is pressed after data entry)
    def ok(self, event):
        position = int(event.widget.grid_info()['row'])  #get the parameter position of the <Return> event
        self.TkField[position].insert(0,'                ')
        self.parameters[self.parameters.keys()[position]] = self.TkField[position].get() #get new data entry
        self.TkField[position].delete(0,END)
        self.TkField[position].insert(0,self.parameters[self.parameters.keys()[position]])
        self.TkField[position].icursor(0)

    #END button is pressed
    def terminate(self):
        self.myparent.destroy()

    #Clear Data Field in GUI (lefthand mouse key is pressed)
    def ClearWidget(self, event):
        position = int(event.widget.grid_info()['row'])  #get the parameter position of the <Button-1> event  
        self.TkField[position].delete(0,END)

    #Analyse the Test Data Set
    #Save the actual and desired results for tes data in 'result.dat'
    #Print the Correct, Wrong and Unknown Statistics
    def AnalyzeOutput(self, testData):
        #Compare actual test results (writes to data file 'result.dat')
        total = { 'good' : 0,
                  'bad' : 0,
                  'unknown' : 0 }        
        actualTestOutput = self.network.activateOnDataset(dataset=testData)
        desiredTestOutput = testData['target']
        resultFile = open(result, 'w')
        resultFile.write( "   Test Outputs\n")
        resultFile.write(" Actual    Desired")
        for m in range(len(actualTestOutput)):        #say, 15 for iris data (10%)
            resultFile.write('\n')
            sample = { 'good': 0,
                       'bad' : 0,
                       'unknown' : 0 }
            for n in range(int(self.parameters['OUTPUT'])):   #say, 3 classes as in iris data
               actual = float(actualTestOutput[m][n])   #class by class
               resultFile.write( "  %2.1f" % actual,)
               resultFile.write( '\t',)
               desired = float(desiredTestOutput[m][n])
               resultFile.write( "    %2.1f\n" % desired )
              
               #for classifier, calculate the errors
               upper = float(self.parameters['UPPER_LIMIT'])
               lower = float(self.parameters['LOWER_LIMIT'])

               #Check for silly values
               if upper >= 1.0 or lower >= 1.0 or lower > upper :
                   print "Illegal setting of upper or lower decision limit"
                   sys.exit()
               #My logic to built result determined by the values of upper and lower limits
               if desired > upper and actual > upper :
                   sample['good'] += 1
               elif desired < lower and actual < lower :
                   sample['good'] += 1
               elif desired > upper and actual < lower :    #corrected 25th April
                   sample['bad'] += 1
               elif desired < lower and actual > upper :    #corrected 25th April
                   sample['bad'] += 1
               else :
                   sample['unknown'] += 1
            if sample['good'] == int(self.parameters['OUTPUT']) :
                total['good'] += 1
            elif sample['bad'] > 0 :
                total['bad'] += 1
            elif sample['bad'] == 0 and sample['unknown'] > 0 :
                total['unknown'] += 1

        #Print Test Result Analysis to Screen
        print  #New line
        percentage = 100.0*float(total['good'])/float(len(actualTestOutput))
        print " Correct\t= %f" % percentage
        percentage = 100.0*float(total['bad'])/float(len(actualTestOutput))
        print " Wrong\t\t= %f" % percentage
        percentage = 100.0*float(total['unknown'])/float(len(actualTestOutput))
        print " Unknown\t= %f" % percentage
        resultFile.close()