Update.
[pytorch.git] / lazy_linear.py
index 2fa6a29..c49f0d0 100755 (executable)
@@ -9,10 +9,10 @@ from torch import nn, Tensor
 
 ######################################################################
 
-class LazyLinear(nn.Module):
 
-    def __init__(self, out_dim, bias = True):
-        super(LazyLinear, self).__init__()
+class LazyLinear(nn.Module):
+    def __init__(self, out_dim, bias=True):
+        super().__init__()
         self.out_dim = out_dim
         self.bias = bias
         self.core = None
@@ -24,22 +24,25 @@ class LazyLinear(nn.Module):
             if self.training:
                 self.core = nn.Linear(x.size(1), self.out_dim, self.bias)
             else:
-                raise RuntimeError('Undefined LazyLinear core in inference mode.')
+                raise RuntimeError("Undefined LazyLinear core in inference mode.")
 
         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)
+    def named_parameters(self, memo=None, prefix=""):
+        assert self.core is not None, "Parameters not yet defined"
+        return super().named_parameters(memo, prefix)
+
 
 ######################################################################
 
 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 = 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()
 
@@ -49,4 +52,3 @@ if __name__ == "__main__":
 
     for n, x in model.named_parameters():
         print(n, x.size())
-