Determine mean and standard deviation in list elements

John4: <p>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:</p>
<pre><code>avSkin = np.sum(S[0], 1)
stdSkin = np.std(S[0], 1)
</code></pre>
<p>RuntimeError: Can’t call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.<br>
I also tried to convert the list elements with np.array or np.asarray but it doesn’t work. How can I solve it?</p>

srishti-git1110: <p>Hi,<br>
Seems like you are trying to convert a tensor that requires gradient directly into a numpy array, like so:</p>
<pre><code class="lang-auto">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.
print(x_num)
</code></pre>
<p>This error then occurs and is expected as converting the tensor to numpy breaks the graph and no gradients can be computed.</p>
<p>You can explicitly detach the tensor from the graph and then convert it to a numpy array like so:</p>
<pre><code class="lang-auto">x_num = x.detach().numpy()
print(x_num) # [2. 3.]
</code></pre>

Shawhed: <p>If you require the gradients of the tensors later on you can use <code>torch.sum(S[0],  1)</code> and <code>torch.std(S[0], 1)</code> otherwise  if you just want to store these information you need to use <code>.detach()</code> to first remove the gradient from the tensor, then <code>.numpy()</code> to turn the tensor to numpy array and then use your code to get the sum and std</p>
<p>so basically :</p>
<p>Gradient required:</p>
<pre><code class="lang-auto">avSkin = torch.sum(S[0], 1)
stdSkin = torch.std(S[0], 1)
</code></pre>
<pre><code class="lang-auto">store the value:
 avSkin = torch.sum(S[0].detach().numpy(), 1)
stdSkin = torch.std(S[0].detach().numpy(), 1)
</code></pre>

John4: <p>Actually, I made a mistake because S[0] is a list of tensors:</p>
<pre><code>    avSkin = torch.sum(S[0].detach().numpy(), 1)
</code></pre>
<p>AttributeError: ‘list’ object has no attribute ‘detach’<br>
Is there a way to perform the sum and the std for list elements too?</p>

srishti-git1110: <p>Hi John,<br>
If I try to simulate your use-case:</p>
<aside class="quote no-group" data-username="John4" data-post="4" data-topic="164234">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/e0b2c6/48.png" class="avatar"> John4:</div>
<blockquote>
<p>S[0] is a list of tensors</p>
</blockquote>
</aside>
<pre><code class="lang-auto">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)]
</code></pre>
<aside class="quote no-group" data-username="John4" data-post="1" data-topic="164234">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/e0b2c6/48.png" class="avatar"> John4:</div>
<blockquote>
<p>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</p>
</blockquote>
</aside>
<pre><code class="lang-auto">import numpy as np
arr = np.array([t.detach().numpy() for t in lst])
print(arr)

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

avg = np.mean(arr, 0)
print(avg) # [2. 2.]
std = np.std(arr, 0)
print(std) # [1. 0.]
</code></pre>
<p>Does this help? Or is there anything else that you are looking for?<br>
Maybe exactly posting what S[0] is could get you better help.</p>
<p>Best,<br>
S</p>

John4: <p>Thank you Srishti! It works for me.</p>