-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone.py
57 lines (47 loc) · 2.34 KB
/
backbone.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
from torchmeta.modules import MetaModule, MetaSequential, MetaConv2d, MetaBatchNorm2d, MetaLinear
import torch.nn as nn
from collections import OrderedDict
def conv3x3(in_channels, out_channels, **kwargs):
return MetaSequential(
MetaConv2d(in_channels, out_channels, kernel_size=3, padding=1, **kwargs),
MetaBatchNorm2d(out_channels, momentum=1., track_running_stats=False),
nn.ReLU(),
nn.MaxPool2d(2)
)
class ConvolutionalNeuralNetwork(MetaModule):
def __init__(self, in_channels, out_features, hidden_size=64):
super(ConvolutionalNeuralNetwork, self).__init__()
self.in_channels = in_channels
self.out_features = out_features
self.hidden_size = hidden_size
self.features = MetaSequential(
conv3x3(in_channels, hidden_size),
conv3x3(hidden_size, hidden_size),
conv3x3(hidden_size, hidden_size),
conv3x3(hidden_size, hidden_size)
)
self.classifier = MetaLinear(1600 * (hidden_size//64), out_features)
def forward(self, inputs, params=None):
features = self.features(inputs, params=self.get_subdict(params, 'features'))
features = features.view((features.size(0), -1))
logits = self.classifier(features, params=self.get_subdict(params, 'classifier'))
return logits
class MetaMLPModel(MetaModule):
def __init__(self, in_features, out_features, hidden_sizes):
super(MetaMLPModel, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.hidden_sizes = hidden_sizes
layer_sizes = [in_features] + hidden_sizes
self.features = MetaSequential(OrderedDict([('layer{0}'.format(i + 1),
MetaSequential(OrderedDict([
('linear', MetaLinear(hidden_size, layer_sizes[i + 1], bias=True)),
('relu', nn.ReLU())
]))) for (i, hidden_size) in enumerate(layer_sizes[:-1])]))
self.classifier = MetaLinear(hidden_sizes[-1], out_features, bias=True)
def forward(self, inputs, params=None):
features = self.features(inputs, params=self.get_subdict(params, 'features'))
logits = self.classifier(features, params=self.get_subdict(params, 'classifier'))
return logits
def ModelMLPSinusoid(hidden_sizes=[40, 40]):
return MetaMLPModel(1, 1, hidden_sizes)