def _install_experts(self): self._top_level_flock_node = SpatialPoolerFlockNode( self._create_expert_params()) self._flock_nodes = [ ExpertFlockNode(self._create_expert_params()), ExpertFlockNode(self._create_expert_params()), self._top_level_flock_node ] for node in self._flock_nodes: self.add_node(node) unsqueeze_node_0 = UnsqueezeNode(0) self.add_node(unsqueeze_node_0) unsqueeze_node_1 = UnsqueezeNode(0) self.add_node(unsqueeze_node_1) Connector.connect(self.se_io.outputs.image_output, unsqueeze_node_0.inputs.input) Connector.connect(unsqueeze_node_0.outputs.output, self._flock_nodes[0].inputs.sp.data_input) Connector.connect(self._flock_nodes[0].outputs.tp.projection_outputs, self._flock_nodes[1].inputs.sp.data_input) Connector.connect(self._flock_nodes[1].outputs.tp.projection_outputs, self._join_node.inputs[0]) Connector.connect(self.se_io.outputs.task_to_agent_label, self._join_node.inputs[1]) Connector.connect(self._join_node.outputs.output, unsqueeze_node_1.inputs.input) Connector.connect(unsqueeze_node_1.outputs.output, self._top_level_flock_node.inputs.sp.data_input)
def _install_experts(self): im_width = self.se_io.get_image_width() im_height = self.se_io.get_image_height() self.input_dims = torch.Size((im_height, im_width, 3)) self.parent_rf_dims = Size2D(im_height // self.EXPERTS_IN_X, im_width // self.EXPERTS_IN_X) lrf_node = ReceptiveFieldNode(input_dims=self.input_dims, parent_rf_dims=self.parent_rf_dims) self.add_node(lrf_node) self._top_level_flock_node = SpatialPoolerFlockNode(self.parent_params) self._mid_node = ExpertFlockNode(self.mid_expert_params) self._conv_node = ConvExpertFlockNode(self.conv_flock_params) self.add_node(self._top_level_flock_node) self.add_node(self._mid_node) self.add_node(self._conv_node) def rescale(inputs, outputs): if self.TRAINING: outputs[0].copy_( inputs[0] * 1000) # large constant to make the label more important else: outputs[0].copy_(inputs[0] * float('nan')) self._rescale_node = LambdaNode(rescale, 1, [(self.se_io.get_num_labels(), )]) self.add_node(self._rescale_node) unsqueeze_node_0 = UnsqueezeNode(0) self.add_node(unsqueeze_node_0) unsqueeze_node_1 = UnsqueezeNode(0) self.add_node(unsqueeze_node_1) Connector.connect(self.se_io.outputs.image_output, lrf_node.inputs.input) Connector.connect(lrf_node.outputs.output, self._conv_node.inputs.sp.data_input) Connector.connect(self._conv_node.outputs.tp.projection_outputs, unsqueeze_node_0.inputs.input) Connector.connect(unsqueeze_node_0.outputs.output, self._mid_node.inputs.sp.data_input) Connector.connect(self._mid_node.outputs.tp.projection_outputs, self._join_node.inputs[0]) Connector.connect(self.se_io.outputs.task_to_agent_label, self._rescale_node.inputs[0]) Connector.connect(self._rescale_node.outputs[0], self._join_node.inputs[1]) Connector.connect(self._join_node.outputs.output, unsqueeze_node_1.inputs.input) Connector.connect(unsqueeze_node_1.outputs.output, self._top_level_flock_node.inputs.sp.data_input)
def topology_simple_dual_loop(self): dataset_node = DatasetSequenceMNISTNode( DatasetMNISTParams(one_hot_labels=False, examples_per_class=1), DatasetSequenceMNISTNodeParams(seqs=[[0, 1, 2], [0, 1, 3]])) flock_size = 1 unsqueeze_node_child = UnsqueezeNode(0) unsqueeze_node_parent = UnsqueezeNode(0) expand_node_child = ExpandNode(0, flock_size) expand_node_parent = ExpandNode(0, flock_size) expert_node_child = ExpertFlockNode( ExpertParams(flock_size=flock_size, n_cluster_centers=4, spatial=SpatialPoolerParams(), temporal=TemporalPoolerParams(incoming_context_size=2, n_providers=2, n_frequent_seqs=8, seq_length=3, seq_lookahead=1))) expert_node_parent = ExpertFlockNode( ExpertParams(flock_size=flock_size, n_cluster_centers=2, spatial=SpatialPoolerParams())) self.add_node(dataset_node) self.add_node(unsqueeze_node_child) self.add_node(unsqueeze_node_parent) self.add_node(expand_node_child) self.add_node(expand_node_parent) self.add_node(expert_node_child) self.add_node(expert_node_parent) Connector.connect(dataset_node.outputs.data, unsqueeze_node_child.inputs.input) Connector.connect(dataset_node.outputs.sequence_id, unsqueeze_node_parent.inputs.input) Connector.connect(unsqueeze_node_child.outputs.output, expand_node_child.inputs.input) Connector.connect(unsqueeze_node_parent.outputs.output, expand_node_parent.inputs.input) Connector.connect(expand_node_child.outputs.output, expert_node_child.inputs.sp.data_input) Connector.connect(expand_node_parent.outputs.output, expert_node_parent.inputs.sp.data_input) Connector.connect(expert_node_parent.outputs.output_context, expert_node_child.inputs.tp.context_input, is_backward=True)
def _add_middle_layer(self, use_temporal_pooler: bool, n_middle_layer_cluster_centers: int): unsqueeze_node = UnsqueezeNode(dim=0) self._add_layer(unsqueeze_node, unsqueeze_node.inputs.input, unsqueeze_node.outputs.output) self._add_layer(*self._make_middle_layer_expert( use_temporal_pooler, n_middle_layer_cluster_centers))
def _create_and_connect_agent(self, join_node: JoinNode, fork_node: ForkNode): params = ExpertParams() params.flock_size = 1 params.n_cluster_centers = 28 params.compute_reconstruction = True params.spatial.cluster_boost_threshold = 1000 params.spatial.buffer_size = 500 params.spatial.batch_size = 500 params.spatial.learning_rate = 0.3 params.spatial.learning_period = 50 # conv_expert = ConvExpertFlockNode(params, name="Conv. expert") # conv_expert = SpatialPoolerFlockNode(params, name=" SP") conv_expert = ExpertFlockNode(params, name=" expert") self.add_node(conv_expert) unsqueeze_node_0 = UnsqueezeNode(0) self.add_node(unsqueeze_node_0) Connector.connect(join_node.outputs.output, unsqueeze_node_0.inputs.input) Connector.connect(unsqueeze_node_0.outputs.output, conv_expert.inputs.sp.data_input) def squeeze(inputs, outputs): outputs[0].copy_(inputs[0].squeeze(0)) squeeze_node = LambdaNode(squeeze, 1, [(sum(fork_node._split_sizes),)], name="squeeze lambda node") self.add_node(squeeze_node) Connector.connect(conv_expert.outputs.sp.predicted_reconstructed_input, squeeze_node.inputs[0]) Connector.connect(squeeze_node.outputs[0], fork_node.inputs.input)
def _install_experts(self, flock_params: List[ExpertParams], model_seed: int): self.flock_nodes = [ ExpertFlockNode(flock_params[0], seed=model_seed), ExpertFlockNode(flock_params[1], seed=model_seed), SpatialPoolerFlockNode(flock_params[2], seed=model_seed) ] self._top_level_flock_node = self.flock_nodes[-1] for node in self.flock_nodes: self.add_node(node) self._join_node = JoinNode(flatten=True) self.add_node(self._join_node) self._install_rescale(self._label_scale) unsqueeze_node = UnsqueezeNode(0) self.add_node(unsqueeze_node) # image -> unsqueeze_node -> SP1 -> SP2 -> join Connector.connect(self.se_io.outputs.image_output, unsqueeze_node.inputs.input) Connector.connect(unsqueeze_node.outputs.output, self.flock_nodes[0].inputs.sp.data_input) Connector.connect(self.flock_nodes[0].outputs.tp.projection_outputs, self.flock_nodes[1].inputs.sp.data_input) Connector.connect(self.flock_nodes[1].outputs.tp.projection_outputs, self._join_node.inputs[0]) # label -> rescale ----> join Connector.connect(self.se_io.outputs.task_to_agent_label, self._rescale_node.inputs[0]) Connector.connect(self._rescale_node.outputs[0], self._join_node.inputs[1]) unsqueeze_node_2 = UnsqueezeNode(0) self.add_node(unsqueeze_node_2) # join -> unsqueeze_node -> top_level_expert Connector.connect(self._join_node.outputs.output, unsqueeze_node_2.inputs.input) Connector.connect(unsqueeze_node_2.outputs.output, self._top_level_flock_node.inputs.sp.data_input)
def _install_experts(self): self._top_level_flock_node = SpatialPoolerFlockNode(self._create_expert_params()) self.add_node(self._top_level_flock_node) self.unsqueeze_node = UnsqueezeNode(0) self.add_node(self.unsqueeze_node) Connector.connect(self.se_io.outputs.image_output, self._join_node.inputs[0]) Connector.connect(self.se_io.outputs.task_to_agent_label, self._join_node.inputs[1]) Connector.connect(self._join_node.outputs[0], self.unsqueeze_node.inputs.input) Connector.connect(self.unsqueeze_node.outputs.output, self._top_level_flock_node.inputs.sp.data_input)
def _install_experts(self): lrf_node = ReceptiveFieldNode(input_dims=self.input_dims, parent_rf_dims=self.parent_rf_dims) self.add_node(lrf_node) self._top_level_flock_node = SpatialPoolerFlockNode(self.parent_params, name="Parent 1 SP") self._conv_node = ConvSpatialPoolerFlockNode(self.conv_flock_params, name="Conv SP flock") self.add_node(self._top_level_flock_node) self.add_node(self._conv_node) scale = 1000 def rescale_up(inputs, outputs): outputs[0].copy_(inputs[0] * scale) def rescale_down(inputs, outputs): outputs[0].copy_(inputs[0] / scale) self._rescale_up_node = LambdaNode(rescale_up, 1, [(20, )], name="upscale 1000") self.add_node(self._rescale_up_node) self._rescale_down_node = LambdaNode( rescale_down, 1, [(1, self._top_level_expert_output_size() + 20)], name="downscale 1000") self.add_node(self._rescale_down_node) unsqueeze_node = UnsqueezeNode(0) self.add_node(unsqueeze_node) Connector.connect(self.se_io.outputs.image_output, lrf_node.inputs.input) Connector.connect(lrf_node.outputs.output, self._conv_node.inputs.sp.data_input) Connector.connect(self._conv_node.outputs.sp.forward_clusters, self._join_node.inputs[0]) Connector.connect(self.se_io.outputs.task_to_agent_label, self._rescale_up_node.inputs[0]) Connector.connect(self._rescale_up_node.outputs[0], self._join_node.inputs[1]) Connector.connect(self._join_node.outputs.output, unsqueeze_node.inputs.input) Connector.connect(unsqueeze_node.outputs.output, self._top_level_flock_node.inputs.sp.data_input) Connector.connect( self._top_level_flock_node.outputs.sp.current_reconstructed_input, self._rescale_down_node.inputs[0])
def topology_lookahead_goal(self): dataset_node = DatasetSequenceMNISTNode( DatasetMNISTParams(one_hot_labels=False, examples_per_class=1), DatasetSequenceMNISTNodeParams( seqs=[[0, 1, 2], [3, 4, 5], [6, 7, 8]], transition_probs=np.array([[0, 0.5, 0.5], [1, 0, 0], [1, 0, 0]]))) unsqueeze_node_child = UnsqueezeNode(0) unsqueeze_node_parent = UnsqueezeNode(0) expert_node_child = ExpertFlockNode( ExpertParams(flock_size=1, n_cluster_centers=9, spatial=SpatialPoolerParams(), temporal=TemporalPoolerParams(n_frequent_seqs=20, seq_length=3, seq_lookahead=1))) expert_node_parent = ExpertFlockNode( ExpertParams(flock_size=1, n_cluster_centers=3, spatial=SpatialPoolerParams())) self.add_node(dataset_node) self.add_node(unsqueeze_node_child) self.add_node(unsqueeze_node_parent) self.add_node(expert_node_child) self.add_node(expert_node_parent) Connector.connect(dataset_node.outputs.data, unsqueeze_node_child.inputs.input) Connector.connect(dataset_node.outputs.sequence_id, unsqueeze_node_parent.inputs.input) Connector.connect(unsqueeze_node_child.outputs.output, expert_node_child.inputs.sp.data_input) Connector.connect(unsqueeze_node_parent.outputs.output, expert_node_parent.inputs.sp.data_input) Connector.connect(expert_node_parent.outputs.output_context, expert_node_child.inputs.tp.context_input, is_backward=True)
def _install_experts(self, flock_params: List[ExpertParams], model_seed: int): self.flock_nodes = [ConvExpertFlockNode(flock_params[0], seed=model_seed), SpatialPoolerFlockNode(flock_params[1], seed=model_seed)] self._top_level_flock_node = self.flock_nodes[-1] for node in self.flock_nodes: self.add_node(node) # lrf self._install_lrf(self._image_size.value, self._experts_on_x) # join output of expert and label self._join_node = JoinNode(flatten=True) self.add_node(self._join_node) # scale node self._install_rescale(self._label_scale) # image -> LRF -> E1 -> join Connector.connect( self.se_io.outputs.image_output, self._node_lrf.inputs.input) Connector.connect( self._node_lrf.outputs.output, self.flock_nodes[0].inputs.sp.data_input) Connector.connect( self.flock_nodes[0].outputs.tp.projection_outputs, self._join_node.inputs[0]) # label -> rescale ---> join Connector.connect( self.se_io.outputs.task_to_agent_label, self._rescale_node.inputs[0]) Connector.connect( self._rescale_node.outputs[0], self._join_node.inputs[1]) self.unsqueeze_node = UnsqueezeNode(0) self.add_node(self.unsqueeze_node) # join -> unsqueeze_node -> top_level_expert Connector.connect( self._join_node.outputs.output, self.unsqueeze_node.inputs.input) Connector.connect( self.unsqueeze_node.outputs.output, self._top_level_flock_node.inputs.sp.data_input)
def __init__(self): super().__init__(device='cuda') # One MNIST producing two sequences, and one ExpertFlock learning to recognize them self.expert_params = ExpertParams() self.expert_params.flock_size = 1 self.expert_params.spatial.input_size = 28 * 28 self.expert_params.n_cluster_centers = 5 self.expert_params.spatial.buffer_size = 100 self.expert_params.spatial.batch_size = 50 self.expert_params.spatial.learning_period = 10 self.expert_params.spatial.cluster_boost_threshold = 100 self.expert_params.temporal.seq_length = 3 self.expert_params.temporal.seq_lookahead = 1 self.expert_params.temporal.buffer_size = 100 self.expert_params.temporal.batch_size = 50 self.expert_params.temporal.learning_period = 50 + self.expert_params.temporal.seq_lookbehind self.expert_params.temporal.incoming_context_size = 1 self.expert_params.temporal.max_encountered_seqs = 100 self.expert_params.temporal.n_frequent_seqs = 20 self.expert_params.temporal.forgetting_limit = 1000 mnist_seq_params = DatasetSequenceMNISTNodeParams( [[0, 1, 2], [3, 1, 4]], np.array([[0.9, 0.1], [0.9, 0.1]])) mnist_params = DatasetMNISTParams(class_filter=[0, 1, 2, 3, 4], one_hot_labels=False, examples_per_class=1) mnist_seq_node = DatasetSequenceMNISTNode(params=mnist_params, seq_params=mnist_seq_params, seed=5) # mnist_node = DatasetMNISTNode(params=mnist_params, seed=5) expert_node = ExpertFlockNode(self.expert_params, seed=2) # self.add_node(mnist_node) self.add_node(mnist_seq_node) self.add_node(expert_node) unsqueeze_node_0 = UnsqueezeNode(0) self.add_node(unsqueeze_node_0) Connector.connect(mnist_seq_node.outputs.data, unsqueeze_node_0.inputs.input) Connector.connect(unsqueeze_node_0.outputs.output, expert_node.inputs.sp.data_input)
def __init__(self, input_data_size: int, labels_size: int, sp_params: Optional[ExpertParams] = None, name: str = "", seed: Optional[int] = None): super().__init__("SpReconstructionLayer", inputs=ClassificationInputs(self), outputs=ClassificationOutputs(self)) join_node = JoinNode(n_inputs=2, flatten=True, name=name + " Join") self.add_node(join_node) self.join_node = join_node Connector.connect(self.inputs.data.output, join_node.inputs[0]) Connector.connect(self.inputs.label.output, join_node.inputs[1]) unsqueeze_node = UnsqueezeNode(0) self.add_node(unsqueeze_node) Connector.connect(join_node.outputs.output, unsqueeze_node.inputs.input) if sp_params is None: sp_params = ExpertParams() sp_node = SpatialPoolerFlockNode(sp_params, name=name + " SP Expert", seed=seed) self.add_node(sp_node) self.sp_node = sp_node Connector.connect(unsqueeze_node.outputs.output, sp_node.inputs.sp.data_input) fork_node = ForkNode(1, [input_data_size, labels_size], name=name + " Fork") self.add_node(fork_node) self.fork_node = fork_node Connector.connect(sp_node.outputs.sp.current_reconstructed_input, fork_node.inputs.input) Connector.connect(fork_node.outputs[1], self.outputs.label.input)
def __init__(self): super().__init__(device='cuda') actions_descriptor = GridWorldActionDescriptor() node_action_monitor = ActionMonitorNode(actions_descriptor) params = GridWorldParams(map_name='MapTwoRoom', reset_strategy=ResetStrategy.ANYWHERE) noise_params = RandomNoiseParams(amplitude=0.0001) node_grid_world = GridWorldNode(params) expert_params = ExpertParams() unsqueeze_node = UnsqueezeNode(dim=0) noise_node = RandomNoiseNode(noise_params) one_hot_node = ToOneHotNode() def f(inputs, outputs): probs = inputs[0] outputs[0].copy_(probs[0, -1, :4] + SMALL_CONSTANT) action_parser = LambdaNode(func=f, n_inputs=1, output_shapes=[(4, )]) expert_params.flock_size = 1 expert_params.n_cluster_centers = 64 expert_params.produce_actions = True expert_params.temporal.seq_length = 17 expert_params.temporal.seq_lookahead = 13 expert_params.temporal.n_frequent_seqs = 700 expert_params.temporal.max_encountered_seqs = 1000 expert_params.temporal.exploration_probability = 0.05 expert_params.temporal.batch_size = 200 expert_params.temporal.buffer_size = 1000 expert_params.temporal.own_rewards_weight = 20 expert_params.temporal.frustration_threshold = 2 expert_params.temporal.compute_backward_pass = True expert_params.compute_reconstruction = True expert_node = ConvExpertFlockNode(expert_params) #expert_node = ExpertFlockNode(expert_params) self.add_node(node_grid_world) self.add_node(node_action_monitor) self.add_node(expert_node) self.add_node(unsqueeze_node) self.add_node(action_parser) self.add_node(noise_node) self.add_node(one_hot_node) Connector.connect(node_grid_world.outputs.egocentric_image_action, noise_node.inputs.input) Connector.connect(noise_node.outputs.output, unsqueeze_node.inputs.input) Connector.connect(unsqueeze_node.outputs.output, expert_node.inputs.sp.data_input) Connector.connect(node_grid_world.outputs.reward, expert_node.inputs.tp.reward_input) Connector.connect(expert_node.outputs.sp.predicted_reconstructed_input, action_parser.inputs[0]) Connector.connect(action_parser.outputs[0], one_hot_node.inputs.input) Connector.connect(one_hot_node.outputs.output, node_action_monitor.inputs.action_in) Connector.connect(node_action_monitor.outputs.action_out, node_grid_world.inputs.agent_action, is_backward=True)
def __init__(self): super().__init__("cuda") actions_descriptor = GridWorldActionDescriptor() node_action_monitor = ActionMonitorNode(actions_descriptor) grid_world_params = GridWorldParams('MapE') grid_world_params.tile_size = 5 node_grid_world = GridWorldNode(grid_world_params) join_node = JoinNode(flatten=True) unsqueeze_node = UnsqueezeNode(dim=0) # GridWorld sizes # egocentric width = grid_world_params.egocentric_width * grid_world_params.tile_size height = grid_world_params.egocentric_height * grid_world_params.tile_size #one-hot matrix width = grid_world_params.world_width height = grid_world_params.world_height fork_node = ForkNode(dim=0, split_sizes=[width * height, 4]) input_size = (1, width * height + 4) random_noise_node_params = RandomNoiseParams() random_noise_node_params.amplitude = 0.1 random_noise_node = RandomNoiseNode(random_noise_node_params) def squeeze(inputs, outputs): outputs[0].copy_(inputs[0].view(-1)) squeeze_node = LambdaNode(squeeze, 1, [(sum(fork_node._split_sizes), )], name="squeeze lambda node") to_one_hot_node = ToOneHotNode() random_action_generator = RandomNumberNode(upper_bound=4) self.add_node(squeeze_node) self.add_node(node_grid_world) self.add_node(unsqueeze_node) self.add_node(node_action_monitor) self.add_node(join_node) self.add_node(fork_node) self.add_node(random_noise_node) self.add_node(to_one_hot_node) self.add_node(random_action_generator) Connector.connect(node_grid_world.outputs.egocentric_image, random_noise_node.inputs.input) # egocentric # Connector.connect(random_noise_node.outputs.output, join_node.inputs[0]) # one-hot matrix Connector.connect(node_grid_world.outputs.output_pos_one_hot_matrix, join_node.inputs[0]) Connector.connect(node_grid_world.outputs.output_action, join_node.inputs[1]) Connector.connect(join_node.outputs.output, unsqueeze_node.inputs.input) self._create_and_connect_agent(unsqueeze_node.outputs.output, squeeze_node.inputs[0], input_size + (1, )) Connector.connect(squeeze_node.outputs[0], fork_node.inputs.input) # Connector.connect(fork_node.outputs[1], # to_one_hot_node.inputs.input) Connector.connect(random_action_generator.outputs.one_hot_output, to_one_hot_node.inputs.input) Connector.connect(to_one_hot_node.outputs.output, node_action_monitor.inputs.action_in) Connector.connect(node_action_monitor.outputs.action_out, node_grid_world.inputs.agent_action, is_backward=True)
def __init__(self): super().__init__('cuda') # MNIST node producing two sequences. # Receptive field looking at the input with window size = 14, 14, stride 7, 7. # 3x3 experts looking at the outputs of the RF node. # 1 expert combining the outputs. mnist_seq_params: DatasetSequenceMNISTNodeParams = DatasetSequenceMNISTNodeParams( [[0, 1, 2], [3, 1, 4]]) mnist_params = DatasetMNISTParams(class_filter=[0, 1, 2, 3, 4], one_hot_labels=False, examples_per_class=1) n_channels_1 = 1 input_dims_1 = torch.Size((28, 28, n_channels_1)) parent_rf_dims_1 = Size2D(14, 14) parent_rf_stride_dims_1 = Stride(7, 7) self.expert_params = ExpertParams() self.expert_params.flock_size = 9 self.expert_params.spatial.input_size = reduce( lambda a, x: a * x, parent_rf_dims_1 + (n_channels_1, )) self.expert_params.n_cluster_centers = 10 self.expert_params.spatial.buffer_size = 100 self.expert_params.spatial.batch_size = 50 self.expert_params.spatial.learning_period = 1 self.expert_params.spatial.cluster_boost_threshold = 100 self.expert_params.spatial.max_boost_time = 200 self.expert_params.temporal.seq_length = 3 self.expert_params.temporal.seq_lookahead = 1 self.expert_params.temporal.buffer_size = 100 self.expert_params.temporal.batch_size = 50 self.expert_params.temporal.learning_period = 50 + self.expert_params.temporal.seq_lookbehind self.expert_params.temporal.incoming_context_size = 1 self.expert_params.temporal.max_encountered_seqs = 100 self.expert_params.temporal.n_frequent_seqs = 20 self.expert_params.temporal.forgetting_limit = 1000 self.parent_expert_params = self.expert_params.clone() self.parent_expert_params.flock_size = 1 self.parent_expert_params.spatial.input_size = \ self.expert_params.flock_size * 2 * self.expert_params.n_cluster_centers # Create the nodes. mnist_node_1 = DatasetSequenceMNISTNode(params=mnist_params, seq_params=mnist_seq_params) mnist_node_2 = DatasetSequenceMNISTNode(params=mnist_params, seq_params=mnist_seq_params) receptive_field_node_1_1 = ReceptiveFieldNode(input_dims_1, parent_rf_dims_1, parent_rf_stride_dims_1) receptive_field_node_1_2 = ReceptiveFieldNode(input_dims_1, parent_rf_dims_1, parent_rf_stride_dims_1) expert_flock_node_1 = ExpertFlockNode(self.expert_params) expert_flock_node_2 = ExpertFlockNode(self.expert_params) join_node = JoinNode(dim=0, n_inputs=2) expert_parent_node = ExpertFlockNode(self.parent_expert_params) self.add_node(mnist_node_1) self.add_node(receptive_field_node_1_1) self.add_node(expert_flock_node_1) self.add_node(mnist_node_2) self.add_node(receptive_field_node_1_2) self.add_node(expert_flock_node_2) self.add_node(join_node) self.add_node(expert_parent_node) unsqueeze_node_0 = UnsqueezeNode(0) self.add_node(unsqueeze_node_0) Connector.connect(mnist_node_1.outputs.data, receptive_field_node_1_1.inputs.input) Connector.connect(receptive_field_node_1_1.outputs.output, expert_flock_node_1.inputs.sp.data_input) Connector.connect(expert_flock_node_1.outputs.tp.projection_outputs, join_node.inputs[0]) Connector.connect(mnist_node_2.outputs.data, receptive_field_node_1_2.inputs.input) Connector.connect(receptive_field_node_1_2.outputs.output, expert_flock_node_2.inputs.sp.data_input) Connector.connect(expert_flock_node_2.outputs.tp.projection_outputs, join_node.inputs[1]) Connector.connect(join_node.outputs.output, unsqueeze_node_0.inputs.input) Connector.connect(unsqueeze_node_0.outputs.output, expert_parent_node.inputs.sp.data_input)
def toploogy_dataset_test(self): dataset_params = DatasetAlphabetParams( symbols="abcd123456789", padding_right=1, sequence_probs=DatasetAlphabetSequenceProbsModeParams( seqs=['abc', '123', '456789', '468'], # transition_probs=[[0.7, 0.3], [0.3, 0.7], ] )) dataset_node = DatasetAlphabetNode(dataset_params) flock_size = 1 # parent_cluster_centers = len(dataset_params.sequence_probs.seqs) parent_cluster_centers = 20 # len(dataset_params.sequence_probs.seqs) unsqueeze_node_child = UnsqueezeNode(0) unsqueeze_node_sequence_id = UnsqueezeNode(0) expand_node_child = ExpandNode(0, flock_size) expand_node_sequence_id = ExpandNode(0, flock_size) child_cluster_centers = len(dataset_params.symbols) - 1 expert_node_child = ExpertFlockNode( ExpertParams(flock_size=flock_size, n_cluster_centers=child_cluster_centers, spatial=SpatialPoolerParams(), temporal=TemporalPoolerParams( incoming_context_size=parent_cluster_centers, n_providers=2, n_frequent_seqs=50, seq_length=3, seq_lookahead=1))) expert_node_parent = ExpertFlockNode( ExpertParams(flock_size=flock_size, n_cluster_centers=parent_cluster_centers, spatial=SpatialPoolerParams(), temporal=TemporalPoolerParams(incoming_context_size=4, n_frequent_seqs=50, seq_length=3, seq_lookahead=1))) expert_node_sequence_id = ExpertFlockNode( ExpertParams(flock_size=flock_size, n_cluster_centers=2, spatial=SpatialPoolerParams())) self.add_node(dataset_node) self.add_node(unsqueeze_node_child) # self.add_node(unsqueeze_node_sequence_id) self.add_node(expand_node_child) # self.add_node(expand_node_sequence_id) self.add_node(expert_node_child) self.add_node(expert_node_parent) # self.add_node(expert_node_sequence_id) Connector.connect(dataset_node.outputs.output, unsqueeze_node_child.inputs.input) Connector.connect(unsqueeze_node_child.outputs.output, expand_node_child.inputs.input) Connector.connect(expand_node_child.outputs.output, expert_node_child.inputs.sp.data_input) # Connector.connect(dataset_node.outputs.sequence_id, unsqueeze_node_sequence_id.inputs.input) # Connector.connect(unsqueeze_node_sequence_id.outputs.output, expand_node_sequence_id.inputs.input) # Connector.connect(expand_node_sequence_id.outputs.output, expert_node_sequence_id.inputs.sp.data_input) Connector.connect(expert_node_child.outputs.tp.projection_outputs, expert_node_parent.inputs.sp.data_input) # Parent context Connector.connect(expert_node_parent.outputs.output_context, expert_node_child.inputs.tp.context_input, is_backward=True)
def _create_node(self) -> WorkerNodeBase: return UnsqueezeNode(dim=1)
def __init__(self, action_count=4, location_vector_size=100, use_grayscale: bool = False): super().__init__("Task 1 - Basic expert", inputs=Task1BasicExpertGroupInputs(self), outputs=Task1BasicExpertGroupOutputs(self)) base_expert_params = ExpertParams() base_expert_params.flock_size = 1 base_expert_params.n_cluster_centers = 100 base_expert_params.compute_reconstruction = False base_expert_params.spatial.cluster_boost_threshold = 1000 base_expert_params.spatial.learning_rate = 0.2 base_expert_params.spatial.batch_size = 1000 base_expert_params.spatial.buffer_size = 1010 base_expert_params.spatial.learning_period = 100 parent_expert_params = ExpertParams() parent_expert_params.flock_size = 1 parent_expert_params.n_cluster_centers = 20 parent_expert_params.compute_reconstruction = True parent_expert_params.temporal.exploration_probability = 0.9 parent_expert_params.spatial.cluster_boost_threshold = 1000 parent_expert_params.spatial.learning_rate = 0.2 parent_expert_params.spatial.batch_size = 1000 parent_expert_params.spatial.buffer_size = 1010 parent_expert_params.spatial.learning_period = 100 parent_expert_params.temporal.context_without_rewards_size = location_vector_size # flock-related nodes flock_node = ExpertFlockNode(base_expert_params) parent_flock_node = ExpertFlockNode(parent_expert_params) join_node = JoinNode(flatten=True) unsqueeze_node_to_base_expert = UnsqueezeNode(0) unsqueeze_node_to_parent_expert = UnsqueezeNode(0) fork_node = ForkNode( 0, [base_expert_params.n_cluster_centers, action_count]) def squeeze(inputs, outputs): outputs[0].copy_(inputs[0].squeeze()) squeeze_node = LambdaNode( squeeze, 1, [(base_expert_params.n_cluster_centers + action_count, )], name="squeeze lambda node") def stack_and_unsqueeze(inputs, outputs): outputs[0].copy_(torch.stack([inputs[0], inputs[1]]).unsqueeze(0)) stack_unsqueeze_node = LambdaNode(stack_and_unsqueeze, 2, [(1, 2, location_vector_size)], name="stack and unsqueeze node") # add nodes to the graph self.add_node(flock_node) self.add_node(unsqueeze_node_to_base_expert) self.add_node(parent_flock_node) self.add_node(unsqueeze_node_to_parent_expert) self.add_node(join_node) self.add_node(fork_node) self.add_node(squeeze_node) self.add_node(stack_unsqueeze_node) Connector.connect(self.inputs.actions.output, join_node.inputs[1]) if use_grayscale: grayscale_node = GrayscaleNode(squeeze_channel=True) self.add_node(grayscale_node) Connector.connect(self.inputs.image.output, grayscale_node.inputs.input) Connector.connect(grayscale_node.outputs.output, unsqueeze_node_to_base_expert.inputs.input) else: Connector.connect(self.inputs.image.output, unsqueeze_node_to_base_expert.inputs.input) Connector.connect(unsqueeze_node_to_base_expert.outputs.output, flock_node.inputs.sp.data_input) Connector.connect(self.inputs.current_location.output, stack_unsqueeze_node.inputs[0]) Connector.connect(self.inputs.target_location.output, stack_unsqueeze_node.inputs[1]) Connector.connect(fork_node.outputs[1], self.outputs.actions.input) # first layer Connector.connect(flock_node.outputs.tp.projection_outputs, join_node.inputs[0]) # second layer Connector.connect(join_node.outputs.output, unsqueeze_node_to_parent_expert.inputs.input) # second layer Connector.connect(unsqueeze_node_to_parent_expert.outputs.output, parent_flock_node.inputs.sp.data_input) Connector.connect(stack_unsqueeze_node.outputs[0], parent_flock_node.inputs.tp.context_input) # actions Connector.connect( parent_flock_node.outputs.sp.predicted_reconstructed_input, squeeze_node.inputs[0]) Connector.connect(squeeze_node.outputs[0], fork_node.inputs.input)
def test_sp_flock_node_accessor_types_and_dimensions(): device = 'cuda' # CPU not supported by SPFlock upper_bound = 107 flock_input_size = upper_bound flock_size = 1 num_cc = 21 # define params sp_params = MnistSpTopology.get_sp_params(num_cluster_centers=num_cc, cluster_boost_threshold=1000, learning_rate=0.1, buffer_size=2 * 30, batch_size=30, input_size=flock_input_size, flock_size=flock_size, max_boost_time=1500) # random_node -> unsqueeze_node, sp_flock random_node = RandomNumberNode(upper_bound=upper_bound) unsqueeze_node = UnsqueezeNode(0) sp_node = SpatialPoolerFlockNode(sp_params.clone()) Connector.connect(random_node.outputs.one_hot_output, unsqueeze_node.inputs.input) Connector.connect(unsqueeze_node.outputs.output, sp_node.inputs.sp.data_input) # update dimensions creator = AllocatingCreator(device=device) random_node.allocate_memory_blocks(creator) unsqueeze_node.allocate_memory_blocks(creator) sp_node.allocate_memory_blocks(creator) # make step random_node.step() unsqueeze_node.step() sp_node.step() # collect the results reconstruction = SpatialPoolerFlockNodeAccessor.get_reconstruction(sp_node) deltas = SpatialPoolerFlockNodeAccessor.get_sp_deltas(sp_node) boosting_durations = SpatialPoolerFlockNodeAccessor.get_sp_boosting_durations( sp_node) output_id = SpatialPoolerFlockNodeAccessor.get_output_id(sp_node) # check result properties assert type(reconstruction) is torch.Tensor assert type(deltas) is torch.Tensor assert type(boosting_durations) is torch.Tensor assert type(output_id) is int assert reconstruction.shape == (flock_size, flock_input_size) assert deltas.shape == (flock_size, num_cc, flock_input_size) assert boosting_durations.shape == (flock_size, num_cc) assert 0 <= output_id < num_cc # test the sp metrics delta = average_sp_delta(deltas) boosting_dur = average_boosting_duration(boosting_durations) nbc = num_boosted_clusters(boosting_durations) assert type(delta) is float assert type(boosting_dur) is float assert 0 <= boosting_dur <= 1000 assert type(nbc) is float assert 0 <= nbc <= 1
def __init__(self): super().__init__(device='cuda') params1 = ExpertParams() params1.flock_size = 1 params1.n_cluster_centers = 10 params1.spatial.buffer_size = 100 params1.temporal.buffer_size = 100 params1.temporal.incoming_context_size = 9 params1.temporal.n_providers = 2 params1.spatial.batch_size = 50 params1.temporal.batch_size = 50 params1.spatial.input_size = 28 * 28 params2 = ExpertParams() params2.flock_size = 1 params2.n_cluster_centers = 9 params2.spatial.buffer_size = 100 params2.temporal.buffer_size = 100 params2.temporal.incoming_context_size = 5 params2.temporal.n_providers = 2 params2.spatial.batch_size = 50 params2.temporal.batch_size = 50 params2.spatial.input_size = params1.n_cluster_centers mnist_seq_params = DatasetSequenceMNISTNodeParams([[0, 1, 2], [3, 1, 4]]) mnist_params = DatasetMNISTParams(class_filter=[0, 1, 2, 3, 4], one_hot_labels=False, examples_per_class=1) mnist_node = DatasetSequenceMNISTNode(params=mnist_params, seq_params=mnist_seq_params) zero_context = ConstantNode( shape=(params2.flock_size, params2.temporal.n_providers, NUMBER_OF_CONTEXT_TYPES, params2.temporal.incoming_context_size), constant=0) node1 = ExpertFlockNode(params1) node2 = ExpertFlockNode(params2) self.add_node(mnist_node) self.add_node(node1) self.add_node(node2) self.add_node(zero_context) unsqueeze_node_0 = UnsqueezeNode(0) self.add_node(unsqueeze_node_0) Connector.connect(mnist_node.outputs.data, unsqueeze_node_0.inputs.input) Connector.connect(unsqueeze_node_0.outputs.output, node1.inputs.sp.data_input) Connector.connect(node1.outputs.tp.projection_outputs, node2.inputs.sp.data_input) Connector.connect(node2.outputs.output_context, node1.inputs.tp.context_input, is_backward=True) Connector.connect(zero_context.outputs.output, node2.inputs.tp.context_input)
def __init__(self): super().__init__(device='cuda') actions_descriptor = GridWorldActionDescriptor() node_action_monitor = ActionMonitorNode(actions_descriptor) params = GridWorldParams(map_name='MapE') noise_params = RandomNoiseParams(amplitude=0.0001) node_grid_world = GridWorldNode(params) expert_params = ExpertParams() unsqueeze_node = UnsqueezeNode(dim=0) noise_node = RandomNoiseNode(noise_params) constant_node = ConstantNode(shape=(1, 1, 3, 48)) one_hot_node = ToOneHotNode() def context(inputs, outputs): con = inputs[0] con[:, :, 1:, 24:] = float('nan') outputs[0].copy_(con) def f(inputs, outputs): probs = inputs[0] outputs[0].copy_(probs[0, -1, :4] + SMALL_CONSTANT) action_parser = LambdaNode(func=f, n_inputs=1, output_shapes=[(4,)]) context_assembler = LambdaNode(func=context, n_inputs=1, output_shapes=[(1, 1, 3, 48)]) expert_params.flock_size = 1 expert_params.n_cluster_centers = 24 expert_params.produce_actions = True expert_params.temporal.seq_length = 9 expert_params.temporal.seq_lookahead = 7 expert_params.temporal.n_frequent_seqs = 700 expert_params.temporal.max_encountered_seqs = 1000 expert_params.temporal.exploration_probability = 0.01 expert_params.temporal.batch_size = 200 expert_params.temporal.own_rewards_weight = 20 expert_params.temporal.incoming_context_size = 48 expert_params.compute_reconstruction = True #expert_node = ConvExpertFlockNode(expert_params) expert_node = ExpertFlockNode(expert_params) self.add_node(node_grid_world) self.add_node(node_action_monitor) self.add_node(expert_node) self.add_node(unsqueeze_node) self.add_node(action_parser) self.add_node(noise_node) self.add_node(constant_node) self.add_node(context_assembler) self.add_node(one_hot_node) Connector.connect(node_grid_world.outputs.egocentric_image_action, noise_node.inputs.input) Connector.connect(noise_node.outputs.output, unsqueeze_node.inputs.input) Connector.connect(unsqueeze_node.outputs.output, expert_node.inputs.sp.data_input) Connector.connect(node_grid_world.outputs.reward, expert_node.inputs.tp.reward_input) Connector.connect(constant_node.outputs.output, context_assembler.inputs[0]) Connector.connect(context_assembler.outputs[0], expert_node.inputs.tp.context_input) Connector.connect(expert_node.outputs.sp.predicted_reconstructed_input, action_parser.inputs[0]) Connector.connect(action_parser.outputs[0], one_hot_node.inputs.input) Connector.connect(one_hot_node.outputs.output, node_action_monitor.inputs.action_in) Connector.connect(node_action_monitor.outputs.action_out, node_grid_world.inputs.agent_action, is_backward=True)
def __init__(self): super().__init__(device='cuda') actions_descriptor = GridWorldActionDescriptor() node_action_monitor = ActionMonitorNode(actions_descriptor) params = GridWorldParams(map_name='MapE') noise_params = RandomNoiseParams(amplitude=0.0001) node_grid_world = GridWorldNode(params) expert_params1 = ExpertParams() unsqueeze_node = UnsqueezeNode(dim=0) noise_node = RandomNoiseNode(noise_params) one_hot_node = ToOneHotNode() def f(inputs, outputs): probs = inputs[0] outputs[0].copy_(probs[0, -1, :4] + SMALL_CONSTANT) action_parser = LambdaNode(func=f, n_inputs=1, output_shapes=[(4, )]) expert_params1.flock_size = 1 expert_params1.n_cluster_centers = 24 expert_params1.produce_actions = True expert_params1.temporal.seq_length = 4 expert_params1.temporal.seq_lookahead = 2 expert_params1.temporal.n_frequent_seqs = 700 expert_params1.temporal.max_encountered_seqs = 1000 expert_params1.temporal.exploration_probability = 0.05 expert_params1.temporal.batch_size = 200 expert_params1.temporal.frustration_threshold = 2 # expert_params.temporal.own_rewards_weight = 20 expert_params1.compute_reconstruction = True expert_params2 = expert_params1.clone() expert_params2.temporal.seq_length = 5 expert_params2.temporal.seq_lookahead = 4 expert_params2.n_cluster_centers = 8 expert_params2.produce_actions = False expert_params2.temporal.frustration_threshold = 10 #expert_params1.temporal.incoming_context_size = 2 * expert_params2.n_cluster_centers expert_node1 = ExpertFlockNode(expert_params1) expert_node2 = ExpertFlockNode(expert_params2) self.add_node(node_grid_world) self.add_node(node_action_monitor) self.add_node(expert_node1) self.add_node(expert_node2) self.add_node(unsqueeze_node) self.add_node(action_parser) self.add_node(noise_node) self.add_node(one_hot_node) Connector.connect(node_grid_world.outputs.output_image_action, noise_node.inputs.input) Connector.connect(noise_node.outputs.output, unsqueeze_node.inputs.input) Connector.connect(unsqueeze_node.outputs.output, expert_node1.inputs.sp.data_input) Connector.connect(expert_node1.outputs.tp.projection_outputs, expert_node2.inputs.sp.data_input) Connector.connect(expert_node2.outputs.output_context, expert_node1.inputs.tp.context_input, is_backward=True) Connector.connect( expert_node1.outputs.sp.predicted_reconstructed_input, action_parser.inputs[0]) Connector.connect(node_grid_world.outputs.reward, expert_node1.inputs.tp.reward_input) Connector.connect(node_grid_world.outputs.reward, expert_node2.inputs.tp.reward_input) Connector.connect(action_parser.outputs[0], one_hot_node.inputs.input) Connector.connect(one_hot_node.outputs.output, node_action_monitor.inputs.action_in) Connector.connect(node_action_monitor.outputs.action_out, node_grid_world.inputs.agent_action, is_backward=True)