# MNIST mean and std

**URL:** <https://community.deeplearning.ai/t/mnist-mean-and-std/883455>\
**Category:** PyTorch: Fundamentals\
**Tags:** dl-ai-learning-platform, week-module-2\
**Created:** [November 16, 2025, 1:59pm UTC](https://community.deeplearning.ai/t/mnist-mean-and-std/883455 "2025-11-16T13:59:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TAHMID\_TANZIM\_LUPIN](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tahmid_tanzim_lupin/32/54488_2.png) [@TAHMID\_TANZIM\_LUPIN](https://community.deeplearning.ai/u/TAHMID_TANZIM_LUPIN)\
**Post date:** [November 16, 2025, 1:59pm UTC](https://community.deeplearning.ai/t/mnist-mean-and-std/883455/1 "2025-11-16T13:59:51Z")

</div>

In C1 M2 Lab 1 ( **Building Your First Image Classifier** ), why are the values of MNIST mean and standard deviation hardcoded to 0.1307 and 0.3081 for transformation normalization? How to calculate the mean & standard deviation for the MNIST dataset to find out how these values are derived?

---

<div class="post-metadata">

**Author:** ![balaji.ambresh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/balaji.ambresh/32/128697_2.png) [@balaji.ambresh](https://community.deeplearning.ai/u/balaji.ambresh)\
**Post date:** [November 16, 2025, 4:12pm UTC](https://community.deeplearning.ai/t/mnist-mean-and-std/883455/3 "2025-11-16T16:12:05Z")

</div>

The values are the mean and standard deviation of the training split of MNIST.

```python
import torch
from torchvision import datasets, transforms

transform = transforms.Compose([transforms.ToTensor()])
trainset = datasets.MNIST('mnist_train', train=True, download=True, transform=transform)
images = torch.stack(list(data[0] for data in trainset), dim=0)

print(images.shape) # torch.Size([60000, 1, 28, 28])
print(images.mean()) # tensor(0.1307)
print(images.std()) # tensor(0.3081)

```

---

<div class="post-metadata">

**Author:** ![Nevermnd](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/nevermnd/32/126021_2.png) [@Nevermnd](https://community.deeplearning.ai/u/Nevermnd)\
**Post date:** [November 17, 2025, 4:43am UTC](https://community.deeplearning.ai/t/mnist-mean-and-std/883455/4 "2025-11-17T04:43:53Z")

</div>

@balaji.ambresh I actually had this question too, though I knew .mean() and .std() were functions of tensors, I wasn’t quite sure how to ‘get at’ the data.

Can you explain a little more what you have going on in this line:

> [@balaji.ambresh](#):
>
> ```auto
> images = torch.stack(list(data[0] for data in trainset), dim=0)
> 
> ```

Particularly the use of .stack ?

---

<div class="post-metadata">

**Author:** ![balaji.ambresh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/balaji.ambresh/32/128697_2.png) [@balaji.ambresh](https://community.deeplearning.ai/u/balaji.ambresh)\
**Post date:** [November 17, 2025, 5:20am UTC](https://community.deeplearning.ai/t/mnist-mean-and-std/883455/5 "2025-11-17T05:20:30Z")

</div>

[torch.stack](https://docs.pytorch.org/docs/stable/generated/torch.stack.html#torch.stack) concatenates a sequence of tensors across the specified dimension (`dim=0` does vertical concatenation). Python `list` is a sequence. Each row of `trainset` has image data as tensor in index 0. The entire training set has 60000 data points. Each image has shape `(1, 28, 28)`. Using [list constructor](https://docs.python.org/3/library/stdtypes.html#typesseq-list), the sequence was created for passing into torch.stack. One could use [list comprehension](https://www.w3schools.com/python/python_lists_comprehension.asp) for constructing the sequence as well.

---

<div class="post-metadata">

**Author:** ![Mubsi](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/mubsi/32/94983_2.png) [@Mubsi](https://community.deeplearning.ai/u/Mubsi)\
**Post date:** [January 18, 2026, 3:55pm UTC](https://community.deeplearning.ai/t/mnist-mean-and-std/883455/6 "2026-01-18T15:55:06Z")

</div>


