This is my attemp to calculate the Jacobians for my application. During training, there weren’t any errors but after some epochs, the program raised the abovementioned error. This is the function that raised that error:
batch_size = theta.shape[0]
theta = theta.clone().requires_grad_(True)
theta_dot = theta_dot.clone().requires_grad_(True)
s_dot = s_dot.clone().requires_grad_(False)
z = z.clone().requires_grad_(False)
→ Lagrangian = self.LagrangianFunction(theta, theta_dot, s_dot, z)
dL_dtheta = torch.autograd.grad(
outputs=Lagrangian.sum(),
inputs=theta,
create_graph=True
)[0]
Based on the error message it seems Lagrangian = self.LagrangianFunction(theta, theta_dot, s_dot, z)
is not differentiable and detaches the output from the computation graph. Could you post the function definition, please?
Certainly, the self.LagrangianFunction() is just a forward() pass of a simple DNN with 3 hidden layers, each has 100 nodes with activation function is LeakyReLU(). The function takes in the given 4 arguments as mentioned.
The computation of dL_dTheta is computable (output valid result) until some epochs elapsed
These are the dimensions of inputs and outputs of LagrangianFunction():
theta: [batch_size, 3]
theta_dot: [batch_size, 3]
s_dot: [batch_size, 3]
z: [batch_size, 1]
Lagrangian: [batch_size, 1]
This is the derived grad_fn of LagrangianFunction() by placing print(Lagrangian.grad_fn) after the computation of Lagrangian. As you can see, the grad_fn disappeared, consequently, the Lagrangian is detached from the computational graph …
This is my explicit Network that I mentioned.
class LAGRANGIAN_NNs(nn.Module):
“”"
@ brief Neural Network for predicting the total Lagrangian function L(theta_dot, s_dot, theta, z).
@ args:
input_dim (int): Dimension of the input tensor. Default is 7.
output_dim (int): Dimension of the output tensor. Default is 1.
hidden_layer_dim (int): Number of neurons in each hidden layer. Default is 128.
num_hidden_layer (int): Number of hidden layers. Default is 5.
activation (list): List of activation functions for each hidden layer.
@ input:
theta_dot (torch.Tensor): Tensor of joint velocities, shape (batch_size, 3).
s_dot (torch.Tensor): Tensor of end-effector velocities, shape (batch_size, 3).
theta (torch.Tensor): Tensor of joint angles, shape (batch_size, 3).
z (torch.Tensor): Tensor of end-effector z-value, shape (batch_size, 1).
@ output:
torch.Tensor: Predicted total Lagrangian function, shape (batch_size, 1).
"""
def __init__(
self,
input_dim: int = 10,
output_dim: int = 1,
hidden_layer_dim: int = 50,
num_hidden_layer: int = 3,
activation: str = 'softplus'
):
super(LAGRANGIAN_NNs, self).__init__()
self.epoch = 0
self.layers = nn.ModuleList([nn.Linear(input_dim, hidden_layer_dim)])
for _ in range(num_hidden_layer - 1):
self.layers.append(nn.Linear(hidden_layer_dim, hidden_layer_dim))
self.layers.append(nn.Linear(hidden_layer_dim, output_dim))
for layer in self.layers:
nn.init.xavier_uniform_(layer.weight)
nn.init.zeros_(layer.bias)
if activation == 'sin':
self.activation = torch.sin
elif activation == 'tanh':
self.activation = torch.tanh
elif activation == 'softplus':
self.activation = torch.nn.Softplus()
elif activation == 'relu':
self.activation = torch.nn.ReLU()
else:
self.activation = torch.nn.LeakyReLU()
def forward(self, theta: torch.Tensor, theta_dot: torch.Tensor, s_dot: torch.Tensor, z: torch.Tensor) -> torch.Tensor:
out = torch.cat([theta, theta_dot, s_dot, z], dim=1)
print(f"After concat requires grad: {out.requires_grad}")
for layer in self.layers[:-1]:
out = self.activation(layer(out)) if isinstance(self.activation, nn.Module) else self.activation(layer(out))
out = self.layers[-1](out)
print(f"Final output requires grad: {out.requires_grad}")
return out
I try print to debug and it turned out the line:
out = torch.cat([theta, theta_dot, s_dot, z], dim=1)
might be the cause. This is the output cell before the error raised:
Based on your debug outputs it seems some of the iterations pass already detached tensors. Try to narrow down why this happens as it seems to come from the tensor creation or any calls before the forward
is called.