Machine Learning library
Framework: Backend engines + library(module, function, …)
library
Other services
Colaboratory
Fast ai
AWS public dataset program
Tensorflow
Static Computational Graph
Tensorflow Fold: Dynamic Computational Graph
sess.run(hypothesis, feed_dict={X: [5]})
sess = tf.Session()
Pytorch
Dynamic Computational Graph
Network structure
Conv 2d
image
pooling
Conv 2d
linear
linear
linear
output
Shape: (32, 32, 3)
In_ch: 3, out_ch: 6, kernel: (5, 5), relu
Shape: (28, 28, 6)
kernel: (2, 2)
Shape: (14, 14, 6)
In_ch: 6, out_ch: 16, kernel: (5, 5), relu
Shape: (10, 10, 16)
pooling kernel: (2, 2)
Shape: (5, 5, 16)
Shape: (120)
In_feat: 300, out_feat: 120, relu
In_feat: 120, out_feat: 84, relu
In_feat: 84, out_feat: 10, softmax
flatten
Shape: (400)
Shape: (84)
Shape: (10)
Import module
import torch
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
Create neural network structure
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
Download and import dataset
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
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)
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)
classes = ('plane', 'car', 'bird', 'cat','deer', 'dog', 'frog',
'horse', 'ship', 'truck')
training
net = Net()
# CrossEntropyLoss=logSoftmax+NLLLoss
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
for epoch in range(2):
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
inputs, labels = data
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if i % 2000 == 1999:
print('[%d, %5d] loss: %.3f' %(epoch + 1, i + 1,
running_loss / 2000))
running_loss = 0.0
Confirm network structure
print("network specification")
print(net)
params = list(net.parameters())
print("nnumber of network parmeters")
print(len(params))
print("ntensor shape of each network parameters")
for param in params:
print(param.size())
Parameter shape
Shape of output of each layers
print(x.size())
Save and load
torch.save(net.state_dict(),"./model_param")
Save model
Load model
net = Net()
net.load_state_dict(torch.load("./model_param"))
net.eval()
Python code에 한글 쓸 경우
# -*- coding: utf-8 -*-
Test
correct = 0
total = 0
with torch.no_grad():
for data in testloader:
images, labels = data
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the network on the 10000 test
images: %d %%' % (
100 * correct / total))
Test
Training on GPU
device = torch.device("cuda:0" if torch.cuda.is_available() else
"cpu")
Get device
Network upload to device
net.to(device)
Data upload to device
inputs, labels = inputs.to(device), labels.to(device)
Show the image and prediction
def imshow(img):
img = img / 2 + 0.5
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
Image show function
Get image and show
dataiter = iter(testloader)
images, labels = dataiter.next()
imshow(torchvision.utils.make_grid(images))
plt.show()
Show the image and prediction
Get prediction result
outputs = net(images)
value, predicted = torch.max(outputs, 1)
print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]]
for j in range(4)))
print('Predicted: ', ' '.join('%5s' % classes[predicted[j]]
for j in range(4)))
print('value: ', ' '.join('%3.3f' % value[j]
for j in range(4)))
Schedule
다음 시간: 각자 논문 읽고 발표하기.
Deep learning study 3

Deep learning study 3

  • 2.
    Machine Learning library Framework:Backend engines + library(module, function, …) library
  • 3.
  • 4.
    Tensorflow Static Computational Graph TensorflowFold: Dynamic Computational Graph sess.run(hypothesis, feed_dict={X: [5]}) sess = tf.Session()
  • 5.
  • 6.
    Network structure Conv 2d image pooling Conv2d linear linear linear output Shape: (32, 32, 3) In_ch: 3, out_ch: 6, kernel: (5, 5), relu Shape: (28, 28, 6) kernel: (2, 2) Shape: (14, 14, 6) In_ch: 6, out_ch: 16, kernel: (5, 5), relu Shape: (10, 10, 16) pooling kernel: (2, 2) Shape: (5, 5, 16) Shape: (120) In_feat: 300, out_feat: 120, relu In_feat: 120, out_feat: 84, relu In_feat: 84, out_feat: 10, softmax flatten Shape: (400) Shape: (84) Shape: (10)
  • 7.
    Import module import torch importtorchvision import torchvision.transforms as transforms import matplotlib.pyplot as plt import numpy as np import torch.nn as nn import torch.nn.functional as F import torch.optim as optim
  • 8.
    Create neural networkstructure 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
  • 9.
    Download and importdataset transform = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) 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) 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) classes = ('plane', 'car', 'bird', 'cat','deer', 'dog', 'frog', 'horse', 'ship', 'truck')
  • 10.
    training net = Net() #CrossEntropyLoss=logSoftmax+NLLLoss criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) for epoch in range(2): running_loss = 0.0 for i, data in enumerate(trainloader, 0): inputs, labels = data optimizer.zero_grad() outputs = net(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() running_loss += loss.item() if i % 2000 == 1999: print('[%d, %5d] loss: %.3f' %(epoch + 1, i + 1, running_loss / 2000)) running_loss = 0.0
  • 11.
    Confirm network structure print("networkspecification") print(net) params = list(net.parameters()) print("nnumber of network parmeters") print(len(params)) print("ntensor shape of each network parameters") for param in params: print(param.size()) Parameter shape Shape of output of each layers print(x.size())
  • 12.
    Save and load torch.save(net.state_dict(),"./model_param") Savemodel Load model net = Net() net.load_state_dict(torch.load("./model_param")) net.eval() Python code에 한글 쓸 경우 # -*- coding: utf-8 -*-
  • 13.
    Test correct = 0 total= 0 with torch.no_grad(): for data in testloader: images, labels = data outputs = net(images) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() print('Accuracy of the network on the 10000 test images: %d %%' % ( 100 * correct / total)) Test
  • 14.
    Training on GPU device= torch.device("cuda:0" if torch.cuda.is_available() else "cpu") Get device Network upload to device net.to(device) Data upload to device inputs, labels = inputs.to(device), labels.to(device)
  • 15.
    Show the imageand prediction def imshow(img): img = img / 2 + 0.5 npimg = img.numpy() plt.imshow(np.transpose(npimg, (1, 2, 0))) Image show function Get image and show dataiter = iter(testloader) images, labels = dataiter.next() imshow(torchvision.utils.make_grid(images)) plt.show()
  • 16.
    Show the imageand prediction Get prediction result outputs = net(images) value, predicted = torch.max(outputs, 1) print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4))) print('Predicted: ', ' '.join('%5s' % classes[predicted[j]] for j in range(4))) print('value: ', ' '.join('%3.3f' % value[j] for j in range(4)))
  • 17.
    Schedule 다음 시간: 각자논문 읽고 발표하기.