import math
import tensorflow as tf
defsqrt(a, lr=0.01, epoches=2000):
g = tf.Graph()with g.as_default():
x = tf.get_variable('x',[], tf.float32)# x = 1.0# y = (x**2 - a) ** 2
y = tf.abs(x**2- a)
opt = tf.train.GradientDescentOptimizer(lr)
train_op = opt.minimize(y)with tf.Session(graph=g)as session:
session.run(tf.global_variables_initializer())for _ inrange(epoches):
session.run(train_op)return session.run(x)# tf.get_default_graph()if __name__ =='__main__':for a inrange(2,10+1):print(a, math.sqrt(a), sqrt(a))
D:\Anaconda\python.exe D:/AI20/06_codes/deeplearning_20/p18_tf_sqrt.py
2020-02-2715:39:58.354334: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
21.41421356237309511.414645931.73205080756887721.718446742.01.98463752.23606797749979-2.200349862.4494897427831782.475227672.6457513110645907-2.604780482.82842712474619032.813838293.0-2.989889103.16227766016837953.181918
Process finished with exit code 0