梯度下降法是非常常見的優化方法,在神經網路的深度學習中更是必會方法,但是直接從深度學習去實現,會比較複雜。本文試圖使用梯度下降來優化最簡單的LSR線性迴歸問題,作為進一步學習的基礎。
import numpy as np import pandas as pd from numpy import * from pandas import * import matplotlib.pyplot as plt x = np.array([[1,2],[2,1],[3,2.5],[4,3], [5,4],[6,5],[7,2.7],[8,4.5], [9,2]]) m, n = np.shape(x) x_data = np.ones((m,n)) x_data[:,:-1] = x[:,:-1] y_data = x[:,-1] print(x_data.shape) print(y_data.shape) m, n = np.shape(x_data) theta = np.ones(n) def batchGradientDescent(maxiter,x,y,theta,alpha): xTrains = x.transpose() for i in range(0,maxiter): hypothesis = np.dot(x,theta) loss = (hypothesis-y) gradient = np.dot(xTrains,loss)/m theta = theta - alpha * gradient cost = 1.0/2*m*np.sum(np.square(np.dot(x,np.transpose(theta))-y)) print("cost: %f"%cost) return theta result = batchGradientDescent(10,x_data,y_data,theta,0.01) print(result) newy = np.dot(x_data,result) fig, ax = plt.subplots() ax.plot(x[:,0],newy, 'k--') ax.plot(x[:,0],x[:,1], 'ro') plt.show() print("final: " + result)