0

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 tfimport math as mdef 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!!**

askedJan 14, 2021 at 10:53
Natalia Resende's user avatar

1 Answer1

1

Specify in your array creation that you want to usefloat32 instead ofint64. Numpy defaults to the 64 bits types, but TensorFlow usesfloat32 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]
answeredJan 14, 2021 at 11:28
Lescurel's user avatar
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.