[Python] why does not my code cause the error 'element 0 of tensors does not require grad and...

Discussão em 'Python' iniciado por Stack, Março 21, 2025.

  1. Stack

    Stack Membro Participativo

    Given the code fragment below, I freeze the weights of the neural network using model.fc1.requires_grad_(False) and model.fc2.requires_grad_(False). If I use loss = criterion(outputs, y_batch) to calculate the loss, the training is fine. But when I use loss = compute_loss(model) to calculate the loss, I get the error RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn. Why does not loss = criterion(outputs, y_batch) cause the error?

    def compute_loss(model):
    for _, params in model.named_parameters():
    loss = sum(params)
    return loss

    class SimpleNet(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
    super(SimpleNet, self).__init__()
    self.fc1 = nn.Linear(input_size, hidden_size)
    self.fc2 = nn.Linear(hidden_size, output_size)

    def forward(self, x):
    x = self.fc1(x)
    x = self.fc2(x)
    return x

    model = SimpleNet(input_size, hidden_size, output_size)

    criterion = nn.CrossEntropyLoss()
    optimizer = optim.SGD(model.parameters(), lr=learning_rate)

    for epoch in range(num_epochs):
    for batch_idx, (X_batch, y_batch) in enumerate(dataloader):
    optimizer.zero_grad()
    outputs = model(X_batch)
    loss = criterion(outputs, y_batch)
    # loss = compute_loss(model)

    loss.backward()
    optimizer.step()

    if (batch_idx + 1) % 10 == 0:
    model.fc1.requires_grad_(False)
    model.fc2.requires_grad_(False)

    print("Training complete!")

    Continue reading...

Compartilhe esta Página