Added an error if named_parameters is called before the first forward()
authorFrancois Fleuret <francois@fleuret.org>
Wed, 13 Jun 2018 19:14:35 +0000 (21:14 +0200)
committerFrancois Fleuret <francois@fleuret.org>
Wed, 13 Jun 2018 19:14:35 +0000 (21:14 +0200)
lazy_linear.py

index 4599f00..b3d3165 100755 (executable)
@@ -23,16 +23,25 @@ class LazyLinear(nn.Module):
 
         return self.core(x)
 
+    def named_parameters(self, memo=None, prefix=''):
+        assert self.core is not None, 'Parameters not yet defined'
+        return super(LazyLinear, self).named_parameters(memo, prefix)
+
 ######################################################################
 
-model = nn.Sequential(nn.Conv2d(1, 8, kernel_size = 5),
-                      nn.ReLU(inplace = True),
-                      LazyLinear(128),
-                      nn.ReLU(inplace = True),
-                      nn.Linear(128, 10))
+if __name__ == "__main__":
+    model = nn.Sequential(nn.Conv2d(3, 8, kernel_size = 5),
+                          nn.ReLU(inplace = True),
+                          LazyLinear(128),
+                          nn.ReLU(inplace = True),
+                          nn.Linear(128, 10))
+
+    # model.eval()
+
+    input = Tensor(100, 3, 32, 32).normal_()
 
-# model.eval()
+    output = model(input)
 
-input = Tensor(100, 1, 32, 32).normal_()
+    for n, x in model.named_parameters():
+        print(n, x.size())
 
-output = model(input)