Spaces:
Sleeping
Sleeping
Commit ·
f458392
1
Parent(s): fae3c76
Add model and requirements for sinusoidal and time embeddings
Browse files- model.py +42 -0
- requirements.txt +3 -0
model.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import math
|
| 4 |
+
|
| 5 |
+
#Varicence scheduler
|
| 6 |
+
T = 1000
|
| 7 |
+
beta_start = 1e-4
|
| 8 |
+
beta_end = 0.02
|
| 9 |
+
betas = torch.linspace(beta_start, beta_end, step=T)
|
| 10 |
+
alphas = 1. - betas
|
| 11 |
+
alphas_cumprod = torch.cumprod(alphas, dim=0)
|
| 12 |
+
|
| 13 |
+
#SinusoidalPositionEmbedding
|
| 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
|
| 27 |
+
|
| 28 |
+
#Time 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(in_dim)
|
| 33 |
+
|
| 34 |
+
self.mlp = nn.Sequential(
|
| 35 |
+
nn.Linear(in_dim, out_dim),
|
| 36 |
+
nn.SiLU(),
|
| 37 |
+
nn.Linear(out_dim , out_dim)
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
def forward(self, timesteps: torch.Tensor) -> torch.Tensor:
|
| 41 |
+
time_emb = self.Sinusoidal_waves(timesteps)
|
| 42 |
+
return self.mlp(time_emb)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
gradio
|