Tensorflow 如何提取隐藏向量(第三个编码器层之后的ReLU输出)作为图像表示
我正在使用时尚Mnsit数据集实现一个自动编码器。编码器的代码-Tensorflow 如何提取隐藏向量(第三个编码器层之后的ReLU输出)作为图像表示,tensorflow,keras,deep-learning,tensorflow2.0,autoencoder,Tensorflow,Keras,Deep Learning,Tensorflow2.0,Autoencoder,我正在使用时尚Mnsit数据集实现一个自动编码器。编码器的代码- class MNISTClassifier(Model): def __init__(self): super(MNISTClassifier, self).__init__() self.encoder = Sequential([ layers.Dense(128, activation = "relu"), layer
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()
现在我想根据从上述自动编码器中提取的图像表示来训练SVM分类器
一旦上述完全连接的自动编码器被训练,对于每个图像,我想提取32-
维度隐藏向量(第三个编码器层之后的ReLU输出)作为
图像表示,然后在32帧的基础上对时装mnist的训练图像进行线性SVM分类器的训练-
尺寸特征
如何提取输出32-
维隐向量
提前谢谢 我建议使用函数API来定义模型的多个输出,因为代码更清晰。但是,您可以通过获得所需任何层的输出并添加到模型的输出中,使用顺序模型来实现这一点 打印您的
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)
您可以使用enc\u outputs=classifier.encoder.predict(images)
其中classifier
是MNISTClassifier
的一个实例。如果我想要从任何中间层(如64个神经元层)输出,那么该怎么办