Determine mean and standard deviation in list elements

Hi everyone. I created a list containing 4 matrixes and I need to calculate the mean and the standard deviation of the columns of the first element of S:

avSkin = np.sum(S[0], 1)
stdSkin = np.std(S[0], 1)

RuntimeError: Can’t call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.
I also tried to convert the list elements with np.array or np.asarray but it doesn’t work. How can I solve it?

Seems like you are trying to convert a tensor that requires gradient directly into a numpy array, like so:

import torch
x = torch.tensor([2.0, 3.0], requires_grad=True)
x_num = x.numpy() # RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.

This error then occurs and is expected as converting the tensor to numpy breaks the graph and no gradients can be computed.

You can explicitly detach the tensor from the graph and then convert it to a numpy array like so:

x_num = x.detach().numpy()
print(x_num) # [2. 3.]

If you require the gradients of the tensors later on you can use torch.sum(S[0], 1) and torch.std(S[0], 1) otherwise if you just want to store these information you need to use .detach() to first remove the gradient from the tensor, then .numpy() to turn the tensor to numpy array and then use your code to get the sum and std

so basically :

Gradient required:

avSkin = torch.sum(S[0], 1)
stdSkin = torch.std(S[0], 1)
store the value:
 avSkin = torch.sum(S[0].detach().numpy(), 1)
stdSkin = torch.std(S[0].detach().numpy(), 1)

Actually, I made a mistake because S[0] is a list of tensors:

    avSkin = torch.sum(S[0].detach().numpy(), 1)

AttributeError: ‘list’ object has no attribute ‘detach’
Is there a way to perform the sum and the std for list elements too?

Hi John,
If I try to simulate your use-case:

import torch
lst = [torch.tensor([1.0, 2.0], requires_grad=True), torch.tensor([3.0, 2], requires_grad=True)] # S[0]
print(lst) # [tensor([1., 2.], requires_grad=True), tensor([3., 2.], requires_grad=True)]
import numpy as np
arr = np.array([t.detach().numpy() for t in lst])

# [[1. 2.]
# [3. 2.]]

avg = np.mean(arr, 0)
print(avg) # [2. 2.]
std = np.std(arr, 0)
print(std) # [1. 0.]

Does this help? Or is there anything else that you are looking for?
Maybe exactly posting what S[0] is could get you better help.


Thank you Srishti! It works for me.