-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodel.py
More file actions
34 lines (29 loc) · 1.29 KB
/
Copy pathmodel.py
File metadata and controls
34 lines (29 loc) · 1.29 KB
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
from torch import nn, Tensor
class LowpassRNN(nn.Module):
def __init__(self, hidden_size: int, num_layers: int, conditioned: bool = True):
super(LowpassRNN, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.conditioned = conditioned
input_size = 2 if conditioned else 1 # [sample, fc_norm] or just [sample]
self.gru = nn.GRU(
input_size=input_size,
hidden_size=hidden_size,
num_layers=num_layers,
batch_first=True, # expects (batch, seq_len, input_size)
)
self.fc = nn.Linear(hidden_size, 1) # one output sample per timestep
def forward(self, x: Tensor, hidden: Tensor | None = None):
"""
Args:
x: (batch_size, buffer_size, input_size) — one timestep per sample
hidden: (num_layers, batch_size, hidden_size) — carried across buffers
Returns:
output: (batch_size, buffer_size, 1)
hidden: (num_layers, batch_size, hidden_size) — to be passed to next buffer
"""
gru_out, hidden = self.gru(
x, hidden
) # gru_out: (batch, buffer_size, hidden_size)
output = self.fc(gru_out) # (batch, buffer_size, 1)
return output, hidden