Yes, this will throw the error, if you don’t wrap the code in the aforementioned guard.
Would this code work for you?
class MyModel(nn.Module):
    def __init__(self):
        super(MyModel, self).__init__()
        self.fc = nn.Linear(10, 10)
        
    def forward(self, x):
        x = self.fc(x)
        return x


model = MyModel()
x = torch.randn(1, 10)
target = torch.randint(0, 10, (1,))
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=1e-3)

for epoch in range(10):
    optimizer.zero_grad()
    output = model(x)
    loss = criterion(output, target)
    loss.backward()
    optimizer.step()
    print('Epoch {}, loss {}'.format(epoch, loss.item()))
    
    with torch.no_grad():
        # Manipulate weight matrix
        model.fc.weight[:5] = nn.Parameter(torch.ones(5, 10))