anamar
anamar

Reputation: 679

ValueError: Tensor conversion requested dtype float32 for Tensor with dtype int32

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?

Upvotes: 28

Views: 42688

Answers (2)

RonithSaju
RonithSaju

Reputation: 11

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.

Upvotes: 1

mrry
mrry

Reputation: 126184

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).

Upvotes: 33

Related Questions