X-Git-Url: https://www.fleuret.org/cgi-bin/gitweb/gitweb.cgi?p=pytorch.git;a=blobdiff_plain;f=lazy_linear.py;h=2fa6a29ef4ab735b1f53fc286e21635e67cf0059;hp=4599f00e1fc4f36844e69a918d6b4ebeb6e883a3;hb=e916a8624b6a09737696c124f35059030f0f20e4;hpb=7b534d513dd1bdad208f2b59baf2d47979f90663 diff --git a/lazy_linear.py b/lazy_linear.py index 4599f00..2fa6a29 100755 --- a/lazy_linear.py +++ b/lazy_linear.py @@ -1,5 +1,10 @@ #!/usr/bin/env python +# Any copyright is dedicated to the Public Domain. +# https://creativecommons.org/publicdomain/zero/1.0/ + +# Written by Francois Fleuret + from torch import nn, Tensor ###################################################################### @@ -23,16 +28,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)