MCPcopy Create free account
hub / github.com/apple/ml-pointersect / TestSomeLinearLayers

Class TestSomeLinearLayers

tests/cdslib/nn/modules/test_linear.py:12–61  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10
11
12class TestSomeLinearLayers(unittest.TestCase):
13 def test_build_model1(self):
14 self._build_model(num_layer=1, dim_input=10, dim_output=5, dim_features=16,
15 nonlinearity='leaky_relu', add_norm_layer=True,
16 norm_fun=nn.LayerNorm, dropout_prob=0.)
17
18 def test_build_model2(self):
19 self._build_model(num_layer=2, dim_input=10, dim_output=5, dim_features=16,
20 nonlinearity='leaky_relu', add_norm_layer=True,
21 norm_fun=nn.LayerNorm, dropout_prob=0.)
22
23 def test_build_model3(self):
24 self._build_model(num_layer=2, dim_input=10, dim_output=5, dim_features=16,
25 nonlinearity='relu', add_norm_layer=True,
26 norm_fun=lambda x: torch.nn.BatchNorm1d(x, eps=1e-1, momentum=0.2, affine=True, track_running_stats=True),
27 dropout_prob=0.)
28
29 def test_build_model4(self):
30 self._build_model(num_layer=3, dim_input=10, dim_output=5, dim_features=[16,8],
31 nonlinearity='tanh', add_norm_layer=True,
32 norm_fun=lambda x: torch.nn.InstanceNorm1d(x),
33 dropout_prob=0.5)
34
35 def _build_model(self, num_layer, dim_input, dim_output, dim_features,
36 nonlinearity='leaky_relu', add_norm_layer=True,
37 norm_fun=nn.LayerNorm, dropout_prob=0.):
38 net = cdsnn.StackedLinearLayers(
39 num_layers=num_layer, dim_input=dim_input, dim_output=dim_output,
40 dim_features=dim_features, nonlinearity=nonlinearity,
41 add_norm_layer=add_norm_layer, norm_fun=norm_fun,
42 dropout_prob=dropout_prob)
43
44 # print(f'num_layer: {num_layer}, dim_input: {dim_input}, dim_output: {dim_output}, '
45 # f'dim_features: {dim_features}, nonlinearity: {nonlinearity}, '
46 # f'add_norm_layer: {add_norm_layer}, norm_fun: {norm_fun}, dropout_prob: {dropout_prob}')
47 # print(net)
48
49 seq_len = 10
50 batch_size = 5
51 # construct input
52 x = torch.randn(seq_len, batch_size, dim_input)
53 # compute output
54 y = net(x)
55 assert y.size(0) == seq_len, f'y.size(0) {y.size(0)} != seq_len {seq_len}'
56 assert y.size(1) == batch_size, f'y.size(1) {y.size(1)} != batch_size {batch_size}'
57 assert y.size(2) == dim_output, f'y.size(2) {y.size(2)} != dim_output {dim_output}'
58
59 # check if it can back-propagate
60 loss = y.sum()
61 loss.backward()
62
63
64

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected