梯度下降码的矢量化 [英] Vectorization of a gradient descent code

查看:98
本文介绍了梯度下降码的矢量化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Matlab上实现批量梯度下降.我在theta的更新步骤中遇到问题. theta是两个分量(两行)的向量. X是一个包含m行(训练样本数)和n=2列(特征数)的矩阵. Y是m行向量.

I am implementing a batch gradient descent on Matlab. I have a problem with the update step of theta. theta is a vector of two components (two rows). X is a matrix containing m rows (number of training samples) and n=2 columns (number of features). Y is an m rows vector.

在更新步骤中,我需要将每个theta(i)设置为

During the update step, I need to set each theta(i) to

theta(i) = theta(i) - (alpha/m)*sum((X*theta-y).*X(:,i))

这可以通过for循环来完成,但是我不知道如何对其进行矢量化(由于X(:,i)项).

This can be done with a for loop, but I can't figure out how to vectorize it (because of the X(:,i) term).

有什么建议吗?

推荐答案

好像您正在尝试进行简单的矩阵乘法,MATLAB最擅长于此.

Looks like you are trying to do a simple matrix multiplication, the thing MATLAB is supposedly best at.

theta = theta - (alpha/m) * (X' * (X*theta-y));

这篇关于梯度下降码的矢量化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆