Esempio n. 1
0
    def __call__(self,x):
        rows,cols = x.shape
        radius = self.density*self.kernel_radius
        crop_radius = int(max(1.25,radius*self.crop_radius_multiplier))

        # find out the matrix coordinates of the winner
        wr,wc = array_argmax(x)

        # convert to sheet coordinates
        wy = rows-wr-1

        # Optimization: Calculate the bounding box around the winner
        # in which weights will be changed
        cmin = max(wc-crop_radius,  0)
        cmax = min(wc+crop_radius+1,cols)
        rmin = max(wr-crop_radius,  0)
        rmax = min(wr+crop_radius+1,rows)
        ymin = max(wy-crop_radius,  0)
        ymax = min(wy+crop_radius+1,rows)
        bb = BoundingBox(points=((cmin,ymin), (cmax,ymax)))

        # generate the kernel matrix and insert it into the correct
        # part of the output array
        kernel = self.neighborhood_kernel_generator(bounds=bb,xdensity=1,ydensity=1,
                                                    size=2*radius,x=wc+0.5,y=wy+0.5)
        x *= 0.0
        x[rmin:rmax,cmin:cmax] = kernel
Esempio n. 2
0
  def determine_next_position(self,img):
      self.maximum=array_argmax(img)
 
      #Coordinates for the box around the brightest pixel
      self.coormin_bbox=(self.maximum[1]-1,self.maximum[0]-1)
      self.coormax_bbox=(self.maximum[1]+1,self.maximum[0]+1)
      self.brightpixel=True
      return(self.maximum,self.coormin_bbox,self.coormax_bbox,self.brightpixel)
Esempio n. 3
0
    def determine_next_position(self,img):
        self.maximum=array_argmax(img)

        #Coordinates for the box around the brightest pixel
        self.coormin_bbox=(self.maximum[1]-1,self.maximum[0]-1)
        self.coormax_bbox=(self.maximum[1]+1,self.maximum[0]+1)
        self.brightpixel=True
        return(self.maximum,self.coormin_bbox,self.coormax_bbox,self.brightpixel)
Esempio n. 4
0
    def __call__(self, iterator, input_activity, output_activity, learning_rate, **params):
        cfs = iterator.proj.cfs.tolist() # CEBALERT: convert to use flatcfs
        rows,cols = output_activity.shape

        # This learning function does not need to scale the learning
        # rate like some do, so it does not use constant_sum_connection_rate()
        single_connection_learning_rate = learning_rate

        ### JABALERT: The learning_radius is normally set by
        ### the learn() function of CFSOM, so it doesn't matter
        ### much that the value accepted here is in matrix and
        ### not sheet coordinates.  It's confusing that anything
        ### would accept matrix coordinates, but the learning_fn
        ### doesn't have access to the sheet, so it can't easily
        ### convert from sheet coords.
        radius = self.learning_radius
        crop_radius = max(1.25,radius*self.crop_radius_multiplier)

        # find out the matrix coordinates of the winner
        #
        # NOTE: when there are multiple projections, it would be
        # slightly more efficient to calculate the winner coordinates
        # within the Sheet, e.g. by moving winner_coords() to CFSOM
        # and passing in the results here.  However, finding the
        # coordinates does not take much time, and requiring the
        # winner to be passed in would make it harder to mix and match
        # Projections and learning rules with different Sheets.
        wr,wc = array_argmax(output_activity)

        # Optimization: Calculate the bounding box around the winner
        # in which weights will be changed, to avoid considering those
        # units below.
        cmin = int(max(wc-crop_radius,0))
        cmax = int(min(wc+crop_radius+1,cols)) # at least 1 between cmin and cmax
        rmin = int(max(wr-crop_radius,0))
        rmax = int(min(wr+crop_radius+1,rows))

        # generate the neighborhood kernel matrix so that the values
        # can be read off easily using matrix coordinates.
        nk_generator = self.neighborhood_kernel_generator
        radius_int = int(ceil(crop_radius))
        rbound = radius_int + 0.5
        bb = BoundingBox(points=((-rbound,-rbound), (rbound,rbound)))

        # Print parameters designed to match fm2d's output
        #print "%d rad= %d std= %f alpha= %f" % (topo.sim._time, radius_int, radius, single_connection_learning_rate)

        neighborhood_matrix = nk_generator(bounds=bb,xdensity=1,ydensity=1,
                                           size=2*radius)

        for r in range(rmin,rmax):
            for c in range(cmin,cmax):
                cwc = c - wc
                rwr = r - wr
                lattice_dist = L2norm((cwc,rwr))
                if lattice_dist <= crop_radius:
                    cf = cfs[r][c]
                    rate = single_connection_learning_rate * neighborhood_matrix[rwr+radius_int,cwc+radius_int]
                    X = cf.get_input_matrix(input_activity)
                    cf.weights += rate * (X - cf.weights)

                    # CEBHACKALERT: see ConnectionField.__init__()
                    cf.weights *= cf.mask
Esempio n. 5
0
    def __call__(self, iterator, input_activity, output_activity, learning_rate, **params):
        cfs = iterator.proj.cfs.tolist() # CEBALERT: convert to use flatcfs
        rows,cols = output_activity.shape

        # This learning function does not need to scale the learning
        # rate like some do, so it does not use constant_sum_connection_rate()
        single_connection_learning_rate = learning_rate
        
        ### JABALERT: The learning_radius is normally set by
        ### the learn() function of CFSOM, so it doesn't matter
        ### much that the value accepted here is in matrix and 
        ### not sheet coordinates.  It's confusing that anything
        ### would accept matrix coordinates, but the learning_fn
        ### doesn't have access to the sheet, so it can't easily
        ### convert from sheet coords.
        radius = self.learning_radius
        crop_radius = max(1.25,radius*self.crop_radius_multiplier)
        
        # find out the matrix coordinates of the winner
        #
        # NOTE: when there are multiple projections, it would be
        # slightly more efficient to calculate the winner coordinates
        # within the Sheet, e.g. by moving winner_coords() to CFSOM
        # and passing in the results here.  However, finding the
        # coordinates does not take much time, and requiring the
        # winner to be passed in would make it harder to mix and match
        # Projections and learning rules with different Sheets.
        wr,wc = array_argmax(output_activity)
        
        # Optimization: Calculate the bounding box around the winner
        # in which weights will be changed, to avoid considering those
        # units below.
        cmin = int(max(wc-crop_radius,0))
        cmax = int(min(wc+crop_radius+1,cols)) # at least 1 between cmin and cmax
        rmin = int(max(wr-crop_radius,0))
        rmax = int(min(wr+crop_radius+1,rows))

        # generate the neighborhood kernel matrix so that the values
        # can be read off easily using matrix coordinates.
        nk_generator = self.neighborhood_kernel_generator
        radius_int = int(ceil(crop_radius))
        rbound = radius_int + 0.5
        bb = BoundingBox(points=((-rbound,-rbound), (rbound,rbound)))

        # Print parameters designed to match fm2d's output
        #print "%d rad= %d std= %f alpha= %f" % (topo.sim._time, radius_int, radius, single_connection_learning_rate)

        neighborhood_matrix = nk_generator(bounds=bb,xdensity=1,ydensity=1,
                                           size=2*radius)

        for r in range(rmin,rmax):
            for c in range(cmin,cmax):
                cwc = c - wc 
                rwr = r - wr 
                lattice_dist = L2norm((cwc,rwr))
                if lattice_dist <= crop_radius:
                    cf = cfs[r][c]
                    rate = single_connection_learning_rate * neighborhood_matrix[rwr+radius_int,cwc+radius_int]
                    X = cf.get_input_matrix(input_activity)
                    cf.weights += rate * (X - cf.weights)

                    # CEBHACKALERT: see ConnectionField.__init__()
                    cf.weights *= cf.mask