MCPcopy Create free account
hub / github.com/OpenDriveLab/TCP / forward

Method forward

roach/models/ppo_policy.py:142–165  ·  view source on GitHub ↗

used in collect_rollouts(), do not clamp actions

(self, obs_dict: Dict[str, np.ndarray], deterministic: bool = False, clip_action: bool = False, only_feature: bool = False, feature_input: np.ndarray = None)

Source from the content-addressed store, hash-verified

140 return values.flatten(), distribution.distribution
141
142 def forward(self, obs_dict: Dict[str, np.ndarray], deterministic: bool = False, clip_action: bool = False, only_feature: bool = False, feature_input: np.ndarray = None):
143 '''
144 used in collect_rollouts(), do not clamp actions
145 '''
146 with th.no_grad():
147 if feature_input is None:
148 obs_tensor_dict = dict([(k, th.as_tensor(v).to(self.device).unsqueeze(0)) for k, v in obs_dict.items()])
149 features = self._get_features(**obs_tensor_dict)
150 else:
151 features = th.tensor(feature_input).to(self.device)
152 if only_feature:
153 return features.cpu().numpy()
154 values = self.value_head(features)
155 distribution, mu, sigma = self._get_action_dist_from_features(features)
156 actions = distribution.get_actions(deterministic=deterministic)
157 log_prob = distribution.log_prob(actions)
158 actions = actions.cpu().numpy()
159 actions = self.unscale_action(actions)
160 if clip_action:
161 actions = np.clip(actions, self.action_space.low, self.action_space.high)
162 values = values.cpu().numpy().flatten()
163 log_prob = log_prob.cpu().numpy()
164 features = features.cpu().numpy()
165 return actions, values, log_prob, mu, sigma, features
166
167 def forward_value(self, obs_dict: Dict[str, np.ndarray]) -> np.ndarray:
168 with th.no_grad():

Callers

nothing calls this directly

Calls 6

_get_featuresMethod · 0.95
unscale_actionMethod · 0.95
flattenMethod · 0.80
get_actionsMethod · 0.45
log_probMethod · 0.45

Tested by

no test coverage detected