-
Notifications
You must be signed in to change notification settings - Fork 170
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
L1 loss added to the models #85
base: main
Are you sure you want to change the base?
Changes from 9 commits
905a9e2
1a8652c
9938bae
a0269b7
ac73bb3
d4d835e
6080a8f
472e3a3
44bbd5c
6ad1c0a
518f64a
e01360d
ee4a8c2
0b9283c
151f7f3
101dafb
20872bc
6394636
cc0078e
d1739e3
e7c96a8
c85c8f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -424,6 +424,20 @@ def _log_p_x_given_z(self, recon_x, x): | |
reduction="none", | ||
).sum(dim=-1) | ||
|
||
elif self.model_config.reconstruction_loss == "l1": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this model, this should correspond to a known distribution. The |
||
# sigma is taken as I_D | ||
recon_loss = ( | ||
-0.5 | ||
* F.l1_loss( | ||
recon_x.reshape(x.shape[0], -1), | ||
x.reshape(x.shape[0], -1), | ||
reduction="none", | ||
).sum(dim=-1) | ||
) | ||
-torch.log(torch.tensor([2 * np.pi]).to(x.device)) * np.prod( | ||
self.input_dim | ||
) / 2 | ||
|
||
return recon_loss | ||
|
||
def _log_z(self, z): | ||
|
@@ -572,6 +586,18 @@ def get_nll(self, data, n_samples=1, batch_size=100): | |
reduction="none", | ||
).sum(dim=-1) | ||
|
||
elif self.model_config.reconstruction_loss == "l1": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The computation of the likelihood cannot be handled with the l1 loss since it does not correspond to a tractable distribution per say. |
||
|
||
log_p_x_given_z = -0.5 * F.l1_loss( | ||
recon_x.reshape(x_rep.shape[0], -1), | ||
x_rep.reshape(x_rep.shape[0], -1), | ||
reduction="none", | ||
).sum(dim=-1) - torch.tensor( | ||
[np.prod(self.input_dim) / 2 * np.log(np.pi * 2)] | ||
).to( | ||
data.device | ||
) # decoding distribution is assumed unit variance N(mu, I) | ||
|
||
log_p_x.append( | ||
log_p_x_given_z | ||
+ log_p_z | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,6 +115,14 @@ def loss_function(self, recon_x, x, loc, concentration, z): | |
reduction="none", | ||
).sum(dim=-1) | ||
|
||
elif self.model_config.reconstruction_loss == "l1": | ||
|
||
recon_loss = F.l1_loss( | ||
recon_x.reshape(x.shape[0], -1), | ||
x.reshape(x.shape[0], -1), | ||
reduction="none", | ||
).sum(dim=-1) | ||
|
||
KLD = self._compute_kl(m=loc.shape[-1], concentration=concentration) | ||
|
||
return (recon_loss + KLD).mean(dim=0), recon_loss.mean(dim=0), KLD.mean(dim=0) | ||
|
@@ -286,6 +294,18 @@ def get_nll(self, data, n_samples=1, batch_size=100): | |
reduction="none", | ||
).sum(dim=-1) | ||
|
||
elif self.model_config.reconstruction_loss == "l1": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The computation of the likelihood cannot be handled with the l1 loss since it does not correspond to a tractable distribution per say. |
||
|
||
log_p_x_given_z = -0.5 * F.l1_loss( | ||
recon_x.reshape(x_rep.shape[0], -1), | ||
x_rep.reshape(x_rep.shape[0], -1), | ||
reduction="none", | ||
).sum(dim=-1) - torch.tensor( | ||
[np.prod(self.input_dim) / 2 * np.log(np.pi * 2)] | ||
).to( | ||
data.device | ||
) # decoding distribution is assumed unit variance N(mu, I) | ||
|
||
log_p_x.append( | ||
log_p_x_given_z + log_p_z - log_q_z_given_x | ||
) # log(2*pi) simplifies | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ class VAEConfig(BaseAEConfig): | |
Parameters: | ||
input_dim (tuple): The input_data dimension. | ||
latent_dim (int): The latent space dimension. Default: None. | ||
reconstruction_loss (str): The reconstruction loss to use ['bce', 'mse']. Default: 'mse' | ||
reconstruction_loss (str): The reconstruction loss to use ['bce', 'l1', 'mse']. Default: 'mse' | ||
""" | ||
|
||
reconstruction_loss: Literal["bce", "mse"] = "mse" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be replaced by reconstruction_loss: Literal["bce", "mse", "l1"] = "mse" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l1 cannot be allowed in this model (see next comment)