-
Notifications
You must be signed in to change notification settings - Fork 0
/
sine_kan.py
79 lines (67 loc) · 3.1 KB
/
sine_kan.py
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
import torch
import torch.nn.functional as F
import math
from typing import *
def forward_step(i_n, grid_size, A, K, C):
ratio = A * grid_size**(-K) + C
i_n1 = ratio * i_n
return i_n1
class SineKANLayer(torch.nn.Module):
def __init__(self, input_dim, output_dim, device='cuda', grid_size=5, is_first=False, add_bias=True, norm_freq=True):
super(SineKANLayer,self).__init__()
self.grid_size = grid_size
self.device = device
self.is_first = is_first
self.add_bias = add_bias
self.input_dim = input_dim
self.output_dim = output_dim
self.A, self.K, self.C = 0.9724108095811765, 0.9884401790754128, 0.999449553483052
self.grid_norm_factor = (torch.arange(grid_size) + 1)
self.grid_norm_factor = self.grid_norm_factor.reshape(1, 1, grid_size)
if is_first:
self.amplitudes = torch.nn.Parameter(torch.empty(output_dim, input_dim, 1).normal_(0, .4) / output_dim / self.grid_norm_factor)
else:
self.amplitudes = torch.nn.Parameter(torch.empty(output_dim, input_dim, 1).uniform_(-1, 1) / output_dim / self.grid_norm_factor)
grid_phase = torch.arange(1, grid_size + 1).reshape(1, 1, 1, grid_size) / (grid_size + 1)
self.input_phase = torch.linspace(0, math.pi, input_dim).reshape(1, 1, input_dim, 1).to(device)
phase = grid_phase.to(device) + self.input_phase
if norm_freq:
self.freq = torch.nn.Parameter(torch.arange(1, grid_size + 1).float().reshape(1, 1, 1, grid_size) / (grid_size + 1)**(1 - is_first))
else:
self.freq = torch.nn.Parameter(torch.arange(1, grid_size + 1).float().reshape(1, 1, 1, grid_size))
for i in range(1, self.grid_size):
phase = forward_step(phase, i, self.A, self.K, self.C)
# self.phase = torch.nn.Parameter(phase)
self.register_buffer('phase', phase)
if self.add_bias:
self.bias = torch.nn.Parameter(torch.ones(1, output_dim) / output_dim)
def forward(self, x):
x_shape = x.shape
output_shape = x_shape[0:-1] + (self.output_dim,)
x = torch.reshape(x, (-1, self.input_dim))
x_reshaped = torch.reshape(x, (x.shape[0], 1, x.shape[1], 1))
s = torch.sin(x_reshaped * self.freq + self.phase)
y = torch.einsum('ijkl,jkl->ij', s, self.amplitudes)
if self.add_bias:
y += self.bias
y = torch.reshape(y, output_shape)
return y
class SineKAN(torch.nn.Module):
def __init__(
self,
layers_hidden: List[int],
grid_size: int = 8,
device: str = 'cuda',
) -> None:
super().__init__()
self.layers = torch.nn.ModuleList([
SineKANLayer(
in_dim, out_dim, grid_size=grid_size, is_first=True
) if i == 0 else SineKANLayer(
in_dim, out_dim, device, grid_size=grid_size,
) for i, (in_dim, out_dim) in enumerate(zip(layers_hidden[:-1], layers_hidden[1:]))
])
def forward(self, x):
for layer in self.layers:
x = layer(x)
return x