我对梯度下降算法的简单实现有疑问- 我编写了以下代码,只是将GD的数学运算转换为matlab代码,但并没有收敛。由于此实现方式的简单性,我认为在这里缺少有关Matlab调试的非常关键的信息。
此代码的目标是估计线性回归系数,该系数使我们得到最小误差。因此,我加载了一些数据('trees.data.txt'),然后将这些数据分成了训练集和测试集,我想使用训练和梯度下降来提取系数,但是不幸的是它不能收敛。
这是迭代方程- theta = theta-(alpha / m)*((X * theta-y)'* X)';
任何人都可以解释一下是什么问题吗?
我的实现-
new DropdownButton(
value: _current,
items: dataMaker.map((item) {
return new DropdownMenuItem<String>(
child: new Text(item.fabricante,
textAlign: TextAlign.center),
value: item.fabricante, //FAIL
);
}).toList(),
onChanged: (selected) => setState(() {
_current = selected;
});
这是前10次迭代的输出-
% Load trees data from file.
data = load('trees.data.txt');
data=data'; % put examples in columns
% Include a row of 1s as an additional intercept feature.
data = [ ones(1,size(data,2)); data ];
% Shuffle examples.
data = data(:, randperm(size(data,2)));
% Split into train and test sets
% The last row of 'data' is the median home price.
train.X = data(1:end-1,1:400);
train.y = data(end,1:400);
test.X = data(1:end-1,401:end);
c = data(end,401:end);
m=size(train.X,2);
n=size(train.X,1);
% Initialize the coefficient vector theta to random values.
theta = rand(n,1);
X = test.X;
y = test.y;
theta_=zeros(size(theta));
delta =0.01; % convergence tolerance
alpha = 0.001; % learning rate
shift = 1000; % big number
iter = 0;
formatSpec = 'iteration: %d, error: %2.4f\n';
while (shift > delta)
iter = iter +1;
grad =zeros(size(theta));
for i = 1:m
grad = grad + (train.X(:,i)'*theta - train.y(i)).*train.X(:,i);
end
%theta_= theta-(alpha*(1/m)*((theta'*X-y)*X')');
theta_= theta - alpha*(1/m)*grad;
shift = norm(theta_ - theta);
fprintf(formatSpec, iter, shift);
theta = theta_;
clear theta_;
end