def train(model, num_epochs):
# Define your execution device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("The model will be running on", device, "device")
# Convert model parameters and buffers to CPU or Cuda
model = model.to(device)
loss_fn = nn.CrossEntropyLoss()
from torch.optim import Adam
optimizer = Adam(model.parameters(), lr=0.0001, weight_decay=0.0001)
# best_accuracy = 0.0
for epoch in range(num_epochs):
running_loss = 0.0
for i, (images, labels) in enumerate(trainloader, 0):
images = images.to(device)
labels = labels.to(device)
print("labels : ", labels)
optimizer.zero_grad()
outputs = model(images)
print(type(outputs))
print(outputs)
print(outputs.data)
_, prediction = torch.max(outputs, 1)
print("prediction : ", prediction)
loss = loss_fn(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if divmod(epoch+1, 2)[1] == 0:
print("[%3d / %3d] loss : %.3f" % (epoch+1, num_epochs, running_loss/100))
running_loss = 0.0
# Result
'Programming > python-deep learning' 카테고리의 다른 글
[Tensorflow] 그림으로 이해하는 CNN 연산과정 (코드포함) (0) | 2023.09.21 |
---|---|
Pytorch로 Tensor Operation 다루는 법! (기본 기능 모음) (0) | 2023.09.01 |
Tensor란 무엇인가? (+ Python Numpy Tensor 구현 및 기본 기능) (1) | 2023.08.31 |
Pretrained model, checkpoints, Fine-Tuning, Transfer Learning 정리 (0) | 2023.07.05 |
torch.view (파이토치 뷰) 함수 설명 (0) | 2022.09.21 |