Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow interchangeability of KANLinear with nn.Linear #6

Merged
merged 1 commit into from
May 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/efficient_kan/kan.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,19 @@ def scaled_spline_weight(self):
)

def forward(self, x: torch.Tensor):
assert x.dim() == 2 and x.size(1) == self.in_features
assert x.size(-1) == self.in_features
original_shape = x.shape
x = x.view(-1, self.in_features)

base_output = F.linear(self.base_activation(x), self.base_weight)
spline_output = F.linear(
self.b_splines(x).view(x.size(0), -1),
self.scaled_spline_weight.view(self.out_features, -1),
)
return base_output + spline_output
output = base_output + spline_output

output = output.view(*original_shape[:-1], self.out_features)
return output

@torch.no_grad()
def update_grid(self, x: torch.Tensor, margin=0.01):
Expand Down