Notice
Recent Posts
Recent Comments
Link
거의 알고리즘 일기장
make NN 본문
In [3]:
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torch.utils.data import Dataset, DataLoader
import numpy as np
import torchvision
import torchvision.transforms as transforms
In [4]:
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5))])
In [5]:
trainset = torchvision.datasets.CIFAR10(root = './data',
train = True,
download = True,
transform = transform)
testset = torchvision.datasets.CIFAR10(root = './data',
train = False,
download = True,
transform = transform)
In [6]:
trainloader = DataLoader(trainset, batch_size=8, shuffle=True, num_workers=2)
testloader = DataLoader(testset, batch_size = 8, shuffle=False, num_workers=2)
In [7]:
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
In [8]:
def imshow(img):
img = img/2 +0.5 # unnormalize
np_img = img.numpy()
#ToTensor의 h x w x c 순에서
#c x h x w 순으로 변경
plt.imshow(np.transpose(np_img, (1,2,0)))
print(np_img.shape)
print((np.transpose(np_img, (1,2,0))).shape)
In [9]:
imgs = 0
for n, (img, labels) in enumerate(trainloader):
print(n, img.shape, labels.shape)
imgs = img
break
In [10]:
net = nn.Conv2d(3, 5, 5)
In [11]:
out1 = net(Variable(imgs))
print(out1.shape)
In [12]:
net2 = nn.Conv2d(5, 10, 5)
In [13]:
out2 = net2(out1)
print(out2.shape)
In [15]:
class my_network(nn.Module):
def __init__(self):
super(my_network, self).__init__()
self.net_1 = nn.Conv2d(3, 5, 5)
self.net_2 = nn.Conv2d(5, 10, 5)
def forward(self, x):
x = self.net_1(x)
x = self.net_2(x)
return x
In [16]:
imgs = 0
for n, (img, labels) in enumerate(trainloader):
print(n, img.shape, labels.shape)
imgs = img
break
In [17]:
my_net = my_network()
In [18]:
out = my_net(Variable(imgs))
print(out.shape)
In [ ]:
반응형
'pytorch 사용법' 카테고리의 다른 글
visdom 사용법 (0) | 2020.09.22 |
---|---|
network를 Sequential을 이용해서 간단하게 만들기 & 모델 저장,불러오기 (0) | 2020.09.22 |
custom dataset사용방법 _ image의 경우 (0) | 2020.09.22 |
optim & criterion (0) | 2020.09.21 |
Data Loader 사용법 (0) | 2020.09.21 |
Comments