* 강사님의 자료는 저작권이 있기 때문에 배운 내용을 최대한 간단하게 정리하는 수준으로 작성하였습니다.
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, Subset
from torhvision import datasets, transforms
from sklearn.model_selection import train_test_split
import numpy as np
# -- train, val split --
validation_rate =
train_indexes, val_indexes, _, _ = train_test_split(
# x indexes,
# y (targets),
stratify = ,
test_size =
)
# -- train_set, val_set --
train_set = Subset( , train_indexes)
val_set = Subset(, val_indexes)
# -- minibatch --
batch_size =
train_batches = DataLoader(train_set, batch_size = batch_size, shuffle = True)
val_batches = DataLoader(val_set, batch_size = batch_size = shuffle = True)
test_batches = DataLoader(test_set, batch_size = batch_size, shuffle = False)
# -- example model --
class ExampleModel(nn.Module):
def __init__(self, input_dim, output_dim):
super().__init__()
self.linear_layers = nn.Sequential(
...
)
def forward(self, x):
y = self.linear_layers(x)
return y
minibatch_size =
input_dim =
output_dim =
model = ExampleModel(input_dim, output_dim)
loss_func = nn.NLLLoss()
optimizer = torch.optim.Adam(model.parameters())
# -- training model --
from copy import deepcopy
def train(model, early_stop, n_epochs, progress_interval):
train_losses, valid_losses, lowest_loss = [], [], np.inf
for epoch in range(n_epochs):
...
return ...
nb_epochs =
progress_interval =
early_stop =
model, lowest_lowss, train_losses, valid_losses = train_model(...)
# -- evaluation with test dataset --
test_loss = 0
correct = 0
model.eval()
...
* written on July 4th