tensorflow - 'numpy.dtype' 对象在 keras 中没有属性 'base_dtype'

标签 tensorflow keras

我有以下代码,我试图理解 keras 的意思并希望获得 pooled_grads 打印。打印时出现以下错误

import numpy as np
import tensorflow as tf

arr3 = np.array([ [
                   [1,2,3],
                   [4,5,6]
                 ],
                 [
                   [1,2,3],
                   [4,5,6]
                 ],
                 [
                   [1,2,3],
                   [4,5,6]
                 ]
                ] 
               )

#print("Arr shape", arr3.shape)

import keras.backend as K

import numpy as np

pooled_grads = K.mean(arr3, axis=(0, 1, 2))

print("------------------------")

print(pooled_grads)

我遇到了错误

AttributeError: 'numpy.dtype' 对象没有属性 'base_dtype'

最佳答案

大多数 Keras 后端函数都期望 Keras 张量作为输入。如果您想使用 NumPy 数组作为输入,请先将其转换为张量,例如使用 K.constant:

pooled_grads = K.mean(K.constant(arr3), axis=(0, 1, 2))

请注意,这里的 pooled_grads 将是另一个张量,因此打印它不会直接为您提供值,而只是对张量对象的引用。为了获取张量的值,您可以使用例如K.get_value:

print(K.get_value(pooled_grads))
# 3.5

关于tensorflow - 'numpy.dtype' 对象在 keras 中没有属性 'base_dtype',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57555407/

相关文章:

c++ - 将tensorflow与另一个库一起使用时出现段错误,两者都链接到eigen3

python - 使用 Debian 8 VirtualBox 安装 Tensorflow 的 Anaconda 失败

keras - 如何在预训练的 ELMO 嵌入中获得相似的词?

python - 格式化图像数据以预测 MNIST 数据模型图像中的数字

tensorflow 自定义循环不会在第一个纪元结束并且进度条运行到无限

keras - 图像分割 - Keras 中的自定义损失函数

tensorflow - Keras 中的神经网络具有两种不同的输入类型 - 图像和值

tensorflow - 如何根据 tensorflow 中的某些谓词从队列中过滤张量?

tensorflow - 此 TensorFlow 示例中的滤镜图像数据在哪里?

python - Keras 如何读取输入数据?