Reputation: 195
I wrote the below code in tensorflow which is a function that I will use to make some calculations based on the input X values
import tensorflow as tf
import math as m
def tf_fn(x):
pi = tf.constant(m.pi)
miu=0.0
o=1.0
f1=1/(tf.sqrt(2*o**2*pi))
f2= tf.exp(-((x - miu)**2)/ 2*o**2)
f3= f1 * f2
return (f3)
x = np.array([0,1,2,3])
tf.print(tf_fn(x))
When I try to print this I get the following error:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-70-fba9acd28831> in <module>()
1 x = np.array([0,1,2,3])
----> 2 tf.print(tf_fn(x))
7 frames
/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)
InvalidArgumentError: cannot compute Mul as input #1(zero-based) was expected to be a float
tensor but is a double tensor [Op:Mul]
My expected output is: [ 0.39894228, 0.24197072, 0.05399097, 0.00443185] I know that the problem is the numpy array that needs to be converted into a tensor. How to do that to get my expected output? Many thanks!!**
Upvotes: 0
Views: 206
Reputation: 11651
Specify in your array creation that you want to use float32
instead of int64
. Numpy defaults to the 64 bits types, but TensorFlow uses float32
for most calculations.
x = np.array([0,1,2,3], np.float32)
should solve your problem.
>>> x = np.array([0,1,2,3], np.float32)
>>> tf_fn(x)
[0.398942292 0.241970733 0.0539909676 0.00443184841]
Upvotes: 1