Created
April 30, 2018 09:04
-
-
Save Oktai15/d4d168ff88ea8ba8a072ae0772889a39 to your computer and use it in GitHub Desktop.
PyTorch: simple example
This file contains 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 | |
import torch.nn.functional as fun | |
data_size = 10 | |
input_size = 28 * 28 | |
hidden1_output = 200 | |
output_size = 1 | |
data = torch.randn(data_size, input_size) | |
target = torch.randn(data_size, output_size) | |
model = nn.Sequential( | |
nn.Linear(input_size, hidden1_output), | |
nn.ReLU(), | |
nn.Linear(hidden1_output, output_size) | |
) | |
opt = torch.optim.SGD(model.parameters(), lr=1e-3) | |
for step in range(100): | |
target_ = model(data) | |
loss = fun.mse_loss(target_, target) | |
loss.backward() | |
opt.step() | |
opt.zero_grad() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment