- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下函数使用梯度下降找到回归线的最佳“theta”。输入 (X,y) 附加在下面。我的问题是代码1和代码2有什么区别?为什么代码 2 可以工作,而代码 1 却不能工作?
提前致谢!
GRADIENTDESCENTMULTI 执行梯度下降来学习 theta,它通过使用学习率 alpha 进行 num_iters 梯度步长来更新 theta
function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)
% Initialize some useful values
m = length(y); % number of training examples
n = length(theta);
J_history = zeros(num_iters, 1);
costs = zeros(n,1);
for iter = 1:num_iters
% code 1 - doesn't work
for c = 1:n
for i = 1:m
costs(c) = costs(c)+(X(i,:)*theta - y(i))*X(i,c);
end
end
% code 2 - does work
E = X * theta - y;
for c = 1:n
costs(c) = sum(E.*X(:,c));
end
% update each theta
for c = 1:n
theta(c) = theta(c) - alpha*costs(c)/m;
end
J_history(iter) = computeCostMulti(X, y, theta);
end
end
function J = computeCostMulti(X, y, theta)
for i=1:m
J = J+(X(i,:)*theta - y(i))^2;
end
J = J/(2*m);
运行代码:
alpha = 0.01;
num_iters = 200;
% Init Theta and Run Gradient Descent
theta = zeros(3, 1);
[theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters);
% Plot the convergence graph
figure;
plot(1:numel(J_history), J_history, '-b', 'LineWidth', 2);
xlabel('Number of iterations');
ylabel('Cost J');
% Display gradient descent's result
fprintf('Theta computed from gradient descent: \n');
fprintf(' %f \n', theta);
fprintf('\n');
X 是
1.0000 0.1300 -0.2237
1.0000 -0.5042 -0.2237
1.0000 0.5025 -0.2237
1.0000 -0.7357 -1.5378
1.0000 1.2575 1.0904
1.0000 -0.0197 1.0904
1.0000 -0.5872 -0.2237
1.0000 -0.7219 -0.2237
1.0000 -0.7810 -0.2237
1.0000 -0.6376 -0.2237
1.0000 -0.0764 1.0904
1.0000 -0.0009 -0.2237
1.0000 -0.1393 -0.2237
1.0000 3.1173 2.4045
1.0000 -0.9220 -0.2237
1.0000 0.3766 1.0904
1.0000 -0.8565 -1.5378
1.0000 -0.9622 -0.2237
1.0000 0.7655 1.0904
1.0000 1.2965 1.0904
1.0000 -0.2940 -0.2237
1.0000 -0.1418 -1.5378
1.0000 -0.4992 -0.2237
1.0000 -0.0487 1.0904
1.0000 2.3774 -0.2237
1.0000 -1.1334 -0.2237
1.0000 -0.6829 -0.2237
1.0000 0.6610 -0.2237
1.0000 0.2508 -0.2237
1.0000 0.8007 -0.2237
1.0000 -0.2034 -1.5378
1.0000 -1.2592 -2.8519
1.0000 0.0495 1.0904
1.0000 1.4299 -0.2237
1.0000 -0.2387 1.0904
1.0000 -0.7093 -0.2237
1.0000 -0.9584 -0.2237
1.0000 0.1652 1.0904
1.0000 2.7864 1.0904
1.0000 0.2030 1.0904
1.0000 -0.4237 -1.5378
1.0000 0.2986 -0.2237
1.0000 0.7126 1.0904
1.0000 -1.0075 -0.2237
1.0000 -1.4454 -1.5378
1.0000 -0.1871 1.0904
1.0000 -1.0037 -0.2237
Y 是
399900
329900
369000
232000
539900
299900
314900
198999
212000
242500
239999
347000
329999
699900
259900
449900
299900
199900
499998
599000
252900
255000
242900
259900
573900
249900
464500
469000
475000
299900
349900
169900
314900
579900
285900
249900
229900
345000
549000
287000
368500
329900
314000
299000
179900
299900
239500
最佳答案
我认为我的工作正常。最主要的是,在代码 1 中,您不断添加 cost(c),但在下一次迭代之前从未将其设置为零。您真正需要更改的唯一一件事是添加类似 cost(c) = 0;
的内容之后for c = 1:n
和之前 for i = 1:m
。我确实必须稍微更改您的代码才能使其为我工作(主要是 computeCostMulti
),并且我更改了绘图以表明两种方法的结果相同。总的来说,这是一段包含这些更改的工作演示代码
close all; clear; clc;
%% Data
X = [1.0000 0.1300 -0.2237; 1.0000 -0.5042 -0.2237; 1.0000 0.5025 -0.2237; 1.0000 -0.7357 -1.5378;
1.0000 1.2575 1.0904; 1.0000 -0.0197 1.0904; 1.0000 -0.5872 -0.2237; 1.0000 -0.7219 -0.2237;
1.0000 -0.7810 -0.2237; 1.0000 -0.6376 -0.2237; 1.0000 -0.0764 1.0904; 1.0000 -0.0009 -0.2237;
1.0000 -0.1393 -0.2237; 1.0000 3.1173 2.4045; 1.0000 -0.9220 -0.2237; 1.0000 0.3766 1.0904;
1.0000 -0.8565 -1.5378; 1.0000 -0.9622 -0.2237; 1.0000 0.7655 1.0904; 1.0000 1.2965 1.0904;
1.0000 -0.2940 -0.2237; 1.0000 -0.1418 -1.5378; 1.0000 -0.4992 -0.2237; 1.0000 -0.0487 1.0904;
1.0000 2.3774 -0.2237; 1.0000 -1.1334 -0.2237; 1.0000 -0.6829 -0.2237; 1.0000 0.6610 -0.2237;
1.0000 0.2508 -0.2237; 1.0000 0.8007 -0.2237; 1.0000 -0.2034 -1.5378; 1.0000 -1.2592 -2.8519;
1.0000 0.0495 1.0904; 1.0000 1.4299 -0.2237; 1.0000 -0.2387 1.0904; 1.0000 -0.7093 -0.2237;
1.0000 -0.9584 -0.2237; 1.0000 0.1652 1.0904; 1.0000 2.7864 1.0904; 1.0000 0.2030 1.0904;
1.0000 -0.4237 -1.5378; 1.0000 0.2986 -0.2237; 1.0000 0.7126 1.0904; 1.0000 -1.0075 -0.2237;
1.0000 -1.4454 -1.5378; 1.0000 -0.1871 1.0904; 1.0000 -1.0037 -0.2237];
y = [399900 329900 369000 232000 539900 299900 314900 198999 212000 242500 239999 347000 329999,...
699900 259900 449900 299900 199900 499998 599000 252900 255000 242900 259900 573900 249900,...
464500 469000 475000 299900 349900 169900 314900 579900 285900 249900 229900 345000 549000,...
287000 368500 329900 314000 299000 179900 299900 239500]';
alpha = 0.01;
num_iters = 200;
% Init Theta and Run Gradient Descent
theta0 = zeros(3, 1);
[theta_result_1, J_history_1] = gradientDescentMulti(X, y, theta0, alpha, num_iters, 1);
[theta_result_2, J_history_2] = gradientDescentMulti(X, y, theta0, alpha, num_iters, 2);
% Plot the convergence graph for both methods
figure;
x = 1:numel(J_history_1);
subplot(5,1,1:4);
plot(x,J_history_1,x,J_history_2);
xlim([min(x) max(x)]);
set(gca,'XTickLabel','');
ylabel('Cost J');
grid on;
subplot(5,1,5);
stem(x,(J_history_1-J_history_2)./J_history_1,'ko');
xlim([min(x) max(x)]);
xlabel('Number of iterations');
ylabel('frac. \DeltaJ');
grid on;
% Display gradient descent's result
fprintf('Theta computed from gradient descent with method 1: \n');
fprintf(' %f \n', theta_result_1);
fprintf('Theta computed from gradient descent with method 2: \n');
fprintf(' %f \n', theta_result_2);
fprintf('\n');
<小时/>
function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters, METHOD)
% Initialize some useful values
m = length(y); % number of training examples
n = length(theta);
J_history = zeros(num_iters, 1);
costs = zeros(n,1);
for iter = 1:num_iters
if METHOD == 1 % code 1 - does work
for c = 1:n
costs(c) = 0;
for i = 1:m
costs(c) = costs(c) + (X(i,:)*theta - y(i)) *X(i,c);
end
end
elseif METHOD == 2 % code 2 - does work
E = X * theta - y;
for c = 1:n
costs(c) = sum(E.*X(:,c));
end
else
error('unknown method');
end
% update each theta
for c = 1:n
theta(c) = theta(c) - alpha*costs(c)/m;
end
J_history(iter) = computeCostMulti(X, y, theta);
end
end
<小时/>
function J = computeCostMulti(X, y, theta)
m = length(y); J = 0;
for mi = 1:m
J = J + (X(mi,:)*theta - y(mi))^2;
end
J = J/(2*m);
end
但是,您实际上只需要添加 cost(c) = 0;
线。
还有;我建议始终添加 close all; clear; clc;
在脚本开头添加一行,以确保在将它们复制并粘贴到堆栈溢出中时它们能够正常工作。
关于matlab - 多变量梯度下降Matlab - 两个代码有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51848436/
我正在尝试调整 tf DeepDream 教程代码以使用另一个模型。现在当我调用 tf.gradients() 时: t_grad = tf.gradients(t_score, t_input)[0
考虑到 tensorflow 中 mnist 上的一个简单的小批量梯度下降问题(就像在这个 tutorial 中),我如何单独检索批次中每个示例的梯度。 tf.gradients()似乎返回批次中所有
当我在 numpy 中计算屏蔽数组的梯度时 import numpy as np import numpy.ma as ma x = np.array([100, 2, 3, 5, 5, 5, 10,
除了数值计算之外,是否有一种快速方法来获取协方差矩阵(我的网络激活)的导数? 我试图将其用作深度神经网络中成本函数中的惩罚项,但为了通过我的层反向传播误差,我需要获得导数。 在Matlab中,如果“a
我有一个计算 3D 空间标量场值的函数,所以我为它提供 x、y 和 z 坐标(由 numpy.meshgrid 获得)的 3D 张量,并在各处使用元素运算。这按预期工作。 现在我需要计算标量场的梯度。
我正在使用内核密度估计 (KDE) ( http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.gaussian_kde.htm
我对 tensorflow gradient documentation 中的示例感到困惑用于计算梯度。 a = tf.constant(0.) b = 2 * a g = tf.gradients(
我有一个 softmax 层(只有激活本身,没有将输入乘以权重的线性部分),我想对其进行向后传递。 我找到了很多关于 SO 的教程/答案来处理它,但它们似乎都使用 X 作为 (1, n_inputs)
仅供引用,我正在尝试使用 Tensorflow 实现梯度下降算法。 我有一个矩阵X [ x1 x2 x3 x4 ] [ x5 x6 x7 x8 ] 我乘以一些特征向量 Y 得到 Z [ y
我目前有一个由几百万个不均匀分布的粒子组成的体积,每个粒子都有一个属性(对于那些好奇的人来说是潜在的),我想为其计算局部力(加速度)。 np.gradient 仅适用于均匀间隔的数据,我在这里查看:S
我正在寻找有关如何实现 Gradient (steepest) Descent 的建议在 C 中。我正在寻找 f(x)=||Ax-y||^2 的最小值,其中给出了 A(n,n) 和 y(n)。 这在
我正在查看 SVM 损失和导数的代码,我确实理解了损失,但我无法理解如何以矢量化方式计算梯度 def svm_loss_vectorized(W, X, y, reg): loss = 0.0 dW
我正在寻找一种有效的方法来计算 Julia 中多维数组的导数。准确地说,我想要一个等效的 numpy.gradient在 Julia 。但是,Julia 函数 diff : 仅适用于二维数组 沿微分维
我在cathesian 2D 系统中有两个点,它们都给了我向量的起点和终点。现在我需要新向量和 x 轴之间的角度。 我知道梯度 = (y2-y1)/(x2-x1) 并且我知道角度 = arctan(g
我有一个 2D 数组正弦模式,想要绘制该函数的 x 和 y 梯度。我有一个二维数组 image_data : def get_image(params): # do some maths on
假设我有一个针对 MNIST 数据的简单 TensorFlow 模型,如下所示 import tensorflow as tf from tensorflow.examples.tutorials.m
我想查看我的 Tensorflow LSTM 随时间变化的梯度,例如,绘制从 t=N 到 t=0 的梯度范数。问题是,如何从 Tensorflow 中获取每个时间步长的梯度? 最佳答案 在图中定义:
我有一个简单的神经网络,我试图通过使用如下回调使用张量板绘制梯度: class GradientCallback(tf.keras.callbacks.Callback): console =
在CIFAR-10教程中,我注意到变量被放置在CPU内存中,但它在cifar10-train.py中有说明。它是使用单个 GPU 进行训练的。 我很困惑..图层/激活是否存储在 GPU 中?或者,梯度
我有一个 tensorflow 模型,其中层的输出是二维张量,例如 t = [[1,2], [3,4]] . 下一层需要一个由该张量的每一行组合组成的输入。也就是说,我需要把它变成t_new = [[
我是一名优秀的程序员,十分优秀!