def test_inverse_projection(self, device): float_dtype = get_float(device) params = ExpertParams() params.flock_size = 2 params.n_cluster_centers = 4 params.spatial.input_size = 5 params.spatial.buffer_size = 7 params.spatial.batch_size = 6 flock = SPFlock(params, AllocatingCreator(device)) flock.cluster_centers = torch.tensor( [[[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0.5, 0.5, 0], [0, 0, 0.5, 0, 0.5]], [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0]]], dtype=float_dtype, device=device) data = torch.tensor([[0, 0, 1, 0], [0.2, 0.3, 0.4, 0.1]], dtype=float_dtype, device=device) result = flock.inverse_projection(data) expected_projection = torch.tensor( [[0, 0, 0.5, 0.5, 0], [0.2, 0.3, 0.4, 0.1, 0]], dtype=float_dtype, device=device) assert same(expected_projection, result)
def test_determine_learning(self, mask, total_data_written, data_since_last_sample, expected_learn_tensor, device): """TODO: one-line summary here. - No data stored, insufficient learning period, so no learning can run - Correct learning period, but insufficient data for batch - Sufficient data, incorrect learning period - Sufficient data and learn period and all forwarded - Sufficient data and learn period, but none forwarded - Sufficient data and learn period, and some forward - Different combinations of all three """ params = ExpertParams() params.flock_size = 7 params.n_cluster_centers = 5 mask = mask.to(device) expected_learn_tensor = expected_learn_tensor.to(device) s_pooler = params.spatial s_pooler.batch_size = 10 s_pooler.learning_period = 5 flock = SPFlock(params, creator=AllocatingCreator(device)) copy_or_fill_tensor(flock.buffer.total_data_written, total_data_written) copy_or_fill_tensor(flock.buffer.data_since_last_sample, data_since_last_sample) learn_tensor = flock._determine_learning(mask) assert same(expected_learn_tensor, learn_tensor)
def create_flock_params( num_cluster_centers: Sequence[int], learning_rate: Sequence[float], buffer_size: Sequence[int], batch_size: Sequence[int], sampling_method: SamplingMethod, cluster_boost_threshold: Sequence[int], flock_size: int, max_boost_time: int, num_layers: int): params_list = [] assert len(num_cluster_centers) == num_layers assert len(buffer_size) == num_layers assert len(batch_size) == num_layers assert len(learning_rate) == num_layers for layer_id in range(0, num_layers): params = ExpertParams() params.n_cluster_centers = num_cluster_centers[layer_id] params.flock_size = flock_size params.spatial.buffer_size = buffer_size[layer_id] params.spatial.batch_size = batch_size[layer_id] params.spatial.cluster_boost_threshold = cluster_boost_threshold[ layer_id] params.spatial.max_boost_time = max_boost_time # should be bigger than any cluster_boost_threshold params.spatial.learning_rate = learning_rate[layer_id] params.spatial.sampling_method = sampling_method # params.compute_reconstruction = True params_list.append(params) # just the top expert computes reconstruction params_list[-1].compute_reconstruction = True return params_list
def test_determine_learning(self, mask, total_data_written, data_since_last_sample, expected_learn_tensor): """Test for the mechanism which determines if the convolutional spatial pooler should learn. Args: mask (torch.Tensor): Mask of which experts performed a forward pass. total_data_written (int): How much data has been written to the buffer. data_since_last_sample (int): How much data has been written since the last learning pass. expected_learn_tensor (torch.Tensor): What mask we expect for the learning pass. - No data stored, insufficient learning period, so no learning can run - Correct learning period, but insufficient data for batch - Sufficient data, incorrect learning period - Sufficient data and learn period and all forwarded - Sufficient data and learn period, but none forwarded """ device = 'cpu' params = ExpertParams() params.flock_size = 7 params.n_cluster_centers = 5 s_pooler = params.spatial s_pooler.batch_size = 10 s_pooler.learning_period = 5 flock = ConvSPFlock(params, AllocatingCreator(device)) flock.common_buffer.total_data_written.fill_(total_data_written) flock.common_buffer.data_since_last_sample.fill_( data_since_last_sample) learn_tensor = flock._determine_learning(mask) assert same(expected_learn_tensor, learn_tensor)
def create_flock(flock_size=2, input_size=7, context_size=1, n_providers=1, device='cuda'): params = ExpertParams() params.flock_size = flock_size params.n_cluster_centers = 5 sp_pooler = params.spatial sp_pooler.input_size = input_size sp_pooler.buffer_size = 10 sp_pooler.batch_size = 3 sp_pooler.learning_rate = 0.1 sp_pooler.cluster_boost_threshold = 1 sp_pooler.max_boost_time = 2 sp_pooler.learning_period = 1 tp_pooler = params.temporal tp_pooler.incoming_context_size = context_size tp_pooler.buffer_size = 15 tp_pooler.batch_size = 10 tp_pooler.learning_period = 1 tp_pooler.seq_length = 4 tp_pooler.seq_lookahead = 2 tp_pooler.n_frequent_seqs = 20 tp_pooler.max_encountered_seqs = 120 tp_pooler.forgetting_limit = 5 tp_pooler.n_providers = n_providers return ExpertFlock(params, AllocatingCreator(device))
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 test_forward_learn_enable_learning(self, enable_learning, SP_type): device = 'cuda' float_dtype = get_float(device) params = ExpertParams() params.flock_size = 1 params.n_cluster_centers = 4 params.spatial.input_size = 2 params.spatial.cluster_boost_threshold = 2 params.spatial.learning_rate = 0.1 params.spatial.learning_period = 1 params.spatial.batch_size = 4 params.spatial.max_boost_time = 10 assert params.spatial.enable_learning # True should be the default value params.spatial.enable_learning = enable_learning flock = SP_type(params, AllocatingCreator(device)) data = torch.tensor([[0., 0], [1., 0], [0., 1], [1, 1]], dtype=float_dtype, device=device) initial_cluster_centers = flock.cluster_centers.clone() for input in data: flock.forward_learn(input.view(1, -1)) # should be different if enable_learning == True assert (not same(initial_cluster_centers, flock.cluster_centers)) == enable_learning
def __init__(self): super().__init__("Single Expert") expert_params = ExpertParams() 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.compute_backward_pass = True expert_params.compute_reconstruction = True expert_node = ExpertFlockNode(expert_params) self.add_node(expert_node) Connector.connect(self.inputs.data.output, expert_node.inputs.sp.data_input) Connector.connect(self.inputs.reward.output, expert_node.inputs.tp.reward_input) Connector.connect(expert_node.outputs.sp.predicted_reconstructed_input, self.outputs.predicted_reconstructed_input.input)
def create_tp_flock(n_cluster_centers=3, flock_size=1, seq_length=3, buffer_size=10, batch_size=5, seq_lookahead=1, n_frequent_seq=4, max_encountered_seq=20, forgetting_limit=40, learning_period=1, incoming_context_size=1, context_prior=DEFAULT_CONTEXT_PRIOR, exploration_probability=0, follow_goals=False, n_providers=1, trained_forward_factory=None, untrained_forward_factory=None, enable_learning=True, device='cuda'): params = ExpertParams() params.flock_size = flock_size params.n_cluster_centers = n_cluster_centers params.flock_size = flock_size tp_params = params.temporal tp_params.incoming_context_size = incoming_context_size tp_params.seq_length = seq_length tp_params.buffer_size = buffer_size tp_params.batch_size = batch_size tp_params.seq_lookahead = seq_lookahead tp_params.n_frequent_seqs = n_frequent_seq tp_params.max_encountered_seqs = max_encountered_seq tp_params.forgetting_limit = forgetting_limit tp_params.learning_period = learning_period tp_params.context_prior = context_prior tp_params.exploration_probability = exploration_probability tp_params.follow_goals = follow_goals tp_params.enable_learning = enable_learning tp_params.n_providers = n_providers tp_params.frustration_threshold = 10000 tp_params.follow_goals = False tp_params.compute_backward_pass = True return TPFlock(params, creator=AllocatingCreator(device), trained_forward_factory=trained_forward_factory, untrained_forward_factory=untrained_forward_factory)
def test_forward_subflock_integration(device): flock_size = 10 subflock_size = 6 n_cluster_centers = 4 context_size = n_cluster_centers buffer_size = 7 float_dtype = get_float(device) n_providers = 1 params = ExpertParams() params.flock_size = flock_size params.n_cluster_centers = n_cluster_centers params.temporal.n_providers = n_providers params.temporal.incoming_context_size = context_size params.temporal.buffer_size = buffer_size params.temporal.batch_size = buffer_size flock, indices, indices_np = get_subflock_integration_testing_flock( params, subflock_size, device) cluster_data = torch.rand((flock_size, n_cluster_centers), dtype=float_dtype, device=device) context_data = torch.rand( (flock_size, n_providers, NUMBER_OF_CONTEXT_TYPES, n_cluster_centers), dtype=float_dtype, device=device) reward_data = torch.rand((flock_size, 2), dtype=float_dtype, device=device) forward_factory = TrainedForwardProcessFactory() forward = forward_factory.create(flock, cluster_data, context_data, reward_data, indices, device) # TODO (Test): add other tensors from the process, check this also for the untrained_forward_process should_update = [ (flock.projection_outputs, forward._projection_outputs), (flock.action_rewards, forward._action_rewards), (flock.action_outputs, forward._action_outputs), ] should_not_update = [ (flock.frequent_seqs, forward._frequent_seqs), (flock.frequent_seq_occurrences, forward._frequent_seq_occurrences), (flock.frequent_context_likelihoods, forward._frequent_context_likelihoods), ] randomize_subflock(should_update, should_not_update) expected_results = calculate_expected_results(should_update, should_not_update, flock_size, indices_np) forward.integrate() check_integration_results(expected_results, should_update, should_not_update)
def _create_expert_params() -> ExpertParams: expert_params = ExpertParams() expert_params.flock_size = 1 expert_params.n_cluster_centers = 200 expert_params.compute_reconstruction = True expert_params.spatial.batch_size = 1000 expert_params.spatial.buffer_size = 1010 expert_params.spatial.cluster_boost_threshold = 200 return expert_params
def test_learning_subflock_integration(): flock_size = 10 subflock_size = 6 n_cluster_centers = 4 context_size = n_cluster_centers buffer_size = 7 device = 'cpu' params = ExpertParams() params.flock_size = flock_size params.n_cluster_centers = n_cluster_centers params.temporal.incoming_context_size = context_size params.temporal.buffer_size = buffer_size params.temporal.batch_size = buffer_size flock, indices, indices_np = get_subflock_integration_testing_flock( params, subflock_size, device) forward = flock._create_learning_process(indices) should_update = [ (flock.all_encountered_seqs, forward._all_encountered_seqs), (flock.all_encountered_seq_occurrences, forward._all_encountered_seq_occurrences), (flock.all_encountered_context_occurrences, forward._all_encountered_context_occurrences), (flock.frequent_seqs, forward._frequent_seqs), (flock.frequent_seq_occurrences, forward._frequent_seq_occurrences), (flock.frequent_context_likelihoods, forward._frequent_context_likelihoods), ] should_not_update = [ (flock.buffer.seq_probs.stored_data, forward._buffer.seq_probs.stored_data), (flock.buffer.outputs.stored_data, forward._buffer.outputs.stored_data), (flock.buffer.contexts.stored_data, forward._buffer.contexts.stored_data), (flock.buffer.clusters.stored_data, forward._buffer.clusters.stored_data), (flock.buffer.current_ptr, forward._buffer.current_ptr), (flock.buffer.total_data_written, forward._buffer.total_data_written), ] randomize_subflock(should_update, should_not_update) expected_results = calculate_expected_results(should_update, should_not_update, flock_size, indices_np) forward.integrate() check_integration_results(expected_results, should_update, should_not_update)
def get_expert_params(self): expert_params = ExpertParams() expert_params.flock_size = EOX * EOY expert_params.n_cluster_centers = 30 expert_params.spatial.buffer_size = 500 expert_params.temporal.buffer_size = 100 expert_params.temporal.incoming_context_size = 10 expert_params.spatial.batch_size = 50 expert_params.temporal.batch_size = 50 expert_params.spatial.input_size = 3 * self.SX // EOX * self.SY // EOY return expert_params
def _make_top_layer( num_labels: int, input_data_size: int, n_cluster_centers: int ) -> (SpReconstructionLayer, InputSlot, OutputSlotBase): sp_params = ExpertParams() sp_params.flock_size = 1 sp_params.n_cluster_centers = n_cluster_centers sp_params.compute_reconstruction = True layer = SpReconstructionLayer(input_data_size=input_data_size, labels_size=num_labels, sp_params=sp_params, name='TOP') return layer, layer.inputs.data, None
def get_subflock_creation_testing_flock( flock_size=10, subflock_size=6, input_size=5, buffer_size=7, batch_size=5, n_cluster_centers=4, device='cpu', sampling_method=SamplingMethod.LAST_N) -> Tuple[SPFlock, torch.Tensor]: # Generate a flock with some data float_dtype = get_float(device) params = ExpertParams() params.flock_size = flock_size params.n_cluster_centers = n_cluster_centers params.spatial.input_size = input_size params.spatial.buffer_size = buffer_size params.spatial.batch_size = batch_size params.spatial.sampling_method = sampling_method flock = SPFlock(params, AllocatingCreator(device)) flock.buffer.inputs.stored_data = torch.rand( (flock_size, buffer_size, input_size), dtype=float_dtype, device=device) flock.buffer.clusters.stored_data = torch.rand( (flock_size, buffer_size, n_cluster_centers), dtype=float_dtype, device=device) # NOTE: This is not one-hot and thus not real data flock.buffer.current_ptr = torch.rand(flock_size, device=device).type(torch.int64) flock.buffer.total_data_written = torch.rand( flock_size, device=device).type(torch.int64) # The bookeeping values which are copied across flock.cluster_boosting_durations = torch.rand( (flock_size, n_cluster_centers), device=device).type(torch.int64) flock.prev_boosted_clusters = torch.clamp( torch.round(torch.rand((flock_size, n_cluster_centers), device=device)), 0, 1).type(torch.uint8) flock.boosting_targets = torch.rand((flock_size, n_cluster_centers), device=device).type(torch.int64) # Indices which are to be subflocked indices = torch.tensor(np.random.choice(flock_size, subflock_size, replace=False), dtype=torch.int64, device=device) return flock, indices
def _create_expert_params(self) -> ExpertParams: expert_params = ExpertParams() expert_params.flock_size = 1 expert_params.n_cluster_centers = self._num_ccs expert_params.compute_reconstruction = True expert_params.spatial.batch_size = 990 expert_params.spatial.buffer_size = self._buffer_size expert_params.spatial.cluster_boost_threshold = self._num_ccs * 2 expert_params.spatial.learning_rate = SeT0BasicTopologyRT211.LEARNING_RATE expert_params.spatial.sampling_method = self._sampling_method expert_params.spatial.learning_period = 10 expert_params.spatial.max_boost_time = 5000 return expert_params
def test_conv_forward(self): params = ExpertParams() params.n_cluster_centers = 10 params.flock_size = 5 params.spatial.input_size = 3 params.spatial.buffer_size = 30 device = 'cuda' float_dtype = get_float(device) creator = AllocatingCreator(device) sp_flock = ConvSPFlock(params, creator) input_data = torch.tensor([[2, 0, 4], [1, 0.3, -1], [2, 0.1, 0.5], [0.7, 0.9, 0.8], [2, 0, 4]], dtype=float_dtype, device=device) forward_mask = sp_flock.forward(input_data) expected_forward_mask = torch.tensor([1, 1, 1, 1, 1], dtype=torch.uint8, device=device) assert same(expected_forward_mask, forward_mask) expected_common_buffer = torch.full( (1, params.spatial.buffer_size, params.spatial.input_size), fill_value=FLOAT_NAN, dtype=float_dtype, device=device) expected_common_buffer[0, 0] = input_data[0] expected_common_buffer[0, 1] = input_data[1] expected_common_buffer[0, 2] = input_data[2] expected_common_buffer[0, 3] = input_data[3] expected_common_buffer[0, 4] = input_data[4] assert same(expected_common_buffer, sp_flock.common_buffer.inputs.stored_data) assert sp_flock.forward_clusters.sum() == params.flock_size assert same(sp_flock.forward_clusters[0], sp_flock.forward_clusters[4]) assert sp_flock.common_buffer.current_ptr == 4 assert sp_flock.common_buffer.total_data_written == 5 assert (sp_flock.buffer.current_ptr == 0).all() assert (sp_flock.buffer.total_data_written == 1).all() for member in range(params.flock_size): assert same(input_data[member], sp_flock.buffer.inputs.stored_data[member, 0])
def test_validate_context_input_error_message(self, sp_data_input_shape, flock_size, context_size, context_input_shape, exception_message): params = ExpertParams() params.flock_size = flock_size params.temporal.incoming_context_size = context_size expert = ExpertFlockNode(params) Connector.connect(self.memory_block(torch.zeros(sp_data_input_shape)), expert.inputs.sp.data_input) Connector.connect(self.memory_block(torch.zeros(context_input_shape)), expert.inputs.tp.context_input) with raises(NodeValidationException, match=exception_message): expert.validate()
def test_inverse_projection(self, device): dtype = get_float(device) params = ExpertParams() params.flock_size = 2 params.n_cluster_centers = 4 params.spatial.input_size = 6 params.spatial.buffer_size = 7 params.spatial.batch_size = 3 params.temporal.n_frequent_seqs = 2 params.temporal.seq_length = 3 input_size = (3, 2) graph = Topology(device) node = ExpertFlockNode(params=params) graph.add_node(node) input_block = MemoryBlock() input_block.tensor = torch.rand((params.flock_size, ) + input_size, dtype=dtype, device=device) Connector.connect(input_block, node.inputs.sp.data_input) graph.prepare() node._unit.flock.sp_flock.cluster_centers = torch.tensor( [[[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 0.5, 0.5, 0, 0], [0, 0, 0.5, 0, 0.5, 0]], [[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0]]], dtype=dtype, device=device) # Just SP inverse projection data = torch.tensor([[0, 0, 1, 0], [0.2, 0.3, 0.4, 0.1]], dtype=dtype, device=device) packet = InversePassOutputPacket(data, node.outputs.tp.projection_outputs) projected = node.recursive_inverse_projection_from_output(packet) # The result of the projection itself would be [[0, 0, 0.5, 0.5, 0, 0], ...], and it should be viewed as (2, 3, 2). expected_projection = torch.tensor( [[[0, 0], [0.5, 0.5], [0, 0]], [[0.2, 0.3], [0.4, 0.1], [0, 0]]], dtype=dtype, device=device) assert same(expected_projection, projected[0].tensor)
def create_flock(input_size, flock_size, incoming_context_size, seq_length=4, n_cluster_centers=20, max_encountered_seqs=1000, n_frequent_seqs=500, sp_buffer_size=3000, sp_batch_size=300, sp_learning_period=20, convolutional: bool = False, sp_max_boost_time=1000, sp_boost_threshold=100, sampling_method: SamplingMethod = SamplingMethod.BALANCED, n_context_providers=1, device='cuda'): params = ExpertParams() params.n_cluster_centers = n_cluster_centers params.flock_size = flock_size params.compute_reconstruction = True sp_params = params.spatial sp_params.input_size = input_size sp_params.buffer_size = sp_buffer_size sp_params.batch_size = sp_batch_size sp_params.learning_rate = 0.1 sp_params.cluster_boost_threshold = sp_boost_threshold sp_params.max_boost_time = sp_max_boost_time sp_params.learning_period = sp_learning_period sp_params.sampling_method = sampling_method tp_params = params.temporal tp_params.buffer_size = 100 tp_params.seq_length = seq_length tp_params.batch_size = 50 + tp_params.seq_length - 1 tp_params.learning_period = 50 tp_params.seq_lookahead = 2 tp_params.n_frequent_seqs = n_frequent_seqs tp_params.max_encountered_seqs = max_encountered_seqs tp_params.forgetting_limit = 5000 tp_params.incoming_context_size = incoming_context_size tp_params.n_providers = n_context_providers if convolutional: print("Created convolutional flock.") return ConvExpertFlock(params, AllocatingCreator(device)) else: print("Created normal flock.") return ExpertFlock(params, AllocatingCreator(device))
def test_forward_learn_streams(self, use_default_stream, sampling_method): params = ExpertParams() params.flock_size = 1 params.n_cluster_centers = 4 params.spatial.input_size = 2 params.spatial.cluster_boost_threshold = 2 params.spatial.learning_rate = 0.1 params.spatial.learning_period = 1 params.spatial.batch_size = 4 params.spatial.buffer_size = 6 params.spatial.max_boost_time = 10 params.spatial.sampling_method = sampling_method device = 'cuda' float_dtype = get_float(device) flock = SPFlock(params, AllocatingCreator(device)) data = torch.tensor([[0., 0], [1., 0], [0., 1], [1, 1]], dtype=float_dtype, device=device) iters = 20 def run(): for itr in range(iters): for k in data: flock.forward_learn(k.view(1, -1)) if use_default_stream: run() else: with torch.cuda.stream(torch.cuda.Stream()): run() expected_cluster_centers = data # Cluster centers have no guarantee of order - so we have to order them manually rounded_cluster_centers = np.around( flock.cluster_centers.cpu().data.numpy()[0], decimals=2) cc_indices = [] for cc in rounded_cluster_centers: cc_indices.append(cc[0] * 2 + cc[1] * 4) sorted_indices = np.argsort(cc_indices) sorted_ccs = flock.cluster_centers[0, sorted_indices, :] assert same(expected_cluster_centers, sorted_ccs, eps=5e-2)
def test_integrate_learning_subflock(self, device): flock_size = 10 subflock_size = 6 input_size = 5 n_cluster_centers = 4 buffer_size = 7 params = ExpertParams() params.flock_size = flock_size params.n_cluster_centers = n_cluster_centers params.spatial.input_size = input_size params.spatial.buffer_size = buffer_size params.spatial.batch_size = buffer_size flock, indices, indices_np = get_subflock_integration_testing_flock( params, subflock_size, device) learning = flock._create_learning_process(indices) should_update = [ (flock.cluster_boosting_durations, learning._cluster_boosting_durations), (flock.prev_boosted_clusters, learning._prev_boosted_clusters), (flock.boosting_targets, learning._boosting_targets), (flock.cluster_centers, learning._cluster_centers), (flock.cluster_center_deltas, learning._cluster_center_deltas) ] should_not_update = [(flock.buffer.inputs.stored_data, learning._buffer.inputs.stored_data), (flock.buffer.clusters.stored_data, learning._buffer.clusters.stored_data), (flock.buffer.current_ptr, learning._buffer.current_ptr), (flock.buffer.total_data_written, learning._buffer.total_data_written)] randomize_subflock(should_update, should_not_update) expected_results = calculate_expected_results(should_update, should_not_update, flock_size, indices_np) # Run the integration learning.integrate() check_integration_results(expected_results, should_update, should_not_update)
def get_sp_params(num_cluster_centers: int, cluster_boost_threshold: int, learning_rate: float, buffer_size: int, batch_size: int, input_size: int, flock_size: int, max_boost_time): params = ExpertParams() params.n_cluster_centers = num_cluster_centers params.spatial.input_size = input_size params.flock_size = flock_size params.spatial.buffer_size = buffer_size params.spatial.batch_size = batch_size params.spatial.cluster_boost_threshold = cluster_boost_threshold params.spatial.max_boost_time = max_boost_time # should be bigger than any cluster_boost_threshold params.spatial.learning_rate = learning_rate return params
def convert_to_expert_params(self): """Parse from the MultipleLayerParams to list of ExpertParams""" self.validate_params_for_n_layers() params_list = [] for layer_id in range(self.num_conv_layers): params = ExpertParams() params.flock_size = 1 # spatial params.n_cluster_centers = self.read_param('n_cluster_centers', layer_id) params.spatial.buffer_size = self.read_param( 'sp_buffer_size', layer_id) params.spatial.batch_size = self.read_param( 'sp_batch_size', layer_id) params.spatial.cluster_boost_threshold = self.read_param( 'cluster_boost_threshold', layer_id) params.spatial.max_boost_time = self.read_param( 'max_boost_time', layer_id) params.spatial.learning_rate = self.read_param( 'learning_rate', layer_id) params.spatial.sampling_method = self.read_param( 'sampling_method', layer_id) params.compute_reconstruction = self.read_param( 'compute_reconstruction', layer_id) # temporal params.temporal.seq_length = self.read_param( 'seq_length', layer_id) params.temporal.seq_lookahead = self.read_param( 'seq_lookahead', layer_id) params.temporal.max_encountered_seqs = self.read_param( 'max_encountered_seqs', layer_id) params.temporal.n_frequent_seqs = self.read_param( 'max_frequent_seqs', layer_id) params.temporal.exploration_probability = self.read_param( 'exploration_probability', layer_id) # done params_list.append(params) return params_list
def __init__(self): super().__init__('cuda') noise_amplitude: float = 0 env_size: Tuple[int, int] = (27, 27) ball_radius: int = 5 switch_shape_after = 200 sp_n_cluster_centers = 200 # free ball_env_params = BallEnvironmentParams( switch_shape_after=switch_shape_after, noise_amplitude=noise_amplitude, ball_radius=ball_radius, env_size=env_size) ball_env = BallEnvironment(ball_env_params) self.add_node(ball_env) self.ball_env = ball_env # topmost layer ep_sp = ExpertParams() ep_sp.flock_size = 1 ep_sp.n_cluster_centers = sp_n_cluster_centers sp_reconstruction_layer = SpReconstructionLayer( env_size[0] * env_size[1], ball_env_params.n_shapes, sp_params=ep_sp, name="L0") self.add_node(sp_reconstruction_layer) self.sp_reconstruction_layer = sp_reconstruction_layer switch_node = SwitchNode(2) self.add_node(switch_node) self.switch_node = switch_node nan_node = ConstantNode(ball_env_params.n_shapes, math.nan) self.add_node(nan_node) Connector.connect(ball_env.outputs.data, sp_reconstruction_layer.inputs.data) Connector.connect(ball_env.outputs.label, switch_node.inputs[0]) Connector.connect(nan_node.outputs.output, switch_node.inputs[1]) Connector.connect(switch_node.outputs.output, sp_reconstruction_layer.inputs.label) self.is_training = True
def test_validate_context_input(self, sp_data_input_shape, flock_size, context_size, context_input_shape, is_valid): params = ExpertParams() params.flock_size = flock_size params.temporal.incoming_context_size = context_size expert = ExpertFlockNode(params) Connector.connect(self.memory_block(torch.zeros(sp_data_input_shape)), expert.inputs.sp.data_input) Connector.connect(self.memory_block(torch.zeros(context_input_shape)), expert.inputs.tp.context_input) if is_valid: expert.validate() else: with raises(NodeValidationException): expert.validate()
def _make_middle_layer_expert(use_temporal_pooler: bool, n_middle_layer_cluster_centers: int) \ -> (NodeBase, InputSlot, OutputSlotBase): params = ExpertParams() params.flock_size = 1 params.n_cluster_centers = n_middle_layer_cluster_centers if use_temporal_pooler: expert_node: ExpertFlockNode = ExpertFlockNode(params) input_slot = expert_node.inputs.sp.data_input output_slot = expert_node.outputs.tp.projection_outputs node = expert_node else: sp_node: SpatialPoolerFlockNode = SpatialPoolerFlockNode(params) input_slot = sp_node.inputs.sp.data_input output_slot = sp_node.outputs.sp.forward_clusters node = sp_node return node, input_slot, output_slot
def test_reconstruct(self, sampling_method, flock_class, device): float_dtype = get_float(device) params = ExpertParams() params.flock_size = 2 params.n_cluster_centers = 4 params.spatial.input_size = 5 params.spatial.buffer_size = 7 params.spatial.batch_size = 6 params.spatial.sampling_method = sampling_method flock = flock_class(params, AllocatingCreator(device)) flock.forward_clusters = torch.tensor( [[0, 0, 1, 0], [0.2, 0.3, 0.4, 0.1]], dtype=float_dtype, device=device) flock.predicted_clusters = torch.tensor( [[0, 0.5, 0.5, 0], [1, 0, 0, 0]], dtype=float_dtype, device=device) flock.cluster_centers = torch.tensor( [[[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0.5, 0.5, 0], [0, 0, 0.5, 0, 0.5]], [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0]]], dtype=float_dtype, device=device) flock.reconstruct(indices=None) expected_reconstructed_input = torch.tensor( [[0, 0, 0.5, 0.5, 0], [0.2, 0.3, 0.4, 0.1, 0]], dtype=float_dtype, device=device) expected_predicted_reconstructed_input = torch.tensor( [[0, 0.5, 0.25, 0.25, 0], [1, 0, 0, 0, 0]], dtype=float_dtype, device=device) assert same(expected_reconstructed_input, flock.current_reconstructed_input) assert same(expected_predicted_reconstructed_input, flock.predicted_reconstructed_input)
def __init__(self, c_n_ccs, c_buffer_size, c_seq_length, c_seq_lookahead, p_seq_length, p_seq_lookahead, p_n_ccs, flock_size): super().__init__("Two Experts") expert_params1 = ExpertParams() expert_params1.flock_size = flock_size expert_params1.n_cluster_centers = c_n_ccs expert_params1.produce_actions = True expert_params1.temporal.seq_length = c_seq_length expert_params1.temporal.seq_lookahead = c_seq_lookahead 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.compute_backward_pass = True expert_params1.temporal.frustration_threshold = 2 expert_params2 = expert_params1.clone() expert_params1.spatial.buffer_size = c_buffer_size expert_params1.compute_reconstruction = True expert_params2.temporal.seq_length = p_seq_length expert_params2.temporal.seq_lookahead = p_seq_lookahead expert_params2.n_cluster_centers = p_n_ccs expert_params2.produce_actions = False expert_params2.temporal.frustration_threshold = 10 expert_node1 = ExpertFlockNode(expert_params1) expert_node2 = ExpertFlockNode(expert_params2) self.add_node(expert_node1) self.add_node(expert_node2) Connector.connect(self.inputs.data.output, expert_node1.inputs.sp.data_input) Connector.connect(self.inputs.reward.output, expert_node1.inputs.tp.reward_input) Connector.connect(self.inputs.reward.output, expert_node2.inputs.tp.reward_input) # Connect the experts to each other. 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) # Connect the group output. Connector.connect(expert_node1.outputs.sp.predicted_reconstructed_input, self.outputs.predicted_reconstructed_input.input)
def test_reconstruct_subflock(self, device, flock_class): float_dtype = get_float(device) params = ExpertParams() params.flock_size = 2 params.n_cluster_centers = 4 params.spatial.input_size = 5 params.spatial.buffer_size = 7 params.spatial.batch_size = 6 flock = flock_class(params, AllocatingCreator(device)) flock.forward_clusters = torch.tensor([[0, 0, 1, 0], [0, 0, 0, 0]], dtype=float_dtype, device=device) flock.predicted_clusters = torch.tensor( [[0, 0.5, 0.5, 0], [0, 0, 0, 0]], dtype=float_dtype, device=device) flock.cluster_centers = torch.tensor( [[[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0.5, 0.5, 0], [0, 0, 0.5, 0, 0.5]], [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0]]], dtype=float_dtype, device=device) indices = torch.tensor([0], dtype=torch.int64, device=device) flock.reconstruct(indices) expected_reconstructed_input = torch.tensor( [[0, 0, 0.5, 0.5, 0], [0, 0, 0, 0, 0]], dtype=float_dtype, device=device) expected_predicted_reconstructed_input = torch.tensor( [[0, 0.5, 0.25, 0.25, 0], [0, 0, 0, 0, 0]], dtype=float_dtype, device=device) assert same(expected_reconstructed_input, flock.current_reconstructed_input) assert same(expected_predicted_reconstructed_input, flock.predicted_reconstructed_input)