How to train a network with two outputs?

username: students
cooked: <p>Hello, I want to get two outputs from my network after train,</p>
<pre><code class="lang-auto">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 torch.utils.data.sampler import SubsetRandomSampler
from torch.utils.data 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 = torch.utils.data.DataLoader(train_data, batch_size=32, num_workers=0)
test_loader = torch.utils.data.DataLoader(test_data, batch_size=32, num_workers=0)
#Define the Convolutional Autoencoder
class ConvAutoencoder(nn.Module):
    def __init__(self):
        super(ConvAutoencoder, self).__init__()
       
        #Encoder
        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)
    
        #Decoder
        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()
print(model)
def train(model, num_epochs=20, batch_size=64, learning_rate=1e-3):
    torch.manual_seed(42)
    criterion = nn.MSELoss() # mean square error loss
    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=learning_rate, 
                                 weight_decay=1e-5) # &lt;--
   # 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)
            loss.backward()
            optimizer.step()
            optimizer.zero_grad()

        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)
</code></pre>
<p>but my code have this error:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
&lt;ipython-input-30-a6c530203f22&gt; in &lt;module&gt;()
      3 model =  ConvAutoencoder()
      4 max_epochs =10
----&gt; 5 outputs = train(model, num_epochs=max_epochs)

3 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py 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         )
-&gt; 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'
</code></pre>
<p>please help me for solve this problem</p>

username: students
cooked: <p>please guide me for remove this error</p>

username: pascal_notsawo
cooked: <p>Your model (its forward function) returns a tuple (x,x1) and not a tensor.<br>
And below, you use this tuple to try to calculate the loss function, hence the error.</p>
<pre><code class="lang-auto"># ...
recon = model(img) # x, x2
loss = criterion(recon, img)
# ...
</code></pre>
<p>I don’t know what you want to achieve as a goal, but looking at it I think it’s <code>x</code> that you want to use in the loss function.</p>
<pre><code class="lang-auto">loss = criterion(recon[0], img)
</code></pre>

username: students
cooked: <p>thaks alot for your helping.</p>

username: students
cooked: <p>If I want to change output one layer and this change again feed to network, what to do?</p>
<p>by hook I give one output layer but I dont know how to replace changed output layer to network.</p>