MCPcopy Create free account
hub / github.com/cure-lab/LTSF-Linear / EncoderLayer

Class EncoderLayer

layers/Transformer_EncDec.py:27–50  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

25
26
27class EncoderLayer(nn.Module):
28 def __init__(self, attention, d_model, d_ff=None, dropout=0.1, activation="relu"):
29 super(EncoderLayer, self).__init__()
30 d_ff = d_ff or 4 * d_model
31 self.attention = attention
32 self.conv1 = nn.Conv1d(in_channels=d_model, out_channels=d_ff, kernel_size=1)
33 self.conv2 = nn.Conv1d(in_channels=d_ff, out_channels=d_model, kernel_size=1)
34 self.norm1 = nn.LayerNorm(d_model)
35 self.norm2 = nn.LayerNorm(d_model)
36 self.dropout = nn.Dropout(dropout)
37 self.activation = F.relu if activation == "relu" else F.gelu
38
39 def forward(self, x, attn_mask=None):
40 new_x, attn = self.attention(
41 x, x, x,
42 attn_mask=attn_mask
43 )
44 x = x + self.dropout(new_x)
45
46 y = x = self.norm1(x)
47 y = self.dropout(self.activation(self.conv1(y.transpose(-1, 1))))
48 y = self.dropout(self.conv2(y).transpose(-1, 1))
49
50 return self.norm2(x + y), attn
51
52
53class Encoder(nn.Module):

Callers 2

__init__Method · 0.90
__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected