Beispiel #1
0
def printKT(Q, i):
    kt.exporttiles(array=Q,
                   height=1,
                   width=len(Q[0]),
                   outer_height=1,
                   outer_width=len(Q),
                   filename="results/obs_W_1_" + i + ".pgm")
Beispiel #2
0
 def __init__(self):
     self.values, self.height, self.width = KT.importimage ("bird_chirp.pnm")
     print "values' shape=", numpy.shape(self.values), " height=", self.height, " width=", self.width
     # values have shape 129*182; now bring them into shape (129,182)
     self.values = numpy.reshape(self.values, (self.height,self.width))
     print "values' shape=", numpy.shape(self.values)
     KT.exporttiles (self.values, self.height, self.width, "/tmp/coco/obs_I_0.pgm") # display
     # transpose this data matrix, so one data point is values[.]
     self.values = numpy.transpose(self.values)
     KT.exporttiles (self.values[0], self.height, 1, "/tmp/coco/obs_J_0.pgm")
     self.t = 0
Beispiel #3
0
 def visualisation(self):
     KT.exporttiles(self.weights11[:, 0:-1], self.nhidden, self.nin,
                    basedir + "obs_V_1_0.pgm")
     KT.exporttiles(self.weights12[:, 0:-1], self.nhidden, self.nin,
                    basedir + "obs_W_1_0.pgm")
     KT.exporttiles(self.weights2[:, 0:-1], self.nhidden, self.nout,
                    basedir + "obs_V_2_1.pgm")
     KT.exporttiles(self.weights4[:, 0:-1], self.nhidden, self.ncritic,
                    basedir + "obs_W_2_1.pgm")
     print 'visualisation updated!!'
Beispiel #4
0
 def __init__(self):
     digitsalph = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     self.values = numpy.zeros((len(digitsalph), 8*8))
     for tt in range(len(digitsalph)):
         filename = "digits_alph/digit" + digitsalph[tt] + ".pgm"
         self.values[tt], h, w = KT.importimage (filename)
         if  h != 8 or w != 8:
             print "digits_alph files don't match expected size!"
     self.datumsize = h*w
     self.seqlen = len(digitsalph)
Beispiel #5
0
    def visualize(self):
        # print(self.weights)
        KTimage.exporttiles(
            self.weights[0],
            self.width, self.height,
            "/tmp/coco/obs_W_{}_{}.pgm".format(0 + 1, 0),
            self.width, self.height)

        KTimage.exporttiles(
            self.weights[1].T,
            self.width, self.height,
            "/tmp/coco/obs_W_{}_{}.pgm".format(1 + 1, 1),
            self.width, self.height)

        KTimage.exporttiles(self.inputs[0], self.width, self.height,
                            "/tmp/coco/obs_S_0.pgm")
        KTimage.exporttiles(self.inputs[1], self.width, self.height,
                            "/tmp/coco/obs_S_1.pgm")
        KTimage.exporttiles(self.outputs[-1], self.width, self.height,
                            "/tmp/coco/obs_S_2.pgm")
Beispiel #6
0
    def train(self, iteration):
        gamma = 0.9
        """ Train the thing """
        # Add the inputs that match the bias node
        self.maxIteration = iteration

        self.error_action_sav = numpy.zeros((2, self.maxIteration / 10))
        self.error_val_sav = numpy.zeros((self.maxIteration / 10))
        self.error_total_action_sav = numpy.zeros((2, self.maxIteration / 10))
        self.error_total_val_sav = numpy.zeros((self.maxIteration / 10))
        self.average_duration_sav = numpy.zeros((self.maxIteration))

        for iteration in range(0, self.maxIteration):

            error = 0.0
            error_val = 0.0
            total_duration = 0

            updateTimes = 0

            self.visualisation()

            for StartPos in range(0, self.__perception.StartPosCount**2):
                self.__perception.newinit()

                reward = 0.0
                duration = 0

                self.hidden = numpy.zeros((self.nhidden))

                landmark_info = numpy.array(
                    self.__perception.sense())  # 2 continuous inputs

                inputs_bias = numpy.concatenate(
                    (landmark_info, -1 * numpy.ones((1))), axis=0)

                # ------- sigmoidal funcion for hidden layer 1 -------
                self.hidden1_y = dot(self.weights11, inputs_bias)
                self.hidden1 = 1.0 / (1.0 +
                                      numpy.exp(-self.beta1 * self.hidden1_y))
                self.hiddenplusone1 = numpy.concatenate(
                    (self.hidden1, -1 * numpy.ones(
                        (1))), axis=0)  # add a bias unit
                # ------- end of sigmoidal funcion for hidden layer 1 -------
                h_out = self.mlpfwd()
                h = self.normalised(h_out)

                h_angle = math.atan2(h[1], h[0])
                action_angle = self.__perception.rand_winner(
                    h_angle, self.sigma)

                action = numpy.zeros((2))
                action[0] = math.cos(action_angle)
                action[1] = math.sin(action_angle)
                action = self.normalised(action)

                # ------- sigmoidal funcion for hidden layer 2 -------
                self.hidden2_y = dot(self.weights12, inputs_bias)
                self.hidden2 = 1.0 / (1.0 +
                                      numpy.exp(-self.beta * self.hidden2_y))
                self.hiddenplusone2 = numpy.concatenate(
                    (self.hidden2, -1 * numpy.ones(
                        (1))), axis=0)  # add a bias unit
                # ------- end of sigmoidal funcion for hidden layer 2 -------
                val = self.valfwd()

                r = self.__perception.reward()  # read reward

                while (r != 1.0) and duration < 1000:

                    duration += 1
                    total_duration += 1
                    updatew11 = numpy.zeros((numpy.shape(self.weights11)))
                    updatew12 = numpy.zeros((numpy.shape(self.weights12)))
                    updatew2 = numpy.zeros((numpy.shape(self.weights2)))
                    updatew4 = numpy.zeros((numpy.shape(self.weights4)))
                    self.__perception.act(action)
                    landmark_info_tic = numpy.array(
                        self.__perception.sense())  # 2 continuous inputs
                    r = self.__perception.reward()  # read reward
                    inputs_bias_tic = numpy.concatenate(
                        (landmark_info_tic, -1 * numpy.ones((1))), axis=0)

                    KT.exporttiles(inputs_bias_tic, self.nin + 1, 1,
                                   basedir + "obs_S_0.pgm")

                    # ------- sigmoidal funcion for hidden layer 1 -------
                    self.hidden1_y = dot(self.weights11, inputs_bias_tic)
                    self.hidden1 = 1.0 / (
                        1.0 + numpy.exp(-self.beta1 * self.hidden1_y))
                    self.hiddenplusone1 = numpy.concatenate(
                        (self.hidden1, -1 * numpy.ones(
                            (1))), axis=0)  # add a bias unit
                    # ------- end of sigmoidal funcion for hidden layer 1 -------

                    h_out = self.mlpfwd()
                    h = self.normalised(h_out)

                    h_angle = math.atan2(h[1], h[0])
                    action_tic_angle = self.__perception.rand_winner(
                        h_angle, self.sigma)

                    action_tic = numpy.zeros((2))
                    action_tic[0] = math.cos(action_tic_angle)
                    action_tic[1] = math.sin(action_tic_angle)
                    action_tic = self.normalised(action_tic)

                    # ------- sigmoidal funcion for hidden layer 2 -------
                    self.hidden2_y = dot(self.weights12, inputs_bias_tic)
                    self.hidden2 = 1.0 / (
                        1.0 + numpy.exp(-self.beta * self.hidden2_y))
                    self.hiddenplusone2 = numpy.concatenate(
                        (self.hidden2, -1 * numpy.ones(
                            (1))), axis=0)  # add a bias unit
                    # ------- end of sigmoidal funcion for hidden layer 2 -------

                    if self.__perception.sel_x > 0.1 and self.__perception.sel_y > 0.1 and self.__perception.sel_x < 0.3 and self.__perception.sel_y < 0.3:
                        KT.exporttiles(self.hidden1, 1, self.nhidden,
                                       basedir + "obs_S_1.pgm")
                        KT.exporttiles(self.hidden2, 1, self.nhidden,
                                       basedir + "obs_S_2.pgm")

                    if self.__perception.sel_x > 0.6 and self.__perception.sel_y > 0.6 and self.__perception.sel_x < 0.7 and self.__perception.sel_y < 0.7:
                        KT.exporttiles(self.hidden1, 1, self.nhidden,
                                       basedir + "obs_A_1.pgm")
                        KT.exporttiles(self.hidden2, 1, self.nhidden,
                                       basedir + "obs_A_2.pgm")

                    val_tic = self.valfwd()

                    # ----- here are the training process--------#

                    if r == 1.0:  # reward achieved
                        target = r
                    else:  # because critic weights now converge.
                        target = gamma * val_tic  # gamma = 0.9
                        # prediction error;
                    deltao = (target - val)

                    error_val += abs(deltao)

                    updatew4 = self.eps * (outer(deltao, self.hiddenplusone2))
                    deltah0 = self.hiddenplusone2 * (
                        1 - self.hiddenplusone2) * (dot(
                            transpose(self.weights4), deltao))
                    updatew12 = self.eta * (outer(deltah0[:-1],
                                                  inputs_bias_tic))

                    self.weights12 += updatew12
                    self.weights4 += updatew4

                    if gamma * val_tic > val or r == 1.0:
                        updateTimes += 1
                        error += abs(action - h)
                        deltao2 = (action - h) / numpy.linalg.norm(action - h)
                        deltah0 = self.hiddenplusone1 * (
                            1 - self.hiddenplusone1) * dot(
                                transpose(self.weights2), deltao2)
                        updatew11 = self.eta * (outer(deltah0[:-1],
                                                      inputs_bias_tic))

                        self.weights11 += updatew11
                        updatew2 = self.eta * (outer(deltao2,
                                                     self.hiddenplusone1))
                        self.weights2 += updatew2

                    ##-------------end update when the critic are higher-----------##

                    landmark_info = landmark_info_tic

                    action = action_tic
                    val = val_tic

            if (iteration % 1 == 0):
                print "iteration:", iteration
                print "Error in val:", error_val, "average per move:", error_val / float(
                    total_duration + 1)
                print "Error in action:", error, "average per move:", error / float(
                    updateTimes + 1)
                print "Total duration:", total_duration
                print "Average duration", total_duration / (
                    self.__perception.StartPosCount**2)
                print "Update Times:", updateTimes

            self.average_duration_sav[iteration] = total_duration / (
                self.__perception.StartPosCount**2)
 def visualisation(self):
     KT.exporttiles(self.weights11[:,0:-1], self.nhidden, self.nin, basedir+"obs_V_1_0.pgm")
     KT.exporttiles(self.weights12[:,0:-1], self.nhidden, self.nin, basedir+"obs_W_1_0.pgm")
     KT.exporttiles(self.weights2[:,0:-1], self.nhidden, self.nout, basedir+"obs_V_2_1.pgm")
     KT.exporttiles(self.weights4[:,0:-1], self.nhidden, self.ncritic, basedir+"obs_W_2_1.pgm")
     print 'visualisation updated!!'
    def train(self,iteration):
        gamma = 0.9
        
        """ Train the thing """    
        # Add the inputs that match the bias node
        self.maxIteration = iteration
        
        
        self.error_action_sav = numpy.zeros((2,self.maxIteration/10))
        self.error_val_sav = numpy.zeros((self.maxIteration/10))
        self.error_total_action_sav = numpy.zeros((2,self.maxIteration/10))
        self.error_total_val_sav = numpy.zeros((self.maxIteration/10))
        self.average_duration_sav = numpy.zeros((self.maxIteration))
        
        for iteration in range(0,self.maxIteration):

            error = 0.0
            error_val = 0.0
            total_duration = 0
            
            updateTimes = 0

            self.visualisation()
            
            for StartPos in range(0, self.__perception.StartPosCount**2):
                self.__perception.newinit()
                
                reward = 0.0
                duration = 0
            
                self.hidden = numpy.zeros((self.nhidden))
            
                landmark_info = numpy.array(self.__perception.sense()) # 2 continuous inputs

            
                inputs_bias = numpy.concatenate((landmark_info,-1*numpy.ones((1))),axis=0)
                
                # ------- sigmoidal funcion for hidden layer 1 -------
                self.hidden1_y = dot(self.weights11,inputs_bias)
                self.hidden1 = 1.0 / (1.0 + numpy.exp(-self.beta1*self.hidden1_y))
                self.hiddenplusone1 = numpy.concatenate((self.hidden1,-1*numpy.ones((1))),axis=0) # add a bias unit
                # ------- end of sigmoidal funcion for hidden layer 1 -------
                h_out = self.mlpfwd()
                h = self.normalised(h_out)

                h_angle = math.atan2(h[1], h[0])
                action_angle = self.__perception.rand_winner(h_angle,self.sigma)

                action = numpy.zeros((2))
                action[0] = math.cos(action_angle)
                action[1] = math.sin(action_angle)
                action = self.normalised(action)

                # ------- sigmoidal funcion for hidden layer 2 -------
                self.hidden2_y = dot(self.weights12,inputs_bias)
                self.hidden2 = 1.0 / (1.0 + numpy.exp(-self.beta*self.hidden2_y))
                self.hiddenplusone2 = numpy.concatenate((self.hidden2,-1*numpy.ones((1))),axis=0) # add a bias unit
                # ------- end of sigmoidal funcion for hidden layer 2 -------
                val = self.valfwd()
                
                r = self.__perception.reward()   # read reward

                
                
                while (r != 1.0) and duration < 1000:
                
                    duration += 1
                    total_duration += 1
                    updatew11 = numpy.zeros((numpy.shape(self.weights11)))
                    updatew12 = numpy.zeros((numpy.shape(self.weights12)))
                    updatew2 = numpy.zeros((numpy.shape(self.weights2)))
                    updatew4 = numpy.zeros((numpy.shape(self.weights4)))
                    self.__perception.act(action)
                    landmark_info_tic = numpy.array(self.__perception.sense()) # 2 continuous inputs
                    r = self.__perception.reward()   # read reward
                    inputs_bias_tic = numpy.concatenate((landmark_info_tic,-1*numpy.ones((1))),axis=0)
                    
                    KT.exporttiles(inputs_bias_tic, self.nin+1, 1, basedir+"obs_S_0.pgm")

                    # ------- sigmoidal funcion for hidden layer 1 -------
                    self.hidden1_y = dot(self.weights11,inputs_bias_tic)
                    self.hidden1 = 1.0 / (1.0 + numpy.exp(-self.beta1*self.hidden1_y))
                    self.hiddenplusone1 = numpy.concatenate((self.hidden1,-1*numpy.ones((1))),axis=0) # add a bias unit
                    # ------- end of sigmoidal funcion for hidden layer 1 -------
                

                    h_out = self.mlpfwd()
                    h = self.normalised(h_out)

                    h_angle = math.atan2(h[1], h[0])
                    action_tic_angle = self.__perception.rand_winner(h_angle,self.sigma)
                    
                    action_tic = numpy.zeros((2))
                    action_tic[0] = math.cos(action_tic_angle)
                    action_tic[1] = math.sin(action_tic_angle)
                    action_tic = self.normalised(action_tic)

                
                    

                    # ------- sigmoidal funcion for hidden layer 2 -------
                    self.hidden2_y = dot(self.weights12,inputs_bias_tic)
                    self.hidden2 = 1.0 / (1.0 + numpy.exp(-self.beta*self.hidden2_y))
                    self.hiddenplusone2 = numpy.concatenate((self.hidden2,-1*numpy.ones((1))),axis=0) # add a bias unit
                    # ------- end of sigmoidal funcion for hidden layer 2 -------

                    

                    if self.__perception.sel_x > 0.1 and self.__perception.sel_y > 0.1 and self.__perception.sel_x < 0.3 and self.__perception.sel_y < 0.3:
                        KT.exporttiles(self.hidden1, 1, self.nhidden, basedir+"obs_S_1.pgm")
                        KT.exporttiles(self.hidden2, 1, self.nhidden, basedir+"obs_S_2.pgm")

                    if self.__perception.sel_x > 0.6 and self.__perception.sel_y > 0.6 and self.__perception.sel_x < 0.7 and self.__perception.sel_y < 0.7:
                        KT.exporttiles(self.hidden1, 1, self.nhidden, basedir+"obs_A_1.pgm")
                        KT.exporttiles(self.hidden2, 1, self.nhidden, basedir+"obs_A_2.pgm")
                
                    val_tic = self.valfwd()
                    
                    # ----- here are the training process--------#

                    if  r == 1.0:                                   # reward achieved
                        target = r 
                    else:                                           # because critic weights now converge.
                        target = gamma * val_tic                    # gamma = 0.9
                                                                    # prediction error; 
                    deltao = (target-val)
                
                    
                    error_val += abs(deltao)
                
                    
                    
                    
                    updatew4 = self.eps * (outer(deltao,self.hiddenplusone2))
                    deltah0 = self.hiddenplusone2*(1-self.hiddenplusone2)*(dot(transpose(self.weights4),deltao))
                    updatew12 = self.eta * (outer(deltah0[:-1],inputs_bias_tic))
                    

                    self.weights12 += updatew12
                    self.weights4 += updatew4
                       
                    if gamma * val_tic > val or r == 1.0:
                        updateTimes += 1
                        error += abs(action  - h)
                        deltao2 =  (action-h) / numpy.linalg.norm(action-h)
                        deltah0  = self.hiddenplusone1 * (1-self.hiddenplusone1) * dot(transpose(self.weights2),deltao2) 
                        updatew11 = self.eta*(outer(deltah0[:-1],inputs_bias_tic))
                        
                        self.weights11 += updatew11
                        updatew2 = self.eta * (outer(deltao2, self.hiddenplusone1))
                        self.weights2 += updatew2
                        
                    ##-------------end update when the critic are higher-----------##
                    
                    
                    landmark_info = landmark_info_tic
                
                    action = action_tic
                    val = val_tic
               

            if (iteration%1 == 0):
                print "iteration:", iteration
                print "Error in val:",  error_val, "average per move:", error_val/float(total_duration+1)
                print "Error in action:", error, "average per move:", error/float(updateTimes+1)
                print "Total duration:", total_duration
                print "Average duration", total_duration / (self.__perception.StartPosCount**2)
                print "Update Times:", updateTimes
            

            self.average_duration_sav[iteration] = total_duration / (self.__perception.StartPosCount**2)
Beispiel #9
0
        deltaOutputLayer = BP_GESCHWINDIGKEIT * np.outer(
            fehler, sigAusgabeHiddenLayer)

        print(deltaOutputLayer)
        deltaHiddenLayer = BP_GESCHWINDIGKEIT * np.outer(
            backHiddenLayer, eingabe)
        print(outputLayer)

        deltaBiasOutputLayer = BP_GESCHWINDIGKEIT * fehler
        deltaBiasHiddenLayer = BP_GESCHWINDIGKEIT * backHiddenLayer

        # Aenderung aller Werte

        outputLayer += deltaOutputLayer
        hiddenLayer += deltaHiddenLayer

        biasOutputLayer += deltaBiasOutputLayer
        biasHiddenLayer += deltaBiasHiddenLayer

        #if (durchlauf % 100 == 0):
        #   print("Durchlauf: " + str(durchlauf))
        #    print("Koordinate: " + str(i))
        #   print(fehler)

kt.exporttiles(array=hiddenLayer,
               height=1,
               width=2,
               outer_height=GROESSE_HD,
               outer_width=1,
               filename="results/obs_H_1_0.pgm")
Beispiel #10
0
 def visualize(self, path):
     """create visualization for weights at <path>"""
     if not os.path.isdir(os.path.dirname(path)):
         os.makedirs(os.path.dirname(path))
     KTimage.exporttiles(self.weights, self.in_size,
                         1, path, 1, self.size)
Beispiel #11
0
    print(bird.dim())

    network = MultiLayerNetwork(
        layout=(in_out_size, 200, in_out_size),
        transfer_function = MultiLayerNetwork.sigmoid_function,
        last_transfer_function = MultiLayerNetwork.direct_function)

    training_data = []
    for i in range(data_sets - 1):
        inputs = bird.sensor()
        bird.act()
        data_set = [inputs, bird.sensor()]
        training_data.append(data_set)

    errors = network.train_until_fit(
        training_data=training_data,
        train_steps=500,
        learn_rate=0.2,
        max_trains=1000)

    # plt.plot(errors)
    # plt.show()

    KTimage.exporttiles(
        X=network.weigths,
        h=network.layout[1],
        w=network.layout[0]+1,
        filename="test",
        x=network.layout[2],
        y=network.layout[1]+1)
Beispiel #12
0
counter = 0
while (True):

    # move the robot somehow
    vrep.simxSetJointTargetVelocity(
        clientID, leftMotorHandle, 3.1415 * 0.25, vrep.simx_opmode_oneshot)
    vrep.simxSetJointTargetVelocity(
        clientID, rightMotorHandle, 3.1415 * 0.1, vrep.simx_opmode_oneshot)

    # get vision sensor image
    err, res, img = vrep.simxGetVisionSensorImage(
        clientID, visionSensorHandle, 0, vrep.simx_opmode_oneshot_wait)
    colval = img_to_numpy(res, img)
    if counter % 10 == 0:
        KT.exporttiles(
            numpy.transpose(colval), res[1], res[0], "/tmp/coco/obs_I_0.pgm", 3, 1)

    # this is only for the err return value - some other functions don't
    # return a reliable err code!
    err, objs = vrep.simxGetObjects(
        clientID, vrep.sim_handle_all, vrep.simx_opmode_oneshot_wait)
    if err != vrep.simx_error_noerror:
        print("Error in loop: simxGetObjects can't get handles anymore!")
        exit(2)

    # initialize to start
    if counter % 100 == 0:
        # stop simulation
        vrep.simxStopSimulation(clientID, vrep.simx_opmode_oneshot_wait)
        # wait a sec .. else the restart doesn't work
        time.sleep(1)
Beispiel #13
0
 def visu(self):
     
     
     for i in range(len(self.W)):
         KT.exporttiles (self.W[i]
         , self.W[i].shape[0], self.W[i].shape[1], '/tmp/coco/obs_W_' + str(i) + '_'+str(i+1)+'.pgm' )# ,  len(self.W[i+1].T)  , 1)
    def mlptrain(self, eta_bias, eta, MaxIterations):
        """ Train the thing """

        updatew_in_hidden1_delay = zeros(
            (shape(self.weights_in_hidden1_delay)))
        updatew_in_hidden2_delay = zeros(
            (shape(self.weights_in_hidden2_delay)))
        updatew_in_hidden1 = zeros((shape(self.weights_in_hidden1)))
        updatew_in_hidden2 = zeros((shape(self.weights_in_hidden2)))
        updatew_hidden1_out = zeros((shape(self.weights_hidden1_out)))
        updatew_hidden2_out = zeros((shape(self.weights_hidden2_out)))
        updatew_hidden1_hidden1 = zeros((shape(self.weights_hidden1_hidden1)))
        updatew_hidden2_hidden2 = zeros((shape(self.weights_hidden2_hidden2)))

        error = 0.0
        for n in range(MaxIterations):
            old_error = error
            error = 0.0
            self.hidden1 = zeros((self.ndata * self.nLayer, self.nhidden1))
            self.hidden2 = zeros((self.ndata * self.nLayer, self.nhidden2))
            self.hidden1_y = zeros((self.ndata * self.nLayer, self.nhidden1))
            self.hidden2_y = zeros((self.ndata * self.nLayer, self.nhidden2))
            self.hidden1_z = zeros((self.ndata * self.nLayer, self.nhidden1))
            self.hidden2_z = zeros((self.ndata * self.nLayer, self.nhidden2))
            #hidden layer states of all samples by one iteration
            self.hidden1[0] = 0.5 * ones(self.nhidden1)
            self.hidden2[0] = 0.5 * ones(self.nhidden2)

            outputs_sav = numpy.zeros(
                (self.ndata * self.nLayer,
                 self.nLayer * self.size_in_h * self.size_in_w))
            horProduct1_sav = numpy.zeros(
                (self.ndata * self.nLayer,
                 self.nLayer * self.size_in_h * self.size_in_w))
            horProduct2_sav = numpy.zeros(
                (self.ndata * self.nLayer,
                 self.nLayer * self.size_in_h * self.size_in_w))

            for self.iter in range(self.ndata * self.nLayer):

                currenttar = self.targets[self.iter]  #current target

                if self.iter % (self.nSampleInLoop) == 0:
                    self.outputs = self.mlpfwd(self.new_inputs[self.iter],
                                               numpy.zeros((self.nin)))
                else:
                    self.outputs = self.mlpfwd(self.new_inputs[self.iter],
                                               self.new_inputs[self.iter - 1])

                outputs_sav[self.iter, :] = self.outputs

                horProduct1_sav[self.iter, :] = self.horProduct1
                horProduct2_sav[self.iter, :] = self.horProduct2

                if self.iter % (self.nSampleInLoop) > 0:
                    error += 0.5 * sum((currenttar - self.outputs)**2)

                    # Different types of output neurons
                    if self.outtype == 'linear':
                        deltao = (currenttar - self.outputs)
                    elif self.outtype == 'logistic':
                        deltao = (currenttar - self.outputs) * self.outputs * (
                            1.0 - self.outputs)
                    elif self.outtype == 'softmax':
                        deltao = (currenttar - self.outputs)
                    else:
                        print "error"

                # error in hidden node of current time-step

                    deltah0_1 = self.hidden1[self.iter] * (
                        1.0 - (self.hidden1[self.iter])) * (dot(
                            transpose(self.weights_hidden1_out),
                            deltao * self.horProduct2)) * (self.__a1)
                    deltah0_2 = self.hidden2[self.iter] * (
                        1.0 - (self.hidden2[self.iter])) * (dot(
                            transpose(self.weights_hidden2_out),
                            deltao * self.horProduct1)) * (self.__a2)

                if (self.iter % self.nLayer > 1):
                    # error in hidden node of previous time-step, caused by recurrent
                    deltah2_2 = self.hidden2[self.iter - 1] * (
                        1.0 - self.hidden2[self.iter - 1]) * (dot(
                            transpose(self.weights_hidden2_hidden2),
                            deltah0_2))
                    deltah1_1 = self.hidden1[self.iter - 1] * (
                        1.0 - self.hidden1[self.iter - 1]) * (dot(
                            transpose(self.weights_hidden1_hidden1),
                            deltah0_1))

                # update of weight between hidden layer and input (current and time-delay), learning only after movement starts
                if self.iter % (self.nSampleInLoop) > 0:
                    updatew_in_hidden1 = eta * (outer(
                        deltah0_1, self.new_inputs[self.iter]))
                    updatew_in_hidden2 = eta * (outer(
                        deltah0_2, self.new_inputs[self.iter]))

                    updatew_in_hidden1_delay = eta * (outer(
                        deltah0_1, self.new_inputs[self.iter - 1]))
                    updatew_in_hidden2_delay = eta * (outer(
                        deltah0_2, self.new_inputs[self.iter - 1]))

                if (self.iter % (self.nSampleInLoop) > 1):
                    updatew_in_hidden2 += eta * (outer(
                        deltah2_2, self.new_inputs[self.iter - 1]))

                # update of weight between hidden layer and output, learning only after movement starts
                if self.iter % (self.nSampleInLoop) > 0:

                    updatew_hidden1_out = eta * (outer(
                        deltao * self.horProduct2,
                        self.hidden1[self.iter] * self.__a1))
                    updatew_hidden2_out = eta * (outer(
                        deltao * self.horProduct1,
                        self.hidden2[self.iter] * self.__a2))

                    updatew_bias1_out = eta_bias * (outer(
                        dot(updatew_hidden1_out,
                            transpose(self.hidden1[self.iter])),
                        self.bias_hidden1_out))
                    updatew_bias2_out = eta_bias * (outer(
                        dot(updatew_hidden2_out,
                            transpose(self.hidden2[self.iter])),
                        self.bias_hidden2_out))

                # update within recurrent weights
                if (self.iter % (self.nSampleInLoop) > 1):
                    updatew_hidden2_hidden2 = eta * (outer(
                        deltah0_2, self.hidden2[self.iter - 1]))
                    updatew_hidden1_hidden1 = eta * (outer(
                        deltah0_1, self.hidden1[self.iter - 1]))

                if (self.iter % (self.nSampleInLoop) > 2):
                    updatew_hidden2_hidden2 += eta * (outer(
                        deltah2_2, self.hidden2[self.iter - 2]))
                    updatew_hidden1_hidden1 += eta * (outer(
                        deltah1_1, self.hidden1[self.iter - 2]))

                if (self.iter % (self.nSampleInLoop) > 0):

                    self.weights_in_hidden1_delay += updatew_in_hidden1_delay
                    self.weights_in_hidden2_delay += updatew_in_hidden2_delay

                if self.iter % (self.nSampleInLoop) > 0:
                    self.weights_in_hidden1 += updatew_in_hidden1
                    self.weights_in_hidden2 += updatew_in_hidden2
                    self.weights_hidden1_out += updatew_hidden1_out
                    self.weights_hidden2_out += updatew_hidden2_out
                    self.weights_bias1_out += updatew_bias1_out
                    self.weights_bias2_out += updatew_bias2_out
                    self.weights_hidden2_hidden2 += updatew_hidden2_hidden2
                    self.weights_hidden1_hidden1 += updatew_hidden1_hidden1

                    self.weights_in_hidden1_delay = numpy.clip(
                        self.weights_in_hidden1_delay, 0.0, numpy.inf)
                    self.weights_in_hidden2_delay = numpy.clip(
                        self.weights_in_hidden2_delay, 0.0, numpy.inf)
                    self.weights_in_hidden1 = numpy.clip(
                        self.weights_in_hidden1, 0.0, numpy.inf)
                    self.weights_in_hidden2 = numpy.clip(
                        self.weights_in_hidden2, 0.0, numpy.inf)
                    self.weights_hidden1_out = numpy.clip(
                        self.weights_hidden1_out, 0.0, numpy.inf)
                    self.weights_hidden2_out = numpy.clip(
                        self.weights_hidden2_out, 0.0, numpy.inf)

                    # ----- Update of intrinsic plasticity ----- (Eq. 9 and 10)

                    inv_mu1 = 1.0 / self.__mu1
                    yz_1 = self.hidden1_y[self.iter] * self.hidden1_z[
                        self.iter]
                    dA_1 = 1.0 / self.__a1 + self.hidden1_y[
                        self.
                        iter] - 2 * yz_1 - inv_mu1 * yz_1 + inv_mu1 * yz_1 * self.hidden1_z[
                            self.iter]
                    dB_1 = 1.0 - 2 * self.hidden1_z[
                        self.iter] - inv_mu1 * self.hidden1_z[
                            self.iter] + inv_mu1 * self.hidden1_z[
                                self.iter] * self.hidden1_z[self.iter]
                    self.__a1 += self.__eta_a * dA_1
                    self.__b1 += self.__eta_b * dB_1

                    inv_mu2 = 1.0 / self.__mu2
                    yz_2 = self.hidden2_y[self.iter] * self.hidden2_z[
                        self.iter]
                    dA_2 = 1.0 / self.__a2 + self.hidden2_y[
                        self.
                        iter] - 2 * yz_2 - inv_mu2 * yz_2 + inv_mu2 * yz_2 * self.hidden2_z[
                            self.iter]
                    dB_2 = 1.0 - 2 * self.hidden2_z[
                        self.iter] - inv_mu2 * self.hidden2_z[
                            self.iter] + inv_mu2 * self.hidden2_z[
                                self.iter] * self.hidden2_z[self.iter]
                    self.__a2 += self.__eta_a * dA_2
                    self.__b2 += self.__eta_b * dB_2
                    # ----- End of update of intrinsic plasticity -----

            # normalization
            for i in range(self.nhidden1):
                self.weights_in_hidden1_delay[i] /= numpy.linalg.norm(
                    self.weights_in_hidden1_delay[i])
                self.weights_in_hidden1[i] /= numpy.linalg.norm(
                    self.weights_in_hidden1[i])
                self.weights_hidden1_hidden1[i] /= numpy.linalg.norm(
                    self.weights_hidden1_hidden1[i])
            for i in range(self.nhidden2):
                self.weights_in_hidden2_delay[i] /= numpy.linalg.norm(
                    self.weights_in_hidden2_delay[i])
                self.weights_in_hidden2[i] /= numpy.linalg.norm(
                    self.weights_in_hidden2[i])
                self.weights_hidden2_hidden2[i] /= numpy.linalg.norm(
                    self.weights_hidden2_hidden2[i])

            self.true_w_hidden1_out = self.weights_hidden1_out[:,
                                                               0:self.nhidden1]
            self.true_w_hidden2_out = self.weights_hidden2_out[:,
                                                               0:self.nhidden2]

            if n % self.visual_step == 0:

                KT.exporttiles(self.hidden1, self.ndata * self.nLayer,
                               self.nLayer, basedir + "obs_H_1.pgm")
                KT.exporttiles(self.hidden2, self.ndata * self.nLayer,
                               self.nhidden2, basedir + "obs_G_1.pgm")
                KT.exporttiles(self.hidden1_z, self.ndata * self.nLayer,
                               self.nLayer, basedir + "obs_H_3.pgm")
                KT.exporttiles(self.hidden2_z, self.ndata * self.nLayer,
                               self.nhidden2, basedir + "obs_G_3.pgm")
                KT.exporttiles(self.__a1, 1, self.nhidden1,
                               basedir + "obs_A_1.pgm")
                KT.exporttiles(self.__b1, 1, self.nhidden1,
                               basedir + "obs_B_1.pgm")
                KT.exporttiles(self.__a2, 1, self.nhidden2,
                               basedir + "obs_C_1.pgm")
                KT.exporttiles(self.__b2, 1, self.nhidden2,
                               basedir + "obs_D_1.pgm")
                KT.exporttiles(self.weights_bias1_out,
                               self.size_in_h * self.nLayer, self.size_in_w,
                               basedir + "obs_E_3.pgm")
                KT.exporttiles(self.weights_bias2_out,
                               self.size_in_h * self.nLayer, self.size_in_w,
                               basedir + "obs_F_3.pgm")
                KT.exporttiles(self.weights_in_hidden1,
                               self.size_in_h * self.nLayer, self.size_in_w,
                               basedir + "obs_W_1_0.pgm", self.nhidden1, 1)
                KT.exporttiles(self.weights_in_hidden2,
                               self.size_in_h * self.nLayer, self.size_in_w,
                               basedir + "obs_V_1_0.pgm", self.nhidden2, 1)
                KT.exporttiles(self.weights_in_hidden1_delay,
                               self.size_in_h * self.nLayer, self.size_in_w,
                               basedir + "obs_W_0_0.pgm", self.nhidden1, 1)
                KT.exporttiles(self.weights_in_hidden2_delay,
                               self.size_in_h * self.nLayer, self.size_in_w,
                               basedir + "obs_V_0_0.pgm", self.nhidden2, 1)

                KT.exporttiles(
                    transpose(self.true_w_hidden1_out) + 0.5,
                    self.size_in_h * self.nLayer, self.size_in_w,
                    basedir + "obs_W_2_1.pgm", self.nhidden1, 1)
                KT.exporttiles(
                    transpose(self.true_w_hidden2_out) + 0.5,
                    self.size_in_h * self.nLayer, self.size_in_w,
                    basedir + "obs_V_2_1.pgm", self.nhidden2, 1)

                KT.exporttiles(self.weights_hidden2_hidden2, self.nhidden2,
                               self.nhidden2, basedir + "obs_V_1_1.pgm")
                KT.exporttiles(self.weights_hidden1_hidden1, self.nhidden1,
                               self.nhidden1, basedir + "obs_W_1_1.pgm")

                KT.exporttiles(outputs_sav + 0.5, self.size_in_h * self.nLayer,
                               self.size_in_w, basedir + "obs_O_2.pgm",
                               self.nLayer, self.ndata)
                KT.exporttiles(horProduct1_sav + 1.0,
                               self.size_in_h * self.nLayer, self.size_in_w,
                               basedir + "obs_M_2.pgm", self.nLayer,
                               self.ndata)
                KT.exporttiles(horProduct2_sav + 1.0,
                               self.size_in_h * self.nLayer, self.size_in_w,
                               basedir + "obs_N_2.pgm", self.nLayer,
                               self.ndata)

                print 'iteration', n, 'error', error
                if abs(error - old_error) < 1e-8:
                    print 'no more improvements during training, existing..'
                    break
                if (error - old_error) > 1e2 and (old_error != 0):
                    print error, old_error
                    print 'error increasing, existing..'
                    break
    def mlptrain(self,eta_bias,eta,MaxIterations):
        """ Train the thing """
        
        
        
        updatew_in_hidden1_delay = zeros((shape(self.weights_in_hidden1_delay))) 
        updatew_in_hidden2_delay = zeros((shape(self.weights_in_hidden2_delay)))
        updatew_in_hidden1 = zeros((shape(self.weights_in_hidden1))) 
        updatew_in_hidden2 = zeros((shape(self.weights_in_hidden2)))
        updatew_hidden1_out = zeros((shape(self.weights_hidden1_out)))
        updatew_hidden2_out = zeros((shape(self.weights_hidden2_out)))
        updatew_hidden1_hidden1 = zeros((shape(self.weights_hidden1_hidden1)))
        updatew_hidden2_hidden2 = zeros((shape(self.weights_hidden2_hidden2)))

        
        error = 0.0
        for n in range(MaxIterations):
            old_error = error
            error = 0.0
            self.hidden1 = zeros((self.ndata*self.nLayer,self.nhidden1))
            self.hidden2 = zeros((self.ndata*self.nLayer,self.nhidden2))
            self.hidden1_y = zeros((self.ndata*self.nLayer, self.nhidden1))
            self.hidden2_y = zeros((self.ndata*self.nLayer, self.nhidden2))
            self.hidden1_z = zeros((self.ndata*self.nLayer, self.nhidden1))
            self.hidden2_z = zeros((self.ndata*self.nLayer, self.nhidden2))
            #hidden layer states of all samples by one iteration
            self.hidden1[0] = 0.5 * ones(self.nhidden1)
            self.hidden2[0] = 0.5 * ones(self.nhidden2)

            outputs_sav = numpy.zeros((self.ndata*self.nLayer, self.nLayer*self.size_in_h*self.size_in_w))
            horProduct1_sav = numpy.zeros((self.ndata*self.nLayer, self.nLayer*self.size_in_h*self.size_in_w))
            horProduct2_sav = numpy.zeros((self.ndata*self.nLayer, self.nLayer*self.size_in_h*self.size_in_w))
            
            for self.iter in range(self.ndata * self.nLayer):
                
                currenttar = self.targets[self.iter] #current target

                

                if self.iter%(self.nSampleInLoop) == 0:
                    self.outputs = self.mlpfwd(self.new_inputs[self.iter], numpy.zeros((self.nin)))
                else:
                    self.outputs = self.mlpfwd(self.new_inputs[self.iter], self.new_inputs[self.iter - 1])

               
                
                outputs_sav[self.iter,:] = self.outputs
                
                horProduct1_sav[self.iter,:] = self.horProduct1
                horProduct2_sav[self.iter,:] = self.horProduct2

                

                if  self.iter%(self.nSampleInLoop) > 0:
                    error += 0.5*sum((currenttar-self.outputs)**2)

                # Different types of output neurons
                    if self.outtype == 'linear':
                        deltao = (currenttar-self.outputs)
                    elif self.outtype == 'logistic':
                        deltao = (currenttar-self.outputs)*self.outputs*(1.0-self.outputs)
                    elif self.outtype == 'softmax':
                        deltao = (currenttar-self.outputs)
                    else:
                        print "error"

                # error in hidden node of current time-step
                    
                    deltah0_1  = self.hidden1[self.iter] * (1.0-(self.hidden1[self.iter]))*(dot(transpose(self.weights_hidden1_out),deltao*self.horProduct2))*(self.__a1)
                    deltah0_2  = self.hidden2[self.iter] * (1.0-(self.hidden2[self.iter]))*(dot(transpose(self.weights_hidden2_out),deltao*self.horProduct1))*(self.__a2)
                                             
                if (self.iter%self.nLayer > 1):
                    # error in hidden node of previous time-step, caused by recurrent
                    deltah2_2 = self.hidden2[self.iter-1]*(1.0-self.hidden2[self.iter-1])*(dot(transpose(self.weights_hidden2_hidden2),deltah0_2))
                    deltah1_1 = self.hidden1[self.iter-1]*(1.0-self.hidden1[self.iter-1])*(dot(transpose(self.weights_hidden1_hidden1),deltah0_1))
                
                    
                # update of weight between hidden layer and input (current and time-delay), learning only after movement starts
                if self.iter%(self.nSampleInLoop) > 0:
                    updatew_in_hidden1 = eta*(outer(deltah0_1,self.new_inputs[self.iter]))
                    updatew_in_hidden2 = eta*(outer(deltah0_2,self.new_inputs[self.iter]))

                    updatew_in_hidden1_delay = eta*(outer(deltah0_1,self.new_inputs[self.iter - 1]))
                    updatew_in_hidden2_delay = eta*(outer(deltah0_2,self.new_inputs[self.iter - 1]))
                
                if (self.iter%(self.nSampleInLoop) > 1):
                    updatew_in_hidden2 += eta*(outer(deltah2_2,self.new_inputs[self.iter - 1]))
                
                # update of weight between hidden layer and output, learning only after movement starts
                if self.iter%(self.nSampleInLoop) > 0:
                    
                    updatew_hidden1_out = eta*(outer(deltao*self.horProduct2,self.hidden1[self.iter]*self.__a1))
                    updatew_hidden2_out = eta*(outer(deltao*self.horProduct1,self.hidden2[self.iter]*self.__a2))
                    
                    updatew_bias1_out = eta_bias*(outer(dot(updatew_hidden1_out, transpose(self.hidden1[self.iter])) , self.bias_hidden1_out))
                    updatew_bias2_out = eta_bias*(outer(dot(updatew_hidden2_out, transpose(self.hidden2[self.iter])) , self.bias_hidden2_out))
                    
                # update within recurrent weights    
                if (self.iter%(self.nSampleInLoop) > 1):
                    updatew_hidden2_hidden2 = eta*(outer(deltah0_2,self.hidden2[self.iter-1]))
                    updatew_hidden1_hidden1 = eta*(outer(deltah0_1,self.hidden1[self.iter-1]))
                    
                if (self.iter%(self.nSampleInLoop) > 2):
                    updatew_hidden2_hidden2 += eta*(outer(deltah2_2,self.hidden2[self.iter-2]))
                    updatew_hidden1_hidden1 += eta*(outer(deltah1_1,self.hidden1[self.iter-2]))


                if (self.iter%(self.nSampleInLoop) > 0):
                    
                    self.weights_in_hidden1_delay += updatew_in_hidden1_delay
                    self.weights_in_hidden2_delay += updatew_in_hidden2_delay

                
                if self.iter%(self.nSampleInLoop) > 0:
                    self.weights_in_hidden1 += updatew_in_hidden1
                    self.weights_in_hidden2 += updatew_in_hidden2
                    self.weights_hidden1_out += updatew_hidden1_out
                    self.weights_hidden2_out += updatew_hidden2_out
                    self.weights_bias1_out += updatew_bias1_out
                    self.weights_bias2_out += updatew_bias2_out
                    self.weights_hidden2_hidden2 += updatew_hidden2_hidden2
                    self.weights_hidden1_hidden1 += updatew_hidden1_hidden1

                    self.weights_in_hidden1_delay = numpy.clip(self.weights_in_hidden1_delay, 0.0, numpy.inf)
                    self.weights_in_hidden2_delay = numpy.clip(self.weights_in_hidden2_delay, 0.0, numpy.inf)
                    self.weights_in_hidden1 = numpy.clip(self.weights_in_hidden1, 0.0, numpy.inf)
                    self.weights_in_hidden2 = numpy.clip(self.weights_in_hidden2, 0.0, numpy.inf)
                    self.weights_hidden1_out = numpy.clip(self.weights_hidden1_out, 0.0, numpy.inf)
                    self.weights_hidden2_out = numpy.clip(self.weights_hidden2_out, 0.0, numpy.inf)

                    # ----- Update of intrinsic plasticity ----- (Eq. 9 and 10)

                    inv_mu1 = 1.0/self.__mu1
                    yz_1 = self.hidden1_y[self.iter]*self.hidden1_z[self.iter]
                    dA_1 = 1.0/self.__a1 + self.hidden1_y[self.iter] - 2*yz_1 - inv_mu1*yz_1 + inv_mu1*yz_1*self.hidden1_z[self.iter]
                    dB_1 = 1.0 - 2*self.hidden1_z[self.iter] - inv_mu1*self.hidden1_z[self.iter] + inv_mu1*self.hidden1_z[self.iter]*self.hidden1_z[self.iter]
                    self.__a1 += self.__eta_a * dA_1
                    self.__b1 += self.__eta_b * dB_1

                    inv_mu2 = 1.0/self.__mu2
                    yz_2 = self.hidden2_y[self.iter]*self.hidden2_z[self.iter]
                    dA_2 = 1.0/self.__a2 + self.hidden2_y[self.iter] - 2*yz_2 - inv_mu2*yz_2 + inv_mu2*yz_2*self.hidden2_z[self.iter]
                    dB_2 = 1.0 - 2*self.hidden2_z[self.iter] - inv_mu2*self.hidden2_z[self.iter] + inv_mu2*self.hidden2_z[self.iter]*self.hidden2_z[self.iter]
                    self.__a2 += self.__eta_a * dA_2
                    self.__b2 += self.__eta_b * dB_2 
                    # ----- End of update of intrinsic plasticity -----
                                
            # normalization
            for i in range(self.nhidden1):
                self.weights_in_hidden1_delay[i] /= numpy.linalg.norm(self.weights_in_hidden1_delay[i])
                self.weights_in_hidden1[i] /= numpy.linalg.norm(self.weights_in_hidden1[i])
                self.weights_hidden1_hidden1[i] /= numpy.linalg.norm(self.weights_hidden1_hidden1[i])
            for i in range(self.nhidden2):
                self.weights_in_hidden2_delay[i] /= numpy.linalg.norm(self.weights_in_hidden2_delay[i])
                self.weights_in_hidden2[i] /= numpy.linalg.norm(self.weights_in_hidden2[i])
                self.weights_hidden2_hidden2[i] /= numpy.linalg.norm(self.weights_hidden2_hidden2[i])
            
            self.true_w_hidden1_out = self.weights_hidden1_out[:, 0:self.nhidden1]
            self.true_w_hidden2_out = self.weights_hidden2_out[:, 0:self.nhidden2]
            


            if n%self.visual_step == 0:
                
                KT.exporttiles(self.hidden1, self.ndata*self.nLayer, self.nLayer, basedir+"obs_H_1.pgm")
                KT.exporttiles(self.hidden2, self.ndata*self.nLayer, self.nhidden2, basedir+"obs_G_1.pgm")
                KT.exporttiles(self.hidden1_z, self.ndata*self.nLayer, self.nLayer, basedir+"obs_H_3.pgm")
                KT.exporttiles(self.hidden2_z, self.ndata*self.nLayer, self.nhidden2, basedir+"obs_G_3.pgm")
                KT.exporttiles(self.__a1, 1, self.nhidden1, basedir+"obs_A_1.pgm")
                KT.exporttiles(self.__b1, 1, self.nhidden1, basedir+"obs_B_1.pgm")
                KT.exporttiles(self.__a2, 1, self.nhidden2, basedir+"obs_C_1.pgm")
                KT.exporttiles(self.__b2, 1, self.nhidden2, basedir+"obs_D_1.pgm")
                KT.exporttiles(self.weights_bias1_out, self.size_in_h*self.nLayer, self.size_in_w, basedir+"obs_E_3.pgm")
                KT.exporttiles(self.weights_bias2_out, self.size_in_h*self.nLayer, self.size_in_w, basedir+"obs_F_3.pgm")
                KT.exporttiles(self.weights_in_hidden1, self.size_in_h*self.nLayer, self.size_in_w, basedir+"obs_W_1_0.pgm", self.nhidden1, 1)
                KT.exporttiles(self.weights_in_hidden2, self.size_in_h*self.nLayer, self.size_in_w, basedir+"obs_V_1_0.pgm", self.nhidden2, 1)
                KT.exporttiles(self.weights_in_hidden1_delay, self.size_in_h*self.nLayer, self.size_in_w, basedir+"obs_W_0_0.pgm", self.nhidden1, 1)
                KT.exporttiles(self.weights_in_hidden2_delay, self.size_in_h*self.nLayer, self.size_in_w, basedir+"obs_V_0_0.pgm", self.nhidden2, 1)
                
                KT.exporttiles(transpose(self.true_w_hidden1_out)+0.5, self.size_in_h*self.nLayer, self.size_in_w, basedir+"obs_W_2_1.pgm", self.nhidden1, 1)
                KT.exporttiles(transpose(self.true_w_hidden2_out)+0.5, self.size_in_h *self.nLayer, self.size_in_w, basedir+"obs_V_2_1.pgm", self.nhidden2, 1)
                
                KT.exporttiles(self.weights_hidden2_hidden2, self.nhidden2, self.nhidden2, basedir+"obs_V_1_1.pgm")
                KT.exporttiles(self.weights_hidden1_hidden1, self.nhidden1, self.nhidden1, basedir+"obs_W_1_1.pgm")
                
                KT.exporttiles(outputs_sav + 0.5, self.size_in_h*self.nLayer, self.size_in_w, basedir+"obs_O_2.pgm", self.nLayer, self.ndata)
                KT.exporttiles(horProduct1_sav + 1.0, self.size_in_h*self.nLayer, self.size_in_w, basedir+"obs_M_2.pgm", self.nLayer, self.ndata)
                KT.exporttiles(horProduct2_sav + 1.0, self.size_in_h*self.nLayer, self.size_in_w, basedir+"obs_N_2.pgm", self.nLayer, self.ndata)
            
                print 'iteration', n, 'error', error
                if abs(error-old_error)<1e-8:
                    print 'no more improvements during training, existing..'
                    break
                if (error - old_error) > 1e2 and (old_error != 0):
                    print error, old_error
                    print 'error increasing, existing..'
                    break
    def __init__(self, size, nObject, beta=1, outtype='linear'):
        """ Constructor """
        # Set up network size

        self.size = size

        self.size_in_h = self.size
        self.size_in_w = self.size
        self.size_out_h = self.size
        self.size_out_w = self.size

        self.visual_step = 100
        self.nLayer = nObject
        self.nhidden1 = self.nLayer  # layer 1 represents object identity. Number of units should be the same as layers
        self.nhidden2 = 4 * self.size**2  # layer 2 represents object movement info. Number of units equals to size of the grid in each layer multiplying 4 directions.
        self.beta = beta

        self.__forward_alpha_v = 0.70  # when doing forward transformation function, y = alpha*y + (1-alpha)*input*w
        self.__forward_alpha_d = 0.0

        self.__a1 = 2.5 * numpy.ones((self.nhidden1))
        self.__b1 = -5.0 * numpy.ones((self.nhidden1))
        self.__mu1 = 0.20

        self.__a2 = 2.5 * numpy.ones((self.nhidden2))
        self.__b2 = -5.0 * numpy.ones((self.nhidden2))
        self.__mu2 = 0.0025

        self.__eta_a = 0.0001
        self.__eta_b = 0.00001

        self.outtype = outtype
        self.nSampleInLoop = self.size + 1
        self.dataIteration = self.size_in_h * self.size_in_w  # starting point of one block, equals to the size of block, i.e. width * height

        self.ndata = self.dataIteration * self.nSampleInLoop * 4  # number of the training data set in one layer (one object)
        print "Generating data with number", self.ndata, 'in one layer'

        self.inputs, self.targets = self.__genLargerData(
        )  # generate data for training

        print 'The complete training data includes', self.ndata * self.nLayer, 'samples.'
        print 'Size of each layer:', self.size**2

        self.nin = shape(self.inputs)[1] * shape(self.inputs)[2]
        self.nout = shape(self.targets)[1] * shape(self.targets)[2]
        #self.new_inputs = self.inputs
        self.new_inputs = numpy.reshape(
            self.inputs, (self.ndata * self.nLayer,
                          self.nLayer * self.size_in_h * self.size_in_w))
        self.targets = numpy.reshape(self.targets,
                                     (self.ndata * self.nLayer, self.nLayer *
                                      self.size_in_h * self.size_in_w))

        KT.exporttiles(self.new_inputs + 1, self.size_in_h * self.nLayer,
                       self.size_in_w, basedir + "obs_T_0.pgm", self.nLayer,
                       self.ndata)
        print 'Training data saved...'

        # Initialise network
        self.weights_in_hidden1 = (
            numpy.random.rand(self.nhidden1, self.nin) - 0.5) * 2 / sqrt(
                self.nin)  #weights between input and hidden1
        self.weights_in_hidden2 = (
            numpy.random.rand(self.nhidden2, self.nin) - 0.5) * 2 / sqrt(
                self.nin)  #weights between input and hidden2

        self.weights_hidden1_out = (
            numpy.random.rand(self.nout, self.nhidden1) - 0.5) * 2 / sqrt(
                self.nhidden1)  #weights between hidden1 and output
        self.weights_hidden2_out = (
            numpy.random.rand(self.nout, self.nhidden2) - 0.5) * 2 / sqrt(
                self.nhidden2)  #weights between hidden2 and output

        self.bias_hidden1_out = -0.1 * numpy.ones((1))
        self.bias_hidden2_out = -0.1 * numpy.ones((1))
        self.weights_bias1_out = (numpy.random.rand(self.nout, 1) - 0.5) * 2
        self.weights_bias2_out = (numpy.random.rand(self.nout, 1) - 0.5) * 2

        self.weights_hidden1_hidden1 = (
            numpy.random.rand(self.nhidden1, self.nhidden1) - 0.5) * 2 / sqrt(
                self.nhidden1)  #weights within hidden1
        self.weights_hidden2_hidden2 = (
            numpy.random.rand(self.nhidden2, self.nhidden2) - 0.5) * 2 / sqrt(
                self.nhidden2)  #weights within hidden2

        self.weights_in_hidden1_delay = (numpy.random.rand(
            self.nhidden1, self.nin) - 0.5) * 2 / sqrt(self.nin)
        self.weights_in_hidden2_delay = (numpy.random.rand(
            self.nhidden2, self.nin) - 0.5) * 2 / sqrt(self.nin)
    def __init__(self,size, nObject, beta=1, outtype='linear'):
        """ Constructor """
        # Set up network size
        

        self.size = size

        self.size_in_h = self.size
        self.size_in_w = self.size
        self.size_out_h = self.size
        self.size_out_w = self.size

        self.visual_step = 100
        self.nLayer = nObject
        self.nhidden1 = self.nLayer # layer 1 represents object identity. Number of units should be the same as layers
        self.nhidden2 = 4 * self.size**2 # layer 2 represents object movement info. Number of units equals to size of the grid in each layer multiplying 4 directions.
        self.beta = beta

        self.__forward_alpha_v = 0.70 # when doing forward transformation function, y = alpha*y + (1-alpha)*input*w
        self.__forward_alpha_d = 0.0

        
        self.__a1 = 2.5*numpy.ones((self.nhidden1))
        self.__b1 = -5.0*numpy.ones((self.nhidden1))
        self.__mu1 = 0.20

        self.__a2 = 2.5*numpy.ones((self.nhidden2))
        self.__b2 = -5.0*numpy.ones((self.nhidden2))
        self.__mu2 = 0.0025
        
        self.__eta_a = 0.0001
        self.__eta_b = 0.00001

        
        
        self.outtype = outtype
        self.nSampleInLoop = self.size + 1
        self.dataIteration = self.size_in_h * self.size_in_w # starting point of one block, equals to the size of block, i.e. width * height

        self.ndata = self.dataIteration * self.nSampleInLoop * 4 # number of the training data set in one layer (one object)
        print "Generating data with number", self.ndata, 'in one layer'
        
        self.inputs, self.targets = self.__genLargerData() # generate data for training
        
        print 'The complete training data includes', self.ndata*self.nLayer, 'samples.'
        print 'Size of each layer:', self.size**2
        
        self.nin = shape(self.inputs)[1] * shape(self.inputs)[2]
        self.nout = shape(self.targets)[1] * shape(self.targets)[2]
        #self.new_inputs = self.inputs
        self.new_inputs = numpy.reshape(self.inputs,(self.ndata*self.nLayer, self.nLayer*self.size_in_h*self.size_in_w))
        self.targets = numpy.reshape(self.targets, (self.ndata*self.nLayer, self.nLayer*self.size_in_h*self.size_in_w))

        
        
        KT.exporttiles(self.new_inputs + 1, self.size_in_h*self.nLayer, self.size_in_w, basedir+"obs_T_0.pgm", self.nLayer, self.ndata)
        print 'Training data saved...'


        # Initialise network
        self.weights_in_hidden1 = (numpy.random.rand(self.nhidden1,self.nin)-0.5)*2/sqrt(self.nin) #weights between input and hidden1
        self.weights_in_hidden2 = (numpy.random.rand(self.nhidden2,self.nin)-0.5)*2/sqrt(self.nin) #weights between input and hidden2
        
        self.weights_hidden1_out = (numpy.random.rand(self.nout,self.nhidden1)-0.5)*2/sqrt(self.nhidden1) #weights between hidden1 and output
        self.weights_hidden2_out = (numpy.random.rand(self.nout,self.nhidden2)-0.5)*2/sqrt(self.nhidden2) #weights between hidden2 and output

        self.bias_hidden1_out = -0.1*numpy.ones((1))
        self.bias_hidden2_out = -0.1*numpy.ones((1))
        self.weights_bias1_out = (numpy.random.rand(self.nout,1)-0.5)*2
        self.weights_bias2_out = (numpy.random.rand(self.nout,1)-0.5)*2

        self.weights_hidden1_hidden1 = (numpy.random.rand(self.nhidden1,self.nhidden1)-0.5)*2/sqrt(self.nhidden1) #weights within hidden1
        self.weights_hidden2_hidden2 = (numpy.random.rand(self.nhidden2,self.nhidden2)-0.5)*2/sqrt(self.nhidden2) #weights within hidden2

        self.weights_in_hidden1_delay = (numpy.random.rand(self.nhidden1,self.nin)-0.5)*2/sqrt(self.nin)
        self.weights_in_hidden2_delay = (numpy.random.rand(self.nhidden2,self.nin)-0.5)*2/sqrt(self.nin)
Beispiel #18
0
    pos_img1 = img_find_cm(binval1)
    print "pos_img1 =", pos_img1

    # get image from vision sensor 2
    err,res,img = vrep.simxGetVisionSensorImage(clientID,visionSensor2Handle,0,vrep.simx_opmode_oneshot_wait)
    colval2 = img_to_numpy(res,img)
    # get the binary image marking the cyan ball in Youbot's gripper
    binval2 = img_binarise(colval2,0.0,0.15,0.7,0.99,0.9,1.0)
    # get the x- and y-coordinates of the cyan ball
    binval2 = numpy.reshape(binval2,(res[1],res[0]))
    pos_img2 = img_find_cm(binval2)
    print "pos_img2 =", pos_img2

    # export images for look.tcl display
    if  counter % 5 == 0:
        KT.exporttiles (numpy.transpose(colval1), res[1], res[0], "/tmp/coco/obs_I_0.pgm", 3, 1)
        KT.exporttiles (binval1, res[1], res[0], "/tmp/coco/obs_J_0.pgm")
        KT.exporttiles (numpy.transpose(colval2), res[1], res[0], "/tmp/coco/obs_I_1.pgm", 3, 1)
        KT.exporttiles (binval2, res[1], res[0], "/tmp/coco/obs_J_1.pgm")

    # write the joint data and the ball-position data into one file (if joints are near target and if cyan ball is seen in both images)
    if  (joint_pos_err < 0.00001 and pos_img1[0] >= 0.0 and pos_img1[1] >= 0.0 and pos_img2[0] >= 0.0 and pos_img2[1] >= 0.0):
        fobj = open("outfile.dat", "a")
        fobj.write("%.6f %.6f %.6f %.6f %.6f %.6f %.6f %.6f\n" % (rand_base_rotate, rand_base_lower, rand_elbow_bend, rand_hand_bend, pos_img1[0], pos_img1[1], pos_img2[0], pos_img2[1]))
        fobj.close()


print('Program ended')
exit(0)

Beispiel #19
0
 def vis(self):
     KT.exporttiles(self.weights_hidden, self.layersize[0], 1,
                    "result/obs_W_1_0.pgm", 1, self.layersize[1])
     KT.exporttiles(self.weights_output, self.layersize[1], 1,
                    "result/obs_W_2_1.pgm", 1, self.layersize[2])