import p15_deriv as deriv
def train(y,x,epoches,learning_rate):
dy_dx = y.deriv(x)
x0=1.0
for _ in range(epoches):
x0 += -dy_dx.eval(**{x.name:x0}) * learning_rate
return x0
if __name__ == '__main__':
x = deriv.Variable('x')
for a in range(2,10+1):
y = (x ** 3 - a) ** 2
x_v= train(y,x,4000,0.005)
print(a,a**0.3333333,x_v)
D:\Anaconda\python.exe D:/AI20/06_codes/deeplearning_20/p16_cuberroot.py
2 1.259921020784516 1.2599210498948727
3 1.4422495174916392 1.442249570307408
4 1.587400978614697 1.5874010519681994
5 1.7099758549400288 1.709975946676697
6 1.8171204843040418 1.8171205928321397
7 1.9129310586926531 1.9129311827723892
8 1.9999998613705687 2.0
9 2.0800836707048664 2.0800838230519045
10 2.154434524672917 2.154434690031885
Process finished with exit code 0
来源:CSDN
作者:HJZ11
链接:https://blog.csdn.net/HJZ11/article/details/104515050