Neural network 可视化自动编码器输出

Neural network 可视化自动编码器输出,neural-network,pytorch,data-visualization,autoencoder,Neural Network,Pytorch,Data Visualization,Autoencoder,我有一个很好的问题,但我被卡住了。。。 我使用Pytorch创建了一个自动编码器,并使用典型的MNIST数据集对其进行了培训,等等: class Autoencoder(nn.Module): def __init__(self, **kwargs): super().__init__() self.encoder_hidden_layer = nn.Linear( in_features=kwargs["input_sh

我有一个很好的问题,但我被卡住了。。。 我使用Pytorch创建了一个自动编码器,并使用典型的MNIST数据集对其进行了培训,等等:

class Autoencoder(nn.Module):
    def __init__(self, **kwargs):
        super().__init__()
        self.encoder_hidden_layer = nn.Linear(
            in_features=kwargs["input_shape"], out_features=kwargs["embedding_dim"]
        )
        self.encoder_output_layer = nn.Linear(
            in_features=kwargs["embedding_dim"], out_features=kwargs["embedding_dim"]
        )
        self.decoder_hidden_layer = nn.Linear(
            in_features=kwargs["embedding_dim"], out_features=kwargs["embedding_dim"]
        )
        self.decoder_output_layer = nn.Linear(
            in_features=kwargs["embedding_dim"], out_features=kwargs["input_shape"]
        )

    def forward(self, features):
        activation = self.encoder_hidden_layer(features)
        activation = torch.relu(activation)

        code = self.encoder_output_layer(activation)
        code = torch.relu(code)
        
        activation = self.decoder_hidden_layer(code)
        activation = torch.relu(activation)

        activation = self.decoder_output_layer(activation)
        reconstructed = torch.relu(activation)

        return reconstructed

model = Autoencoder(input_shape=784, embedding_dim=128)

criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.0001) 
我现在想要的是可视化重建的图像,但我不知道怎么做。我知道这很简单,但我找不到办法。我知道输出的形状是
[128784]
,因为批大小是128,784是28x28(x1通道)

谁能告诉我如何从重建的张量中得到图像


非常感谢你

首先,必须将张量广播到
128x28x28

reconstructed = x.reshape(128, 1, 28, 28)
然后,可以使用的函数将其中一个批处理元素转换为图像。以下将显示第一幅图像:

import torchvision.transforms as T
img = T.ToPILImage()(reconstructed[0])
img.show()

首先,必须将张量广播到
128x28x28

reconstructed = x.reshape(128, 1, 28, 28)
然后,可以使用的函数将其中一个批处理元素转换为图像。以下将显示第一幅图像:

import torchvision.transforms as T
img = T.ToPILImage()(reconstructed[0])
img.show()

非常感谢你!很容易!现在还有一个问题:如何在Jupyter笔记本中显示图像?因为它是在我电脑的应用程序中打开的。我如何每次都显示相同的示例?我的意思是,对于每一个时代,看看自动编码器在同一幅图像上是如何改进的。非常感谢你!!这将使您可以在Jupyter中预览您的图像。您可以在每个历元的数组中暂停第一批的第一个图像。最后,您将能够显示该数组的每个元素。再次感谢您!你太棒了!非常感谢你!很容易!现在还有一个问题:如何在Jupyter笔记本中显示图像?因为它是在我电脑的应用程序中打开的。我如何每次都显示相同的示例?我的意思是,对于每一个时代,看看自动编码器在同一幅图像上是如何改进的。非常感谢你!!这将使您可以在Jupyter中预览您的图像。您可以在每个历元的数组中暂停第一批的第一个图像。最后,您将能够显示该数组的每个元素。再次感谢您!你太棒了!