이어서 작성합니다
Training Our First Neural Network with PyTorch


Loss Function



import torch
import torch.nn.Functional as F
y = 1
num_classes = 3
#numpy에서는 수동으로 해야하지만,
# Create the one-hot encoded vector using NumPy
one_hot_numpy = np.array([0, 1, 0])
# torch에서는 F 가 자동으로 수행해줌
# Create the one-hot encoded vector using PyTorch
one_hot_pytorch = F.one_hot(torch.tensor(y), num_classes)
Example
import torch
import torch.nn as nn
import torch.nn.functional as F
y = [2]
scores = torch.tensor([[0.1, 6.0, -2.0, 3.2]])
# Create a one-hot encoded vector of the label y
one_hot_label = F.one_hot(torch.tensor(y), scores.shape[1])
# Create the cross entropy loss function
criterion = nn.CrossEntropyLoss()
# Calculate the cross entropy loss
loss = criterion(scores.double(), one_hot_label.double())
print(loss)






cross-entropy는 분류 문제에만 사용됨
Sigmoid, Softmax역시 분류 문제에만 사용됨


Dataloader 사용 : 정/역방향 전달에서 모델을 통해 전달되는 데이터의 "배치"를 생성할 수 있음

