【问题标题】:Cannot convert a symbolic Tensor to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported无法将符号张量转换为 numpy 数组。此错误可能表明您正在尝试将张量传递给 NumPy 调用,这是不受支持的
【发布时间】:2021-03-30 02:12:06
【问题描述】:

tensorflow 2.3.1 版 numpy 1.19.5 版 keras 2.4.3 版

我想看看keras模型的中间层。

train_data = tf.constant([[0, 0], [0, 1], [1, 0], [1, 1]])
train_labels = tf.constant([[0], [0], [0], [1]])
input_tensor = Input(shape=2)
x1 = Dense(10, kernel_initializer=initializer)(input_tensor)
x2 = Activation('sigmoid')(x1)
output_tensor = layers.Dense(1, activation='sigmoid', kernel_initializer=initializer)(x2)
model = Model(inputs=input_tensor, outputs=output_tensor)
model.summary()
model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['acc'])
print('weight', model.get_weights())
history = model.fit(train_data, train_labels, epochs=800)

#error
test = tf.constant(x2)
print(x2)

我们得到了

NotImplementedError:无法将符号张量 (activation/Sigmoid:0) 转换为 numpy 数组。此错误可能表明您正在尝试将张量传递给 NumPy 调用,这是不受支持的

更改numpy的版本并不能解决问题。

【问题讨论】:

  • 这不是numpy 的问题!你知道symbolic Tensor 是什么吗?如果没有,您可能需要进一步研究tensorflow 基础知识。此外,当询问错误时,请帮我们一个忙并显示错误发生的位置。至少代码中的那一行,更好的是整个回溯。我们不必猜测。

标签: numpy tensorflow keras


【解决方案1】:

x2 是一个 keras 张量,编辑错误部分以打印其值

#error part edited

import tensorflow.keras.backend as K
x2 = K.print_tensor(x2, message='x2 = ')
print(x2)

附上gist供参考

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 2022-01-10
    • 2021-01-02
    • 2020-02-17
    相关资源
    最近更新 更多