Skip to content

Commit cbcbc0b

Browse files
committed
shifter init unit test
1 parent 56c30f5 commit cbcbc0b

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

neuralpredictors/layers/shifters/mlp.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import torch
44
from torch import nn
55
from torch.nn import ModuleDict
6-
from torch.nn.init import xavier_normal
6+
from torch.nn.init import xavier_normal_
77

88
from .base import Shifter
99

@@ -37,11 +37,12 @@ def regularizer(self):
3737
return 0
3838

3939
def initialize(self):
40-
for layer in self.mlp:
41-
if isinstance(layer, nn.Linear):
42-
xavier_normal_(layer.weight)
43-
if layer.bias is not None:
44-
nn.init.zeros_(layer.bias)
40+
with torch.no_grad():
41+
for layer in self.mlp:
42+
if isinstance(layer, nn.Linear):
43+
xavier_normal_(layer.weight)
44+
if layer.bias is not None:
45+
nn.init.zeros_(layer.bias)
4546

4647
def forward(self, pupil_center, trial_idx=None):
4748
if trial_idx is not None:
@@ -77,8 +78,5 @@ def __init__(
7778
for k in data_keys:
7879
self.add_module(k, MLP(input_channels, hidden_channels_shifter, shift_layers, bias))
7980

80-
def initialize(self, **kwargs):
81-
pass
82-
8381
def regularizer(self, data_key):
8482
return self[data_key].regularizer() * self.gamma_shifter

test/test_shifter_init.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import torch
2+
from torch import nn
3+
from unittest.mock import patch
4+
5+
from neuralpredictors.layers.shifters import MLP
6+
7+
def test_xavier_initialization_is_used():
8+
with patch("neuralpredictors.layers.shifters.mlp.xavier_normal_") as mock_xavier:
9+
MLP(shift_layers=3)
10+
11+
# One Linear layer per shift layer
12+
assert mock_xavier.call_count == 3

0 commit comments

Comments
 (0)