Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DON'T MERGE] Normalize GAIL encoder inputs #4670

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self, specs: BehaviorSpec, settings: GAILSettings) -> None:

def evaluate(self, mini_batch: AgentBuffer) -> np.ndarray:
with torch.no_grad():
self._discriminator_network.update_normalization(mini_batch)
estimates, _ = self._discriminator_network.compute_estimate(
mini_batch, use_vail_noise=False
)
Expand Down Expand Up @@ -70,7 +71,7 @@ def __init__(self, specs: BehaviorSpec, settings: GAILSettings) -> None:
self._settings = settings

encoder_settings = NetworkSettings(
normalize=False,
normalize=True,
hidden_units=settings.encoding_size,
num_layers=2,
vis_encode_type=EncoderType.SIMPLE,
Expand Down Expand Up @@ -104,6 +105,13 @@ def __init__(self, specs: BehaviorSpec, settings: GAILSettings) -> None:
linear_layer(estimator_input_size, 1), torch.nn.Sigmoid()
)

def update_normalization(self, mini_batch: AgentBuffer) -> None:
"""
Updates the normalization of this Discriminator's encoder.
"""
vec_inputs, _ = self.get_state_inputs(mini_batch)
self.encoder.update_normalization(vec_inputs)

def get_action_input(self, mini_batch: AgentBuffer) -> torch.Tensor:
"""
Creates the action Tensor. In continuous case, corresponds to the action. In
Expand Down Expand Up @@ -271,9 +279,14 @@ def compute_gradient_magnitude(
use_vail_noise = True
z_mu = self._z_mu_layer(hidden)
hidden = torch.normal(z_mu, self._z_sigma * use_vail_noise)
estimate = self._estimator(hidden).squeeze(1).sum()
gradient = torch.autograd.grad(estimate, encoder_input, create_graph=True)[0]
estimate = self._estimator(hidden).squeeze(1)
gradient = torch.autograd.grad(
estimate,
encoder_input,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think it makes more sense to compute the gradient with respect to the normalized observations rather than the raw ones. Not sure at all how to do this without having to take the normalization out of the NetworkBody.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think without doing this it's actually broken and doesn't train.

We could do the ugly thing of reaching into the NetworkBody and grabbing the normalized obs, or grabbing the normalizer and applying it to the observation.

grad_outputs=torch.ones(estimate.shape),
create_graph=True,
)[0]
# Norm's gradient could be NaN at 0. Use our own safe_norm
safe_norm = (torch.sum(gradient ** 2, dim=1) + self.EPSILON).sqrt()
safe_norm = (torch.sum(torch.pow(gradient, 2), dim=1) + self.EPSILON).sqrt()
gradient_mag = torch.mean((safe_norm - 1) ** 2)
return gradient_mag