Esempio n. 1
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"
Esempio n. 2
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)
Esempio n. 3
0
    def __setstate__(self,state_dict):
        """
        Method to support unpickling of sparse weights object.
        """

        self.__dict__.update(state_dict)
        self.weights = sparse.csarray_float(self.weight_shape[0],self.weight_shape[1])
        rowInds, colInds, values = self.triplets
        self.weights.setTriplets(rowInds,colInds,values)
        del self.triplets
        del self.weight_shape
Esempio n. 4
0
    def __setstate__(self,state_dict):
        """
        Method to support unpickling of sparse weights object.
        """

        self.__dict__.update(state_dict)
        self.weights = sparse.csarray_float(self.weight_shape[0],self.weight_shape[1])
        rowInds, colInds, values = self.triplets
        self.weights.setTriplets(rowInds,colInds,values)
        del self.triplets
        del self.weight_shape
Esempio n. 5
0
    def __call__(self, projection, **params):
        time = math.ceil(topo.sim.time())

        if self.disk_mask:
            self.disk = pattern.Disk(size=1.0,smoothing=0.0)

        # Get CF and src sheet shapes
        cf_x,cf_y = projection.dest.activity.shape
        src_x,src_y = projection.src.activity.shape

        # Initialize sparse triplet arrays
        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=sparse_type)

        # Create new sparse matrix to accumulate into
        sum_sparse = sparse.csarray_float(projection.src.activity.shape,projection.dest.activity.shape)

        # Counters for logging
        sprout_sum = 0; prune_sum = 0; unit_total = 0
        self.mask_total = 0

        if (time == 0):
            if not hasattr(self,"initial_conns"):
                self.initial_conns = {}
            self.initial_conns[projection.name] = projection.n_conns()
        elif (time % self.interval) == 0:
            idx=0
            for cidx,cf in enumerate(projection.flatcfs):
                temp_weights = cf.weights
                dense_unit_mask = (1.0 - (temp_weights>0.0))
                dim1,dim2 = temp_weights.shape

                sprout_count,prune_idx,nnz = self.calc_ratios(temp_weights)

                self.prune(temp_weights,prune_idx)
                nnz_pp = np.count_nonzero(temp_weights)
                prune_sum += (nnz_pp-nnz)

                self.sprout(temp_weights,dense_unit_mask,sprout_count)
                nnz_ps = np.count_nonzero(temp_weights)
                sprout_sum += nnz_ps - nnz_pp
                unit_total += nnz_ps

                # Populate sparse array chunk
                temp_sparse = sparse.csarray_float(projection.src.activity.shape,projection.dest.activity.shape)
                x1,x2,y1,y2 = cf.input_sheet_slice.tolist()
                for cnx in range(dim1):
                    val_array[idx:idx+dim2] = temp_weights[cnx,:]
                    x_val = (x1+cnx) * src_y + y1
                    x_array[idx:idx+dim2] = range(x_val,x_val+dim2)
                    y_array[idx:idx+dim2] = cidx
                    idx += dim2

                # Populate combined sparse array with sparse array chunk
                if (cidx+1)%cf_y == 0:
                    nnz_idx = val_array.nonzero()
                    temp_sparse.setTriplets(x_array[nnz_idx],y_array[nnz_idx],val_array[nnz_idx])
                    sum_sparse += temp_sparse
                    x_array *= 0; y_array *= 0; val_array *= 0.0
                    idx=0
            projection.weights = sum_sparse
            del temp_sparse, sum_sparse
            projection.weights.compress()

            self.message("%s pruned by %d and sprouted %d, connection is now %f%% dense" % (projection.name,prune_sum,sprout_sum,(float(unit_total)/self.mask_total)*100))
Esempio n. 6
0
    def __call__(self, projection, **params):
        time = math.ceil(topo.sim.time())

        if self.disk_mask:
            self.disk = ig.Disk(size=1.0,smoothing=0.0)

        # Get CF and src sheet shapes
        cf_x,cf_y = projection.dest.activity.shape
        src_x,src_y = projection.src.activity.shape

        # Initialize sparse triplet arrays
        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=sparse_type)

        # Create new sparse matrix to accumulate into
        sum_sparse = sparse.csarray_float(projection.src.activity.shape,projection.dest.activity.shape)

        # Counters for logging
        sprout_sum = 0; prune_sum = 0; unit_total = 0
        self.mask_total = 0

        if (time == 0):
            if not hasattr(self,"initial_conns"):
                self.initial_conns = {}
            self.initial_conns[projection.name] = projection.n_conns()
        elif (time % self.interval) == 0:
            idx=0
            for cidx,cf in enumerate(projection.flatcfs):
                temp_weights = cf.weights
                dense_unit_mask = (1.0 - (temp_weights>0.0))
                dim1,dim2 = temp_weights.shape

                sprout_count,prune_idx,nnz = self.calc_ratios(temp_weights)

                self.prune(temp_weights,prune_idx)
                nnz_pp = np.count_nonzero(temp_weights)
                prune_sum += (nnz_pp-nnz)

                if sprout_count:
                    self.sprout(temp_weights,dense_unit_mask,sprout_count)
                nnz_ps = np.count_nonzero(temp_weights)
                sprout_sum += nnz_ps - nnz_pp
                unit_total += nnz_ps

                # Populate sparse array chunk
                temp_sparse = sparse.csarray_float(projection.src.activity.shape,projection.dest.activity.shape)
                x1,x2,y1,y2 = cf.input_sheet_slice.tolist()
                for cnx in range(dim1):
                    val_array[idx:idx+dim2] = temp_weights[cnx,:]
                    x_val = (x1+cnx) * src_y + y1
                    x_array[idx:idx+dim2] = range(x_val,x_val+dim2)
                    y_array[idx:idx+dim2] = cidx
                    idx += dim2

                # Populate combined sparse array with sparse array chunk
                if (cidx+1)%cf_y == 0:
                    nnz_idx = val_array.nonzero()
                    temp_sparse.setTriplets(x_array[nnz_idx],y_array[nnz_idx],val_array[nnz_idx])
                    sum_sparse += temp_sparse
                    x_array *= 0; y_array *= 0; val_array *= 0.0
                    idx=0
            projection.weights = sum_sparse
            del temp_sparse, sum_sparse
            projection.weights.compress()