-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c0d13c5
Showing
91 changed files
with
3,492 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import torch | ||
from torch import autograd | ||
|
||
|
||
x = torch.tensor(1.) | ||
a = torch.tensor(1., requires_grad=True) | ||
b = torch.tensor(2., requires_grad=True) | ||
c = torch.tensor(3., requires_grad=True) | ||
|
||
y = a**2 * x + b * x + c | ||
|
||
print('before:', a.grad, b.grad, c.grad) | ||
grads = autograd.grad(y, [a, b, c]) | ||
print('after :', grads[0], grads[1], grads[2]) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
torch.Tensor{1,2,3} | ||
|
||
function gradUpdate(mlp,x,y,learningRate) | ||
local criterion = nn.ClassNLLCriterion() | ||
pred = mlp:forward(x) | ||
local err = criterion:forward(pred, y); | ||
mlp:zeroGradParameters(); | ||
local t = criterion:backward(pred, y); | ||
mlp:backward(x, t); | ||
mlp:updateParameters(learningRate); | ||
end |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import torch | ||
import time | ||
print(torch.__version__) | ||
print(torch.cuda.is_available()) | ||
# print('hello, world.') | ||
|
||
|
||
a = torch.randn(10000, 1000) | ||
b = torch.randn(1000, 2000) | ||
|
||
t0 = time.time() | ||
c = torch.matmul(a, b) | ||
t1 = time.time() | ||
print(a.device, t1 - t0, c.norm(2)) | ||
|
||
device = torch.device('cuda') | ||
a = a.to(device) | ||
b = b.to(device) | ||
|
||
t0 = time.time() | ||
c = torch.matmul(a, b) | ||
t2 = time.time() | ||
print(a.device, t2 - t0, c.norm(2)) | ||
|
||
t0 = time.time() | ||
c = torch.matmul(a, b) | ||
t2 = time.time() | ||
print(a.device, t2 - t0, c.norm(2)) | ||
|
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import torch | ||
|
||
print(torch.__version__) | ||
print('gpu:', torch.cuda.is_available()) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# DeepLearningTutorials |
Binary file not shown.
Empty file.
Binary file not shown.
Empty file.
Binary file not shown.
Empty file.
Binary file not shown.
Empty file.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import numpy as np | ||
from mpl_toolkits.mplot3d import Axes3D | ||
from matplotlib import pyplot as plt | ||
import torch | ||
|
||
|
||
|
||
def himmelblau(x): | ||
return (x[0] ** 2 + x[1] - 11) ** 2 + (x[0] + x[1] ** 2 - 7) ** 2 | ||
|
||
|
||
x = np.arange(-6, 6, 0.1) | ||
y = np.arange(-6, 6, 0.1) | ||
print('x,y range:', x.shape, y.shape) | ||
X, Y = np.meshgrid(x, y) | ||
print('X,Y maps:', X.shape, Y.shape) | ||
Z = himmelblau([X, Y]) | ||
|
||
fig = plt.figure('himmelblau') | ||
ax = fig.gca(projection='3d') | ||
ax.plot_surface(X, Y, Z) | ||
ax.view_init(60, -30) | ||
ax.set_xlabel('x') | ||
ax.set_ylabel('y') | ||
plt.show() | ||
|
||
|
||
# [1., 0.], [-4, 0.], [4, 0.] | ||
x = torch.tensor([-4., 0.], requires_grad=True) | ||
optimizer = torch.optim.Adam([x], lr=1e-3) | ||
for step in range(20000): | ||
|
||
pred = himmelblau(x) | ||
|
||
optimizer.zero_grad() | ||
pred.backward() | ||
optimizer.step() | ||
|
||
if step % 2000 == 0: | ||
print ('step {}: x = {}, f(x) = {}' | ||
.format(step, x.tolist(), pred.item())) |
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
import torch.optim as optim | ||
from torchvision import datasets, transforms | ||
|
||
|
||
batch_size=200 | ||
learning_rate=0.01 | ||
epochs=10 | ||
|
||
train_loader = torch.utils.data.DataLoader( | ||
datasets.MNIST('../data', train=True, download=True, | ||
transform=transforms.Compose([ | ||
transforms.ToTensor(), | ||
transforms.Normalize((0.1307,), (0.3081,)) | ||
])), | ||
batch_size=batch_size, shuffle=True) | ||
test_loader = torch.utils.data.DataLoader( | ||
datasets.MNIST('../data', train=False, transform=transforms.Compose([ | ||
transforms.ToTensor(), | ||
transforms.Normalize((0.1307,), (0.3081,)) | ||
])), | ||
batch_size=batch_size, shuffle=True) | ||
|
||
|
||
|
||
w1, b1 = torch.randn(200, 784, requires_grad=True),\ | ||
torch.zeros(200, requires_grad=True) | ||
w2, b2 = torch.randn(200, 200, requires_grad=True),\ | ||
torch.zeros(200, requires_grad=True) | ||
w3, b3 = torch.randn(10, 200, requires_grad=True),\ | ||
torch.zeros(10, requires_grad=True) | ||
|
||
torch.nn.init.kaiming_normal_(w1) | ||
torch.nn.init.kaiming_normal_(w2) | ||
torch.nn.init.kaiming_normal_(w3) | ||
|
||
|
||
def forward(x): | ||
x = x@w1.t() + b1 | ||
x = F.relu(x) | ||
x = x@w2.t() + b2 | ||
x = F.relu(x) | ||
x = x@w3.t() + b3 | ||
x = F.relu(x) | ||
return x | ||
|
||
|
||
|
||
optimizer = optim.SGD([w1, b1, w2, b2, w3, b3], lr=learning_rate) | ||
criteon = nn.CrossEntropyLoss() | ||
|
||
for epoch in range(epochs): | ||
|
||
for batch_idx, (data, target) in enumerate(train_loader): | ||
data = data.view(-1, 28*28) | ||
|
||
logits = forward(data) | ||
loss = criteon(logits, target) | ||
|
||
optimizer.zero_grad() | ||
loss.backward() | ||
# print(w1.grad.norm(), w2.grad.norm()) | ||
optimizer.step() | ||
|
||
if batch_idx % 100 == 0: | ||
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( | ||
epoch, batch_idx * len(data), len(train_loader.dataset), | ||
100. * batch_idx / len(train_loader), loss.item())) | ||
|
||
|
||
test_loss = 0 | ||
correct = 0 | ||
for data, target in test_loader: | ||
data = data.view(-1, 28 * 28) | ||
logits = forward(data) | ||
test_loss += criteon(logits, target).item() | ||
|
||
pred = logits.data.max(1)[1] | ||
correct += pred.eq(target.data).sum() | ||
|
||
test_loss /= len(test_loader.dataset) | ||
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( | ||
test_loss, correct, len(test_loader.dataset), | ||
100. * correct / len(test_loader.dataset))) |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
import torch.optim as optim | ||
from torchvision import datasets, transforms | ||
|
||
|
||
batch_size=200 | ||
learning_rate=0.01 | ||
epochs=10 | ||
|
||
train_loader = torch.utils.data.DataLoader( | ||
datasets.MNIST('../data', train=True, download=True, | ||
transform=transforms.Compose([ | ||
transforms.ToTensor(), | ||
transforms.Normalize((0.1307,), (0.3081,)) | ||
])), | ||
batch_size=batch_size, shuffle=True) | ||
test_loader = torch.utils.data.DataLoader( | ||
datasets.MNIST('../data', train=False, transform=transforms.Compose([ | ||
transforms.ToTensor(), | ||
transforms.Normalize((0.1307,), (0.3081,)) | ||
])), | ||
batch_size=batch_size, shuffle=True) | ||
|
||
|
||
|
||
class MLP(nn.Module): | ||
|
||
def __init__(self): | ||
super(MLP, self).__init__() | ||
|
||
self.model = nn.Sequential( | ||
nn.Linear(784, 200), | ||
nn.ReLU(inplace=True), | ||
nn.Linear(200, 200), | ||
nn.ReLU(inplace=True), | ||
nn.Linear(200, 10), | ||
nn.ReLU(inplace=True), | ||
) | ||
|
||
def forward(self, x): | ||
x = self.model(x) | ||
|
||
return x | ||
|
||
net = MLP() | ||
optimizer = optim.SGD(net.parameters(), lr=learning_rate) | ||
criteon = nn.CrossEntropyLoss() | ||
|
||
for epoch in range(epochs): | ||
|
||
for batch_idx, (data, target) in enumerate(train_loader): | ||
data = data.view(-1, 28*28) | ||
|
||
logits = net(data) | ||
loss = criteon(logits, target) | ||
|
||
optimizer.zero_grad() | ||
loss.backward() | ||
# print(w1.grad.norm(), w2.grad.norm()) | ||
optimizer.step() | ||
|
||
if batch_idx % 100 == 0: | ||
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( | ||
epoch, batch_idx * len(data), len(train_loader.dataset), | ||
100. * batch_idx / len(train_loader), loss.item())) | ||
|
||
|
||
test_loss = 0 | ||
correct = 0 | ||
for data, target in test_loader: | ||
data = data.view(-1, 28 * 28) | ||
logits = net(data) | ||
test_loss += criteon(logits, target).item() | ||
|
||
pred = logits.data.max(1)[1] | ||
correct += pred.eq(target.data).sum() | ||
|
||
test_loss /= len(test_loader.dataset) | ||
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( | ||
test_loss, correct, len(test_loader.dataset), | ||
100. * correct / len(test_loader.dataset))) |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
import torch.optim as optim | ||
from torchvision import datasets, transforms | ||
|
||
|
||
batch_size=200 | ||
learning_rate=0.01 | ||
epochs=10 | ||
|
||
train_loader = torch.utils.data.DataLoader( | ||
datasets.MNIST('../data', train=True, download=True, | ||
transform=transforms.Compose([ | ||
transforms.ToTensor(), | ||
transforms.Normalize((0.1307,), (0.3081,)) | ||
])), | ||
batch_size=batch_size, shuffle=True) | ||
test_loader = torch.utils.data.DataLoader( | ||
datasets.MNIST('../data', train=False, transform=transforms.Compose([ | ||
transforms.ToTensor(), | ||
transforms.Normalize((0.1307,), (0.3081,)) | ||
])), | ||
batch_size=batch_size, shuffle=True) | ||
|
||
|
||
|
||
|
||
|
||
|
||
class MLP(nn.Module): | ||
|
||
def __init__(self): | ||
super(MLP, self).__init__() | ||
|
||
self.model = nn.Sequential( | ||
nn.Linear(784, 200), | ||
nn.LeakyReLU(inplace=True), | ||
nn.Linear(200, 200), | ||
nn.LeakyReLU(inplace=True), | ||
nn.Linear(200, 10), | ||
nn.LeakyReLU(inplace=True), | ||
) | ||
|
||
def forward(self, x): | ||
x = self.model(x) | ||
|
||
return x | ||
|
||
device = torch.device('cuda:0') | ||
net = MLP().to(device) | ||
optimizer = optim.SGD(net.parameters(), lr=learning_rate) | ||
criteon = nn.CrossEntropyLoss().to(device) | ||
|
||
for epoch in range(epochs): | ||
|
||
for batch_idx, (data, target) in enumerate(train_loader): | ||
data = data.view(-1, 28*28) | ||
data, target = data.to(device), target.cuda() | ||
|
||
logits = net(data) | ||
loss = criteon(logits, target) | ||
|
||
optimizer.zero_grad() | ||
loss.backward() | ||
# print(w1.grad.norm(), w2.grad.norm()) | ||
optimizer.step() | ||
|
||
if batch_idx % 100 == 0: | ||
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( | ||
epoch, batch_idx * len(data), len(train_loader.dataset), | ||
100. * batch_idx / len(train_loader), loss.item())) | ||
|
||
|
||
test_loss = 0 | ||
correct = 0 | ||
for data, target in test_loader: | ||
data = data.view(-1, 28 * 28) | ||
data, target = data.to(device), target.cuda() | ||
logits = net(data) | ||
test_loss += criteon(logits, target).item() | ||
|
||
pred = logits.data.max(1)[1] | ||
correct += pred.eq(target.data).sum() | ||
|
||
test_loss /= len(test_loader.dataset) | ||
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( | ||
test_loss, correct, len(test_loader.dataset), | ||
100. * correct / len(test_loader.dataset))) |
Binary file not shown.
Oops, something went wrong.