Пример #1
0
    def __init__(self, params=None, debug=False):
        """
        Args:
            params: dict with parameters for spatial and temporal poolers
            debug: whether to print debug messages
        """
        params = utils.fill_default_params(params, default_params)
        self.debug = debug

        assert (params['maxCoincidenceCount'] > params['requestedGroupsCount'])
        if params['spatialPoolerAlgorithm'] == 'gaussian':
            assert (params['sigma'] >= 0)
        assert (params['maxDistance'] >= 0)

        self.sp = SpatialPooler(
            algorithm=params['spatialPoolerAlgorithm'],
            max_distance=params['maxDistance'],
            sigma=params['sigma'],
            max_coincidence_count=params['maxCoincidenceCount'],
            rare_coincidence_threshold=params['rareCoincidenceThreshold'],
            ignore_background_pattern=params['ignoreBackgroundPattern'],
            background_color=params['backgroundColor'],
            debug=self.debug)

        self.tp = TemporalPooler(
            algorithm=params['temporalPoolerAlgorithm'],
            transition_memory=params['transitionMemory'],
            requested_group_count=params['requestedGroupsCount'],
            symmetrizeTAM=params['symmetrizeTAM'],
            debug=self.debug)

        self.tp_first_run = True
        self.y = None  # spatial inference in fw direction
        self.fw_mesage = None  # forward output of the node
Пример #2
0
    def __init__(self, level_no, params=None, previous_level_size=None,
                 debug=False):

        self.level_no = level_no
        self.debug = debug
        if previous_level_size is None:
            raise ValueError("previous_level_size must not be None.")

        self.mode = 'learn'  # learn or infer modes
        self.nodes = []  # nodes in the current level

        if params is not None:
            params = utils.fill_default_params(params, default_params)
            self.node_cloning = params['nodeCloning']
            self.size = params['size']  # num. of nodes [width, height]
            self.overlap = params['overlap']
            self.sparsify = params['sparsify']  # TODO

            # Create nodes
            if self.node_cloning:
                if self.debug:
                    print("Level %d: Creating one master node." % self.level_no)
                # create one master node
                self.nodes.append(BaseNode(params, debug=self.debug))
            else:
                if self.debug:
                    print("Level %d: Creating %d nodes." %
                          (self.level_no, np.prod(params['size'])))
                # create all nodes
                for i in range(np.prod(params['size'])):
                    self.nodes.append(BaseNode(params, debug=self.debug))
        else:
            raise ValueError("params must not be of type None.")

        # node_id: [list of node_ids from level-1]
        self.links = {}
        if self._is_first_level():
            # in 1-to-1 correnspondence to image patches
            for i in range(np.prod(self.size)):
                self.links[i] = [i]
        else:
            # TODO for rectangular regions
            # TODO factorize so it can be tested
            # every node in this level gets linked with equal number of nodes
            # in the previous level arraged in squares
            #
            # Example for [4, 4] -> [2,2]
            # 12 13 14 15      0: [0, 1, 4, 5]
            #  8  9 10 11      1: [2, 3, 6, 7]
            #  4  5  6  7  ->  2: [8, 9, 12, 13]
            #  0  1  2  3      3: [10, 11, 14, 15]
            #
            self.links = self._get_links(self.size, previous_level_size)

        self.above_links = None  # backwards mapping to self.link