【问题标题】:TypeError: 'Tensor' object is not callableTypeError:“张量”对象不可调用
【发布时间】:2017-06-14 04:36:51
【问题描述】:

我正在尝试显示卷积神经网络每一层的输出。 我使用的后端是 TensorFlow。 代码如下:

import ....
from keras import backend as K

model = Sequential()

model.add(Convolution2D(32, 3, 3, input_shape = (1,28,28))) 
convout1 = Activation('relu')
model.add(convout1)

(X_train, y_train), (X_test, y_test) = mnist_dataset = mnist.load_data("mnist.pkl")
reshaped = X_train.reshape(X_train.shape[0], 1, X_train.shape[1], X_train.shape[2])


from random import randint
img_to_visualize = randint(0, len(X_train) - 1)


# Generate function to visualize first layer
# ERROR HERE
convout1_f = K.function([model.input(train=False)], convout1.get_output(train=False))   #ERROR HERE
convolutions = convout1_f(reshaped[img_to_visualize: img_to_visualize+1])

完整的错误是:

convout1_f = K.function([model.input(train=False)], convout1.get_output(train=False)) TypeError: 'Tensor' object is not 可调用

非常感谢任何评论或建议。谢谢。

【问题讨论】:

  • 我的答案正确吗?你试过了吗?

标签: machine-learning tensorflow neural-network keras conv-neural-network


【解决方案1】:

get_outputget_input 方法都返回 TheanoTensorFlow 张量。由于此对象的性质,它不可调用。

为了编译一个函数,你应该只提供层张量和一个名为learning_phase 的特殊 Keras 张量,它设置你的模型应该被调用的选项。

answer 之后,您的函数应如下所示:

convout1_f = K.function([model.input, K.learning_phase()], convout1.get_output)

请记住,调用函数时需要传递TrueFalse,以便在学习或训练阶段模式下进行模型计算。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 2020-12-15
    • 2018-12-25
    相关资源
    最近更新 更多