-
Notifications
You must be signed in to change notification settings - Fork 0
/
learner.py
96 lines (80 loc) · 2.63 KB
/
learner.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2021 Théo Morales <theo.morales.fr@gmail.com>
#
# Distributed under terms of the MIT license.
"""
Dummy learner examples. Hopefully drop in your own!
"""
import torch.nn.functional as F
import torch.nn as nn
import torch
class DummiePolyLearner(nn.Module):
"""For the sin wave 3rd deg polynomial regression"""
def __init__(self):
super().__init__()
self.net = nn.Linear(3, 1)
def forward(self, x):
# Prepare the input tensor (x, x^2, x^3).
p = torch.tensor([1, 2, 3])
xx = x.pow(p)
return self.net(xx)
class MLP(nn.Module):
def __init__(self, device):
super().__init__()
self.net = nn.Sequential(
nn.Linear(1, 40),
nn.ReLU(),
nn.Linear(40, 40),
nn.ReLU(),
nn.Linear(40, 1)).to(device)
def forward(self, x):
return self.net(x)
class ConvNetClassifier(nn.Module):
def __init__(self, device, input_channels: int, n_classes: int):
super().__init__()
self.cnn = nn.Sequential(
nn.Conv2d(input_channels, 64, 3),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 64, 3),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 64, 3),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 64, 3),
nn.BatchNorm2d(64),
nn.ReLU())
self.flc = nn.Sequential(
# nn.Flatten(start_dim=1),
nn.Linear(64*20*20, n_classes)).to(device)
# nn.Softmax()).to(device) # No softmax because we use Cross Entropy loss
def forward(self, x):
x = self.cnn(x)
x = x.view(x.size(0), -1)
x = self.flc(x)
return x
class MLPClassifier(nn.Module):
def __init__(self, device, input_shape: tuple, n_classes: int):
super().__init__()
self.net = nn.Sequential(
nn.Linear(reduce(operator.mul, input_shape, 1), 256),
nn.BatchNorm1d(256),
nn.ReLU(),
nn.Linear(256, 128),
nn.BatchNorm1d(128),
nn.ReLU(),
nn.Linear(128, 64),
nn.BatchNorm1d(64),
nn.ReLU(),
nn.Linear(64, 64),
nn.BatchNorm1d(64),
nn.ReLU(),
nn.Flatten(),
nn.Linear(64, n_classes),
nn.Softmax()).to(device)
def forward(self, x):
return self.net(x)