ValueError: Tensor conversion requested dtype float32 for Tensor with dtype int32
Asked Answered
S

2

27

I get following error

ValueError: Tensor conversion requested dtype float32 for Tensor with dtype int32: 'Tensor("Placeholder_1:0", shape=TensorShape([Dimension(128), Dimension(2)]), dtype=int32)'

when I try to calculate cross entropy loss

losses = tf.nn.softmax_cross_entropy_with_logits(scores, input_y)

I use Python 3.4.3.

Any ideas why?

Subrogation answered 13/2, 2016 at 16:35 Comment(0)
V
32

It sounds like you have defined input_y—which I am assuming is a tf.placeholder()—as having type tf.int32. Either change this to tf.float32 or add a cast: tf.cast(input_y, tf.float32) or tf.to_float(input_y).

Varuna answered 13/2, 2016 at 16:54 Comment(3)
i can't find cast or to_float in tflearn, which module do we have to import?Soubise
tf.cast() and tf.to_float() are TensorFlow functions, so you'd get them using import tensorflow as tf.Varuna
you can also define the datatype while assignment dtype=tf.float32Coarse
C
1

I replaced %tensorflow_version 1.x in my code with !pip install tensorflow==1.15.5 and the error disappeared. Not limited to just version 1.15.5, it works for some other TensorFlow version 1s as well.

Capitation answered 11/4, 2021 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.