-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmodel.py
50 lines (37 loc) · 1.78 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import torch
from torch import nn
from torch.nn import functional as F
from core.model import BaseModel
'''
The LogisticRegression model is taken from FedML repository. For more information regarding this model,
please refer to https://github.com/FedML-AI/FedML/blob/master/python/fedml/model/linear/lr.py.
'''
class LogisticRegression(torch.nn.Module):
def __init__(self, input_dim, output_dim):
super(LogisticRegression, self).__init__()
self.linear = torch.nn.Linear(input_dim, output_dim)
def forward(self, x):
o = self.linear(x.view(-1,28*28))
outputs = torch.sigmoid(o)
#outputs = torch.sigmoid(self.linear(x))
return outputs
class LR(BaseModel):
'''This is a PyTorch model with some extra methods'''
def __init__(self, model_config):
super().__init__()
self.net = LogisticRegression(model_config['input_dim'], model_config['output_dim'])
def loss(self, input: torch.Tensor) -> torch.Tensor:
'''Performs forward step and computes the loss'''
device = 'cuda' if torch.cuda.is_available() else 'cpu'
features, labels = input['x'].to(device), input['y'].to(device)
output = self.net.forward(features)
criterion = nn.CrossEntropyLoss().to(device)
return criterion(output, labels.long())
def inference(self, input):
'''Performs forward step and computes metrics'''
device = 'cuda' if torch.cuda.is_available() else 'cpu'
features, labels = input['x'].to(device), input['y'].to(device)
output = self.net.forward(features)
n_samples = features.shape[0]
accuracy = torch.mean((torch.argmax(output, dim=1) == labels).float()).item()
return {'output':output, 'acc': accuracy, 'batch_size': n_samples}