【问题标题】:Gradient descent vectorised computation dimensions not correct梯度下降矢量化计算维度不正确
【发布时间】:2016-05-21 10:43:09
【问题描述】:

我有 1 个输入层、2 个隐藏层和 1 个输出层,对于具有输出 y 的单个训练示例 x,我计算如下:

x = [1;0;1]; 
y = [1;1;1]; 

    theta1 =

        4.7300    3.2800    1.4600
             0         0         0
        4.7300    3.2800    1.4600

    theta2 =

        8.8920    8.8920    8.8920
        6.1670    6.1670    6.1670
        2.7450    2.7450    2.7450

    theta3 =

        9.4460    6.5500    2.9160
        9.3510    6.4850    2.8860
        8.8360    6.1270    2.7270

theta1 控制输入层和 layer1 之间的映射 theta2 控制 layer1 和 layer2 之间的映射 theta3 控制 layer2 和输出层之间的映射

但是要使用 计算梯度下降: theta(i) = theta(i) - (alpha/m .* (x .* theta(i)-y)' * x)' 其中 i = 1 或 2 或 3 x 和 y 的尺寸不正确。如果 x 和 y 是 1x9 而不是 1x3,则尺寸是正确的(正确的意思是可以执行 theta 计算而不会出错)。我是否需要更改我的神经网络的架构,或者我可以将 x 和 y 设置为
x = [1;0;1;0;0;0;0;0;0]; y = [1;1;1;0;0;0;0;0;0];这样矩阵乘法就可以解决了吗?

更新:

alpha=learning rate (.00001)
m=number of training examples (1)
theta(i) refers to theta1,theta2,theta3

我正在使用 Vectorization of a gradient descent code 中所述的矢量化梯度下降

更新2:

这个 matlab 代码似乎可以工作:

m = 1; 
alpha = .0000001; 
x = [1;0;1]; 
y = [1; 1; 1]; 
theta1 = [4.7300 3.2800 1.4600; 0 0 0; 4.7300 3.2800 1.4600]; 
theta1 = theta1 - (alpha/m) * (x' * (theta1 * x - y));

theta1 = theta1 - (alpha/m) * (x' * (theta1 * x - y)); 是矢量化梯度下降的正确实现吗?

我知道这会导致将 theta 矩阵展开为 theta 向量的问题,因为维度将不同,但使用 theta 矩阵而不是 theta 向量是否正确?

更新: 公式修改自Vectorization of a gradient descent code 其中梯度下降为:theta = theta - (alpha/m) * (X' * (X*theta-y)); 我将其更改为 theta = theta - (alpha/m) * (x' * (theta * x - y));,因此 (X*theta-y); 更改为 (theta * x - y);

【问题讨论】:

  • 声明x.*theta(i) 看起来很可疑,因为无论x 是1x3 还是1x9,尺寸都不匹配。您能否在您的帖子中澄清一些事情:theta(i) 是否真的写在您的代码中,或者您只是在这里写它来同时引用theta1theta2theta3alpham 是常量吗?
  • @Geoff 请查看问题更新“由于尺寸不匹配,声明 x.*theta(i) 似乎有问题。”我同意,我不确定这是我的网络架构还是其他问题。

标签: matlab machine-learning neural-network gradient-descent


【解决方案1】:

在您引用的 post 中,X 是一个包含 m 行(训练样本数)的矩阵。在您的情况下,m = 1,因此 X 成为行向量。在初始化时, x 是一个列向量。因此,最简单的更改是设置x = x'y = y',这样您的输入和输出都成为行向量。

公式仍然是

theta3 = theta3 - (alpha/m) * (x' * (x*theta3-y)) = 
  9.4458   6.5499   2.9160
  9.3510   6.4850   2.8860
  8.8358   6.1269   2.7270

theta2 和 theta1 的更新规则类似。

此外,错误术语x*theta3-y 始终与输入x 具有相同的形状;并且原始更新量x' * (x*theta3-y) 始终与theta3 具有相同的形状。

【讨论】:

  • 你更新了函数'theta1 = theta1 - (alpha/m) * (x' * (theta1 * x - y));'到'theta3 - alpha / m .* x * (theta3 * x - y)' 所以我的梯度下降函数不正确'?
  • 我可能错了,你的公式有参考吗?
  • 是的,鉴于参考,我知道问题出在哪里。
  • 谢谢,你能扩展一下你的新梯度下降函数背后的直觉吗?
  • 和你的参考一样。如果你想知道为什么会这样,你需要在每一层提供激活函数。
猜你喜欢
  • 1970-01-01
  • 2014-01-11
  • 2018-03-26
  • 1970-01-01
  • 2014-12-26
  • 2016-06-13
  • 1970-01-01
  • 2018-05-16
  • 2020-04-04
相关资源
最近更新 更多