You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
RuntimeError: Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
My error
RuntimeError: Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.
My code
training loop and testing loop
RANDOM_SEED = 42
torch.manual_seed(RANDOM_SEED)
epochs = 100
put the data to printed device
X_train_1 = X_train_1.to(device)
y_train_1 = y_train_1.to(device)
X_test_1 = X_test_1.to(device)
y_test_1 = y_test_1.to(device)
loss_train_1 = []
loss_test_1 = []
training loop
for epoch in range(epochs):
change to train mode
model_1.train()
forward pass
y_train_1_pred = model_1(X_train_1)
calculate loss function
loss_1 = loss_fn_1(y_train_1_pred, y_train_1)
set parameters to zero_grad
optimizer_1.zero_grad()
backward
loss_1.backward()
update
optimizer_1.step()
change to test mode
model_1.eval()
with no_grad
with torch.inference_mode():
# forward pass
y_test_1_pred = model_1(X_test_1)
store the loss
if epoch % 10 == 0:
loss_train_1.append(loss_1)
loss_test_1.append(loss_1_test)
print(f"epoch:{epoch}|train loss:{loss_1}|test loss:{loss_1_test}")
What I've tried so far
i dont konw why ,and after searching for google and gpt ,i didnt get the answer, plsease help me
Beta Was this translation helpful? Give feedback.
All reactions