How to train a network with two outputs?

Hello, I want to get two outputs from my network after train,

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import matplotlib.pyplot as plt
from import SubsetRandomSampler
from import DataLoader
from torchvision import datasets, transforms

transform = transforms.ToTensor()
train_data = datasets.MNIST(root='data', train=True, download=True, transform=transform)
test_data = datasets.MNIST(root='data', train=False, download=True, transform=transform)
#Prepare data loaders
train_loader =, batch_size=32, num_workers=0)
test_loader =, batch_size=32, num_workers=0)
#Define the Convolutional Autoencoder
class ConvAutoencoder(nn.Module):
    def __init__(self):
        super(ConvAutoencoder, self).__init__()
        self.conv1 = nn.Conv2d(1, 16, 3, stride=2, padding=1)
        self.conv2 = nn.Conv2d(16, 8, 3, stride=2, padding=1)
        self.conv3 = nn.Conv2d(8,8,3)
        self.conv4 = nn.ConvTranspose2d(8, 8, 3)
        self.conv5 = nn.ConvTranspose2d(8, 16, 3, stride=2, padding=1, output_padding=1)
        self.conv6 = nn.ConvTranspose2d(16, 1, 3, stride=2, padding=1, output_padding=1)

    def forward(self, x):
        x = F.relu(self.conv1(x))      
        x = F.relu(self.conv2(x))
        x = F.relu(self.conv3(x))  

        x = F.relu(self.conv4(x))
        x1 = F.relu(self.conv5(x))
        x = F.relu(self.conv6(x1))
        #out = self.out(x)

        return x,x1

#Instantiate the model
model = ConvAutoencoder()
def train(model, num_epochs=20, batch_size=64, learning_rate=1e-3):
    criterion = nn.MSELoss() # mean square error loss
    optimizer = torch.optim.Adam(model.parameters(),
                                 weight_decay=1e-5) # <--
   # train_loader =train_loader;

    outputs = []
    for epoch in range(num_epochs):
        for data in train_loader:
            img, _ = data
            recon = model(img)
            loss = criterion(recon, img)

        print('Epoch:{}, Loss:{:.4f}'.format(epoch+1, float(loss)))
        outputs.append((epoch, img, recon),)
    return outputs

model =  ConvAutoencoder()
max_epochs =10
outputs = train(model, num_epochs=max_epochs)

but my code have this error:

AttributeError                            Traceback (most recent call last)
<ipython-input-30-a6c530203f22> in <module>()
      3 model =  ConvAutoencoder()
      4 max_epochs =10
----> 5 outputs = train(model, num_epochs=max_epochs)

3 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/ in mse_loss(input, target, size_average, reduce, reduction)
   3077             mse_loss, (input, target), input, target, size_average=size_average, reduce=reduce, reduction=reduction
   3078         )
-> 3079     if not (target.size() == input.size()):
   3080         warnings.warn(
   3081             "Using a target size ({}) that is different to the input size ({}). "

AttributeError: 'tuple' object has no attribute 'size'

please help me for solve this problem

please guide me for remove this error

Your model (its forward function) returns a tuple (x,x1) and not a tensor.
And below, you use this tuple to try to calculate the loss function, hence the error.

# ...
recon = model(img) # x, x2
loss = criterion(recon, img)
# ...

I don’t know what you want to achieve as a goal, but looking at it I think it’s x that you want to use in the loss function.

loss = criterion(recon[0], img)

thaks alot for your helping.

If I want to change output one layer and this change again feed to network, what to do?

by hook I give one output layer but I dont know how to replace changed output layer to network.