p16_cuberroot立方根

梦想的初衷 提交于 2020-02-26 14:37:56
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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!