基于Python fminunc 的替代方法

 更新时间:2020年02月29日 15:17:08   作者:csdn_inside  
今天小编就为大家分享一篇基于Python fminunc 的替代方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

最近闲着没事,想把coursera上斯坦福ML课程里面的练习,用Python来实现一下,一是加深ML的基础,二是熟悉一下numpy,matplotlib,scipy这些库。

在EX2中,优化theta使用了matlab里面的fminunc函数,不知道Python里面如何实现。搜索之后,发现stackflow上有人提到用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``
 
 where x is an 1-D array with shape (n,) and `args`
 is a tuple of the fixed parameters needed to completely
 specify the function.
x0 : ndarray, shape (n,)
 Initial guess. Array of real elements of size (n,),
 where '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) <optimize.minimize-neldermead>`
  - 'Powell'  :ref:`(see here) <optimize.minimize-powell>`
  - 'CG'   :ref:`(see here) <optimize.minimize-cg>`
  - 'BFGS'  :ref:`(see here) <optimize.minimize-bfgs>`
  - 'Newton-CG' :ref:`(see here) <optimize.minimize-newtoncg>`
  - 'L-BFGS-B' :ref:`(see here) <optimize.minimize-lbfgsb>`
  - 'TNC'   :ref:`(see here) <optimize.minimize-tnc>`
  - 'COBYLA'  :ref:`(see here) <optimize.minimize-cobyla>`
  - 'SLSQP'  :ref:`(see here) <optimize.minimize-slsqp>`
  - 'trust-constr':ref:`(see here) <optimize.minimize-trustconstr>`
  - 'dogleg'  :ref:`(see here) <optimize.minimize-dogleg>`
  - 'trust-ncg' :ref:`(see here) <optimize.minimize-trustncg>`
  - 'trust-exact' :ref:`(see here) <optimize.minimize-trustexact>`
  - 'trust-krylov' :ref:`(see here) <optimize.minimize-trustkrylov>`
  - custom - a callable object (added in version 0.14.0),
   see below 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 computing the gradient vector. Only for CG, BFGS,
 Newton-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,)``
 
 where x is an array with shape (n,) and `args` is a tuple with
 the fixed parameters. Alternatively, the keywords
 {'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 with the objective function. If False, the gradient
 will 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
 success: True
  x: array([-25.16131858, 0.20623159, 0.20147149])

此外,由于知道cost在0.203左右,所以我用最笨的梯度下降试了一下,由于后面实在是太慢了,所以设置while J>0.21,循环了大概13W次。。可见,使用集成好的优化算法是多么重要。。。还有,在以前的理解中,如果一个学习速率不合适,J会一直发散,但是昨天的实验发现,有的速率开始会发散,后面还是会收敛。

以上这篇基于Python fminunc 的替代方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • selenium+python自动化测试之多窗口切换

    selenium+python自动化测试之多窗口切换

    这篇文章主要介绍了selenium+python自动化测试之多窗口切换,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • Python qrcode 生成一个二维码的实例详解

    Python qrcode 生成一个二维码的实例详解

    在本篇文章里小编给大家整理的是关于Python qrcode 生成一个二维码的实例内容,需要的朋友们可以学习参考下。
    2020-02-02
  • 编写Python小程序来统计测试脚本的关键字

    编写Python小程序来统计测试脚本的关键字

    这篇文章主要介绍了编写Python小程序来统计测试脚本的关键字的方法,文中的实例不仅可以统计关键字数量,还可以按主关键字来归类,需要的朋友可以参考下
    2016-03-03
  • 解决Pyinstaller打包软件失败的一个坑

    解决Pyinstaller打包软件失败的一个坑

    这篇文章主要介绍了解决Pyinstaller打包软件失败的一个坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • Python字符串逆序的实现方法【一题多解】

    Python字符串逆序的实现方法【一题多解】

    今天小编就为大家分享一篇关于Python字符串逆序的实现方法【一题多解】,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-02-02
  • Python字符串str超详细详解(适合新手!)

    Python字符串str超详细详解(适合新手!)

    str函数是Python的内置函数,它将参数转换成字符串类型,即人适合阅读的形式,下面这篇文章主要给大家介绍了关于Python字符串str超详细详解的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-11-11
  • python利用socket实现客户端和服务端之间进行通信

    python利用socket实现客户端和服务端之间进行通信

    这篇文章主要介绍了python实现客户端和服务端之间进行通信,文章通过python利用socket展开详情介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-05-05
  • Python提升循环速度的高效方法小姐

    Python提升循环速度的高效方法小姐

    Python编程中,循环是一种常见的操作,但是如果处理大规模数据或者需要频繁执行的循环,往往会导致程序运行速度变慢,下面我们就来看看有什么办法可以提升循环速度吧
    2024-03-03
  • Python JWT 介绍和使用详解

    Python JWT 介绍和使用详解

    这篇文章主要介绍了Python JWT 介绍和使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • python虚拟机之描述器实现原理与源码分析

    python虚拟机之描述器实现原理与源码分析

    在本篇文章当中主要给大家介绍描述器背后的实现原理,通过分析 cpython对应的源代码了解与描述器相关的字节码的指令,我们就可以真正了解到描述器背后的原理,需要的朋友可以参考下
    2023-05-05

最新评论