-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetwork.py
More file actions
36 lines (27 loc) · 907 Bytes
/
network.py
File metadata and controls
36 lines (27 loc) · 907 Bytes
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
import numpy as np
import torch
# Sine activation
class Sine(torch.nn.Module):
def __init__(self):
super().__init__()
def forward(self, input):
return torch.sin(input)
# Siren weights initialisation scheme
def init_weights(m):
if isinstance(m, torch.nn.Linear):
m.weight.data.uniform_(-np.sqrt(6 / m.in_features), np.sqrt(6 / m.in_features))
m.bias.data.fill_(0)
# Siren
def Siren(hparams):
depth, width = hparams['depth'], hparams['width']
# Fourier features
if hparams['num_features']:
dim = 2 * hparams['num_features']
else:
dim = hparams['dim']
layers = [torch.nn.Linear(dim, width), Sine()]
for i in range(1, depth - 1):
layers.append(torch.nn.Linear(width, width))
layers.append(Sine())
layers.append(torch.nn.Linear(width, 1))
return torch.nn.Sequential(*layers).apply(init_weights)