|
|
|
|
|
|
else 0 |
|
|
|
) |
|
|
|
|
|
|
|
self.visual_processors, self.vector_processors, encoder_input_size = ModelUtils.create_input_processors( |
|
|
|
self.processors, encoder_input_size = ModelUtils.create_input_processors( |
|
|
|
observation_shapes, |
|
|
|
self.h_size, |
|
|
|
network_settings.vis_encode_type, |
|
|
|
|
|
|
else: |
|
|
|
self.lstm = None # type: ignore |
|
|
|
|
|
|
|
def update_normalization(self, vec_inputs: List[torch.Tensor]) -> None: |
|
|
|
for vec_input, vec_enc in zip(vec_inputs, self.vector_processors): |
|
|
|
vec_enc.update_normalization(vec_input) |
|
|
|
def update_normalization(self, net_inputs: List[torch.Tensor]) -> None: |
|
|
|
for _in, enc in zip(net_inputs, self.processors): |
|
|
|
enc.update_normalization(_in) |
|
|
|
for n1, n2 in zip(self.vector_processors, other_network.vector_processors): |
|
|
|
for n1, n2 in zip(self.processors, other_network.processors): |
|
|
|
n1.copy_normalization(n2) |
|
|
|
|
|
|
|
@property |
|
|
|
|
|
|
def forward( |
|
|
|
self, |
|
|
|
vec_inputs: List[torch.Tensor], |
|
|
|
vis_inputs: List[torch.Tensor], |
|
|
|
net_inputs: List[torch.Tensor], |
|
|
|
for idx, processor in enumerate(self.vector_processors): |
|
|
|
vec_input = vec_inputs[idx] |
|
|
|
processed_vec = processor(vec_input) |
|
|
|
for idx, processor in enumerate(self.processors): |
|
|
|
net_input = net_inputs[idx] |
|
|
|
if not exporting_to_onnx.is_exporting() and len(net_input.shape) > 3: |
|
|
|
net_input = net_input.permute([0, 3, 1, 2]) |
|
|
|
processed_vec = processor(net_input) |
|
|
|
for idx, processor in enumerate(self.visual_processors): |
|
|
|
vis_input = vis_inputs[idx] |
|
|
|
if not exporting_to_onnx.is_exporting(): |
|
|
|
vis_input = vis_input.permute([0, 3, 1, 2]) |
|
|
|
processed_vis = processor(vis_input) |
|
|
|
encodes.append(processed_vis) |
|
|
|
|
|
|
|
if len(encodes) == 0: |
|
|
|
raise Exception("No valid inputs to network.") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def forward( |
|
|
|
self, |
|
|
|
vec_inputs: List[torch.Tensor], |
|
|
|
vis_inputs: List[torch.Tensor], |
|
|
|
net_inputs: List[torch.Tensor], |
|
|
|
vec_inputs, vis_inputs, actions, memories, sequence_length |
|
|
|
net_inputs, actions, memories, sequence_length |
|
|
|
) |
|
|
|
output = self.value_heads(encoding) |
|
|
|
return output, memories |
|
|
|
|
|
|
@abc.abstractmethod |
|
|
|
def update_normalization(self, vector_obs: List[torch.Tensor]) -> None: |
|
|
|
def update_normalization(self, net_inputs: List[torch.Tensor]) -> None: |
|
|
|
""" |
|
|
|
Updates normalization of Actor based on the provided List of vector obs. |
|
|
|
:param vector_obs: A List of vector obs as tensors. |
|
|
|
|
|
|
@abc.abstractmethod |
|
|
|
def get_dists( |
|
|
|
self, |
|
|
|
vec_inputs: List[torch.Tensor], |
|
|
|
vis_inputs: List[torch.Tensor], |
|
|
|
net_inputs: List[torch.Tensor], |
|
|
|
masks: Optional[torch.Tensor] = None, |
|
|
|
memories: Optional[torch.Tensor] = None, |
|
|
|
sequence_length: int = 1, |
|
|
|
|
|
|
@abc.abstractmethod |
|
|
|
def forward( |
|
|
|
self, |
|
|
|
vec_inputs: List[torch.Tensor], |
|
|
|
vis_inputs: List[torch.Tensor], |
|
|
|
net_inputs: List[torch.Tensor], |
|
|
|
masks: Optional[torch.Tensor] = None, |
|
|
|
memories: Optional[torch.Tensor] = None, |
|
|
|
) -> Tuple[torch.Tensor, int, int, int, int]: |
|
|
|
|
|
|
@abc.abstractmethod |
|
|
|
def critic_pass( |
|
|
|
self, |
|
|
|
vec_inputs: List[torch.Tensor], |
|
|
|
vis_inputs: List[torch.Tensor], |
|
|
|
net_inputs: List[torch.Tensor], |
|
|
|
memories: Optional[torch.Tensor] = None, |
|
|
|
sequence_length: int = 1, |
|
|
|
) -> Tuple[Dict[str, torch.Tensor], torch.Tensor]: |
|
|
|
|
|
|
@abc.abstractmethod |
|
|
|
def get_dist_and_value( |
|
|
|
self, |
|
|
|
vec_inputs: List[torch.Tensor], |
|
|
|
vis_inputs: List[torch.Tensor], |
|
|
|
net_inputs: List[torch.Tensor], |
|
|
|
masks: Optional[torch.Tensor] = None, |
|
|
|
memories: Optional[torch.Tensor] = None, |
|
|
|
sequence_length: int = 1, |
|
|
|
|
|
|
def memory_size(self) -> int: |
|
|
|
return self.network_body.memory_size |
|
|
|
|
|
|
|
def update_normalization(self, vector_obs: List[torch.Tensor]) -> None: |
|
|
|
self.network_body.update_normalization(vector_obs) |
|
|
|
def update_normalization(self, net_inputs: List[torch.Tensor]) -> None: |
|
|
|
self.network_body.update_normalization(net_inputs) |
|
|
|
|
|
|
|
def sample_action(self, dists: List[DistInstance]) -> List[torch.Tensor]: |
|
|
|
actions = [] |
|
|
|
|
|
|
|
|
|
|
def get_dists( |
|
|
|
self, |
|
|
|
vec_inputs: List[torch.Tensor], |
|
|
|
vis_inputs: List[torch.Tensor], |
|
|
|
net_inputs: List[torch.Tensor], |
|
|
|
vec_inputs, vis_inputs, memories=memories, sequence_length=sequence_length |
|
|
|
net_inputs, memories=memories, sequence_length=sequence_length |
|
|
|
) |
|
|
|
if self.action_spec.is_continuous(): |
|
|
|
dists = self.distribution(encoding) |
|
|
|
|
|
|
|
|
|
|
def forward( |
|
|
|
self, |
|
|
|
vec_inputs: List[torch.Tensor], |
|
|
|
vis_inputs: List[torch.Tensor], |
|
|
|
net_inputs: List[torch.Tensor], |
|
|
|
masks: Optional[torch.Tensor] = None, |
|
|
|
memories: Optional[torch.Tensor] = None, |
|
|
|
) -> Tuple[torch.Tensor, int, int, int, int]: |
|
|
|
|
|
|
dists, _ = self.get_dists(vec_inputs, vis_inputs, masks, memories, 1) |
|
|
|
dists, _ = self.get_dists(net_inputs, masks, memories, 1) |
|
|
|
if self.action_spec.is_continuous(): |
|
|
|
action_list = self.sample_action(dists) |
|
|
|
action_out = torch.stack(action_list, dim=-1) |
|
|
|
|
|
|
|
|
|
|
def critic_pass( |
|
|
|
self, |
|
|
|
vec_inputs: List[torch.Tensor], |
|
|
|
vis_inputs: List[torch.Tensor], |
|
|
|
net_inputs: List[torch.Tensor], |
|
|
|
vec_inputs, vis_inputs, memories=memories, sequence_length=sequence_length |
|
|
|
net_inputs, memories=memories, sequence_length=sequence_length |
|
|
|
vec_inputs: List[torch.Tensor], |
|
|
|
vis_inputs: List[torch.Tensor], |
|
|
|
net_inputs: List[torch.Tensor], |
|
|
|
vec_inputs, vis_inputs, memories=memories, sequence_length=sequence_length |
|
|
|
net_inputs, memories=memories, sequence_length=sequence_length |
|
|
|
) |
|
|
|
if self.action_spec.is_continuous(): |
|
|
|
dists = self.distribution(encoding) |
|
|
|
|
|
|
|
|
|
|
def critic_pass( |
|
|
|
self, |
|
|
|
vec_inputs: List[torch.Tensor], |
|
|
|
vis_inputs: List[torch.Tensor], |
|
|
|
net_inputs: List[torch.Tensor], |
|
|
|
memories: Optional[torch.Tensor] = None, |
|
|
|
sequence_length: int = 1, |
|
|
|
) -> Tuple[Dict[str, torch.Tensor], torch.Tensor]: |
|
|
|
|
|
|
actor_mem, critic_mem = torch.split(memories, self.memory_size // 2, -1) |
|
|
|
value_outputs, critic_mem_out = self.critic( |
|
|
|
vec_inputs, vis_inputs, memories=critic_mem, sequence_length=sequence_length |
|
|
|
net_inputs, memories=critic_mem, sequence_length=sequence_length |
|
|
|
) |
|
|
|
if actor_mem is not None: |
|
|
|
# Make memories with the actor mem unchanged |
|
|
|
|
|
|
|
|
|
|
def get_dist_and_value( |
|
|
|
self, |
|
|
|
vec_inputs: List[torch.Tensor], |
|
|
|
vis_inputs: List[torch.Tensor], |
|
|
|
net_inputs: List[torch.Tensor], |
|
|
|
masks: Optional[torch.Tensor] = None, |
|
|
|
memories: Optional[torch.Tensor] = None, |
|
|
|
sequence_length: int = 1, |
|
|
|
|
|
|
critic_mem = None |
|
|
|
actor_mem = None |
|
|
|
dists, actor_mem_outs = self.get_dists( |
|
|
|
vec_inputs, |
|
|
|
vis_inputs, |
|
|
|
memories=actor_mem, |
|
|
|
sequence_length=sequence_length, |
|
|
|
masks=masks, |
|
|
|
net_inputs, memories=actor_mem, sequence_length=sequence_length, masks=masks |
|
|
|
vec_inputs, vis_inputs, memories=critic_mem, sequence_length=sequence_length |
|
|
|
net_inputs, memories=critic_mem, sequence_length=sequence_length |
|
|
|
) |
|
|
|
if self.use_lstm: |
|
|
|
mem_out = torch.cat([actor_mem_outs, critic_mem_outs], dim=-1) |
|
|
|
|
|
|
|
|
|
|
def update_normalization(self, vector_obs: List[torch.Tensor]) -> None: |
|
|
|
super().update_normalization(vector_obs) |
|
|
|
self.critic.network_body.update_normalization(vector_obs) |
|
|
|
def update_normalization(self, net_inputs: List[torch.Tensor]) -> None: |
|
|
|
super().update_normalization(net_inputs) |
|
|
|
self.critic.network_body.update_normalization(net_inputs) |
|
|
|
|
|
|
|
|
|
|
|
class GlobalSteps(nn.Module): |
|
|
|