【发布时间】:2021-03-18 03:25:21
【问题描述】:
我提出了一个非常菜鸟的问题,但我被困住了...... 我用 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)
我现在想要的是可视化重建的图像,但我不知道该怎么做。我知道这很简单,但我找不到方法。我知道输出的形状是[128,784],因为batch_size 是128,而784 是28x28(x1channel)。
谁能告诉我如何从重建的张量中获取图像?
非常感谢!
【问题讨论】:
标签: neural-network pytorch data-visualization autoencoder