From: Francois Fleuret Date: Wed, 13 Jun 2018 19:14:35 +0000 (+0200) Subject: Added an error if named_parameters is called before the first forward() X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=bbc4ab46de8cb2e05f83e35e15f3adfd23490089;p=pytorch.git Added an error if named_parameters is called before the first forward() --- diff --git a/lazy_linear.py b/lazy_linear.py index 4599f00..b3d3165 100755 --- a/lazy_linear.py +++ b/lazy_linear.py @@ -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)