conda install pytorch torchvision cpuonly -c pytorch
수식
\begin{bmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
\end{bmatrix}
\[ \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ \end{bmatrix} \]
코드
import torch
# torch.ones((2,3), dtype=torch.int8)
# torch.rand((2,3))
1, 2 ,3], [4, 5, 6]]) torch.tensor([[
tensor([[1, 2, 3],
[4, 5, 6]])
수식
y = 2 \times x+3
\[ y = 2 \times x + 3 \] 미분 결과
\[ \frac{\partial y}{\partial x} = 2 \]
코드
= torch.tensor([[1., 2., 3.],
x 4., 5., 6.]], requires_grad=True)
[
= 2 * x + 3
y print(y)
tensor([[ 5., 7., 9.],
[11., 13., 15.]], grad_fn=<AddBackward0>)
수식
\begin{bmatrix}
1 \\
2 \\
3 \\
4
\end{bmatrix}
\to
\begin{bmatrix}
1 & 2 \\
3 & 4 \\
\end{bmatrix}
\[ \begin{bmatrix} 1 \\ 2 \\ 3 \\ 4 \end{bmatrix} \to \begin{bmatrix} 1 & 2 \\ 3 & 4 \\ \end{bmatrix} \]
코드
= torch.tensor([1., 2., 3., 4.])
a
2, 2))
torch.reshape(a, (# a.view((2, 2))
tensor([[1., 2.],
[3., 4.]])
패션 이미지
\(y\) 라벨
라벨 | 라벨 명칭 | 라벨 | 라벨 명칭 |
---|---|---|---|
0 | T-shirt/top | 5 | Sandal |
1 | Trouser | 6 | Shirt |
2 | Pullover | 7 | Sneaker |
3 | Dress | 8 | Bag |
4 | Coat | 9 | Ankle boot |
import torch
from torch import nn
from torchvision import datasets, transforms
import torchvision.models as models
= transforms.Compose([transforms.ToTensor(),
transform 0.5,), (0.5,)),
transforms.Normalize((
])= 64
batch_size
## 훈련 데이터 다운로드
= datasets.FashionMNIST('data/', download=False, train=True, transform=transform)
trainset = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=False)
trainloader
## 시험 데이터 다운로드
= datasets.FashionMNIST('data/', download=False, train=False, transform=transform)
testset = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=False) testloader
## 아키텍처 정의
class FashionNetwork(nn.Module):
def __init__(self):
super().__init__()
self.hidden1 = nn.Linear(784, 256)
self.hidden2 = nn.Linear(256, 128)
self.output = nn.Linear(128, 10)
self.log_softmax = nn.LogSoftmax()
self.activation = nn.ReLU()
self.drop = nn.Dropout(p=0.25)
def forward(self, x):
= self.hidden1(x)
x = self.activation(x)
x = self.drop(x)
x = self.hidden2(x)
x = self.activation(x)
x = self.drop(x)
x = self.output(x)
x = self.log_softmax(x)
output
return output
= FashionNetwork().to('cuda')
model
print(model)
FashionNetwork(
(hidden1): Linear(in_features=784, out_features=256, bias=True)
(hidden2): Linear(in_features=256, out_features=128, bias=True)
(output): Linear(in_features=128, out_features=10, bias=True)
(log_softmax): LogSoftmax(dim=None)
(activation): ReLU()
(drop): Dropout(p=0.25, inplace=False)
)
from torch import optim
# 오차함수와 최적화 관련 패러미터 설정
= nn.NLLLoss()
criterion = optim.Adam(model.parameters(), lr = 0.005)
optimizer
# 신경망 학습
= 10
epochs = []
losses
torch.cuda.is_available()
torch.cuda.current_device()0)
torch.cuda.get_device_name(0)
torch.cuda.device(
= torch.device(str("cuda:0") if torch.cuda.is_available() else "cpu")
device
= model.to('cuda:0')
model
for _ in range(epochs):
= 0
running_loss for image, label in trainloader:
optimizer.zero_grad()= image.view(image.shape[0],-1).to('cuda')
image = model(image) # 앞서 정의한 모형을 학습
pred = criterion(pred, label.to('cuda')) # 오차 계산
loss # 역전파
loss.backward() # 스텝
optimizer.step() += loss.item() # 오차값을 총 오차에 더함
running_loss else:
print(f'Training loss: {running_loss/len(trainloader):.4f}')
/len(trainloader))
losses.append(running_loss
# Training loss: 0.5576
# Training loss: 0.4172
# Training loss: 0.3854
# Training loss: 0.3652
# Training loss: 0.3495
# Training loss: 0.3365
# Training loss: 0.3280
# Training loss: 0.3180
# Training loss: 0.3094
# Training loss: 0.3015
## 딥러닝 모형 저장
"data/fashion.pth")
torch.save(model.state_dict(),
# 리스트 학습 손실값 저장
# import json
# with open("data/fashion_mnist_losses.json", "w") as fp:
# json.dump(losses, fp)
import torch
from torch import nn
from torchvision import datasets, transforms
import json
## 예측모형 불러오기
= FashionNetwork()
model 'data/fashion.pth'), strict=False) model.load_state_dict(torch.load(
_IncompatibleKeys(missing_keys=['hidden1.weight', 'hidden1.bias', 'hidden2.weight', 'hidden2.bias', 'output.weight', 'output.bias'], unexpected_keys=['fc1.weight', 'fc1.bias', 'fc2.weight', 'fc2.bias', 'fc3.weight', 'fc3.bias', 'fc4.weight', 'fc4.bias'])
eval()
model.
# 테스트 데이터에서 첫 이미지 추출
FashionNetwork(
(hidden1): Linear(in_features=784, out_features=256, bias=True)
(hidden2): Linear(in_features=256, out_features=128, bias=True)
(output): Linear(in_features=128, out_features=10, bias=True)
(log_softmax): LogSoftmax(dim=None)
(activation): ReLU()
(drop): Dropout(p=0.25, inplace=False)
)
= iter(testloader).next()
testimgs, testlabels = testimgs[0].view(1, 784)
img
with torch.no_grad():
= model(img) logps
<string>:20: UserWarning: Implicit dimension choice for log_softmax has been deprecated. Change the call to include dim=X as an argument.
= torch.exp(logps)
ps = list(ps.numpy()[0])
probabilities = probabilities.index(max(probabilities))
prediction # print(prediction)
def mnist_label(label):
= {
output_mapping 0: "T-shirt/Top",
1: "Trouser",
2: "Pullover",
3: "Dress",
4: "Coat",
5: "Sandal",
6: "Shirt",
7: "Sneaker",
8: "Bag",
9: "Ankle Boot"
}= (label.item() if type(label) == torch.Tensor else label)
label return output_mapping[label]
mnist_label(prediction)
'Shirt'
import os
'KMP_DUPLICATE_LIB_OK']='True'
os.environ[
import matplotlib.pyplot as plt
0][0].numpy().squeeze(), cmap='gray_r');
plt.imshow(testimgs[ plt.show()