Standard deviation

username: julioeu99
cooked: <p>Hi, i want to calculate the standard deviation for this distance:<br>
de+=distance.euclidean(output,input)</p>
<p>That´s what i tried:<br>
print(de.std(dim=1))</p>
<p>Then i get the following error: AttributeError: ‘float’ object has no attribute ‘std’<br>
What am i doing wrong here?</p>

username: Nikronic
cooked: <p>Hi,</p>
<p>I think based on your error, <code>euclidean</code> distance will return a float number for any two nd matrix. So <code>de+=</code> is always a float number. <code>std</code> cannot be computed for a single float number.</p>
<p>Could you please provide what library you are using for calculating euclidean distance?</p>
<p>But typically, you can calculate it using <code>torch.dist(output, input, 2)</code>.  Here is the <a href="https://pytorch.org/docs/master/generated/torch.dist.html" rel="nofollow noopener">documentation</a>.</p>
<p>Bests</p>

username: julioeu99
cooked: <p>Wrong question, sorry. How can i calculate the mean of de?</p>
<pre><code>       ne=ne.detach().cpu().numpy()           
       naa=naa.detach().cpu().numpy()           
       de=0
       for ne, naa in zip(ne, naa):          
         de+=distance.euclidean(naa,ne)</code></pre>

username: Nikronic
cooked: <aside class="quote no-group" data-username="Nikronic" data-post="2" data-topic="83143">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nikronic/48/71873_2.png" class="avatar"> Nikronic:</div>
<blockquote>
<p>But typically, you can calculate it using <code>torch.dist(output, input, 2)</code> . Here is the <a href="https://pytorch.org/docs/master/generated/torch.dist.html" rel="noopener nofollow ugc">documentation</a>.</p>
</blockquote>
</aside>
<p>I think <code>torch.dist(ne, naa, 2)</code> will do the trick. No need to convert it to numpy or transfer it to CPU.</p>

username: julioeu99
cooked: <p>I need to do what i did, for other things in the program. What i want, is just to calculate the mean of the values given by variable: de<br>
I know that it is very easy, but can you explain me, how can i do that?</p>

username: Nikronic
cooked: <p>Could you please print the shape of <code>ne</code> and <code>naa</code>?</p>

username: julioeu99
cooked: <p>Of course. Both shape: (784,)</p>

username: Nikronic
cooked: <p>With this shape of inputs, <code>de</code> only can be a single float number. Mean of a single number is itself.<br>
By below code</p>
<aside class="quote no-group" data-username="julioeu99" data-post="3" data-topic="83143">
<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/f9ae1b/48.png" class="avatar"> julioeu99:</div>
<blockquote>
<pre><code class="lang-auto">for ne, naa in zip(ne, naa):          
    de+=distance.euclidean(naa,ne)
</code></pre>
</blockquote>
</aside>
<p>You are adding 768 distances which will result in a single number. Are you looking for having a list of 768 separate distances then calculating mean for them?</p>

username: julioeu99
cooked: <p>This loop is always printing the distance values, and then end, when finished iterating all data.</p>
<pre><code class="lang-auto">      ne=ne.detach().cpu().numpy()           
      naa=naa.detach().cpu().numpy()           
      de=0
      for ne, naa in zip(ne, naa):          
        de+=distance.euclidean(naa,ne)
      print(de/size_batch)
</code></pre>
<p>I want to calculate the mean of all printed values, it is clear now what i want to do?<br>
The print, prints values like: 3.21412<br>
3.124124<br>
3.112412<br>
3.42121<br>
And i want to calculate the mean of all this printed values.</p>

username: Nikronic
cooked: <p>In this case you just doing fine. Just use different variables for looping:</p>
<pre><code class="lang-auto">ne = np.randn(768,)
naa = np.randn(768,)

de=np.zeros((1, ))
for ne_, naa_ in zip(ne, naa):          
    de+=np.sqrt((ne_ - naa_)**2)
de/ne.shape[0]
</code></pre>
<p>But using loops are not efficient. The more optimized way to this using numpy:</p>
<pre><code class="lang-auto">np.sum(np.sqrt((ne - naa)**2)) / ne.shape[0]
</code></pre>
<p>Sklearn:</p>
<pre><code class="lang-auto">from sklearn.metrics.pairwise import euclidean_distances
np.sum(np.diag(euclidean_distances(ne.reshape(-1, 1), naa.reshape(-1, 1))))/ne.shape[0]
</code></pre>
<p>Sorry that I did not understand your question very well.</p>
<p>Bests</p>

username: julioeu99
cooked: <p>Thank you, for the detailed explaination you helped me a lot. Just for curiosity, in my previous loop, how can i calculate the mean?</p>