我正在使用时装Mnsit数据集实现一个自动编码器。编码器的代码-
class MNISTClassifier(Model):
def __init__(self):
super(MNISTClassifier, self).__init__()
self.encoder = Sequential([
layers.Dense(128, activation = "relu"),
layers.Dense(64, activation = "relu"),
layers.Dense(32, activation = "relu")
])
self.decoder = Sequential([
layers.Dense(64, activation = "relu"),
layers.Dense(128, activation= "relu"),
layers.Dense(784, activation= "relu")
])
def call(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded
autoencoder = MNISTClassifier()现在,我想对从上述自动编码器中提取的图像表示进行支持向量机分类器的训练,即一旦对上述完全连接的自动编码器进行训练,对于每一幅图像,我希望提取32维隐藏向量(第三层后的ReLU输出)作为图像表示,然后根据32维特征对时装界mnist的训练图像进行线性支持向量机分类器的训练。
如何提取输出32维隐向量??
预先多谢
发布于 2021-06-02 11:42:03
我建议使用Functional 来定义模型的多个输出,因为代码更加清晰。但是,您可以通过获取您想要的任何层的输出并添加到您的模型的输出来使用顺序模型。
打印您的model.summary()并检查您的层,以找到您想要分支的层。您可以使用model.layers[index].output通过其索引访问每个层的输出。
然后,您可以创建您想要的层的多输出模型,如下所示:
third_layer = model.layers[2]
last_layer = model.layers[-1]
my_model = Model(inputs=model.input, outputs=(third_layer.output, last_layer.output))然后,您可以访问您定义的两个层的输出:
third_layer_predict, last_layer_predict = my_model.predict(X_test)https://stackoverflow.com/questions/67770595
复制相似问题