In [3]:
import torch
In [4]:
x = torch.Tensor(5, 3)
x
Out[4]:
In [5]:
x = torch.rand(5, 3)
x
Out[5]:
In [6]:
x.size()
Out[6]:
In [7]:
y = torch.rand(5, 3)
x + y
Out[7]:
In [8]:
torch.add(x, y)
Out[8]:
In [9]:
result = torch.Tensor(5, 3)
torch.add(x, y, out=result)
result
Out[9]:
In [10]:
print(y)
y.add_(x)
print(y)
In [11]:
x
Out[11]:
In [12]:
x[:, 1]
Out[12]:
In [13]:
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)
print(x.size())
print(y.size())
print(z.size())
In [14]:
a = torch.ones(5)
a
Out[14]:
In [15]:
b = a.numpy()
b
Out[15]:
In [16]:
a.add_(1)
print(a)
print(b)
In [17]:
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
In [18]:
torch.cuda.is_available()
Out[18]:
In [19]:
import torch
from torch.autograd import Variable
In [20]:
x = Variable(torch.ones(2, 2), requires_grad=True)
print(x)
In [21]:
y = x + 2
print(y)
In [22]:
y.grad_fn
Out[22]:
In [23]:
x.grad_fn
In [24]:
z = y * y * 3
out = z.mean()
print(z, out)
In [25]:
out.backward()
In [26]:
print(x.grad)
In [27]:
x = torch.randn(3)
x = Variable(x, requires_grad=True)
y = x * 2
while y.data.norm() < 1000:
y = y * 2
print(y)
In [28]:
gradients = torch.FloatTensor([0.1, 1.0, 0.0001])
y.backward(gradients)
print(x.grad)
In [29]:
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
In [30]:
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5)
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 = 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:]
num_features = 1
for s in size:
num_features *= s
return num_features
In [31]:
net = Net()
print(net)
In [32]:
params = list(net.parameters())
print(len(params))
In [33]:
print(params[0].size())
In [34]:
input = Variable(torch.randn(1, 1, 32, 32))
out = net(input)
print(out)
In [35]:
net.zero_grad()
out.backward(torch.randn(1, 10))
In [44]:
output = net(input)
target = Variable(torch.arange(1, 11))
print(output.size(), target.size())
criterion = nn.MSELoss()
loss = criterion(output, target)
print(loss)
In [45]:
print(loss.grad_fn)
print(loss.grad_fn.next_functions[0][0])
print(loss.grad_fn.next_functions[0][0].next_functions[0][0])
In [47]:
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 [51]:
learning_rate = 0.01
for f in net.parameters():
f.data.sub_(f.grad.data * learning_rate)
In [52]:
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()
In [4]:
import torch
import torchvision
import torchvision.transforms as transforms
In [5]:
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) # [0, 1] => [-1, 1]
])
In [6]:
transform
Out[6]:
In [7]:
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 [8]:
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 [9]:
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
In [10]:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
def imshow(img):
img = img / 2 + 0.5 # unnormalize [-1, 1] => [0, 1]
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0))) # [c, h, w] => [h, w, c]
images, labels = iter(trainloader).next()
imshow(torchvision.utils.make_grid(images))
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))
In [11]:
images.size()
Out[11]:
In [12]:
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)
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)
return x
net = Net()
In [13]:
net
Out[13]:
In [14]:
import torch.optim as optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
In [15]:
for epoch in range(2):
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
inputs, labels = data
inputs, labels = Variable(inputs), Variable(labels)
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# 2000 iterations (mini-batch) ごとにログ出力
running_loss += loss.data[0]
if i % 2000 == 1999:
print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('Finished training')
In [16]:
images, labels = iter(testloader).next()
imshow(torchvision.utils.make_grid(images))
In [17]:
print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4)))
In [18]:
outputs = net(Variable(images))
outputs.size()
Out[18]:
In [19]:
_, predicted = torch.max(outputs.data, 1)
In [20]:
predicted.size()
Out[20]:
In [21]:
print('Predicted: ', ' '.join('%5s' % classes[predicted[j]] for j in range(4)))
In [22]:
correct = 0
total = 0
for data in testloader:
images, labels = data
outputs = net(Variable(images))
# 推論時はlabelsはVariableで囲まない
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
# print(type(predicted), type(labels))
correct += (predicted == labels).sum()
print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct / total))
In [30]:
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() # 正解だと1、誤りだと0が入っている
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]))