Created
April 9, 2020 22:47
-
-
Save zomux/ca860532dc18a38cefba962d3a068285 to your computer and use it in GitHub Desktop.
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
| mport torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| # Test double grad for convolution | |
| embed_layer = nn.Embedding(100, 256) | |
| conv_layer = nn.Conv1d(256, 256, 3, padding=1) | |
| linear_layer = nn.Linear(256, 100) | |
| energy_layer = nn.Linear(256, 1) | |
| embed_layer.cuda() | |
| conv_layer.cuda() | |
| linear_layer.cuda() | |
| energy_layer.cuda() | |
| x = torch.randint(100, (1, 10)).cuda() | |
| print("x=", x) | |
| embed = embed_layer(x) | |
| states = conv_layer(embed.transpose(1,2)).transpose(1,2) | |
| energy = energy_layer(states).mean() | |
| grad = torch.autograd.grad( | |
| energy, | |
| embed, | |
| create_graph=True | |
| )[0] | |
| new_states = embed + grad | |
| logits = linear_layer(new_states) | |
| loss = F.cross_entropy(logits.squeeze(0), x.flatten()) | |
| loss.backward() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment