Skip to content

Instantly share code, notes, and snippets.

@rbrigden
Created June 20, 2018 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbrigden/c26c418d0d8e9d83f30105737ed32e04 to your computer and use it in GitHub Desktop.
Save rbrigden/c26c418d0d8e9d83f30105737ed32e04 to your computer and use it in GitHub Desktop.
Apply torch functions to weight parameters
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