I’m trying to do transfer learning with swin_t. I ran the following code to hopefully freeze the layers and add an output one like this:
weights = Swin_T_Weights.DEFAULT
model = swin_t(weights=weights)
for param in model.parameters():
param.requires_grad = False
model.fc = nn.Linear(768, 37)
model = model.to(device)
However when I try training I get the following error
“Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn”
Not sure what’s wrong. Resnet50 worked fine using the same code.
Would the following work?
for m in model.modules():
if isinstance(m, nn.Linear):
m.requires_grad_ = False