adityachaubey commited on
Commit
b147273
·
1 Parent(s): 36ddeb2

Fix assertions and tensor device handling in Sinusoidal_embedding class

Browse files
Files changed (1) hide show
  1. model.py +4 -4
model.py CHANGED
@@ -14,13 +14,13 @@ alphas_cumprod = torch.cumprod(alphas, dim=0)
14
  class Sinusoidal_embedding(torch.nn.Module):
15
  def __init__(self, dim:int):
16
  super().__init__()
17
- assert self.dim % 2 == 0 , 'Embeddings must be divisble by 2'
18
- dim = self.dim
19
 
20
  def forward(self, time_stamps:torch.Tensor) -> torch.Tensor :
21
  half_dim = self.dim // 2
22
  scale = math.log(10000)/(half_dim -1 )
23
- freq = torch.exp(torch.arange(half_dim, dtype=torch.float32)* -scale)
24
  embeddings = time_stamps[:, None] * freq[None, :]
25
  embeddings = torch.cat((embeddings.sin(), embeddings.cos()), dim=-1)
26
  return embeddings
@@ -29,7 +29,7 @@ class Sinusoidal_embedding(torch.nn.Module):
29
  class Time_embeddings(torch.nn.Module):
30
  def __init__(self, in_dim:int, out_dim:int):
31
  super().__init__
32
- self.Sinusoidal_waves = Sinusoidal_embedding(in_dim)
33
 
34
  self.mlp = nn.Sequential(
35
  nn.Linear(in_dim, out_dim),
 
14
  class Sinusoidal_embedding(torch.nn.Module):
15
  def __init__(self, dim:int):
16
  super().__init__()
17
+ assert dim % 2 == 0 , 'Embeddings must be divisble by 2'
18
+ self.dim = dim
19
 
20
  def forward(self, time_stamps:torch.Tensor) -> torch.Tensor :
21
  half_dim = self.dim // 2
22
  scale = math.log(10000)/(half_dim -1 )
23
+ freq = torch.exp(torch.arange(half_dim, dtype=torch.float32, device=time_stamps.device)* -scale)
24
  embeddings = time_stamps[:, None] * freq[None, :]
25
  embeddings = torch.cat((embeddings.sin(), embeddings.cos()), dim=-1)
26
  return embeddings
 
29
  class Time_embeddings(torch.nn.Module):
30
  def __init__(self, in_dim:int, out_dim:int):
31
  super().__init__
32
+ self.Sinusoidal_waves = Sinusoidal_embedding(dim=in_dim)
33
 
34
  self.mlp = nn.Sequential(
35
  nn.Linear(in_dim, out_dim),