MCPcopy Create free account
hub / github.com/GGA23/GrokFormer / SineEncoding

Class SineEncoding

model.py:12–32  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10
11
12class SineEncoding(nn.Module):
13 def __init__(self, k, hidden_dim=128):
14 super(SineEncoding, self).__init__()
15 self.constant = 100
16 self.hidden_dim = hidden_dim
17 self.eig_ws = nn.ModuleList([nn.Linear(hidden_dim + 1, 1) for i in range(k)])
18 self.k = k
19
20 def forward(self, e):
21 # input: [N]
22 # output: [N, k]
23 out_e = []
24 ee = e.unsqueeze(1)
25 for i in range(self.k):
26 eeig = torch.full(ee.shape, torch.tensor(1.0)).to(e.device)
27 ei = ee.pow(i + 1)
28 div = torch.FloatTensor(np.arange(1, int(self.hidden_dim / 2) + 1)).to(e.device)
29 pe = ei * div
30 eeig = torch.cat((eeig, torch.sin(pe), torch.cos(pe)), dim=1)
31 out_e.append(self.eig_ws[i](eeig))
32 return out_e
33
34
35class FeedForwardNetwork(nn.Module):

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected