Tensorflow 如何从keras中的张量对象中获得输出?

Tensorflow 如何从keras中的张量对象中获得输出?,tensorflow,neural-network,theano,keras,conv-neural-network,Tensorflow,Neural Network,Theano,Keras,Conv Neural Network,我正在使用Keras预先训练好的VGG16模型,我想可视化每一层的输出。但是,layer.output返回一个张量对象-如何将其转换为允许我获得图像输出的东西 model = VGG16(weights='imagenet', include_top=True) img_path = 'elephant.jpg' img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.ex

我正在使用Keras预先训练好的VGG16模型,我想可视化每一层的输出。但是,layer.output返回一个张量对象-如何将其转换为允许我获得图像输出的东西

model = VGG16(weights='imagenet', include_top=True)
img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

features = model.predict(x)

layer1 = model.layers[1] #I want the output of the second layer
layer1.output  #returns a tensor object
此外,当我尝试访问特定节点的输出时,它会返回一个张量:

layer1.get_output_at(0)

非常感谢您的帮助。谢谢。

您需要评估张量,最好是将模型配置为在运行predict时返回张量

e、 g


另外,请查看这篇博客文章,其中介绍了一个可能与您尝试的类似的练习:

检查非常感谢您的回答-我只需将input=input(shape=(224224,3))更改为input=model.input,否则会引发错误。希望你不介意我在你的回答中也改变这一点。再次感谢:)
layer_outputs = [layer.output for layer in model.layers]
viz_model = Model(input=model.input, output=layer_outputs)
...
features = viz_model.predict(x)
for feature_map in features:
   ...