def do_spatial_learning(self, patterns, ignore_empty=False): """Learn spatial patterns. Args: patterns: a list of nodes' outputs from the previous layer or the sensor ignore_empty: a boolean indicating whether to ignore empty patterns """ # prepare patterns -- merge child outputs if necessary merged_patterns = [] flatten = self.nodes[0].sp.algorithm is not 'product' for i in range(np.prod(self.size)): node_patterns = itemgetter(*self.links[i])(patterns) if flatten: node_patterns = np.asarray(utils.flatten_list(node_patterns)) else: node_patterns = np.asarray(node_patterns) merged_patterns.append(node_patterns) if self.node_cloning: # one master node receives all the input for p in merged_patterns: # ignore empty patterns if self._is_first_level() and ignore_empty: if np.min(p) == np.max(p): continue self.nodes[0].do_spatial_learning(p) else: # each node gets its own input for n, p in zip(self.nodes, merged_patterns): # ignore empty patterns if self._is_first_level() and ignore_empty: if np.min(p) == np.max(p): continue n.do_spatial_learning(p)
def testFlatten(self): nested_list = [[1, 2, 3], [4, 5, 6]] correct_list = [1, 2, 3, 4, 5, 6] flatten_list = utils.flatten_list(nested_list) self.assertEqual(len(flatten_list), 6) self.assertListEqual(flatten_list, correct_list)
def do_temporal_learning(self, patterns, is_reset=False): for i, n in enumerate(self.nodes): if is_reset: n.do_temporal_learning(None, is_reset=True) else: node_patterns = itemgetter(*self.links[i])(patterns) node_patterns = np.asarray(utils.flatten_list(node_patterns)) n.do_temporal_learning(node_patterns)
def infer(self, patterns, merge_output=False): """Run infererence in each node in the level with the corresponding pattern from the previous level (input image). Args: patterns: a list of patterns from the level below or from the sensor merge_output: whether to return concatenated inference vector or a list of inference vectors for each pattern Returns: an inference vector of a list of inference vectors """ output = [] # nodes' outputs in the same order as in self.nodes for i, n in enumerate(self.nodes): node_patterns = itemgetter(*self.links[i])(patterns) node_patterns = np.asarray(utils.flatten_list(node_patterns)) output.append(n.infer(node_patterns)) if merge_output: # from [[output1], [output2]] makes [output1, output2] return np.asarray(utils.flatten_list(output)) else: return np.asarray(output)