为了账号安全,请及时绑定邮箱和手机立即绑定

形状未在渐变下降中对齐

形状未在渐变下降中对齐

牛魔王的故事 2022-06-02 11:59:57
我试图通过计算函数中的值来获得梯度下降。并在我的代码中出错def gradient_descent(X, y, theta, alpha, num_iters):m = len(y)cost_history = np.zeros(num_iters)theta_history = np.zeros((num_iters,2))for i in range(num_iters):    prediction = np.reshape(np.dot(np.transpose(theta), X),97)    theta = theta -(1/m)*alpha*( X.T.dot((prediction - y)))    theta_history[i,:] =theta.T    J_history[i]  = cal_cost(theta,X,y)return theta, J_history"""Args----X (numpy mxn array) - The example inputs, first column is expected   to be all 1's.y (numpy m array) - A vector of the correct outputs of length mtheta (numpy nx1 array) - An array of the set of theta parameters   to evaluatealpha (float) - The learning rate to use for the iterative gradient   descentnum_iters (int) - The number of gradient descent iterations to performReturns-------theta (numpy nx1 array) - The final theta parameters discovered after    out gradient descent.J_history (numpy num_itersx1 array) - A history of the calculated    cost for each iteration of our descent."""以下是我传递给函数和变量的参数theta = np.zeros( (2, 1) )iterations = 1500;alpha = 0.01theta, J = gradient_descent(X, y, theta, alpha, iterations)错误信息是:ValueError:形状(97,2)和(97,)未对齐:2(dim 1)!= 97(dim 0)
查看完整描述

1 回答

?
大话西游666

TA贡献1817条经验 获得超14个赞

我不确定你在哪里得到 ValueError,但形状为 (97,) 的 ndarray 需要np.expand_dims在其上运行,如下所示:

np.expand_dims(vector, axis=-1)

这将使向量具有形状 (97,1),因此它应该被对齐/能够被广播。


查看完整回答
反对 回复 2022-06-02
  • 1 回答
  • 0 关注
  • 80 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号