In [25]:
%matplotlib inline
import torch
import numpy as np
In [26]:
x = torch.Tensor(5, 3)
print(x)
In [27]:
x = torch.rand(5, 3)
print(x)
In [28]:
print(x.size())
In [29]:
y = torch.rand(5, 3)
In [30]:
print(x + y)
In [31]:
print(torch.add(x, y))
In [32]:
result = torch.Tensor(5, 3)
torch.add(x, y, out=result)
print(result)
In [33]:
y.add_(x)
print(y)
In [34]:
print(x[:, 1])
In [35]:
a = torch.ones(5)
print(a)
In [36]:
b = a.numpy()
print(b)
In [37]:
a.add_(1)
print(a)
print(b)
In [38]:
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
In [39]:
torch.cuda.is_available()
Out[39]:
In [40]:
import torch
from torch.autograd import Variable
In [41]:
x = Variable(torch.ones(2, 2), requires_grad=True)
In [42]:
x
Out[42]:
In [43]:
y = x + 2
print(y)
In [44]:
y.creator
Out[44]:
In [45]:
z = y * y * 3
out = z.mean()
In [46]:
out.backward()
In [47]:
x.grad
Out[47]:
In [48]:
x = torch.randn(3)
x = Variable(x, requires_grad=True)
y = x * 2
while y.data.norm() < 1000:
y = y * 2
print(y)
In [49]:
gradients = torch.FloatTensor([0.1, 1.0, 0.0001])
y.backward(gradients)
print(x.grad)
In [60]:
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5) # 1 input channel, 6 output channels, 5x5 kernel
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
print("***", type(x))
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def num_flat_features(self, x):
size = x.size()[1:] # batch以外の次元を取得
num_features = 1
for s in size:
num_features *= s
return num_features
net = Net()
print(net)
In [61]:
# view()はnumpyのreshape()と同じ
import torch
a = torch.ones((5, 5))
print(a.size())
a = a.view(-1, 25)
print(a.size())
In [62]:
params = list(net.parameters())
print(len(params))
print(params[0].size()) # conv1のW
In [63]:
input = Variable(torch.randn(1, 1, 32, 32))
out = net(input)
print(out)
In [54]:
net.zero_grad()
out.backward(torch.randn(1, 10))
In [55]:
output = net(input)
target = Variable(torch.arange(1, 11))
criterion = nn.MSELoss()
In [56]:
loss = criterion(output, target)
print(loss)
In [57]:
net.zero_grad()
print('conv1.bias.grad before backward')
print(net.conv1.bias.grad)
loss.backward()
print('conv1.bias.grad after backward')
print(net.conv1.bias.grad)
In [58]:
learning_rate = 0.01
for f in net.parameters():
f.data.sub_(f.grad.data * learning_rate)
In [59]:
import torch.optim as optim
optimizer = optim.SGD(net.parameters(), lr=0.01)
optimizer.zero_grad()
output = net(input)
loss = criterion(output, target)
loss.backward()
optimizer.step() # update paramters!
In [1]:
import torch
import torchvision
import torchvision.transforms as transforms
In [2]:
transform = transforms.Compose(
[transforms.ToTensor(),
# [0, 1] => [-1, 1]
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]
)
In [3]:
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2)
In [4]:
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False, num_workers=2)
In [5]:
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
In [6]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
In [7]:
def imshow(img):
img = img / 2 + 0.5
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
dataiter = iter(trainloader)
images, labels = dataiter.next()
print(images.size(), labels.size())
imshow(torchvision.utils.make_grid(images))
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))
In [8]:
# Define a CNN
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2) # パラメータがないレイヤもOK?
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 5 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x) # softmaxは含めない
return x
net = Net()
In [9]:
net
Out[9]:
In [10]:
import torch.optim as optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
In [11]:
for epoch in range(2):
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
inputs, labels = data # minibatch
# データはVariableで囲む
inputs, labels = Variable(inputs), Variable(labels)
# 各ミニバッチごとに蓄積した勾配はリセットする
# ミニバッチ単位でパラメータ更新するため
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.data[0]
if i % 2000 == 1999: # 2000 minibatchごとに表示
print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('Finished Training')
In [44]:
dataiter = iter(testloader)
images, labels = dataiter.next()
imshow(torchvision.utils.make_grid(images))
print('Ground Truth:', ' '.join('%5s' % classes[labels[j]] for j in range(4)))
In [45]:
outputs = net(Variable(images))
In [46]:
outputs
Out[46]:
In [47]:
_, predicted = torch.max(outputs.data, 1)
predicted[1].numpy()[0]
Out[47]:
In [48]:
print('Predicted: ', ' '.join('%5s' % classes[predicted[j].numpy()[0]] for j in range(4)))
In [50]:
correct = 0
total = 0
for data in testloader:
images, labels = data
outputs = net(Variable(images))
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum()
print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct / total))
In [51]:
class_correct = list(0. for i in range(10))
class_total = list(0. for i in range(10))
for data in testloader:
images, labels = data
outputs = net(Variable(images))
_, predicted = torch.max(outputs.data, 1)
c = (predicted == labels).squeeze()
for i in range(4):
label = labels[i]
class_correct[label] += c[i]
class_total[label] += 1
for i in range(10):
print('Accuracy of %5s : %2d %%' % (classes[i], 100 * class_correct[i] / class_total[i]))
In [ ]: