-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.py
36 lines (28 loc) · 1.03 KB
/
model.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
import os
import time
from typing import Tuple
import torch
import torch.nn.functional
from torch import nn, Tensor
class Model(nn.Module):
def __init__(self):
super().__init__()
# TODO: CODE BEGIN
raise NotImplementedError
# TODO: CODE END
def forward(self, images: Tensor) -> Tuple[Tensor, Tensor]:
# TODO: CODE BEGIN
raise NotImplementedError
# TODO: CODE END
def loss(self, logits: Tensor, labels: Tensor) -> Tensor:
# TODO: CODE BEGIN
raise NotImplementedError
# TODO: CODE END
def save(self, path_to_checkpoints_dir: str, step: int) -> str:
path_to_checkpoint = os.path.join(path_to_checkpoints_dir,
'model-{:s}-{:d}.pth'.format(time.strftime('%Y%m%d%H%M'), step))
torch.save(self.state_dict(), path_to_checkpoint)
return path_to_checkpoint
def load(self, path_to_checkpoint: str) -> 'Model':
self.load_state_dict(torch.load(path_to_checkpoint))
return self