-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodels.py
More file actions
104 lines (86 loc) · 3.15 KB
/
Copy pathmodels.py
File metadata and controls
104 lines (86 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import torch
import torch.nn as nn
from utils import VPJBatchNorm
class Flow(nn.Module):
def __init__(self, dim=2, dim_hidden=64):
super(Flow, self).__init__()
self.nonlinear = nn.SiLU()
self.dim = dim
self.mlp = nn.Sequential(
nn.Linear(dim, dim_hidden),
self.nonlinear,
nn.Linear(dim_hidden, dim_hidden),
self.nonlinear,
nn.Linear(dim_hidden, dim_hidden),
self.nonlinear,
nn.Linear(dim_hidden, dim),
VPJBatchNorm(dim, affine=False),
)
def forward(self, x):
output = self.mlp(x)
return output
class FlowKernel(nn.Module):
def __init__(self, dim=2, dim_hidden=64, num_kernels=4):
super(FlowKernel, self).__init__()
self.num_kernels = num_kernels
self.nonlinear = nn.SiLU()
self.dim = dim
self.mlp = nn.Sequential(
nn.Linear(dim * (2 * num_kernels + 1), dim_hidden),
self.nonlinear,
nn.Linear(dim_hidden, dim_hidden),
self.nonlinear,
nn.Linear(dim_hidden, dim),
VPJBatchNorm(dim, affine=False),
)
def position_encoding(self, x):
"""
Apply positional encoding to input coordinates as in NeRF.
For each dimension, adds sin/cos encodings at different frequencies
while preserving the original coordinate.
Args:
x: Input tensor of shape [batch, dim]
Returns:
Encoded tensor with shape [batch, dim * (2 * num_kernels + 1)]
"""
batch_size, dim = x.shape
# Initialize output tensor that will include original coordinates
encoded = [x]
# Apply encoding for each frequency
for i in range(self.num_kernels):
# 2^i gives increasing frequency for each level
freq = 2.0**i
# Add sin and cos encodings for each dimension
sin_encoding = torch.sin(x * freq)
cos_encoding = torch.cos(x * freq)
encoded.append(sin_encoding)
encoded.append(cos_encoding)
# Concatenate all encodings along the feature dimension
return torch.cat(encoded, dim=-1)
def forward(self, x):
x = self.position_encoding(x)
output = self.mlp(x)
return output
class FlowAugmented(nn.Module):
def __init__(self, score_model, dim=2, dim_hidden=64):
super(FlowAugmented, self).__init__()
self.score_model = score_model
self.nonlinear = nn.SiLU()
self.dim = dim
self.mlp = nn.Sequential(
nn.Linear(dim + score_model.dim, dim_hidden),
VPJBatchNorm(dim_hidden),
self.nonlinear,
nn.Linear(dim_hidden, dim_hidden),
VPJBatchNorm(dim_hidden),
self.nonlinear,
nn.Linear(dim_hidden, dim),
VPJBatchNorm(dim, affine=False),
)
def parameters(self):
# not include score_model parameters
return self.mlp.parameters()
def forward(self, x):
score = self.score_model.score(x, t=0.1)
input = torch.cat([x, score], dim=-1)
return self.mlp(input)