# Denoising Autoencoder

**URL:** <https://discuss.pytorch.org/t/denoising-autoencoder/71924>\
**Category:** Uncategorized\
**Created:** [March 4, 2020, 12:55am UTC](https://discuss.pytorch.org/t/denoising-autoencoder/71924 "2020-03-04T00:55:03Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedram1](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pedram1/32/21032_2.png) [@pedram1](https://discuss.pytorch.org/u/pedram1)\
**Post date:** [March 4, 2020, 12:55am UTC](https://discuss.pytorch.org/t/denoising-autoencoder/71924/1 "2020-03-04T00:55:03Z")

</div>

Hi everyone  
I have a stupid question,  
Is anyone knows that what should be the form of loss function in an Denoising Autoencoder?  
should it be like below?;

loss = criterion (model (noisy\_data),noise\_less\_data)  
basically model (noisy\_data) is the model will be trained with inputs that are corrupted data and loss function calculates the difference (here MSE) between output of the model and the data that are not noisy ?

In this way that makes no sense to me. because if we already have access to noiseless data, then what’s the point of building up a denoising autoencoder?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [March 4, 2020, 1:30am UTC](https://discuss.pytorch.org/t/denoising-autoencoder/71924/2 "2020-03-04T01:30:59Z")

</div>

The goal would be to train the model to be able to denoise new data.  
The same question might apply to why we would like to train a model to classify dogs and cats, if we already have the labels. 😉

---

<div class="post-metadata">

**Author:** ![pedram1](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pedram1/32/21032_2.png) [@pedram1](https://discuss.pytorch.org/u/pedram1)\
**Post date:** [March 4, 2020, 4:19am UTC](https://discuss.pytorch.org/t/denoising-autoencoder/71924/3 "2020-03-04T04:19:14Z")

</div>

That totally makes sense. 🙂  
One more question:  
Do u if there is any method (model) that can be do in an unsupervised way? I mean without seeing noiseless data, to Denise noisy data?  
Tnx

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [March 4, 2020, 4:37am UTC](https://discuss.pytorch.org/t/denoising-autoencoder/71924/4 "2020-03-04T04:37:48Z")

</div>

This is most likely not the state of the art technique anymore, but have a look at [Deep Image Prior](https://github.com/DmitryUlyanov/deep-image-prior).

---

<div class="post-metadata">

**Author:** ![pedram1](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pedram1/32/21032_2.png) [@pedram1](https://discuss.pytorch.org/u/pedram1)\
**Post date:** [March 4, 2020, 3:55pm UTC](https://discuss.pytorch.org/t/denoising-autoencoder/71924/5 "2020-03-04T15:55:06Z")

</div>

I have one more question,about that:

For both noiseless data as well as noisy input of the NN, I used dataloader (I have two data loader for each) Now I am a bit of worry because the shuffle option is set to True in both data loader. Do you think this might affect my training because I should compare same pixel from the noisy data with the same pixel from the noiseless data …

---

<div class="post-metadata">

**Author:** ![simaiden](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/simaiden/32/21043_2.png) [@simaiden](https://discuss.pytorch.org/u/simaiden)\
**Post date:** [March 4, 2020, 5:18pm UTC](https://discuss.pytorch.org/t/denoising-autoencoder/71924/6 "2020-03-04T17:18:27Z")

</div>

You should use one dataloader instead of two, and get the noise and noiseless data at the same time

---

<div class="post-metadata">

**Author:** ![pedram1](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pedram1/32/21032_2.png) [@pedram1](https://discuss.pytorch.org/u/pedram1)\
**Post date:** [March 4, 2020, 6:33pm UTC](https://discuss.pytorch.org/t/denoising-autoencoder/71924/7 "2020-03-04T18:33:42Z")

</div>

Can you please give me a clue ?  
below u can see my DataLoader codes:

class CustomDataset(torch.utils.data.Dataset):  
def **init** (self, data):  
self.data = data

```
def __getitem__ (self, index):
    return self.data[index]

def __len__ (self):
    return len(self.data)

```

x = data\_ll  
separation = int(x.shape[0] \* 0.8)  
train = x[:separation]  
test = x[separation:]

train\_dataset = CustomDataset(train)  
test\_dataset = CustomDataset(test)

train\_loader\_ll = torch.utils.data.DataLoader(train\_dataset, batch\_size=2621,shuffle = False)  
test\_loader\_ll = torch.utils.data.DataLoader(test\_dataset, batch\_size=655,shuffle = False)  
print ("//////////////////////////Training dataset//////////////////////////")  
for train\_sample in train\_loader\_ll:  
print(train\_sample.shape)

print ("//////////////////////////Test dataset//////////////////////////")

for test\_sample in test\_loader\_ll:  
print(test\_sample.shape)

Basically I use this twice. One for noisy data and the other for noiseless data.

---

<div class="post-metadata">

**Author:** ![simaiden](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/simaiden/32/21043_2.png) [@simaiden](https://discuss.pytorch.org/u/simaiden)\
**Post date:** [March 4, 2020, 6:41pm UTC](https://discuss.pytorch.org/t/denoising-autoencoder/71924/8 "2020-03-04T18:41:02Z")

</div>

What it’s the format of your data? I mean `x` have both noise and noise less data or only one of them?

---

<div class="post-metadata">

**Author:** ![pedram1](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pedram1/32/21032_2.png) [@pedram1](https://discuss.pytorch.org/u/pedram1)\
**Post date:** [March 4, 2020, 6:52pm UTC](https://discuss.pytorch.org/t/denoising-autoencoder/71924/9 "2020-03-04T18:52:38Z")

</div>

x here is a tensor of the size (65536,1,92) and it is just noisy data.  
I made one another class one trainload for the noiseless data.

---

<div class="post-metadata">

**Author:** ![Abhi](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/abhi/32/29120_2.png) [@Abhi](https://discuss.pytorch.org/u/Abhi)\
**Post date:** [September 24, 2020, 6:54am UTC](https://discuss.pytorch.org/t/denoising-autoencoder/71924/10 "2020-09-24T06:54:21Z")

</div>

I wish to build a Denoising autoencoder I just use a small definition from another PyTorch thread to add noise in the MNIST dataset. While training my model gives identical loss results. please tell me what I am doing wrong.

def add\_noise(inputs):

```
noise = torch.randn_like(inputs)*0.3
return inputs + noise 

```

 ![Screenshot_120](https://discuss.pytorch.org/uploads/default/original/3X/c/a/ca08daa1ebaf40d1d3bed8c078e579834d2893f9.png)

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [September 25, 2020, 8:39am UTC](https://discuss.pytorch.org/t/denoising-autoencoder/71924/11 "2020-09-25T08:39:31Z")

</div>

Could you post the model definition so that we could take a look, if you are accidentally detaching the computation graph?

---

<div class="post-metadata">

**Author:** ![Abhi](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/abhi/32/29120_2.png) [@Abhi](https://discuss.pytorch.org/u/Abhi)\
**Post date:** [September 25, 2020, 9:08am UTC](https://discuss.pytorch.org/t/denoising-autoencoder/71924/12 "2020-09-25T09:08:58Z")

</div>

Thankyou @ptrblck for the reply. I am posting my model. and please tell me what is wrong

 ![Screenshot_124](https://discuss.pytorch.org/uploads/default/original/3X/5/5/554e847d60f4a3902fc0e5c6d7453adb51da0bee.png)

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [September 25, 2020, 4:56pm UTC](https://discuss.pytorch.org/t/denoising-autoencoder/71924/13 "2020-09-25T16:56:03Z")

</div>

The model looks generally alright, but I’m not sure if you really want to apply the last `F.relu` on the output. As a debugging step you could remove it and rerun your training.

PS: you can post code snippets by wrapping them into three backticks ```, which would allow to copy and debug your code 😉
