-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
46 lines (31 loc) · 1.24 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from torch import nn
from torchvision import models
import torch
class Resnet(nn.Module):
def __init__(self, device, output_dim):
super(Resnet, self).__init__()
self.device = device
self.model = self.create_model(output_dim)
def create_model(self, output_dim):
model = models.resnet18(pretrained=True)
model.fc = nn.Linear(512, output_dim)
return model
def train_model(self, loader, epochs,):
optim = torch.optim.Adam(self.model.parameters(), lr=3e-4)
criterion = torch.nn.CrossEntropyLoss()
self.model.to(self.device)
for epoch in range(epochs):
for idx, (input, target) in enumerate(loader):
input = input.to(self.device)
target = target.to(self.device)
print(input)
output = self.model(input)
print("calculated")
loss = criterion(output, target)
print("reached")
optim.zero_grad()
loss.backward()
optim.step()
if idx % 100 == 0:
print(f"Epoch {epoch}, Batch {idx}")
print(f"Loss: {loss.item()}")