I have an image encoder and a question encoder branch combined together than connecting to 2 fc layers. How should I add the parameter together to update the network parameter?
I have tried some code:
params = model.img_encoder.fc.parameters() + model.qu_encoder.parameters() + \
model.fc1.parameters() + model.fc2.parameters()
optimizer = optim.Adam(params, lr=1e^-3)
but it will return
TypeError: unsupported operand type(s) for +: 'generator' and 'generator'
any idea or suggestion how to update the parameters of the combined network?
def concat_generators(*args):
for gen in args:
yield from gen
params = concat_generators(model.img_encoder.fc.parameters(), model.qu_encoder.parameters() , model.fc1.parameters(), model.fc2.parameters())
optimizer = torch.optim.Adam(params, lr=learning_rate)
This may work.
You got an error because you parameters()
are not lists, but generators, and you can’t sum generators.
Yes, it work !!! But I think I should watch some tutorial about generator first
Glad it worked! Read stuff about generators, it’s important as they are used everywhere in pytorch
I found anothor solution. The following sample could work, too.
params = list(model.img_encoder.fc.parameters()) + list(model.qu_encoder.parameters()) \
+ list(model.fc1.parameters()) + list(model.fc2.parameters())
optimizer = optim.Adam(params, lr = LEARNING_RATE)