-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_house.py
144 lines (123 loc) · 4.56 KB
/
model_house.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import torch
from torch import Tensor, nn
from torch.nn.utils.rnn import PackedSequence, unpack_sequence
class Perceptron(nn.Module):
def __init__(self, input_dim: int, num_classes: int = 41) -> None:
super(Perceptron, self).__init__()
self.fc = nn.Sequential(nn.Flatten(), nn.Linear(input_dim, num_classes))
def forward(self, X: Tensor) -> Tensor:
return self.fc(X)
class MLP(nn.Module):
def __init__(self, input_dim: int, num_classes: int = 41) -> None:
super(MLP, self).__init__()
self.fc = nn.Sequential(
nn.Flatten(),
nn.Linear(input_dim, 1024),
nn.BatchNorm1d(1024),
nn.ReLU(),
nn.Dropout(),
nn.Linear(1024, 768),
nn.BatchNorm1d(768),
nn.ReLU(),
nn.Dropout(),
nn.Linear(768, 512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Dropout(),
nn.Linear(512, 256),
nn.BatchNorm1d(256),
nn.ReLU(),
nn.Dropout(),
nn.Linear(256, 128),
nn.BatchNorm1d(128),
nn.ReLU(),
nn.Dropout(),
nn.Linear(128, 64),
nn.BatchNorm1d(64),
nn.ReLU(),
nn.Dropout(),
nn.Linear(64, num_classes),
)
def forward(self, X: Tensor) -> Tensor:
return self.fc(X)
class ResBlock(nn.Module):
def __init__(self, hidden_dim: int, layers: int, use_bn: bool = False, dropout: float = 0.5) -> None:
super().__init__()
buff = []
for _ in range(layers):
buff.append(nn.Linear(hidden_dim, hidden_dim))
if use_bn:
buff.append(nn.BatchNorm1d(hidden_dim))
buff.extend([nn.ReLU(), nn.Dropout(dropout)])
self._fc = nn.Sequential(*buff)
def forward(self, X: Tensor) -> Tensor:
Y = self._fc(X)
return Y + X
class ResMLP(nn.Module):
def __init__(self, input_dim: int, num_classes: int = 41) -> None:
super().__init__()
self._net = nn.Sequential(
nn.Flatten(),
nn.Linear(input_dim, 512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Dropout(),
ResBlock(512, 2, use_bn=True, dropout=0.5),
ResBlock(512, 2, use_bn=True, dropout=0.5),
nn.Linear(512, num_classes),
)
def forward(self, X: Tensor) -> Tensor:
return self._net(X)
class SimpleRNN(nn.Module):
def __init__(self, embed_size: int, hidden_size: int, num_layers: int = 1, num_classes: int = 41) -> None:
super().__init__()
self.rnn = nn.RNN(
input_size=embed_size, hidden_size=hidden_size, num_layers=num_layers, bidirectional=True, batch_first=True
)
self.fc = nn.Sequential(
nn.Linear(hidden_size * 2, hidden_size * 4),
nn.ReLU(),
nn.Dropout(),
nn.Linear(hidden_size * 4, num_classes),
)
def forward(self, X: PackedSequence) -> Tensor:
outputs, _ = self.rnn(X)
outputs = torch.cat(unpack_sequence(outputs), dim=0)
return self.fc(outputs)
class LSTM(nn.Module):
def __init__(self, embed_size: int, hidden_size: int, num_layers: int = 1, num_classes: int = 41) -> None:
super().__init__()
self.rnn = nn.LSTM(
input_size=embed_size, hidden_size=hidden_size, num_layers=num_layers, bidirectional=True, batch_first=True
)
self.fc = nn.Sequential(
nn.Linear(hidden_size * 2, hidden_size * 4),
nn.ReLU(),
nn.Dropout(),
nn.Linear(hidden_size * 4, num_classes),
)
def forward(self, X: PackedSequence) -> Tensor:
outputs, _ = self.rnn(X)
outputs = torch.cat(unpack_sequence(outputs), dim=0)
return self.fc(outputs)
class GRU(nn.Module):
def __init__(self, embed_size: int, hidden_size: int, num_layers: int = 1, num_classes: int = 41) -> None:
super().__init__()
self.rnn = nn.GRU(
input_size=embed_size,
hidden_size=hidden_size,
num_layers=num_layers,
dropout=0.5,
bidirectional=True,
batch_first=True,
)
self.fc = nn.Sequential(
nn.Linear(hidden_size * 2, hidden_size * 4),
nn.ReLU(),
nn.Dropout(),
nn.Linear(hidden_size * 4, num_classes),
)
def forward(self, X: PackedSequence) -> Tensor:
outputs, _ = self.rnn(X)
outputs = torch.cat(unpack_sequence(outputs), dim=0)
return self.fc(outputs)