Last active
April 11, 2018 04:39
-
-
Save gSrikar/13e93b926d6105dc9de9e2bf2dd694c8 to your computer and use it in GitHub Desktop.
Example that resolves the TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type int32 of argument 'x' or ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float64 in Tensorflow.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
node1 = tf.constant(5) # dtype is int32 | |
node2 = tf.constant(6.0) # dtype is float32 | |
print('Node 1: ', node1) # Node 1: Tensor("Const:0", shape=(), dtype=int32) | |
print('Node 2: ', node2) # Node 2: Tensor("Const_1:0", shape=(), dtype=float32) | |
# Multiplication | |
# Convert the node1 type to float32 before multiplying | |
# If you don't convert the types, you'll get TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type int32 of argument 'x' | |
node3 = tf.multiply(tf.to_float(node1, name='ToFloat'), node2) # dtype is float32 | |
# Convert the node2 type to int32 before multiplying | |
node4 = tf.multiply(node1, tf.to_int32(node2, name='ToInt32')) # dtype is int32 | |
print('Node3: Multiply: ', node3) # Node3: Multiply: Tensor("Mul:0", shape=(), dtype=float32) | |
print('Node4: Multiply: ', node4) # Node4: Multiply: Tensor("Mul_1:0", shape=(), dtype=int32) | |
# Run inside a session to get the values | |
with tf.Session() as sess: | |
node3Value = sess.run(node3) | |
node4Value = sess.run(node4) | |
print('Multiplication value at node 3: ', node3Value) # Multiplication value at node 3: 30.0 | |
print('Multiplication value at node 4: ', node4Value) # Multiplication value at node 4: 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment