Beispiel #1
0
    def __init__(self, train_data, hyper,  n_targets=None, label_targets=None):
        """
    ------------

    train_data: pandas DataFrame
                Contains columns for features and for target variables. The names of the target variables ends
                with the suffix "_tau"
    hyper:      dictionary
                It contains the hyperparameters necessary to run all the functionalities of the model.
                 They are the following:
                "structure" is a list of integers determining the number of neurons in each hidden layer
                "epochs" an integer specifying the maximum number of epochs to run during every training session
                "learning_rate" a float giving the learning rate of the gradient descend
                "momentum" a float giving the value of the momentum for the algorithm
                "batch" a bool. If True the method performs full batch learning, i.e. updates of the weights is done
                using all the instances of the training set. Else, normal online method is performed
                Other parameters regarding cross validation are explained in the base class

        """
        Regression.__init__(self, train_data, hyper, n_targets=n_targets, label_targets=label_targets)

        self.N = FeedForwardNetwork()
        self.structure = [self.n_feature] + hyper['structure'] + [self.n_target]

        self._build_net(self.structure)
        self.res_params = [self.N.params[i] for i in range(len(self.N.params))]

        self.train_fraction = hyper['train_fraction']
        self.seed = hyper['seed']
        self.epochs = hyper['epochs']
        self.learning_rate = hyper['learning_rate']
        self.momentum = hyper['momentum']
        self.batch = bool(hyper['batch'])
Beispiel #2
0
class BMBPTester:
    __fnn = None
    fnnname = 'buildBMTrainer.xml'
    srctestname = 'tester.xlsx'

    def __init__(self, psrctestname='tester.xlsx', pfnnname='buildBMTrainer.xml'):
        self.fnnname = pfnnname
        self.srctestname = psrctestname
        self.__fnn = FeedForwardNetwork()

    def test(self):
        self.__fnn = NetworkReader.readFrom(self.fnnname)
        workbook = xlrd.open_workbook(self.srctestname)
        sheet1 = workbook.sheet_by_index(0)
        x = np.zeros((sheet1.nrows, sheet1.ncols), dtype=np.float)
        for i in range(sheet1.nrows):
            for j in range(sheet1.ncols):
                x[i][j] = sheet1.cell(i, j).value
        stestx = MinMaxScaler()
        xtest = stestx.fit_transform(x)
        sy = joblib.load('sy.pkl')
        print(sy)
        values = []
        for x1 in xtest:
            values.append(sy.inverse_transform(self.__fnn.activate(x1).reshape(-1, 1)))
            print(self.__fnn.activate(x1))
        print(values)
Beispiel #3
0
class MyNet:
    def __init__(self, file='config.xml'):
        self.net = FeedForwardNetwork()
        self.file = file

    def constructNet(self, input, hidden, output):
        inputLayer = LinearLayer(input)
        hiddenLayer = TanhLayer(hidden)
        outputLayer = LinearLayer(output)

        self.net.addInputModule(inputLayer)
        self.net.addModule(hiddenLayer)
        self.net.addOutputModule(outputLayer)

        conn1 = FullConnection(inputLayer, hiddenLayer)
        conn2 = FullConnection(hiddenLayer, outputLayer)

        self.net.addConnection(conn1)
        self.net.addConnection(conn2)

    def setup(self):
        self.net.sortModules()

    def saveToFile(self, file='config.xml'):
        NetworkWriter.writeToFile(self.net, file)

    def loadFromFile(self, file='config.xml'):
        self.net = NetworkReader.readFrom(file)
Beispiel #4
0
class MyNet:

	def __init__(self, file='config.xml'):
		self.net = FeedForwardNetwork()
		self.file = file


	def constructNet(self, input, hidden, output): 
		inputLayer = LinearLayer(input)
		hiddenLayer = TanhLayer(hidden)
		outputLayer = LinearLayer(output)

		self.net.addInputModule(inputLayer)
		self.net.addModule(hiddenLayer)
		self.net.addOutputModule(outputLayer)

		conn1 = FullConnection(inputLayer, hiddenLayer)
		conn2 = FullConnection(hiddenLayer, outputLayer)

		self.net.addConnection(conn1)
		self.net.addConnection(conn2)

	
	def setup(self):
		self.net.sortModules()

	
	def saveToFile(self,file='config.xml'):
		NetworkWriter.writeToFile(self.net, file)


	def loadFromFile(self, file='config.xml'):
		self.net = NetworkReader.readFrom(file)
    def __init__(self):

        self.Q = FeedForwardNetwork()

        # La funcion de valor se representa con una red neuronal
        # Input: S = (Angulo, Velocidad angular, Posicion), A = accion
        # Output: Valor
        # 2 capas ocultas de 5 neuronas cada una
        # Funcion de activacion sigmoidea
        inLayer = SigmoidLayer(4, name="Input Layer")
        hiddenLayer1 = SigmoidLayer(5, name="Hidden Layer 1")
        hiddenLayer2 = SigmoidLayer(5, name="Hidden Layer 2")
        outLayer = SigmoidLayer(1, name="Output Layer")

        self.Q.addInputModule(inLayer)
        self.Q.addModule(hiddenLayer1)
        self.Q.addModule(hiddenLayer2)
        self.Q.addOutputModule(outLayer)

        connInToHidden1 = FullConnection(inLayer, hiddenLayer1)
        connHidden1ToHidden2 = FullConnection(hiddenLayer1, hiddenLayer2)
        connHidden2ToOut = FullConnection(hiddenLayer2, outLayer)

        self.Q.addConnection(connInToHidden1)
        self.Q.addConnection(connHidden1ToHidden2)
        self.Q.addConnection(connHidden2ToOut)

        self.Q.sortModules()
Beispiel #6
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)
Beispiel #7
0
 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)
Beispiel #8
0
 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 __init__(self, genes=None):
        self.net = FeedForwardNetwork()

        inLayer = LinearLayer(Brain.G_INPUTNODES, name='input')
        hiddenLayer1 = SigmoidLayer(Brain.G_HIDDENNODES_L1, name='hidden1')
        hiddenLayer2 = SigmoidLayer(Brain.G_HIDDENNODES_L2, name='hidden2')
        outLayer = SigmoidLayer(Brain.G_OUTPUTNODES, name='out')
        bias = BiasUnit(name='bias')

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

        in_to_hidden1 = FullConnection(inLayer, hiddenLayer1)
        hidden1_to_hidden2 = FullConnection(hiddenLayer1, hiddenLayer2)
        hidden2_to_out = FullConnection(hiddenLayer2, outLayer)
        bias_to_hidden1 = FullConnection(bias, hiddenLayer1)
        bias_to_hidden2 = FullConnection(bias, hiddenLayer2)
        bias_to_out = FullConnection(bias, outLayer)

        self.net.addConnection(in_to_hidden1)
        self.net.addConnection(hidden1_to_hidden2)
        self.net.addConnection(hidden2_to_out)
        self.net.addConnection(bias_to_hidden1)
        self.net.addConnection(bias_to_hidden2)
        self.net.addConnection(bias_to_out)

        self.net.sortModules()

        if genes != None:
            self.import_genes(genes)
Beispiel #10
0
    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)
Beispiel #11
0
 def __init__(self, network, camada_entrada, camada_oculta, camada_saida):
     self.network = network
     self.network = FeedForwardNetwork()
     self.camada_entrada = camada_entrada
     self.camada_oculta = camada_oculta
     self.camada_saida = camada_saida
     self.ligacao_entrada_oculta = None
     self.ligacao_oculta_saida = None
     self.defineArquitetura()
Beispiel #12
0
def main(f_samples):
    f_reading = open(f_samples, 'r')
    global data
    data = []

    for line in f_reading:
        line = line.split()
        data.append( (float(line[0]), float(line[-1])) )

    #function
    data_module = lambda x: map( lambda z: data[z], filter( lambda y: y% 5 == x, xrange(len(data)) ) )

    global data1
    data1 = [data_module(0), data_module(1), data_module(2), data_module(3), data_module(4)]

    global data_transformed
    data_transformed = take(data, rate = 60)

    global data_transformed_training
    data_transformed_training = map( lambda x: data_transformed[x], filter( lambda x: uniform(0, 1) > 0.3, xrange(len(data_transformed)) ))

    #Learning process-----------------------------------------------------------------

    global net, samples, trainer
    net = FeedForwardNetwork()
    inLayer = LinearLayer(3)
    hiddenLayer0 = SigmoidLayer(1)
    hiddenLayer1 = SigmoidLayer(3)
    outLayer = LinearLayer(1)

    net.addInputModule(inLayer)
#    net.addModule(hiddenLayer0)
#    net.addModule(hiddenLayer1)
    net.addOutputModule(outLayer)

#    net.addConnection(FullConnection(inLayer, hiddenLayer0))
    net.addConnection(FullConnection(inLayer, outLayer))
#    net.addConnection(FullConnection(hiddenLayer0, outLayer))
#    net.addConnection(FullConnection(hiddenLayer0, hiddenLayer1))
#    net.addConnection(FullConnection(hiddenLayer1, outLayer))
    net.sortModules()
    print net
    ##Net with 3 inputs, 8 hidden neurons in a layerand 8 in another, and 1 out.
    #net = buildNetwork(3,8,8,1)
    ##Set with 2 inputs and one output for each sample
    samples = SupervisedDataSet(3,1)

    for i in data_transformed_training:
        samples.addSample(i['past'], i['next'] - i['average'])
    trainer = BackpropTrainer(net, samples)

    print 'Training'
    trainer.trainUntilConvergence(maxEpochs= 10)

    print 'Comparing'
    compare_net_samples(net, data_transformed)
    print "Number of samples %d for training." %len(data_transformed_training)
Beispiel #13
0
    def __init__(self, x, y, direction):
        self.age = 0

        # position
        self.x = x
        self.y = y

        # number of fruits peeled
        self.num_peeled = 0
        self.num_eaten = 0
        self.num_moved = 0

        # orientation (0 - 359 degrees)
        self.direction = direction

        # touching anything
        self.touching = None
        self.sees = None

        # hunger sensor
        self.hunger = 2000
        self.avg_hunger = 0

        ###
        # Neural Network
        #
        # Inputs:
        # 1. sees_peeled_orange
        # 2. sees_unpeeled_orange
        # 3. sees_peeled_banana
        # 4. sees_unpeeled_banana
        # 5. sees_animat
        # 6. sees_wall
        # 7. hunger
        # 8. touching_peeled_orange
        # 9. touching_unpeeled_orange
        # 10. touching_peeled_banana
        # 11. touching_unpeeled_banana
        # 12. touching_animat
        # 13. touching_wall
        ###

        self.net = FeedForwardNetwork()
        self.net.addInputModule(LinearLayer(13, name='in'))
        self.net.addModule(SigmoidLayer(14, name='hidden'))
        self.net.addOutputModule(LinearLayer(5, name='out'))
        self.net.addConnection(
            FullConnection(self.net['in'], self.net['hidden']))
        self.net.addConnection(
            FullConnection(self.net['hidden'], self.net['out']))
        self.net.sortModules()

        # thresholds for deciding an action
        self.move_threshold = 0
        self.peel_threshold = 0
        self.eat_threshold = 0
Beispiel #14
0
  def __init__(self, hidden_neuron_num=1, hidden_type='sigmoid'):
    self.hidden_neuron_num = hidden_neuron_num
    self.hidden_type = hidden_type

    self.net = FeedForwardNetwork()
    self.samples = SupervisedDataSet(784, 784)

    self.vectorizer = ImageVectorizer()

    self.add_layers()
    self.add_connections()
    self.sort()
def initMaxentNetwork():
  """Builds a network with just a sigmoid output layer, i.e. a multi-class maximum entropy model."""
  fnn = FeedForwardNetwork()
  inLayer = LinearLayer(numFeatures)
  fnn.addInputModule(inLayer)
  outLayer = SigmoidLayer(3)
  fnn.addOutputModule(outLayer)
  fnn.addConnection(FullConnection(inLayer, outLayer))
  fnn.sortModules()
  return fnn
Beispiel #16
0
    def __init__(self, hidden_layers, ally_champ_obj_list,
                 enemy_champ_obj_list):

        self.ally_champ_obj_list = ally_champ_obj_list
        self.enemy_champ_obj_list = enemy_champ_obj_list

        self.set_nodes()

        self.network = FeedForwardNetwork()

        connect_queue = Queue.Queue()

        for layer in xrange(0, hidden_layers):
            connect_queue.put(
                TanhLayer(self.input_node_count,
                          name='hidden_layer_{}'.format(layer)))

        connect_queue.put(SigmoidLayer(1, name='output_layer'))

        prev_layer = LinearLayer(self.input_node_count, name='input_layer')
        self.network.addInputModule(prev_layer)

        while not connect_queue.empty():

            current_layer = connect_queue.get()
            if current_layer.name == 'output_layer':
                self.network.addOutputModule(current_layer)
            else:
                self.network.addModule(current_layer)

            bias = BiasUnit()
            bias_connection = FullConnection(
                bias,
                current_layer,
                name="bias_to_{}_connection".format(current_layer.name))
            self.network.addModule(bias)
            self.network.addConnection(bias_connection)

            connection = FullConnection(prev_layer,
                                        current_layer,
                                        name="{}_to_{}_connection".format(
                                            prev_layer.name,
                                            current_layer.name))
            self.network.addConnection(connection)

            prev_layer = current_layer

        self.network.sortModules()
 def __init__(self, x, y, direction):
   self.age = 0
   # position
   self.x = x
   self.y = y
   # number of going back and forth for different foods
   self.backForth = 0
   self.LastFood = None # the last food animat ate
   # orientation (0 - 359 degrees)
   self.direction = direction
   # carrying food
   self.food = None
   # touching anything
   self.touching = None
   self.sees = None
   # hunger sensor
   self.fruit_hunger = 2000
   self.veggie_hunger = 2000
   self.avg_fruit_hunger = 0
   self.avg_veggie_hunger = 0
   # neural net
   self.net = FeedForwardNetwork()
   self.net.addInputModule(LinearLayer(12, name='in'))
   self.net.addModule(SigmoidLayer(13, name='hidden'))
   self.net.addOutputModule(LinearLayer(6, name='out'))
   self.net.addConnection(FullConnection(self.net['in'], self.net['hidden']))
   self.net.addConnection(FullConnection(self.net['hidden'], self.net['out']))
   self.net.sortModules()
   # thresholds for deciding an action
   self.move_threshold = 0
   self.pickup_threshold = 0
   self.putdown_threshold = 0
   self.eat_threshold = 0
  def __init__(self, input_size, output_size, number_of_layers=3, size_of_hidden_layers=3, type_of_hidden_layer='sigmoid', net_bias=False, epochs=100):
    self.net = FeedForwardNetwork()
    self.num_epochs = epochs
    # set up layers of the network
    layers = []

    for i in range(number_of_layers):
      if i == 0:
        layers.append(LinearLayer(input_size))
        self.net.addInputModule(layers[i])
      elif i == (number_of_layers-1):
        layers.append(LinearLayer(output_size))
        self.net.addOutputModule(layers[i])
        self.net.addConnection(FullConnection(layers[i-1], layers[i]))
      else:
        if type_of_hidden_layer == 'linear':
          layers.append(LinearLayer((input_size + output_size) / 2))
        elif type_of_hidden_layer == 'sigmoid':
          layers.append(SigmoidLayer((input_size + output_size) / 2))
        elif type_of_hidden_layer == 'tanh':
          layers.append(TanhLayer((input_size + output_size) / 2))
        self.net.addModule(layers[i])
        self.net.addConnection(FullConnection(layers[i-1], layers[i]))

    self.net.sortModules()
    self.input_size = input_size
    self.output_size = output_size
def buildNonGravityNet(recurrent=False):
    if recurrent:
        net = RecurrentNetwork()
    else:
        net = FeedForwardNetwork()
    l1 = LinearLayer(2)
    l2 = LinearLayer(3)
    s1 = SigmoidLayer(2)
    l3 = LinearLayer(1)
    net.addInputModule(l1)
    net.addModule(l2)
    net.addModule(s1)
    net.addOutputModule(l3)
    net.addConnection(IdentityConnection(l1, l2, outSliceFrom=1))
    net.addConnection(IdentityConnection(l1, l2, outSliceTo=2))
    net.addConnection(IdentityConnection(l2, l3, inSliceFrom=2))
    net.addConnection(IdentityConnection(l2, l3, inSliceTo=1))
    net.addConnection(IdentityConnection(l1, s1))
    net.addConnection(IdentityConnection(l2, s1, inSliceFrom=1))
    net.addConnection(IdentityConnection(s1, l3, inSliceFrom=1))
    if recurrent:
        net.addRecurrentConnection(IdentityConnection(s1, l1))
        net.addRecurrentConnection(
            IdentityConnection(l2, l2, inSliceFrom=1, outSliceTo=2))
    net.sortModules()
    return net
Beispiel #20
0
    def __init__(self, hidden_layers, data_index_size):

        self.network = FeedForwardNetwork()

        connect_queue = Queue.Queue()
        
        for layer in xrange(0, hidden_layers):
            connect_queue.put(TanhLayer(data_index_size, name = 'hidden_layer_{}'.format(layer)))

        connect_queue.put(SigmoidLayer(1, name = 'output_layer'))

        prev_layer = LinearLayer(data_index_size, name = 'input_layer')
        self.network.addInputModule(prev_layer)
        
        while not connect_queue.empty():
            print 'layer'
            current_layer = connect_queue.get()
            if current_layer.name == 'output_layer':
                self.network.addOutputModule(current_layer)
            else:
                self.network.addModule(current_layer)

            bias = BiasUnit()
            bias_connection = FullConnection(bias, current_layer, name = "bias_to_{}_connection".format(current_layer.name))
            self.network.addModule(bias)
            self.network.addConnection(bias_connection)
            
            connection = FullConnection(prev_layer, current_layer, name = "{}_to_{}_connection".format(prev_layer.name, current_layer.name))
            self.network.addConnection(connection)
            
            prev_layer = current_layer

        print 'sorting....'
        self.network.sortModules()
Beispiel #21
0
	def __init__(self, genes=None):
		self.net = FeedForwardNetwork()

		inLayer = LinearLayer(Brain.G_INPUTNODES, name='input')
		hiddenLayer1 = SigmoidLayer(Brain.G_HIDDENNODES_L1, name='hidden1')
		hiddenLayer2 = SigmoidLayer(Brain.G_HIDDENNODES_L2, name='hidden2')
		outLayer = SigmoidLayer(Brain.G_OUTPUTNODES, name='out')
		bias = BiasUnit(name='bias')

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

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

		self.net.sortModules()

		if genes != None:
			self.import_genes(genes)
Beispiel #22
0
    def __init__(self, grid_size, hidden_list):
        """Sets up the neural network.

        @param grid_size: the size of the grid, for specifying the input layer.
        @param hidden_list: a list containing the number of nodes in each hidden layer.
        """
        self.net = FeedForwardNetwork()

        in_layer = LinearLayer(grid_size*grid_size)
        self.net.addInputModule(in_layer)
        out_layer = LinearLayer(4)
        self.net.addOutputModule(out_layer)

        hidden_layers = []
        for i in hidden_list:
            hidden_layer = SigmoidLayer(i)
            hidden_layers.append(hidden_layer)
            self.net.addModule(hidden_layer)

        self.net.addConnection(FullConnection(in_layer, hidden_layers[0]))
        if len(hidden_layers) > 1:
            for i in range(1, len(hidden_layers) - 1):
                self.net.addConnection(FullConnection(hidden_layers[i], hidden_layers[i+1]))
        self.net.addConnection(FullConnection(hidden_layers[-1], out_layer))

        self.net.sortModules()
	def PrepareModel(self, savedmodel = None):
		
		if savedmodel != None:
			self.trainer = savedmodel
		else:
			attributescount=len(self.traindata[0])
			self.ds = SupervisedDataSet(attributescount, 1)
			for i in range(len(self.traindata)):
				self.ds.appendLinked(self.traindata[i], self.trainlabel[i])
		
			self.net = FeedForwardNetwork()
			inLayer = LinearLayer(len(self.traindata[0]))
			self.net.addInputModule(inLayer)
			hiddenLayers=[]
			for i in range(self.hiddenlayerscount):
				hiddenLayer=SigmoidLayer(self.hiddenlayernodescount)
				hiddenLayers.append(hiddenLayer)
				self.net.addModule(hiddenLayer)
			outLayer = LinearLayer(1)
			self.net.addOutputModule(outLayer)
		
			layers_connections=[]
			layers_connections.append(FullConnection(inLayer, hiddenLayers[0]))
			for i in range(self.hiddenlayerscount-1):
				layers_connections.append(FullConnection(hiddenLayers[i-1], hiddenLayers[i]))
			layers_connections.append(FullConnection(hiddenLayers[-1], outLayer))
		
			for layers_connection in layers_connections:
				self.net.addConnection(layers_connection)
			self.net.sortModules()
			
			#training the self.network
			self.trainer = BackpropTrainer(self.net, self.ds)
			self.trainer.train()
  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
Beispiel #25
0
def build_fnn():
    fnn = FeedForwardNetwork()
    inLayer = LinearLayer(2)
    hiddenLayer = TanhLayer(50)
    outLayer = SoftmaxLayer(2)
    fnn.addInputModule(inLayer)
    fnn.addModule(hiddenLayer)
    fnn.addOutputModule(outLayer)
    return fnn
Beispiel #26
0
def _new_1h_net(window):
    net     = FeedForwardNetwork()
    inl     = SigmoidLayer(window*window*2+1)
    hidden1 = SigmoidLayer(window*window*2)
    outl    = SigmoidLayer(1)
    net.addInputModule(inl)
    net.addModule(hidden1)
    net.addOutputModule(outl)
    c1 = FullConnection(inl, hidden1)
    c2 = FullConnection(hidden1, outl)
    net.addConnection(c1)
    net.addConnection(c2)
    return net
Beispiel #27
0
    def PrepareModel(self, savedmodel=None):

        if savedmodel != None:
            self.trainer = savedmodel
        else:
            attributescount = len(self.traindata[0])
            nrclass = len(set(self.trainlabel))
            self.ds = ClassificationDataSet(attributescount,
                                            target=nrclass,
                                            nb_classes=nrclass,
                                            class_labels=list(
                                                set(self.trainlabel)))

            for i in range(len(self.traindata)):
                self.ds.appendLinked(self.traindata[i], [self.trainlabel[i]])
            self.ds._convertToOneOfMany()

            self.net = FeedForwardNetwork()
            inLayer = LinearLayer(len(self.traindata[0]))
            self.net.addInputModule(inLayer)
            hiddenLayers = []
            for i in range(self.hiddenlayerscount):
                hiddenLayer = SigmoidLayer(self.hiddenlayernodescount)
                hiddenLayers.append(hiddenLayer)
                self.net.addModule(hiddenLayer)
            outLayer = SoftmaxLayer(nrclass)
            self.net.addOutputModule(outLayer)

            layers_connections = []
            layers_connections.append(FullConnection(inLayer, hiddenLayers[0]))
            for i in range(self.hiddenlayerscount - 1):
                layers_connections.append(
                    FullConnection(hiddenLayers[i - 1], hiddenLayers[i]))
            layers_connections.append(
                FullConnection(hiddenLayers[-1], outLayer))

            for layers_connection in layers_connections:
                self.net.addConnection(layers_connection)
            self.net.sortModules()

            #training the network
            self.trainer = BackpropTrainer(self.net, self.ds)
            self.trainer.train()
Beispiel #28
0
 def __init__(self, inputsize, outputsize, hiden=[1]):
     self.inputsize = inputsize
     self.outputsize = outputsize
     self.hiden = hiden
     self.err = 1
     self.old_err = 1
     #print type(self.hiden)
     if type(self.hiden) == str:
         #print "type str"
         self.hiden = self.hiden[1:-1]
         b = self.hiden.split(", ")
         c = []
         for i in b:
             c.append(int(i))
         self.hiden = c[:]
     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)
Beispiel #29
0
    def initNetwork(self):
        #Intiailize Neural Nets
        self.neuralNet = FeedForwardNetwork()

        #Define and add each set of layers
        inLayer = LinearLayer(5)
        hiddenLayer = SigmoidLayer(15)
        outLayer = LinearLayer(5)

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

        #Create conenctions
        in_to_hidden = FullConnection(inLayer, hiddenLayer)
        hidden_to_out = FullConnection(hiddenLayer, outLayer)
        self.neuralNet.addConnection(in_to_hidden)
        self.neuralNet.addConnection(hidden_to_out)

        #Sort the NeuralNet
        self.neuralNet.sortModules()

        #Add supervised data sets
        ds = SupervisedDataSet(NNInitializer.NUMBER_OF_INPUTS,
                               NNInitializer.NUMBER_OF_OUTPUTS)
        inputSet, outputSet = self.loadTrainingSet('scents_based_input',
                                                   'scents_based_output')
        #inputSet, outputSet = self.generateTrainingSet()

        print "Adding samples to data set...."
        for i, val in enumerate(inputSet):
            #	print "Input Set: "
            #	print inputSet[i]
            #	print "Output Set: "
            #	print outputSet[i]
            ds.addSample(inputSet[i], outputSet[i])
        print "Done."

        print "Starting training...."
        #Perform Training
        trainer = BackpropTrainer(self.neuralNet, ds)
        trainer.train()
        print "Done."
 def __init__(self, n_in, n_hidden, n_out):
     self.net = FeedForwardNetwork()
     inLayer = LinearLayer(n_in)
     hiddenLayer1 = SigmoidLayer(n_hidden)
     hiddenLayer2 = SigmoidLayer(n_hidden)
     outLayer = LinearLayer(n_out)
     self.net.addInputModule(inLayer)
     self.net.addModule(hiddenLayer1)
     self.net.addModule(hiddenLayer2)
     self.net.addOutputModule(outLayer)
     in_to_hidden = FullConnection(inLayer, hiddenLayer1)
     hidden_to_out = FullConnection(hiddenLayer2, outLayer)
     hidden_to_hidden = FullConnection(hiddenLayer1, hiddenLayer2)
     self.net.addConnection(in_to_hidden)
     self.net.addConnection(hidden_to_hidden)
     self.net.addConnection(hidden_to_out)
     self.net.sortModules()
     #self.net.params
     self.ds = SupervisedDataSet(n_in, n_out)
Beispiel #31
0
    def __init__(self, hidden_layers, data_index_size):

        self.network = FeedForwardNetwork()

        connect_queue = Queue.Queue()

        for layer in xrange(0, hidden_layers):
            connect_queue.put(
                TanhLayer(data_index_size,
                          name='hidden_layer_{}'.format(layer)))

        connect_queue.put(SigmoidLayer(1, name='output_layer'))

        prev_layer = LinearLayer(data_index_size, name='input_layer')
        self.network.addInputModule(prev_layer)

        while not connect_queue.empty():
            print 'layer'
            current_layer = connect_queue.get()
            if current_layer.name == 'output_layer':
                self.network.addOutputModule(current_layer)
            else:
                self.network.addModule(current_layer)

            bias = BiasUnit()
            bias_connection = FullConnection(
                bias,
                current_layer,
                name="bias_to_{}_connection".format(current_layer.name))
            self.network.addModule(bias)
            self.network.addConnection(bias_connection)

            connection = FullConnection(prev_layer,
                                        current_layer,
                                        name="{}_to_{}_connection".format(
                                            prev_layer.name,
                                            current_layer.name))
            self.network.addConnection(connection)

            prev_layer = current_layer

        print 'sorting....'
        self.network.sortModules()
def simple_network(data, digit, train_ds, test_ds):
#     n = buildNetwork(train_ds.indim, 1, train_ds.outdim, outclass=SoftmaxLayer)
    n = FeedForwardNetwork()
    inLayer = LinearLayer(64)
    outLayer = SoftmaxLayer(10)
    n.addInputModule(inLayer)
    n.addOutputModule(outLayer)
    n.addConnection(FullConnection(inLayer, outLayer))
    n.sortModules()
    trainer = BackpropTrainer(n, dataset=train_ds, momentum=0.1, verbose=True,
                              weightdecay=0.01)
    trainer.trainUntilConvergence(maxEpochs=25)
    result = percentError(trainer.testOnClassData(dataset=test_ds),
                          test_ds['class'])
#     result = validate(trainer, train_ds, 5, 10)
    print 'Simple network - Percent Error', result
    return result
	def __init__(self, num_features, num_hidden_neurons):
		super(NNet,self).__init__(num_features)

		self.ds = SupervisedDataSet(num_features, 1)

		self.net = FeedForwardNetwork()
		self.net.addInputModule(LinearLayer(num_features, name='in'))
		self.net.addModule(LinearLayer(num_hidden_neurons, name='hidden'))
		self.net.addOutputModule(LinearLayer(1, name='out'))
		self.net.addConnection(FullConnection(self.net['in'], self.net['hidden'], name='c1'))
		self.net.addConnection(FullConnection(self.net['hidden'], self.net['out'], name='c2'))
		self.net.sortModules()
Beispiel #34
0
    def train(self):

        n = FeedForwardNetwork()

        dataModel = SongFactory(self.major).getModels()

        ds = SupervisedDataSet(static.NUM_OF_INPUTS, 1)

        #adds samples from the data received from songfactory and the k
        for data in dataModel:
            for input, target in data.model:
                print input, target
                ds.addSample(input, target)


        #instantiate the network
        self.net = FeedForwardNetwork()
        bias = BiasUnit()
        self.net.addModule(bias)

        #create the layers of the network
        inLayer = LinearLayer(static.NUM_OF_INPUTS)
        outLayer = LinearLayer(1)
        hidden1 = SigmoidLayer(25)
        hidden2 = SigmoidLayer(5)

        #add the layers
        self.net.addInputModule(inLayer)
        self.net.addOutputModule(outLayer)
        self.net.addModule(hidden1)
        self.net.addModule(hidden2)

        #create the connection
        in_h1 = FullConnection(inLayer,hidden1)
        h1_h2 = FullConnection(hidden1, hidden2)
        h2_out = FullConnection(hidden2, outLayer)
        b_h1  = FullConnection(bias, hidden1)
        b_h2  = FullConnection(bias, hidden2)

        #add the connection
        self.net.addConnection(in_h1)
        self.net.addConnection(h1_h2)
        self.net.addConnection(h2_out)
        self.net.addConnection(b_h1)
        self.net.addConnection(b_h2)

        self.net.sortModules()

        #trainer to edit the network
        trainer = BackpropTrainer(self.net, ds, learningrate = 0.003)

        trainer.trainEpochs(25)
Beispiel #35
0
    def create_network(self, nFeatures, hidden1Size=20, nClasses=1):
        # create network object
        self.ffn = FeedForwardNetwork()

        # create layer objects
        inLayer = LinearLayer(nFeatures, name="input")
        hiddenLayer = SigmoidLayer(hidden1Size, name="hidden1")
        #hiddenLayer2 = SigmoidLayer(hidden2Size, name="hidden2")
        outLayer = LinearLayer(nClasses, name="output")

        # add layers to feed forward network
        self.ffn.addInputModule(inLayer)
        self.ffn.addModule(hiddenLayer)
        #self.ffn.addModule(hiddenLayer2)
        self.ffn.addOutputModule(outLayer)

        # add bias unit to layers
        self.ffn.addModule(BiasUnit(name='bias'))

        # establish connections between layers
        self.in_to_hidden = FullConnection(inLayer, hiddenLayer)
        #hidden_to_hidden = FullConnection(hiddenLayer, hiddenLayer2)
        self.hidden_to_out = FullConnection(hiddenLayer, outLayer)

        # print "into hidden: {}".format(len(in_to_hidden.params))
        # print "into out: {}".format(len(hidden_to_out.params))

        # add connections to network
        self.ffn.addConnection(self.in_to_hidden)
        #self.ffn.addConnection(hidden_to_hidden)
        self.ffn.addConnection(self.hidden_to_out)

        # necessary, sort layers into correct/certain order
        self.ffn.sortModules()

        # dataset object
        self.train_ds = SupervisedDataSet(nFeatures, nClasses)
        self.validate_ds = SupervisedDataSet(nFeatures, nClasses)
Beispiel #36
0
 def set_network(self, in_count, hidden_counts, out_count):
     assert len(hidden_counts) > 0
     self.in_count = in_count
     self.out_count = out_count
     self.net = FeedForwardNetwork()
     in_layer = LinearLayer(in_count)
     hidden_layers = [SigmoidLayer(count) for count in hidden_counts]
     out_layer = SigmoidLayer(out_count)
     self.net.addInputModule(in_layer)
     for layer in hidden_layers:
         self.net.addModule(layer)
     self.net.addOutputModule(out_layer)
     in_connection = FullConnection(in_layer, hidden_layers[0])
     hidden_connections = [
         FullConnection(layer1, layer2)
         for layer1, layer2 in zip(hidden_layers[0:-1], hidden_layers[1:])
     ]
     out_connection = FullConnection(hidden_layers[-1], out_layer)
     self.net.addConnection(in_connection)
     for connection in hidden_connections:
         self.net.addConnection(connection)
     self.net.addConnection(out_connection)
     self.net.sortModules()
Beispiel #37
0
    def narcolepsy(self, naps, awakenings, obesity):
        parameters = [naps, awakenings, obesity]

        # Init network
        network = FeedForwardNetwork()
        # Init Layers
        inLayer = LinearLayer(3)
        outLayer = LinearLayer(1)
        # Init connection
        in_to_out = FullConnection(inLayer, outLayer)
        # Add modules
        network.addInputModule(inLayer)
        network.addInputModule(outLayer)
        # Add connections
        network.addConnection(in_to_out)
        # Sort
        network.sortModules()
        # Set equal weights
        # TODO: Use learning to learn weights over time
        # in_to_out._setParameters([.1,.1,.1])
        probability = network.activate(parameters)[0]

        return probability
Beispiel #38
0
    def __init__(self, alpha):
        self.name = "ANNApprox"
        self.network = FeedForwardNetwork()
        inLayer = LinearLayer(4)
        hiddenLayer = SigmoidLayer(12)
        outLayer = LinearLayer(1)
        self.network.addInputModule(inLayer)
        self.network.addModule(hiddenLayer)
        self.network.addOutputModule(outLayer)
        in_to_hidden = FullConnection(inLayer, hiddenLayer)
        hidden_to_out = FullConnection(hiddenLayer, outLayer)
        self.network.addConnection(in_to_hidden)
        self.network.addConnection(hidden_to_out)

        # Last step to make sure everything works in the connections
        self.network.sortModules()

        self.dataset = SupervisedDataSet(4, 1)
        self.trainer = BackpropTrainer(self.network,
                                       self.dataset,
                                       learningrate=alpha,
                                       momentum=0.0,
                                       verbose=True)
Beispiel #39
0
    def insomnia(self, falling_asleep, awakenings, cant_fall_back, low_sleep_hours):
        parameters = [falling_asleep, waking_up, cant_fall_back, low_sleep_hours]

        # Init network
        network = FeedForwardNetwork()
        # Init Layers
        inLayer = LinearLayer(4)
        outLayer = LinearLayer(1)
        # Init connection
        in_to_out = FullConnection(inLayer, outLayer)
        # Add modules
        network.addInputModule(inLayer)
        network.addInputModule(outLayer)
        # Add connections
        network.addConnection(in_to_out)
        # Sort
        network.sortModules()
        # Set equal weights
        # TODO: Use learning to learn weights over time
        # in_to_out._setParameters([.1,.1,.1,.1])
        probability = network.activate(parameters)[0]

        return probability
Beispiel #40
0
    def __init__(self, input_path, output_path):
        self.n = FeedForwardNetwork()
        self.pix_size = 50
        self.input_value = utils.getImages(utils.readLines(input_path))
        self.output_value = utils.readLines(output_path)
        self.inputUnits = self.pix_size * self.pix_size
        self.nbHiddenLayers = 1
        self.hiddenUnits = 500
        self.outputUnits = len(results)
        self.ds = SupervisedDataSet(
            self.pix_size * self.pix_size, len(results))

        self.initializeDataSet()
        self.initilizeNetwork()
        self.trainingOnDataSet()
Beispiel #41
0
    def PrepareModel(self, savedmodel=None):

        if savedmodel != None:
            self.trainer = savedmodel
        else:
            attributescount = len(self.traindata[0])
            self.ds = SupervisedDataSet(attributescount, 1)
            for i in range(len(self.traindata)):
                self.ds.appendLinked(self.traindata[i], self.trainlabel[i])

            self.net = FeedForwardNetwork()
            inLayer = LinearLayer(len(self.traindata[0]))
            self.net.addInputModule(inLayer)
            hiddenLayers = []
            for i in range(self.hiddenlayerscount):
                hiddenLayer = SigmoidLayer(self.hiddenlayernodescount)
                hiddenLayers.append(hiddenLayer)
                self.net.addModule(hiddenLayer)
            outLayer = LinearLayer(1)
            self.net.addOutputModule(outLayer)

            layers_connections = []
            layers_connections.append(FullConnection(inLayer, hiddenLayers[0]))
            for i in range(self.hiddenlayerscount - 1):
                layers_connections.append(
                    FullConnection(hiddenLayers[i - 1], hiddenLayers[i]))
            layers_connections.append(
                FullConnection(hiddenLayers[-1], outLayer))

            for layers_connection in layers_connections:
                self.net.addConnection(layers_connection)
            self.net.sortModules()

            #training the self.network
            self.trainer = BackpropTrainer(self.net, self.ds)
            self.trainer.train()
Beispiel #42
0
    def fit(self, X, y):
        self.n = FeedForwardNetwork()

        self.n.addInputModule(SigmoidLayer(self.inp_neu, name='in'))
        self.n.addModule(SigmoidLayer(self.hid_neu, name='hidden'))
        self.n.addOutputModule(LinearLayer(self.out_neu, name='out'))
        self.n.addConnection(FullConnection(self.n['in'], self.n['hidden'], name='c1'))
        self.n.addConnection(FullConnection(self.n['hidden'], self.n['out'], name='c2'))

        self.n.sortModules() #initialisation

        self.tstdata, trndata = self.data(X,y).splitWithProportion(self.split_prop)

        trainer = BackpropTrainer(self.n, trndata, learningrate=self.learn_rate, momentum=self.nomentum, weightdecay=self.weight_dec)
        trainer.trainUntilConvergence(verbose=True, maxEpochs=self.epochs)

        return self
Beispiel #43
0
	def __init__(self,layer_type):
		self.inputLayer = LinearLayer(2)
		self.hiddenLayer = layer_type(10)
		self.outputLayer = layer_type(2)

		self.net = FeedForwardNetwork()
		self.net.addInputModule(self.inputLayer)
		self.net.addModule(self.hiddenLayer)
		self.net.addOutputModule(self.outputLayer)

		self.inputToHidden = FullConnection(self.inputLayer,self.hiddenLayer)
		self.hiddenToOutput = FullConnection(self.hiddenLayer,self.outputLayer)

		self.net.addConnection(self.inputToHidden)
		self.net.addConnection(self.hiddenToOutput)

		self.net.sortModules()
    def construct(self, sensor_states, behaviors):

        input_len = len(sensor_states[0])
        state_len = input_len + len(self._behavior_to_list(''))

        # Initialize the network
        self.net = FeedForwardNetwork()

        input_layer = SigmoidLayer(state_len)
        hidden_layer = SigmoidLayer( int(state_len * 1.5) )
        output_layer = SigmoidLayer(input_len)

        input_to_hidden = FullConnection(input_layer, hidden_layer)
        hidden_to_output = FullConnection(hidden_layer, output_layer)

        self.net.addInputModule( input_layer )
        self.net.addModule( hidden_layer )
        self.net.addOutputModule( output_layer )

        self.net.addConnection(input_to_hidden)
        self.net.addConnection(hidden_to_output)

        self.net.sortModules()

        # Build the data set
        ds = SupervisedDataSet(state_len, input_len)

        previous_state = sensor_states[0]
        for i in range(1, len(sensor_states) - 1):
            behavior = behaviors[i-1]
            current_state = sensor_states[i]

            a = tuple( previous_state + self._behavior_to_list(behavior) )
            b = tuple( current_state )

            ds.addSample(a, b)

        # Train the network
        trainer = BackpropTrainer(self.net, ds, learningrate=0.3)
        for i in range(1000):
            t1 = datetime.datetime.now()
            err = trainer.train()
            t2 = datetime.datetime.now()
            print '%d: %f (%s)' % (i, err, t2 - t1)
            if self.training_callback is not None:
                self.training_callback(i, self)
Beispiel #45
0
    def createJeffersonStyleNetwork(in_count=2,
                                    hidden_count=5,
                                    output_count=4,
                                    recurrent=True,
                                    in_to_out_connect=True,
                                    name=None):
        """
        Creates a Jefferson-esque neural network for trail problem.


        Returns:
            pybrain.network. The neural network.

        """

        if recurrent:
            ret_net = RecurrentNetwork(name=name)
        else:
            ret_net = FeedForwardNetwork(name=name)

        in_layer = LinearLayer(in_count, name="food")
        hidden_layer = SigmoidLayer(hidden_count, name="hidden")
        output_layer = LinearLayer(output_count, name="move")

        ret_net.addInputModule(in_layer)
        ret_net.addModule(hidden_layer)
        ret_net.addOutputModule(output_layer)

        in_to_hidden = FullConnection(in_layer, hidden_layer)
        hidden_to_out = FullConnection(hidden_layer, output_layer)

        ret_net.addConnection(in_to_hidden)
        ret_net.addConnection(hidden_to_out)

        if in_to_out_connect:
            in_to_out = FullConnection(in_layer, output_layer)
            ret_net.addConnection(in_to_out)

        if recurrent:
            hidden_to_hidden = FullConnection(hidden_layer, hidden_layer)
            ret_net.addRecurrentConnection(hidden_to_hidden)

        ret_net.sortModules()

        return ret_net
 def __init__(self,n_in,n_hidden,n_out):
     self.net = FeedForwardNetwork()
     inLayer = LinearLayer(n_in)
     hiddenLayer1 = SigmoidLayer(n_hidden) 
     hiddenLayer2 = SigmoidLayer(n_hidden) 
     outLayer = LinearLayer(n_out)
     self.net.addInputModule(inLayer)
     self.net.addModule(hiddenLayer1)
     self.net.addModule(hiddenLayer2)
     self.net.addOutputModule(outLayer)
     in_to_hidden = FullConnection(inLayer, hiddenLayer1)
     hidden_to_out = FullConnection(hiddenLayer2, outLayer)
     hidden_to_hidden = FullConnection(hiddenLayer1, hiddenLayer2)
     self.net.addConnection(in_to_hidden)
     self.net.addConnection(hidden_to_hidden)
     self.net.addConnection(hidden_to_out)
     self.net.sortModules()
     #self.net.params
     self.ds = SupervisedDataSet(n_in, n_out)
Beispiel #47
0
    def __init__(self, datadir, insize=None, outsize=None, paramfile=None):
        self.datadir = datadir
        if insize == None:
            g = runner.Game()
            ip = self._game2input(g)
            self.insize = len(ip)
        else:
            self.insize = insize
        if outsize == None:
            self.outsize = 1
        else:
            self.outsize = outsize
        if paramfile:
            f = os.path.join(self.datadir, paramfile)
            self.nn = NetworkReader.readFrom(f)
            try:
                self.name = re.search("(.*)-bestof-(.*)", paramfile).group(1)
            except AttributeError:
                self.name = "blondie-%s" % (datetime.datetime.now())
        else:
            self.nn = FeedForwardNetwork()
            tmpname = "blondie-%s" % (datetime.datetime.now())
            self.name = re.sub("[.: ]", "-", tmpname)

            inLayer = LinearLayer(self.insize)
            hiddenLayer1 = SigmoidLayer(self.insize)
            hiddenLayer2 = SigmoidLayer(self.insize)
            outLayer = LinearLayer(self.outsize)

            self.nn.addInputModule(inLayer)
            self.nn.addModule(hiddenLayer1)
            self.nn.addModule(hiddenLayer2)
            self.nn.addOutputModule(outLayer)

            in_to_hidden1 = FullConnection(inLayer, hiddenLayer1)
            hidden1_to_hidden2 = FullConnection(hiddenLayer1, hiddenLayer2)
            hidden2_to_out = FullConnection(hiddenLayer2, outLayer)

            self.nn.addConnection(in_to_hidden1)
            self.nn.addConnection(hidden1_to_hidden2)
            self.nn.addConnection(hidden2_to_out)

            self.nn.sortModules()
	def initNetwork(self):
		#Intiailize Neural Nets
		self.neuralNet = FeedForwardNetwork()

		#Define and add each set of layers
		inLayer = LinearLayer(5)
		hiddenLayer = SigmoidLayer(15)
		outLayer = LinearLayer(5)

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

		#Create conenctions
		in_to_hidden = FullConnection(inLayer, hiddenLayer)
		hidden_to_out = FullConnection(hiddenLayer, outLayer)
		self.neuralNet.addConnection(in_to_hidden)
		self.neuralNet.addConnection(hidden_to_out)

		#Sort the NeuralNet
		self.neuralNet.sortModules()
		
		#Add supervised data sets
		ds = SupervisedDataSet(NNInitializer.NUMBER_OF_INPUTS, NNInitializer.NUMBER_OF_OUTPUTS)
		inputSet, outputSet = self.loadTrainingSet('scents_based_input',
													'scents_based_output')
		#inputSet, outputSet = self.generateTrainingSet()

		print "Adding samples to data set...."
		for i,val  in enumerate(inputSet):
		#	print "Input Set: "
		#	print inputSet[i]
		#	print "Output Set: "
		#	print outputSet[i]
			ds.addSample(inputSet[i], outputSet[i])
		print "Done."

		print "Starting training...."
		#Perform Training
		trainer = BackpropTrainer(self.neuralNet, ds)
		trainer.train()
		print "Done."
Beispiel #49
0
    def init_network(self, inputs, targets):
        """ Build the network as a full-connected feed-forward net.
        Uses the softmax function for the output layer as recommended for classification"""

        self.n = FeedForwardNetwork()

        inLayer = LinearLayer(inputs, name='in' )
        hiddenLayer = SigmoidLayer(self.hidden_layer, name='hidden')
        outLayer = SoftmaxLayer(targets, name='out')
        
        self.n.addInputModule(inLayer)
        self.n.addModule(hiddenLayer)
        self.n.addOutputModule(outLayer)
        
        in_to_hidden = FullConnection(inLayer, hiddenLayer)
        hidden_to_out = FullConnection(hiddenLayer, outLayer)
        
        self.n.addConnection(in_to_hidden)
        self.n.addConnection(hidden_to_out)
        
        self.n.sortModules()
    def __init__(self):
        super(NeuralNetworkPlayer, self).__init__()

        # Create the network
        self.net = FeedForwardNetwork()

        # Internal Layers
        inLayer = LinearLayer(5)
        hiddenLayer1 = SigmoidLayer(6)
        hiddenLayer2 = SigmoidLayer(6)
        outLayer = LinearLayer(7)

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

        self.net.addConnection(FullConnection(inLayer, hiddenLayer1))
        self.net.addConnection(FullConnection(hiddenLayer1, hiddenLayer2))
        self.net.addConnection(FullConnection(hiddenLayer2, outLayer))

        self.net.sortModules()
Beispiel #51
0
    def __init__(self, hidden_layers, ally_champ_obj_list, enemy_champ_obj_list):

        self.ally_champ_obj_list = ally_champ_obj_list
        self.enemy_champ_obj_list = enemy_champ_obj_list

        self.set_nodes()

        self.network = FeedForwardNetwork()

        connect_queue = Queue.Queue()
        
        for layer in xrange(0, hidden_layers):
            connect_queue.put(TanhLayer(self.input_node_count, name = 'hidden_layer_{}'.format(layer)))

        connect_queue.put(SigmoidLayer(1, name = 'output_layer'))

        prev_layer = LinearLayer(self.input_node_count, name = 'input_layer')
        self.network.addInputModule(prev_layer)
        
        while not connect_queue.empty():
            
            current_layer = connect_queue.get()
            if current_layer.name == 'output_layer':
                self.network.addOutputModule(current_layer)
            else:
                self.network.addModule(current_layer)

            bias = BiasUnit()
            bias_connection = FullConnection(bias, current_layer, name = "bias_to_{}_connection".format(current_layer.name))
            self.network.addModule(bias)
            self.network.addConnection(bias_connection)
            
            connection = FullConnection(prev_layer, current_layer, name = "{}_to_{}_connection".format(prev_layer.name, current_layer.name))
            self.network.addConnection(connection)
            
            prev_layer = current_layer

        self.network.sortModules()
	def PrepareModel(self, savedmodel = None):
		
		if savedmodel != None:
			self.trainer = savedmodel
		else:
			attributescount=len(self.traindata[0])
			nrclass = len(set(self.trainlabel))
			self.ds = ClassificationDataSet(attributescount, target=nrclass, nb_classes=nrclass, class_labels=list(set(self.trainlabel)))
				
			for i in range(len(self.traindata)):
				self.ds.appendLinked(self.traindata[i], [self.trainlabel[i]])
			self.ds._convertToOneOfMany()
	
			self.net = FeedForwardNetwork()
			inLayer = LinearLayer(len(self.traindata[0]))
			self.net.addInputModule(inLayer)
			hiddenLayers=[]
			for i in range(self.hiddenlayerscount):
				hiddenLayer=SigmoidLayer(self.hiddenlayernodescount)
				hiddenLayers.append(hiddenLayer)
				self.net.addModule(hiddenLayer)
			outLayer = SoftmaxLayer(nrclass)
			self.net.addOutputModule(outLayer)
			
			layers_connections=[]
			layers_connections.append(FullConnection(inLayer, hiddenLayers[0]))
			for i in range(self.hiddenlayerscount-1):
				layers_connections.append(FullConnection(hiddenLayers[i-1], hiddenLayers[i]))
			layers_connections.append(FullConnection(hiddenLayers[-1], outLayer))
		
			for layers_connection in layers_connections:
				self.net.addConnection(layers_connection)
			self.net.sortModules()
			
			#training the network
			self.trainer = BackpropTrainer(self.net, self.ds)
			self.trainer.train()
Beispiel #53
0
class NeuralNet:
  def __init__(self, hidden_neuron_num=1, hidden_type='sigmoid'):
    self.hidden_neuron_num = hidden_neuron_num
    self.hidden_type = hidden_type

    self.net = FeedForwardNetwork()
    self.samples = SupervisedDataSet(784, 784)

    self.vectorizer = ImageVectorizer()

    self.add_layers()
    self.add_connections()
    self.sort()

  def add_layers(self):
    self.inLayer = LinearLayer(784, name='in')
    self.outLayer = LinearLayer(784, name='out')

    if self.hidden_type == 'sigmoid':
      self.hiddenLayer = SigmoidLayer(self.hidden_neuron_num, name='hidden')
    else:
      # I found I had to overwrite the output layer to sigmoid to get the 
      # hidden layer to work as linear
      self.hiddenLayer = LinearLayer(self.hidden_neuron_num, name='hidden')
      self.outLayer = SigmoidLayer(784, name='out')

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

  def add_connections(self):
    self.in_to_hidden = FullConnection(self.inLayer, self.hiddenLayer)
    self.hidden_to_out = FullConnection(self.hiddenLayer, self.outLayer)

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

  def sort(self):
    self.net.sortModules()

  def activate(self, vector):
    return self.net.activate(vector)

  def train(self, paths):
    for path in paths:
      vector = self.vectorizer.image_to_vector(path) 
      vector = numpy.float64([el / 255.0 for el in vector])
      self.samples.addSample(vector, vector)

    trainer = BackpropTrainer(self.net, self.samples, learningrate=.5, lrdecay=0.98)
    for i in range(1,20):
      error = trainer.train()
      print "error for %(i)ith iteration: %(error)f" % locals()

  def input_weights_of_hidden_layer(self):
    weights = self.in_to_hidden.params
    hidden_weights_by_neuron = numpy.split(weights, self.hidden_neuron_num)
    return hidden_weights_by_neuron

  def input_weights_of_out_layer(self):
    weights = self.hidden_to_out.params
    hidden_weights_by_neuron = numpy.split(weights, self.hidden_neuron_num)
    return hidden_weights_by_neuron
from pybrain.structure import FeedForwardNetwork
from pybrain.structure import LinearLayer, SigmoidLayer, SoftmaxLayer
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.structure import FullConnection
from pybrain.datasets import SupervisedDataSet
import numpy

size_x = 20
hidden = 10
net = FeedForwardNetwork()
inLayer = LinearLayer(size_x * size_x)
hiddenLayer = SigmoidLayer(hidden)
outLayer = LinearLayer(size_x * size_x)
net.addInputModule(inLayer)
net.addModule(hiddenLayer)
net.addOutputModule(outLayer)
in_to_hidden = FullConnection(inLayer, hiddenLayer)
hidden_to_out = FullConnection(hiddenLayer, outLayer)
net.addConnection(in_to_hidden)
net.addConnection(hidden_to_out)
net.sortModules()

print "Adding Samples"
ds = SupervisedDataSet(size_x * size_x, size_x * size_x)
for i in range(1000):
    data = numpy.random.randn(size_x * size_x)
    ds.addSample(data, data)

print "Training"
trainer = BackpropTrainer(net, ds)
for i in range(100):
		noise30Data = np.load('dataset30noise.npy')[0:2000]
		labels      = np.load('datalabels.npy')[0:2000]
		testSet     = np.load('testdata.npy')[0:5000]
		test5noise  = np.load('test5noise.npy')[0:5000]
		test10noise = np.load('test10noise.npy')[0:5000]
		test15noise = np.load('test15noise.npy')[0:5000]
		test20noise = np.load('test20noise.npy')[0:5000]
		test25noise = np.load('test25noise.npy')[0:5000]
		test30noise = np.load('test30noise.npy')[0:5000]
		testLabels  = np.load('testlabels.npy')[0:5000]
		print "data loaded"

	
	inputComponents  = np.shape(dataSet)[1]
	outputComponents = 10 
	network = FeedForwardNetwork()
	
	inputLayer  = LinearLayer(inputComponents,name='input')
	hiddenLayer = SigmoidLayer(inputComponents, name='hidden')
	outputLayer = SigmoidLayer(outputComponents,name='out')

	data = ClassificationDataSet(inputComponents, 1, nb_classes = 10)
	in_hidden = FullConnection(inputLayer, hiddenLayer)
	hidden_out = FullConnection(hiddenLayer, outputLayer)

	network.addInputModule(inputLayer)
	network.addModule(hiddenLayer)
	network.addOutputModule(outputLayer)

	network.addConnection(in_hidden)
	network.addConnection(hidden_out)
import math
import numpy as np
import scipy as sp
from scipy import ndimage, misc
import random
import skimage
from skimage import data, filter, io
import matplotlib.pyplot as plt

from pybrain.tools.shortcuts import buildNetwork
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.structure import FeedForwardNetwork, LinearLayer, SigmoidLayer, FullConnection

net = FeedForwardNetwork()
inLayer = LinearLayer(4)
hiddenLayer = SigmoidLayer(2)
outLayer = LinearLayer(4)

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

in_to_hidden = FullConnection(inLayer, hiddenLayer)
hidden_to_out = FullConnection(hiddenLayer, outLayer)

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

net.sortModules()
Beispiel #57
0
#!/usr/bin/env python
from pybrain.structure import FeedForwardNetwork
from pybrain.structure import LinearLayer, SigmoidLayer
from pybrain.structure import FullConnection


#Se construye la red
n = FeedForwardNetwork()
#Se construyen las capas
inLayer = LinearLayer(2, name="input")
hiddenLayer = SigmoidLayer(3, name="hid")
outLayer = LinearLayer(1, name="output")

#Se agregan las capas a la red
n.addInputModule(inLayer)
n.addModule(hiddenLayer)
n.addOutputModule(outLayer)

#Se conectan las capas
in_to_hidden = FullConnection(inLayer, hiddenLayer, name="con1")
hidden_to_out = FullConnection(hiddenLayer, outLayer, name="con2")

#Se agregan las conexiones
n.addConnection(in_to_hidden)
n.addConnection(hidden_to_out)

#All the elements are in place now, so we can do the final step that makes our MLP usable, which is to call the .sortModules() method:
n.sortModules()

print n
print n.activate([3,7])