import math
defsqrt(a, epoches=4000, alpha=0.01):defy(x, a):returnabs(x**2- a)defdy_dx(x, a):
dy = x**2- a
return2* x *(1if dy >0else-1)defdx(x, a, alpha):return- dy_dx(x, a)* alpha
x =1.0for _ inrange(epoches):
x += dx(x, a, alpha)return x
if __name__ =='__main__':for n inrange(2,10+1):print('%s, %8.4f, %8.4f'%(n, math.sqrt(n), sqrt(n)))
D:\Anaconda\python.exe D:/AI20/06_codes/deeplearning_20/p14_sqrt2.py
2,1.4142,1.43333,1.7321,1.75074,2.0000,1.97405,2.2361,2.22576,2.4495,2.41117,2.6458,2.61198,2.8284,2.82959,3.0000,2.944910,3.1623,3.1903
Process finished with exit code 0