在做机器学习最优化的时候,参数通常以矩阵形式存在,现网上在矩阵参数优化内容方面例子较少,遂写下这篇博文。
例如:
目标函数:
其中
以下为具体代码:
import numpy as np
import scipy.optimize as opt
import matplotlib.pyplot as plt
points = []
# 目标函数
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()