def test_invalid_visual_input_size(encoder_type): with pytest.raises(UnityTrainerException): obs_spec = create_observation_specs_with_shapes([( ModelUtils.MIN_RESOLUTION_FOR_ENCODER[encoder_type] - 1, ModelUtils.MIN_RESOLUTION_FOR_ENCODER[encoder_type], 1, )]) ModelUtils.create_input_processors(obs_spec, 20, encoder_type, 20, False)
def test_create_inputs(encoder_type, normalize, num_vector, num_visual): vec_obs_shape = (5,) vis_obs_shape = (84, 84, 3) obs_shapes = [] for _ in range(num_vector): obs_shapes.append(vec_obs_shape) for _ in range(num_visual): obs_shapes.append(vis_obs_shape) h_size = 128 obs_spec = create_observation_specs_with_shapes(obs_shapes) encoders, embedding_sizes = ModelUtils.create_input_processors( obs_spec, h_size, encoder_type, h_size, normalize ) total_output = sum(embedding_sizes) vec_enc = [] vis_enc = [] for i, enc in enumerate(encoders): if len(obs_shapes[i]) == 1: vec_enc.append(enc) else: vis_enc.append(enc) assert len(vec_enc) == num_vector assert len(vis_enc) == num_visual assert total_output == int(num_visual * h_size + vec_obs_shape[0] * num_vector) if num_vector > 0: assert isinstance(vec_enc[0], VectorInput) for enc in vis_enc: assert isinstance(enc, ModelUtils.get_encoder_for_type(encoder_type))
def __init__( self, observation_shapes: List[Tuple[int, ...]], network_settings: NetworkSettings, encoded_act_size: int = 0, ): super().__init__() self.normalize = network_settings.normalize self.use_lstm = network_settings.memory is not None self.h_size = network_settings.hidden_units self.m_size = (network_settings.memory.memory_size if network_settings.memory is not None else 0) self.visual_processors, self.vector_processors, encoder_input_size = ModelUtils.create_input_processors( observation_shapes, self.h_size, network_settings.vis_encode_type, normalize=self.normalize, ) total_enc_size = encoder_input_size + encoded_act_size self.linear_encoder = LinearEncoder(total_enc_size, network_settings.num_layers, self.h_size) if self.use_lstm: self.lstm = LSTM(self.h_size, self.m_size) else: self.lstm = None # type: ignore
def __init__( self, sensor_specs: List[SensorSpec], network_settings: NetworkSettings, encoded_act_size: int = 0, ): super().__init__() self.normalize = network_settings.normalize self.use_lstm = network_settings.memory is not None self.h_size = network_settings.hidden_units self.m_size = (network_settings.memory.memory_size if network_settings.memory is not None else 0) self.processors, self.embedding_sizes = ModelUtils.create_input_processors( sensor_specs, self.h_size, network_settings.vis_encode_type, normalize=self.normalize, ) total_enc_size = sum(self.embedding_sizes) + encoded_act_size self.linear_encoder = LinearEncoder(total_enc_size, network_settings.num_layers, self.h_size) if self.use_lstm: self.lstm = LSTM(self.h_size, self.m_size) else: self.lstm = None # type: ignore
def __init__( self, observation_specs: List[ObservationSpec], h_size: int, vis_encode_type: EncoderType, normalize: bool = False, ): """ Returns an ObservationEncoder that can process and encode a set of observations. Will use an RSA if needed for variable length observations. """ super().__init__() self.processors, self.embedding_sizes = ModelUtils.create_input_processors( observation_specs, h_size, vis_encode_type, normalize=normalize ) self.rsa, self.x_self_encoder = ModelUtils.create_residual_self_attention( self.processors, self.embedding_sizes, h_size ) if self.rsa is not None: total_enc_size = sum(self.embedding_sizes) + h_size else: total_enc_size = sum(self.embedding_sizes) self.normalize = normalize self._total_enc_size = total_enc_size self._total_goal_enc_size = 0 self._goal_processor_indices: List[int] = [] for i in range(len(observation_specs)): if observation_specs[i].observation_type == ObservationType.GOAL_SIGNAL: self._total_goal_enc_size += self.embedding_sizes[i] self._goal_processor_indices.append(i)
def __init__( self, observation_specs: List[ObservationSpec], network_settings: NetworkSettings, encoded_act_size: int = 0, ): super().__init__() self.normalize = network_settings.normalize self.use_lstm = network_settings.memory is not None self.h_size = network_settings.hidden_units self.m_size = (network_settings.memory.memory_size if network_settings.memory is not None else 0) self.processors, self.embedding_sizes = ModelUtils.create_input_processors( observation_specs, self.h_size, network_settings.vis_encode_type, normalize=self.normalize, ) entity_num_max: int = 0 var_processors = [ p for p in self.processors if isinstance(p, EntityEmbedding) ] for processor in var_processors: entity_max: int = processor.entity_num_max_elements # Only adds entity max if it was known at construction if entity_max > 0: entity_num_max += entity_max if len(var_processors) > 0: if sum(self.embedding_sizes): self.x_self_encoder = LinearEncoder( sum(self.embedding_sizes), 1, self.h_size, kernel_init=Initialization.Normal, kernel_gain=(0.125 / self.h_size)**0.5, ) self.rsa = ResidualSelfAttention(self.h_size, entity_num_max) total_enc_size = sum(self.embedding_sizes) + self.h_size else: total_enc_size = sum(self.embedding_sizes) total_enc_size += encoded_act_size self.linear_encoder = LinearEncoder(total_enc_size, network_settings.num_layers, self.h_size) if self.use_lstm: self.lstm = LSTM(self.h_size, self.m_size) else: self.lstm = None # type: ignore
def test_create_inputs(encoder_type, normalize, num_vector, num_visual): vec_obs_shape = (5, ) vis_obs_shape = (84, 84, 3) obs_shapes = [] for _ in range(num_vector): obs_shapes.append(vec_obs_shape) for _ in range(num_visual): obs_shapes.append(vis_obs_shape) h_size = 128 vis_enc, vec_enc, total_output = ModelUtils.create_input_processors( obs_shapes, h_size, encoder_type, normalize) vec_enc = list(vec_enc) vis_enc = list(vis_enc) assert len(vec_enc) == (1 if num_vector >= 1 else 0) assert len(vis_enc) == num_visual assert total_output == int(num_visual * h_size + vec_obs_shape[0] * num_vector) if num_vector > 0: assert isinstance(vec_enc[0], VectorInput) for enc in vis_enc: assert isinstance(enc, ModelUtils.get_encoder_for_type(encoder_type))
def __init__( self, observation_specs: List[ObservationSpec], h_size: int, vis_encode_type: EncoderType, normalize: bool = False, ): """ Returns an ObservationEncoder that can process and encode a set of observations. Will use an RSA if needed for variable length observations. """ super().__init__() self.processors, self.embedding_sizes = ModelUtils.create_input_processors( observation_specs, h_size, vis_encode_type, normalize=normalize) self.rsa, self.x_self_encoder = ModelUtils.create_residual_self_attention( self.processors, self.embedding_sizes, h_size) if self.rsa is not None: total_enc_size = sum(self.embedding_sizes) + h_size else: total_enc_size = sum(self.embedding_sizes) self.normalize = normalize self._total_enc_size = total_enc_size