Use PI (π) in TensorFlow: A Beginner Guide

By | December 30, 2019

There is not a pre-defined constant tensor called: tf.pi. If you want to use PI (π) in tensorflow, you will find answer in this tutorial.

Preliminary

import math

There is a math.pi in python math package. You can use it in your tensorflow application.

For example:

import tensorflow as tf
import numpy as np
import math

w1 = tf.Variable(np.array([[1,2,3,4,5],[6,7,8,9,0]]), dtype = tf.float32, name = 'w1')
w2 = math.pi * w1   

init = tf.global_variables_initializer() 
init_local = tf.local_variables_initializer()

with tf.Session() as sess:
    sess.run([init, init_local])
    print(sess.run([w2]))

Run this tensorflow script, you will get result:

[array([[ 3.1415927,  6.2831855,  9.424778 , 12.566371 , 15.707964 ],
       [18.849556 , 21.99115  , 25.132742 , 28.274334 ,  0.       ]],
      dtype=float32)]

It means we have used PI (π) in tensorflow successfully.

Leave a Reply