基于Python fminunc 的替代办法
最近闲着没事,想把coursera上斯坦福ML课程里面的练习,用Python来实现一下,一是加深ML的基础,二是熟悉一下numpy,matplotlib,scipy这些库。
在EX2中,优化theta使用了matlab里面的fminunc函数,不知道Python里面如何实现。搜索之后,发现stackflo上有人提到用scipy库里面的minimize函数来替代。我尝试直接调用我的costfunction和grad,程序报错,提示(3,)和(100,1)dim维度不等,gradient vector不对之类的,试了N多次后,终于发现问题何在。。
来看看使用np.info(minimize)查看函数的介绍,传入的参数有
fun : callable The objective function to be minimized. ``fun(x, args) -> float`` here x is an 1-D array ith shape (n,) and `args` is a tuple of the fixed parameters needed to pletely specify the function. x0 : ndarray, shape (n,) Initial guess. Array of real elements of size (n,), here 'n' is the number of independent variables. args : tuple, optional Extra arguments passed to the objective function and its derivatives (`fun`, `jac` and `hess` functions). method : str or callable, optional Type of solver. Should be one of - 'Nelder-Mead' :ref:`(see here)` - 'Poell' :ref:`(see here) ` - 'CG' :ref:`(see here) ` - 'BFGS' :ref:`(see here) ` - 'Neton-CG' :ref:`(see here) ` - 'L-BFGS-B' :ref:`(see here) ` - 'TNC' :ref:`(see here) ` - 'COBYLA' :ref:`(see here) ` - 'SLSQP' :ref:`(see here) ` - 'trust-constr':ref:`(see here) ` - 'dogleg' :ref:`(see here) ` - 'trust-ncg' :ref:`(see here) ` - 'trust-exact' :ref:`(see here) ` - 'trust-krylov' :ref:`(see here) ` - custom - a callable object (added in version 0.14.0), see belo for description. If not given, chosen to be one of ``BFGS``, ``L-BFGS-B``, ``SLSQP``, depending if the problem has constraints or bounds. jac : {callable, '2-point', '3-point', 'cs', bool}, optional Method for puting the gradient vector. only for CG, BFGS, Neton-CG, L-BFGS-B, TNC, SLSQP, dogleg, trust-ncg, trust-krylov, trust-exact and trust-constr. If it is a callable, it should be a function that returns the gradient vector: ``jac(x, args) -> array_like, shape (n,)`` here x is an array ith shape (n,) and `args` is a tuple ith the fixed parameters. Alternatively, the keyords {'2-point', '3-point', 'cs'} select a finite difference scheme for numerical estimation of the gradient. Options '3-point' and 'cs' are available only to 'trust-constr'. If `jac` is a Boolean and is True, `fun` is assumed to return the gradient along ith the objective function. If False, the gradient ill be estimated using '2-point' finite difference estimation.
需要注意的是fun关键词参数里面的函数,需要把优化的theta放在第一个位置,X,y,放到后面。并且,theta在传入的时候一定要是一个一维shape(n,)的数组,不然会出错。
然后jac是梯度,这里的有两个地方要注意,第一个是传入的theta依然要是一个一维shape(n,),第二个是返回的梯度也要是一个一维shape(n,)的数组。
,关键在于传入的theta一定要是一个1D shape(n,)的,不然就不行。我之前为了方便已经把theta塑造成了一个(n,1)的列向量,导致使用minimize时会报错。所以,学会用help看说明可谓是相当重要啊~
import numpy as np
import pandas as pd
import scipy.optimize as op
def LoadData(filename):
data=pd.read_csv(filename,header=None)
data=np.array(data)
return data
def ReshapeData(data):
m=np.size(data,0)
X=data[:,0:2]
Y=data[:,2]
Y=Y.reshape((m,1))
return X,Y
def InitData(X):
m,n=X.shape
initial_theta = np.zeros(n + 1)
Vecones = np.ones((m, 1))
X = np.column_stack((VecOnes, X))
return X,initial_theta
def sigmoid(x):
z=1/(1+np.exp(-x))
return z
def costFunction(theta,X,Y):
m=X.shape[0]
J = (-np.dot(Y.T, np.log(sigmoid(X.dot(theta)))) -
np.dot((1 - Y).T, np.log(1 - sigmoid(X.dot(theta))))) / m
return J
def gradient(theta,X,Y):
m,n=X.shape
theta=theta.reshape((n,1))
grad=np.dot(X.T,sigmoid(X.dot(theta))-Y)/m
return grad.flatten()
if __name__=='__main__':
data = LoadData('ex2data1csv.csv')
X, Y = ReshapeData(data)
X, initial_theta = InitData(X)
result = op.minimize(fun=costFunction, x0=initial_theta, args=(X, Y), method='TNC', jac=gradient)
print(result)
结果如下,符合MATLAB里面用fminunc优化的结果(fminunc:cost:0.203,theta:-25.161,0.206,0.201)
fun: array([0.2034977]) jac: array([8.95038682e-09, 8.16149951e-08, 4.74505693e-07]) message: 'Local minimum reached (|pg| ~= 0)' nfev: 36 nit: 17 status: 0 suess: True x: array([-25.16131858, 0.20623159, 0.20147149])
,由于知道cost在0.203左右,所以我用最笨的梯度下降试了一下,由于后面实在是太慢了,所以设置hile J>0.21,循环了大概13W次。。可见,使用集成好的优化算法是多么重要。。。还有,在以前的理解中,如果一个学习速率不合适,J会一直发散,昨天的实验发现,有的速率开始会发散,后面还是会收敛。
以上这篇基于Python fminunc 的替代方法就是我分享给大家的全部内容了,电脑维修网希望能给大家一个参考,也电脑维修网希望大家多多支持考高分网。
空调维修
- 我的世界电脑版运行身份怎么弄出来(我的世界
- 空调抽湿是什么意思,设置抽湿的温度有什么意
- 方太燃气灶有一个打不着火 怎么修复与排查方法
- 夏季免费清洗汽车空调的宣传口号
- 清洗完空调后出现漏水现象
- iphone6能玩什么游戏(iphone6游戏)
- 如何设置电脑密码锁屏(如何设置电脑密码锁屏
- win10删除开机密码提示不符合密码策略要求
- 电脑w7显示不是正版(w7不是正版怎么解决)
- 万家乐z8热水器显示e7解决 怎么修复与排查方法
- 1匹空调多少瓦数(1匹空调多少瓦)
- 安卓手机连接电脑用什么软件好(关于安卓手机
- 电脑网页看视频卡是什么原因(爱拍看视频卡)
- 华帝燃气灶点火器一直响然后熄火怎么办:问题
- 电脑壁纸怎么换(关于电脑壁纸怎么换的介绍)
- 冬天空调的出风口应该朝什么方向(冬天空调风