Exemplo n.º 1
0
    def __init__(self,cf,input_sheet,x=0.0,y=0.0,template=BoundingBox(radius=0.1),
                 mask=patterngenerator.Constant(),
                 min_matrix_radius=1):
        """
        From an existing copy of ConnectionField (CF) that acts as a
        template, create a new CF that shares weights with the
        template CF.  Copies all the properties of CF to stay
        identical except the weights variable that actually contains
        the data.
        
        The only difference from a normal CF is that the weights of
        the CF are implemented as a numpy view into the single master
        copy of the weights stored in the CF template.
        """
        # CEBALERT: There's no call to super's __init__; see JAHACKALERT
        # below.
        template = copy(template)

        if not isinstance(template,Slice):
            template = Slice(template,input_sheet,force_odd=True,
                             min_matrix_radius=min_matrix_radius)

        # Note: if passed in, mask is shared between CFs (but not if created here)
        if not hasattr(mask,'view'):
            mask = _create_mask(patterngenerator.Constant(),
                               template.compute_bounds(input_sheet),
                               input_sheet,True,0.5) 



        self._has_norm_total=False
        self.mask=mask 
        weights_slice = self._create_input_sheet_slice(input_sheet,x,y,template,min_matrix_radius=min_matrix_radius)
        self.weights = weights_slice.submatrix(cf.weights)
Exemplo n.º 2
0
    def __init__(self,cf,input_sheet,x=0.0,y=0.0,template=BoundingBox(radius=0.1),
                 mask=patterngenerator.Constant(),
                 min_matrix_radius=1):
        """
        From an existing copy of ConnectionField (CF) that acts as a
        template, create a new CF that shares weights with the
        template CF.  Copies all the properties of CF to stay
        identical except the weights variable that actually contains
        the data.
        
        The only difference from a normal CF is that the weights of
        the CF are implemented as a numpy view into the single master
        copy of the weights stored in the CF template.
        """
        # CEBALERT: There's no call to super's __init__; see JAHACKALERT
        # below.
        template = copy(template)

        if not isinstance(template,Slice):
            template = Slice(template,input_sheet,force_odd=True,
                             min_matrix_radius=min_matrix_radius)

        # Note: if passed in, mask is shared between CFs (but not if created here)
        if not hasattr(mask,'view'):
            mask = _create_mask(patterngenerator.Constant(),
                               template.compute_bounds(input_sheet),
                               input_sheet,True,0.5) 



        self._has_norm_total=False
        self.mask=mask 
        weights_slice = self._create_input_sheet_slice(input_sheet,x,y,template,min_matrix_radius=min_matrix_radius)
        self.weights = weights_slice.submatrix(cf.weights)
Exemplo n.º 3
0
    def _create_cfs(self):
        """
        Creates the CF objects, initializing the weights one by one
        and adding them to the sparse weights object in chunks.
        """

        vectorized_create_cf = simple_vectorize(self._create_cf)
        self.cfs = vectorized_create_cf(*self._generate_coords())
        self.flatcfs = list(self.cfs.flat)
        self.weights = sparse.csarray_float(self.src.activity.shape,
                                            self.dest.activity.shape)

        cf_x, cf_y = self.dest.activity.shape
        src_x, src_y = self.src.activity.shape

        y_array = np.zeros((src_x * src_y * cf_y), dtype=np.int32)
        x_array = np.zeros((src_x * src_y * cf_y), dtype=np.int32)
        val_array = np.zeros((src_x * src_y * cf_y), dtype=np.float32)

        # Iterate over the CFs
        for x in range(cf_x):
            temp_sparse = sparse.csarray_float(self.src.activity.shape,
                                               self.dest.activity.shape)
            idx = 0
            for y in range(cf_y):
                x1, x2, y1, y2 = self.cfs[x][y].input_sheet_slice.tolist()
                if self.same_cf_shape_for_all_cfs:
                    mask_template = self.mask_template
                else:
                    mask_template = _create_mask(self.cf_shape,
                                                 self.bounds_template,
                                                 self.src, self.autosize_mask,
                                                 self.mask_threshold)
                weights = self.cfs[x][y]._init_weights(mask_template)
                cn_x, cn_y = weights.shape
                y_val = x * cf_y + y
                for cnx in range(cn_x):
                    val_array[idx:idx + cn_y] = weights[cnx, :]
                    x_val = (x1 + cnx) * src_y + y1
                    x_array[idx:idx + cn_y] = range(x_val, x_val + cn_y)
                    y_array[idx:idx + cn_y] = y_val
                    idx += cn_y
            nnz_idx = val_array.nonzero()
            temp_sparse.setTriplets(x_array[nnz_idx], y_array[nnz_idx],
                                    val_array[nnz_idx])
            self.weights += temp_sparse
            x_array *= 0
            y_array *= 0
            val_array *= 0.0
        del temp_sparse
        self.weights.compress()
        self.apply_learn_output_fns()
        print self.name, "loaded"
Exemplo n.º 4
0
    def _create_cfs(self):
        """
        Creates the CF objects, initializing the weights one by one
        and adding them to the sparse weights object in chunks.
        """

        vectorized_create_cf = simple_vectorize(self._create_cf)
        self.cfs = vectorized_create_cf(*self._generate_coords())
        self.flatcfs = list(self.cfs.flat)
        self.weights = sparse.csarray_float(self.src.activity.shape,self.dest.activity.shape)

        cf_x,cf_y = self.dest.activity.shape
        src_x,src_y = self.src.activity.shape

        y_array = np.zeros((src_x*src_y*cf_y),dtype=np.int32)
        x_array = np.zeros((src_x*src_y*cf_y),dtype=np.int32)
        val_array = np.zeros((src_x*src_y*cf_y),dtype=np.float32)

        # Iterate over the CFs
        for x in range(cf_x):
            temp_sparse = sparse.csarray_float(self.src.activity.shape,self.dest.activity.shape)
            idx = 0
            for y in range(cf_y):
                cf = self.cfs[x][y]
                label = cf.label + ('-%d' % self.seed if self.seed is not None else '')
                name = "%s_CF (%.5f, %.5f)" % ('' if label is None else label, cf.x,cf.y)
                x1,x2,y1,y2 = cf.input_sheet_slice.tolist()
                if self.same_cf_shape_for_all_cfs:
                    mask_template = self.mask_template
                else:
                    mask_template = _create_mask(self.cf_shape,self.bounds_template,
                                                 self.src,self.autosize_mask,
                                                 self.mask_threshold, name=name)
                weights = self.cfs[x][y]._init_weights(mask_template)
                cn_x,cn_y = weights.shape
                y_val = x * cf_y + y
                for cnx in range(cn_x):
                    val_array[idx:idx+cn_y] = weights[cnx,:]
                    x_val = (x1+cnx) * src_y + y1
                    x_array[idx:idx+cn_y] = range(x_val,x_val+cn_y)
                    y_array[idx:idx+cn_y] = y_val
                    idx += cn_y
            nnz_idx = val_array.nonzero()
            temp_sparse.setTriplets(x_array[nnz_idx],y_array[nnz_idx],val_array[nnz_idx])
            self.weights += temp_sparse
            x_array *= 0; y_array *= 0; val_array *= 0.0
        del temp_sparse
        self.weights.compress()
        self.debug("Sparse projection %r loaded" % self.name)
Exemplo n.º 5
0
    def __init__(self,initialize_cfs=True,**params):
        """
        Initialize the Projection with a set of cf_type objects
        (typically SparseConnectionFields), each located at the
        location in the source sheet corresponding to the unit in the
        target sheet. The cf_type objects are stored in the 'cfs'
        array.

        The nominal_bounds_template specified may be altered: the
        bounds must be fitted to the Sheet's matrix, and the weights
        matrix must have odd dimensions. These altered bounds are
        passed to the individual connection fields.

        A mask for the weights matrix is constructed. The shape is
        specified by cf_shape; the size defaults to the size
        of the nominal_bounds_template.
        """

        super(CFProjection,self).__init__(**params)

        self.weights_generator.set_dynamic_time_fn(None,sublistattr='generators')
        # get the actual bounds_template by adjusting a copy of the
        # nominal_bounds_template to ensure an odd slice, and to be
        # cropped to sheet if necessary
        self._slice_template = Slice(copy(self.nominal_bounds_template),
                                     self.src,force_odd=True,
                                     min_matrix_radius=self.min_matrix_radius)

        self.bounds_template = self._slice_template.compute_bounds(self.src)

        self.mask_template = _create_mask(self.cf_shape,self.bounds_template,
                                         self.src,self.autosize_mask,
                                         self.mask_threshold)

        self.n_units = self._calc_n_units()

        self.activity = np.array(self.dest.activity)
        self.norm_total = np.array(self.dest.activity,dtype=np.float64)
        self.has_norm_total = False

        if initialize_cfs:
            self._create_cfs()

        if self.apply_output_fns_init:
            self.apply_learn_output_fns()

        self.input_buffer = None
Exemplo n.º 6
0
    def _init_weights(self,mask_template):

        if not hasattr(mask_template,'view'):
            mask = _create_mask(mask_template,self.weights_slice.compute_bounds(self.input_sheet),self.input_sheet,True,0.5)

        mask = self.weights_slice.submatrix(mask_template)
        mask = np.array(mask,copy=1)

        w = self.weights_generator(x=self.x,y=self.y,bounds=self.get_bounds(self.input_sheet),
                                   xdensity=self.input_sheet.xdensity,
                                   ydensity=self.input_sheet.ydensity,
                                   mask=mask)

        w = w.astype(sparse_type)

        for of in self.output_fns:
            of(w)

        return w
Exemplo n.º 7
0
    def _init_weights(self,mask_template):

        if not hasattr(mask_template,'view'):
            mask = _create_mask(mask_template,self.weights_slice.compute_bounds(self.input_sheet),self.input_sheet,True,0.5)

        mask = self.weights_slice.submatrix(mask_template)
        mask = np.array(mask,copy=1)

        w = self.weights_generator(x=self.x,y=self.y,bounds=self.get_bounds(self.input_sheet),
                                   xdensity=self.input_sheet.xdensity,
                                   ydensity=self.input_sheet.ydensity,
                                   mask=mask)

        w = w.astype(sparse_type)

        for of in self.output_fns:
            of(w)

        return w
Exemplo n.º 8
0
    def _init_weights(self, mask_template):

        if not hasattr(mask_template, "view"):
            mask = _create_mask(
                mask_template, self.weights_slice.compute_bounds(self.input_sheet), self.input_sheet, True, 0.5
            )

        mask = self.weights_slice.submatrix(mask_template)
        mask = np.array(mask, copy=1)

        pattern_params = dict(
            x=self.x,
            y=self.y,
            bounds=self.get_bounds(self.input_sheet),
            xdensity=self.input_sheet.xdensity,
            ydensity=self.input_sheet.ydensity,
            mask=mask,
        )

        controlled_weights = (
            param.Dynamic.time_dependent
            and isinstance(param.Dynamic.time_fn, param.Time)
            and self.independent_weight_generation
        )

        if controlled_weights:
            with param.Dynamic.time_fn as t:
                t(0)  # Initialize at time zero.
                # Controls random streams
                label = "" if self.label is None else self.label
                name = "%s_CF (%.5f, %.5f)" % (label, self.x, self.y)
                w = self.weights_generator(**dict(pattern_params, name=name))
        else:
            w = self.weights_generator(**pattern_params)

        w = w.astype(sparse_type)

        for of in self.output_fns:
            of(w)

        return w
Exemplo n.º 9
0
    def _init_weights(self,mask_template):

        if not hasattr(mask_template,'view'):
            mask = _create_mask(mask_template,
                                self.weights_slice.compute_bounds(
                                    self.input_sheet),
                                self.input_sheet,True,0.5)

        mask = self.weights_slice.submatrix(mask_template)
        mask = np.array(mask,copy=1)



        pattern_params = dict(x=self.x,y=self.y,
                              bounds=self.get_bounds(self.input_sheet),
                              xdensity=self.input_sheet.xdensity,
                              ydensity=self.input_sheet.ydensity,
                              mask=mask)

        controlled_weights = (param.Dynamic.time_dependent
                              and isinstance(param.Dynamic.time_fn,
                                             param.Time)
                              and self.independent_weight_generation)

        if controlled_weights:
            with param.Dynamic.time_fn as t:
                t(0)                        # Initialize at time zero.
                # Controls random streams
                label = '' if self.label is None else self.label
                name = "%s_CF (%.5f, %.5f)" % (label, self.x, self.y)
                w = self.weights_generator(**dict(pattern_params,
                                                  name=name))
        else:
            w = self.weights_generator(**pattern_params)

        w = w.astype(sparse_type)

        for of in self.output_fns:
            of(w)

        return w