Covariance Matrix for the. Dataset

username: jmandivarapu1
cooked: <p>I want to calculate the mean,standard deviation and <strong>Covariance matrix</strong> of MNIST Dataset for only digits 1,2,3…etc separately.  (<a href="http://www-inst.eecs.berkeley.edu/~cs70/sp15/notes/n21.pdf" rel="noopener nofollow ugc">http://www-inst.eecs.berkeley.edu/~cs70/sp15/notes/n21.pdf</a>) Page 12</p>
<p>I calculated the mean and SD using the below. I need covariance matrix tooo</p>
<pre><code class="lang-auto">
Any help would be really great.

Screen Shot 2020-04-02 at 11.56.26 PM
Screen Shot 2020-04-02 at 11.56.26 PM
2414×1014 295 KB

def online_mean_and_sd(trainLoader):
    """Compute the mean and sd in an online fashion

        Var[x] = E[X^2] - E^2[X]
    """
    cnt = 0
    fst_moment = torch.empty(3)
    snd_moment = torch.empty(3)

    for images, _ in trainLoader:

        b, c, h, w = images.shape
        nb_pixels = b * h * w
        sum_ = torch.sum(images, dim=[0, 2, 3])
        sum_of_square = torch.sum(images ** 2, dim=[0, 2, 3])
        fst_moment = (cnt * fst_moment + sum_) / (cnt + nb_pixels)
        snd_moment = (cnt * snd_moment + sum_of_square) / (cnt + nb_pixels)

        cnt += nb_pixels

    return fst_moment, torch.sqrt(snd_moment - fst_moment ** 2)
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/b/3b38ce9e9aa5aacdb982b437d0b54e3f7fc7ecf5.png" data-download-href="https://discuss.pytorch.org/uploads/default/3b38ce9e9aa5aacdb982b437d0b54e3f7fc7ecf5" title="Screen Shot 2020-04-03 at 10.39.09 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/b/3b38ce9e9aa5aacdb982b437d0b54e3f7fc7ecf5_2_690x414.png" alt="Screen Shot 2020-04-03 at 10.39.09 PM" data-base62-sha1="8rTSwvb4Vor9HiWrFQctzr9p90F" width="690" height="414" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/b/3b38ce9e9aa5aacdb982b437d0b54e3f7fc7ecf5_2_690x414.png, https://discuss.pytorch.org/uploads/default/optimized/3X/3/b/3b38ce9e9aa5aacdb982b437d0b54e3f7fc7ecf5_2_1035x621.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/b/3b38ce9e9aa5aacdb982b437d0b54e3f7fc7ecf5_2_1380x828.png 2x" data-dominant-color="F2F0F0"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screen Shot 2020-04-03 at 10.39.09 PM</span><span class="informations">1478×888 183 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: hiyyg
cooked: <p>Have you found any solution?</p>

username: Soumya_Kundu
cooked: <aside class="quote no-group" data-username="jmandivarapu1" data-post="1" data-topic="75291">
<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/jmandivarapu1/48/3544_2.png" class="avatar"> jmandivarapu1:</div>
<blockquote>
<p>I want to calculate the mean,standard deviation and <strong>Covariance matrix</strong> of MNIST Dataset for only digits 1,2,3…etc separately. (<a href="http://www-inst.eecs.berkeley.edu/~cs70/sp15/notes/n21.pdf" rel="noopener nofollow ugc">http://www-inst.eecs.berkeley.edu/~cs70/sp15/notes/n21.pdf </a>) Page 12</p>
<p>I calculated the mean and SD using the below. I need covariance matrix tooo</p>
</blockquote>
</aside>
<p>This has the above and more.</p>
<p><a href="https://benmilanko.com/projects/mnist_with_pca/" class="onebox" target="_blank" rel="noopener nofollow ugc">https://benmilanko.com/projects/mnist_with_pca/</a></p>