Created
June 20, 2018 02:31
-
-
Save rbrigden/c26c418d0d8e9d83f30105737ed32e04 to your computer and use it in GitHub Desktop.
Apply torch functions to weight parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import torch.nn as nn | |
from torch.autograd import Variable | |
affine = nn.Linear(10, 10) | |
# A linear mapping to a random vector... just for quick demo purposes | |
x = Variable(torch.randn(100, 10)) | |
y = Variable(torch.randn(100, 10)) | |
weird_loss = torch.mean(torch.exp(affine.weight)) | |
mse_loss = nn.MSELoss() | |
pred_loss = mse_loss(affine(x), y) | |
net_loss = pred_loss + weird_loss | |
net_loss.backward() | |
# should work just fine! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment