【问题标题】:Printing tensor value output of model.output in keras在keras中打印model.output的张量值输出
【发布时间】:2020-01-17 14:39:35
【问题描述】:

所以我正在尝试打印预训练 VGG16 模型的 model.output(张量)的值,但不幸的是,下面列出的所有方法都不适用于我的情况,这是我从之前的回答中发现的类似的问题。

1)首先基于this帖子我尝试使用

打印(K.eval(model.output()))

但它引发了错误

TypeError: 'Tensor' 对象不可调用

2) 然后在浏览this 帖子后,我尝试使用

K.print_tensor(model.output, message='model.output = ')

approach 但这次没有输出输出。

这是我的代码:

from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.models import Model
import keras.backend as K
from matplotlib import pyplot
from numpy import expand_dims
import numpy as np


# load the model
model = VGG16()

img = load_img('../input/treebird/bird.jpg', target_size=(224, 224))
img = img_to_array(img)           
img = expand_dims(img, axis=0)      
img = preprocess_input(img)       

prediction = model.predict(img)

print(model.output)                                 #Tensor("predictions_19/Softmax:0",       shape=(?, 1000), dtype=float32)
print(K.eval(model.output()))                          # throws TypeError: 'Tensor' object is not callable
K.print_tensor(model.output, message='model.output = ')

我在上述方法的实现中是否缺少某些地方,或者在这种情况下我应该使用其他方法来打印张量?

【问题讨论】:

    标签: python-3.x tensorflow keras deep-learning conv-neural-network


    【解决方案1】:

    只发布代码的相关部分。

    首先,您必须清除会话,以便模型中的输入节点每次都具有相同的名称。使用model.summary() 知道输入节点名称是什么。

    然后,您将不得不使用feed_dict 来传递张量,如代码所示。

    K.clear_session()
    
    # load the model
    model = VGG16()
    model.summary()
    
    print(model.output.eval(session=K.get_session(), feed_dict={'input_1:0': img}))   
    

    【讨论】:

      猜你喜欢
      • 2021-12-16
      • 2017-09-12
      • 1970-01-01
      • 2017-11-10
      • 2020-01-08
      • 1970-01-01
      • 2019-07-28
      • 2017-09-23
      相关资源
      最近更新 更多