使用scipy.optimize进行目标函数中的矩阵参数优化

来源:互联网 发布:话费源码 编辑:程序博客网 时间:2024/05/16 15:09

在做机器学习最优化的时候,参数通常以矩阵形式存在,现网上在矩阵参数优化内容方面例子较少,遂写下这篇博文。
例如:
目标函数:T(θ)=12||XθY||
其中Xm×n特征矩阵,Ym×l标签矩阵,θn×l参数矩阵。
以下为具体代码:

import numpy as npimport scipy.optimize as optimport matplotlib.pyplot as pltpoints = []# 目标函数def obj_fun(theta, x, y_):    theta = theta.reshape(3, 3)    pre_dis = np.dot(x, theta)    loss = np.sum((pre_dis - y_)**2) / 2    points.append(loss)    return loss# 偏导数def prime(theta, x, y_):    theta = theta.reshape(3, 3)    pre_dis = np.dot(x, theta)    gradient = np.dot(np.transpose(x), (pre_dis - y_))    return np.ravel(gradient)   # 将二维矩阵展开成一维向量if __name__ == "__main__":    feature = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 5, 9]])    dis_ = np.array([[0.1, 0.3, 0.6], [0.2, 0.4, 0.4], [0.3, 0.2, 0.5], [0.1, 0.3, 0.6]])    init_theta = np.ones([3, 3])    # result = opt.fmin(obj_fun, init_theta, args=(feature, dis_))    # result = opt.fmin_bfgs(obj_fun, init_theta, prime, args=(feature, dis_))    result = opt.fmin_l_bfgs_b(obj_fun, init_theta, prime, args=(feature, dis_))    print(result)    # 注意使用fmin_l_bfgs_b算法时,优化得到的参数在result[0]中,其他算法即在result中    m_theta = np.array(result[0]).reshape(3, 3)    print(np.dot(feature, m_theta))    print(obj_fun(m_theta, feature, dis_))    plt.plot(np.arange(len(points)), points)    plt.show()
阅读全文
0 0