venkysmarty
venkysmarty

Reputation: 11441

'numpy.dtype' object has no attribute 'base_dtype' in keras

I have below code which I am trying to understand keras mean and want to get pooled_grads print. While printing I am getting below error

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)

I am getting below error

AttributeError: 'numpy.dtype' object has no attribute 'base_dtype'

Upvotes: 3

Views: 7591

Answers (1)

javidcf
javidcf

Reputation: 59731

Most Keras backend functions expect Keras tensors as inputs. If you want to use a NumPy array as input, convert it to a tensor first, for example with K.constant:

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

Note that pooled_grads here will be another tensor, so printing it will not give you the value directly, but just a reference to the tensor object. In order to get the value of the tensor, you can use for example K.get_value:

print(K.get_value(pooled_grads))
# 3.5

Upvotes: 4

Related Questions