import matplotlib.pyplot as plt import numpy as np def f(x): """構造函數""" #print("calculating f(x) {}".format(x)) return 0.19 * x*x*x - 1.2 * (x*x) - 6 * x + 2.76 def g(x): """導數""" return 0.57 * x * x - 2.4 * x - 6 def autoGD(init_x, lr, thresh, epoch): x = init_x draw_x,draw_y = [],[] for i in range(epoch): grad = g(x) x = x - lr * grad print("Epoch {}: x={:.5f} grad={} y={}".format(i,x,grad,f(x))) if i % 200 == 0: draw_x.append(x) draw_y.append(f(x)) if abs(grad) < thresh: break return draw_x,draw_y
主要是使用sympy庫,具體方法請查看sympy庫的APIpython
import numpy as np import matplotlib.pyplot as plt import sympy from sympy import S, Symbol def f(exp,x): return exp.evalf(subs={a:x}) def g(exp,x): return sympy.diff(exp,a).evalf(subs={a:x}) def sgd(exp,init_x,lr,thresh,epoch): x = init_x for i in range(epoch): grad = g(exp,x) x -= grad * lr print("[Epoch : {}] x : {} grad : {}".format(i,x,grad)) if abs(grad) < thresh: break def draw(exp,begin,end): x = np.linspace(begin,end,100) y = [] for ix in x: iy = exp.evalf(subs={a:ix}) y.append(iy) plt.plot(x,y) plt.show() def main(): a = input('請輸入變量:') b = input('請使用以上變量進行函數式子的表達:') a = Symbol(a) exp = S(b) draw(exp,-5,5) sgd(exp,random.randint(0,10),1e-2,1e-3,200) if __name__ == "__main__": main()