Search code examples
python-2.7tensorflowneural-networkdeeplearning4j

Data Type not understood in tensorflow


mean , variance = tf.nn.moments(X_train, axes = 1, keep_dims = True)

I am trying to get the mean and variance using tf.nn.moments() as shown above. However, I am encountering the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-43-fc383f99b15b> in <module>()
     33 Y_train = Y_train.reshape(1,355)
     34 X_mean = tf.reduce_mean(X_train, axis = 1, keepdims = True)
---> 35 mean , variance = tf.nn.moments(X_train, axes = 1, keep_dims = True)
     36 X_train = tf.divide(tf.subtract(X_train,mean),tf.sqrt(variance))
     37 #Y_train = Y_train/(Y_train.max(axis = 1, keepdims = True))

/Users/abhinandanchiney/anaconda2/lib/python2.7/site-      packages/tensorflow/python/ops/nn_impl.pyc in moments(x, axes, shift, name, keep_dims)
    664     # sufficient statistics. As a workaround we simply perform the operations
    665     # on 32-bit floats before converting the mean and variance back to fp16
--> 666     y = math_ops.cast(x, dtypes.float32) if x.dtype == dtypes.float16 else x
    667     # Compute true mean while keeping the dims for proper broadcasting.
    668     mean = math_ops.reduce_mean(y, axes, keepdims=True, name="mean")

 TypeError: data type not understood

Kindly help where I am going wrong.


Solution

  • tf.nn.moments is expecting a tensor, not a numpy array:

    Args:

    • x: A Tensor.

    Try this:

    x = tf.convert_to_tensor(X_train)
    mean , variance = tf.nn.moments(x, axes = 1, keep_dims = True)