Machine learning 使用Keras进行的特征提取不包括类标签

Machine learning 使用Keras进行的特征提取不包括类标签,machine-learning,keras,feature-extraction,convolutional-neural-network,Machine Learning,Keras,Feature Extraction,Convolutional Neural Network,我使用Keras在MNIST数据集上应用图像分类,实现是可用的。我使用此实现,但添加了以下方法来从经过训练的图像中提取特征: def feature_extraction(model, x_test, path_to_save): extract = keras.Model(inputs=model.input, outputs=model.get_layer('dense_1').output) features = extract.predict(x_test) np

我使用Keras在MNIST数据集上应用图像分类,实现是可用的。我使用此实现,但添加了以下方法来从经过训练的图像中提取特征:

def feature_extraction(model, x_test, path_to_save):
    extract = keras.Model(inputs=model.input, outputs=model.get_layer('dense_1').output)
    features = extract.predict(x_test)
    np.savetxt(path_to_save, features, delimiter=",")

输出文件不包含类标签,即文件中的每一行都是一组要素,但没有类标签。是否有办法修改此方法,使其同时添加每个提取特征(即每行)的类别标签?

您可以获得多个输出,以便特征和预测的标签概率:

extract = Model(model.input, [model.get_layer('dense_1').output, model.output])
features, labels = extract.predict(x_test)
labels = np.argmax(labels, axis=1)

预测现在将为您提供两个输出。您可能希望
np.argmax
获得实际的类标签,而不是取决于模型的概率。

非常感谢。如何使用
np.argmax
?请您在示例中也演示一下,好吗?很抱歉,您知道我为什么会得到
功能,labels=extract.predict(x_测试)ValueError:太多的值无法解包(预期为2)
?似乎
predict
不输出labelsCare进行调试?也许打印它,检查它返回什么,返回多少值,我还没有查看模型,所以我不确定。