MCPcopy Create free account
hub / github.com/SJTU-DENG-Lab/WLA / SinusoidalPositionalEncoding

Class SinusoidalPositionalEncoding

models/action_model/action_encoder.py:24–54  ·  view source on GitHub ↗

Produces a sinusoidal encoding of shape (B, T, w) given timesteps of shape (B, T).

Source from the content-addressed store, hash-verified

22
23
24class SinusoidalPositionalEncoding(nn.Module):
25 """
26 Produces a sinusoidal encoding of shape (B, T, w)
27 given timesteps of shape (B, T).
28 """
29
30 def __init__(self, embedding_dim):
31 super().__init__()
32 self.embedding_dim = embedding_dim
33
34 def forward(self, timesteps):
35 # timesteps: shape (B, T)
36 # We'll compute sin/cos frequencies across dim T
37 timesteps = timesteps.float() # ensure float
38
39 B, T = timesteps.shape
40 device = timesteps.device
41
42 half_dim = self.embedding_dim // 2
43 # typical log space frequencies for sinusoidal encoding
44 exponent = -torch.arange(half_dim, dtype=torch.float, device=device) * (
45 torch.log(torch.tensor(10000.0)) / half_dim
46 )
47 # Expand timesteps to (B, T, 1) then multiply
48 freqs = timesteps.unsqueeze(-1) * exponent.exp() # (B, T, half_dim)
49
50 sin = torch.sin(freqs)
51 cos = torch.cos(freqs)
52 enc = torch.cat([sin, cos], dim=-1) # (B, T, w)
53
54 return enc
55
56
57class ActionEncoder(nn.Module):

Callers 2

__init__Method · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected