avalon1511:

I printed all of the parameters of the generator object and it seems like b is not considered a parameter. Confused about what’s going on


I don’t know why this would happen, as b is returned as a parameter in all of these implementations:
class ShiftLayer(nn.Module):
    def __init__(self, b):
        super().__init__()
        self.b = nn.Parameter(torch.tensor(b))
        
    def forward(self, x):
        return x + self.b
    
shift = ShiftLayer(1.)
print(list(shift.parameters()))
print(dict(shift.named_parameters()))

model = nn.Sequential(
    nn.Linear(1, 1),
    ShiftLayer(2.)
)
print(list(model.parameters()))
print(dict(model.named_parameters()))



class Generator(nn.Module):
    def __init__(self, b_val):
        super(Generator, self).__init__()
        self.main = nn.Sequential(
            # input is Z, going into a convolution
            nn.ConvTranspose2d( nz, ngf * 8, 4, 1, 0, bias=False),
            nn.BatchNorm2d(ngf * 8),
            nn.ReLU(True),
            # state size. (ngf*8) x 4 x 4
            nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ngf * 4),
            nn.ReLU(True),
            # state size. (ngf*4) x 8 x 8
            nn.ConvTranspose2d( ngf * 4, ngf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ngf * 2),
            nn.ReLU(True),
            # state size. (ngf*2) x 16 x 16
            nn.ConvTranspose2d( ngf * 2, ngf, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ngf),
            ShiftLayer(b_val),
            nn.ReLU(True),
            # state size. (ngf) x 32 x 32
            nn.ConvTranspose2d( ngf, nc, 4, 2, 1, bias=False),
            nn.Tanh()
            # state size. (nc) x 64 x 64
        )
    
    def forward(self, input):
        return self.main(input)

nz = 100
ngf = 64
nc = 3
gen = Generator(10.)

for name, param in gen.named_parameters():
    if 'b' in name and 'bias' not in name:
        print(name, param)
> main.11.b Parameter containing:
  tensor(10., requires_grad=True)