How to get data in test phase to changed model

students: <p>hello, I can not in phase test after changing one layer, to giving data for changed model and taking the images in output of test phase. please guide me for get image to changed model and take outputs…<br>
my code:</p>
<pre><code class="lang-auto">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

#Converting data to torch.FloatTensor
transform = transforms.ToTensor()

# Download the training and test datasets
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))
        x = F.relu(self.conv5(x))
        x = F.relu(self.conv6(x))

        return x

#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)

for k in range(0, max_epochs, 9):
    plt.figure(figsize=(9, 2))
    imgs = outputs[k][1].detach().numpy()
    recon = outputs[k][2].detach().numpy()
    for i, item in enumerate(imgs):
        if i &gt;= 9: break
        plt.subplot(2, 9, i+1)
        plt.imshow(item[0])
        
    for i, item in enumerate(recon):
        if i &gt;= 9: break
        plt.subplot(2, 9, 9+i+1)
        plt.imshow(item[0])


a=(ConvAutoencoder().conv3.weight)
a0=a[:,0,:,:]
a1=a[:,1,:,:]
a2=a[:,2,:,:]
a3=a[:,3,:,:]
a4=a[:,4,:,:]
a5=a[:,5,:,:]
a6=a[:,6,:,:]
a7=a[:,7,:,:]
a0=a1
a1=a2
a2=a3
a3=a4
a4=a5
a5=a6
a6=a7
a7=a0

a = torch.cat((a0, a1, a2, a3, a4, a5, a6, a7))
               
model = ConvAutoencoder()
a = a.reshape(8, 8, 3, 3)

model.conv3.weight = nn.Parameter(a)

print(model.conv3.weight)

#test phase

def test(model,test_loader):

    with torch.no_grad():
     for data in test_loader:
      output = model(data)
    return output 
output.view(1, 28, 28)         
</code></pre>
<p>error in test phase:</p>
<pre><code class="lang-auto">
NameError                                 Traceback (most recent call last)
&lt;ipython-input-23-b8f79c214741&gt; in &lt;module&gt;()
     25   #      plt.imshow(item)
     26 
---&gt; 27 output.view(1, 28, 28)

NameError: name 'output' is not defined</code></pre>

ptrblck: <p>The <code>output</code> variable is undefined in the global scope, so you won’t be able to use it there.<br>
Did you mean to use <code>outputs</code> instead?</p>

students: <p>yes… but I dont how to change my code in test phase.</p>