In [10]:
import tensorflow as tf

with tf.Session() as sess:
    add = sess.run( tf.add(4, 40) )
    print( f'Add : {add}' )
    
    subtract = sess.run( tf.subtract(add, 2 ) )
    print( f'Subtract: {subtract}' )
    
    mult = sess.run( tf.multiply(subtract, 3) )
    print( f'Multiply: {mult}' )


Add : 44
Subtract: 42
Multiply: 126

both side of the operation should be of the same type


In [11]:
x = tf.constant(4.0)
y = tf.constant(2)

with tf.Session() as sess:
    badOperation = sess.run( tf.multiply(x, y))
    print( 'Never here' )


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Users/darienmt/miniconda3/envs/IntroToTensorFlow/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in apply_op(self, op_type_name, name, **keywords)
    490                 as_ref=input_arg.is_ref,
--> 491                 preferred_dtype=default_dtype)
    492           except TypeError as err:

/Users/darienmt/miniconda3/envs/IntroToTensorFlow/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype)
    715         if ret is None:
--> 716           ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
    717 

/Users/darienmt/miniconda3/envs/IntroToTensorFlow/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _TensorTensorConversionFunction(t, dtype, name, as_ref)
    588         "Tensor conversion requested dtype %s for Tensor with dtype %s: %r"
--> 589         % (dtype.name, t.dtype.name, str(t)))
    590   return t

ValueError: Tensor conversion requested dtype float32 for Tensor with dtype int32: 'Tensor("Const_1:0", shape=(), dtype=int32)'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-11-d43fc9661d0f> in <module>()
      3 
      4 with tf.Session() as sess:
----> 5     badOperation = sess.run( tf.multiply(x, y))
      6     print( 'Never here' )

/Users/darienmt/miniconda3/envs/IntroToTensorFlow/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py in multiply(x, y, name)
    355 
    356 def multiply(x, y, name=None):
--> 357   return gen_math_ops._mul(x, y, name)
    358 multiply.__doc__ = gen_math_ops._mul.__doc__.replace("Mul", "`tf.multiply`")
    359 

/Users/darienmt/miniconda3/envs/IntroToTensorFlow/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py in _mul(x, y, name)
   1623     A `Tensor`. Has the same type as `x`.
   1624   """
-> 1625   result = _op_def_lib.apply_op("Mul", x=x, y=y, name=name)
   1626   return result
   1627 

/Users/darienmt/miniconda3/envs/IntroToTensorFlow/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in apply_op(self, op_type_name, name, **keywords)
    520                   "%s type %s of argument '%s'." %
    521                   (prefix, dtypes.as_dtype(attrs[input_arg.type_attr]).name,
--> 522                    inferred_from[input_arg.type_attr]))
    523 
    524           types = [values.dtype]

TypeError: Input 'y' of 'Mul' Op has type int32 that does not match type float32 of argument 'x'.

cast operation should be applied


In [12]:
with tf.Session() as sess:
    notSobadOperation = sess.run( tf.multiply( tf.cast(x, tf.int32), y))
    print( f'Everything ok : {notSobadOperation} ' )


Everything ok : 8 

In [ ]: