def validate(self): """Checks if the input can be correctly parsed.""" if self.params.spatial.cluster_boost_threshold < self.params.n_cluster_centers: logger.warning( "Spatial pooler: number of cluster centers is greater than cluster boost threshold," " which means that unless batch_size is large, it is probable that even some useful clusters" " will get boosted!") # validate flock shape - just to be sure, it is actually called even before in get_receptive_field_shape() derive_flock_shape(self.inputs.sp.data_input.tensor.shape, self.params.flock_size)
def validate(self): """Checks if all inputs have valid shapes. Rewards have to be either one float which will be translated into a tuple (positive, negative) reward and used for all experts, or separate tuples one for each expert in either (flock_size, 2) or (flock_shape, 2). Context input should be either (flock_size, n_providers, NUMBER_OF_CONTEXT_TYPES, self.params.temporal.incoming_context_size) or (flock_shape, n_providers, NUMBER_OF_CONTEXT_TYPES, self.params.temporal.incoming_context_size). """ flock_shape = derive_flock_shape(self.inputs.sp.data_input.tensor.shape, self.params.flock_size) reward_input = self.inputs.tp.reward_input.tensor if reward_input is not None: expected_shapes = [(1,), (self.params.flock_size, 2), flock_shape + (2,), (2,)] if reward_input.size() not in expected_shapes: raise NodeValidationException(f"Reward input has unexpected shape {reward_input.size()}, " f"expected one of {expected_shapes}.") # Validate context_input shape context_input = self.inputs.tp.context_input.tensor if context_input is not None: matcher_pattern = ( TensorShapePatternMatcher.Sum(self.params.flock_size), TensorShapePatternMatcher.Sum(self.params.temporal.n_providers, greedy=True), TensorShapePatternMatcher.Exact((NUMBER_OF_CONTEXT_TYPES,)), TensorShapePatternMatcher.Sum(self.params.temporal.incoming_context_size, greedy=True) ) matcher = TensorShapePatternMatcher(matcher_pattern) if not matcher.matches(context_input.shape): pattern_str = ", ".join(map(str, matcher_pattern)) raise NodeValidationException( f"Context input has unexpected shape {list(context_input.shape)}, " f"expected pattern: [{pattern_str}]")
def get_receptive_field_shape(self) -> Tuple[Any]: flock_shape = derive_flock_shape(self.inputs.sp.data_input.tensor.shape, self.params.flock_size) n_flock_shape_dims = len(flock_shape) receptive_field_shape = self.inputs.sp.data_input.tensor.size()[n_flock_shape_dims:] return receptive_field_shape
def prepare_slots_from_flock(self, flock: SPFlock, output_shape_provider: OutputShapeProvider, data_input_shape: Tuple[Any], flock_size: int): super().prepare_slots_from_flock(flock, output_shape_provider, data_input_shape, flock_size) self.forward_clusters.tensor = flock.forward_clusters flock_shape = derive_flock_shape(data_input_shape, flock_size) self.forward_clusters.reshape_tensor(shape=flock_shape, dim=0)
def prepare_slots(self, unit: ExpertFlockUnit): data_input_shape = self._owner.inputs.sp.data_input.tensor.shape flock_size = self._owner.params.flock_size self.sp.prepare_slots_from_flock(unit.flock.sp_flock, self._owner, data_input_shape, flock_size) self.tp.prepare_slots_from_flock(unit.flock.tp_flock, data_input_shape, flock_size) self.output_context.tensor = unit.flock.output_context flock_shape = derive_flock_shape(data_input_shape, flock_size) # self.current_reconstructed_input.reshape_tensor(shape=flock_shape, dim=0) self.output_context.reshape_tensor(shape=flock_shape, dim=0)
def prepare_slots_from_flock(self, flock: TPFlock, data_input_shape: Optional[Tuple[Any]], flock_size: int): super().prepare_slots_from_flock(flock, data_input_shape, flock_size) self.projection_outputs.tensor = flock.projection_outputs self.action_outputs.tensor = flock.action_rewards self.action_outputs.tensor = flock.action_outputs self.projection_outputs.tensor = flock.projection_outputs self.best_matching_context.tensor = flock.best_matching_context if (data_input_shape is not None) and (flock_size is not None): flock_shape = derive_flock_shape(data_input_shape, flock_size) self.projection_outputs.reshape_tensor(shape=flock_shape, dim=0)
def prepare_slots_from_flock(self, flock: SPFlock, output_shape_provider: OutputShapeProvider, data_input_shape: Tuple[Any], flock_size: int): super().prepare_slots_from_flock(flock, output_shape_provider, data_input_shape, flock_size) self.current_reconstructed_input.tensor = flock.current_reconstructed_input self.predicted_reconstructed_input.tensor = flock.predicted_reconstructed_input # Reshape some of the tensors to match the input space. self.current_reconstructed_input.reshape_tensor( output_shape_provider.output_shape) self.predicted_reconstructed_input.reshape_tensor( output_shape_provider.output_shape) flock_shape = derive_flock_shape(data_input_shape, flock_size) self.current_reconstructed_input.reshape_tensor(shape=flock_shape, dim=0) self.predicted_reconstructed_input.reshape_tensor(shape=flock_shape, dim=0)
def test_derive_flock_shape_exception(self, input_shape, flock_size): with raises(IllegalArgumentException): derive_flock_shape(input_shape, flock_size)
def test_derive_flock_shape(self, input_shape, flock_size, expected_flock_shape): result = derive_flock_shape(input_shape, flock_size) assert expected_flock_shape == result