I am trying to train my model with the following code, and I am getting a runtime error: “RuntimeError: Trying to backward through the graph a second time, but the saved intermediate results have already been freed. Specify retain_graph=True when calling backward the first time.”
Here is my code:
	criterion = nn.MSELoss(reduction='mean')
	optimizer = torch.optim.SGD(model.parameters(), lr=.1, momentum=.9, weight_decay=1e-4)
	for i in range(100):
		for space in train_spaces:

			optimizer.zero_grad()

			input_ = model.space_to_input(space)
			input_2 = input_.repeat(len(input_), 1)
			
			predictions = model.forward(input_)
			predictions = predictions.masked_fill(input_2 == -1, -1)

			loss = criterion(predictions, input_2)			
				
			loss.backward()
		
			optimizer.step()