我有以下代码,我试图理解 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/