1111 XNORWeightBinarizer
1212)
1313
14+
1415class BinaryLayersTestCase (unittest .TestCase ):
1516 def setUp (self ) -> None :
1617 self .test_bconfig = BConfig (
1718 activation_pre_process = BasicInputBinarizer ,
1819 activation_post_process = BasicScaleBinarizer ,
1920 weight_pre_process = XNORWeightBinarizer
2021 )
21- self .data = torch .tensor ([- 0.05263 , - 0.05068 , - 0.03849 , 0.03104 , 0.0772 , 0.03038 , - 0.06640 , 0.05894 ,
22- 0.13059 , 0.03433 , - 0.25811 , 0.13785 ]).view (1 , 3 , 2 , 2 )
23- self .weights = torch .tensor ([- 0.0252 , 0.0084 , - 0.0676 , 0.0891 , - 0.0010 , 0.0518 , 0.0380 , 0.2866 ,
24- - 0.0050 ])
22+ self .data = torch .tensor ([- 0.05263 , - 0.05068 , - 0.03849 , 0.03104 , 0.0772 , 0.03038 , - 0.06640 , 0.05894 ,
23+ 0.13059 , 0.03433 , - 0.25811 , 0.13785 ]).view (1 , 3 , 2 , 2 )
24+ self .weights = torch .tensor ([- 0.0252 , 0.0084 , - 0.0676 , 0.0891 , - 0.0010 , 0.0518 , 0.0380 , 0.2866 ,
25+ - 0.0050 ])
2526
2627 def tearDown (self ) -> None :
2728 pass
@@ -31,38 +32,38 @@ def test_linear_layer(self):
3132 layer .weight .data .copy_ (self .weights .view (3 , 3 ))
3233 x = self .data [:, :, 0 , 0 ].view (1 , 3 )
3334 layer = prepare_binary_model (layer , bconfig = self .test_bconfig )
34-
35+
3536 output = layer (x )
3637 expected = torch .tensor ([[0.0337 , - 0.0473 , - 0.1099 ]])
3738 self .assertTrue (torch .allclose (expected , output , atol = 1e-4 ))
38-
39+
3940 def test_conv1d_layer (self ):
4041 layer = nn .Conv1d (3 , 3 , 1 , bias = False )
4142 layer .weight .data .copy_ (self .weights .view (3 , 3 , 1 ))
42- x = self .data [:,:,:, 0 ].view (1 , 3 , 2 )
43+ x = self .data [:, :, :, 0 ].view (1 , 3 , 2 )
4344 layer = prepare_binary_model (layer , bconfig = self .test_bconfig )
44-
45+
4546 output = layer (x )
46- expected = torch .tensor ([[[ 0.0337 , 0.0337 ],
47- [- 0.0473 , - 0.0473 ],
48- [- 0.1099 , - 0.1099 ]]])
47+ expected = torch .tensor ([[[0.0337 , 0.0337 ],
48+ [- 0.0473 , - 0.0473 ],
49+ [- 0.1099 , - 0.1099 ]]])
4950 self .assertTrue (torch .allclose (expected , output , atol = 1e-4 ))
50-
51+
5152 def test_conv2d_layer (self ):
5253 layer = nn .Conv2d (3 , 3 , 1 , bias = False )
5354 layer .weight .data .copy_ (self .weights .view (3 , 3 , 1 , 1 ))
5455 x = self .data
5556 layer = prepare_binary_model (layer , bconfig = self .test_bconfig )
56-
57+
5758 output = layer (x )
58- expected = torch .tensor ([[[[ 0.0337 , 0.0337 ],
59- [ 0.0337 , - 0.0337 ]],
59+ expected = torch .tensor ([[[[0.0337 , 0.0337 ],
60+ [ 0.0337 , - 0.0337 ]],
6061
61- [[- 0.0473 , - 0.0473 ],
62- [- 0.0473 , 0.0473 ]],
62+ [[- 0.0473 , - 0.0473 ],
63+ [- 0.0473 , 0.0473 ]],
6364
64- [[- 0.1099 , - 0.1099 ],
65- [- 0.1099 , 0.1099 ]]]])
65+ [[- 0.1099 , - 0.1099 ],
66+ [- 0.1099 , 0.1099 ]]]])
6667 self .assertTrue (torch .allclose (expected , output , atol = 1e-4 ))
6768
6869
0 commit comments