[英]Stuck implementing simple neural network
我一直在用这个砖墙砸我的头看似永恒,我似乎无法绕过它。 我正在尝试仅使用numpy和矩阵乘法来实现自动编码器。 没有theano或keras技巧允许。
我将描述问题及其所有细节。 它起初有点复杂,因为有很多变量,但它确实非常简单。
我们知道什么
1) X
是m
× n
矩阵,它是我们的输入。 输入是该矩阵的行。 每个输入都是一个n
维行向量,我们有m
个。
2)我们(单个)隐藏层中的神经元数量,即k
。
3)我们的神经元的激活功能(乙状结肠,将表示为g(x)
)及其衍生物g'(x)
我们不知道什么,想找到什么
总的来说,我们的目标是找到6个矩阵: w1
是n
乘k
, b1
是m
乘k
, w2
是k
乘n
,b2是m
乘n
, w3
是n
乘n
和b3
是m
乘n
。
它们随机初始化,我们找到使用梯度下降的最佳解决方案。
这个过程
首先我们计算z1 = Xw1+b1
。 它是m
乘以k
并且是隐藏层的输入。 然后我们计算h1 = g(z1)
,它只是将sigmoid函数应用于z1
所有元素。 自然它也是m
乘以k
并且是我们隐藏层的输出。
然后我们计算z2 = h1w2+b2
,它是m
乘以n
并且是我们神经网络输出层的输入。 然后我们计算h2 = g(z2)
,它自然也是m
乘以n
并且是我们神经网络的输出。
最后,我们获取此输出并对其执行一些线性运算符: Xhat = h2w3+b3
也是m
乘以n
并且是我们的最终结果。
我被卡住了
我想要最小化的成本函数是均方误差。 我已经用numpy代码实现了它
def cost(x, xhat):
return (1.0/(2 * m)) * np.trace(np.dot(x-xhat,(x-xhat).T))
问题是找到关于w1,b1,w2,b2,w3,b3
的成本的导数。 我们称之为成本S
在得出自己并以数字方式检查自己之后 ,我确定了以下事实:
1) dSdxhat = (1/m) * np.dot(xhat-x)
2) dSdw3 = np.dot(h2.T,dSdxhat)
3) dSdb3 = dSdxhat
4) dSdh2 = np.dot(dSdxhat, w3.T)
但我不能为我的生活弄清楚dSdz2。 这是一堵砖墙。
从链规则来看,应该是dSdz2 = dSdh2 * dh2dz2,但尺寸不匹配。
计算S相对于z2的导数的公式是什么?
编辑 - 这是我自动编码器的整个前馈操作的代码。
import numpy as np
def g(x): #sigmoid activation functions
return 1/(1+np.exp(-x)) #same shape as x!
def gGradient(x): #gradient of sigmoid
return g(x)*(1-g(x)) #same shape as x!
def cost(x, xhat): #mean squared error between x the data and xhat the output of the machine
return (1.0/(2 * m)) * np.trace(np.dot(x-xhat,(x-xhat).T))
#Just small random numbers so we can test that it's working small scale
m = 5 #num of examples
n = 2 #num of features in each example
k = 2 #num of neurons in the hidden layer of the autoencoder
x = np.random.rand(m, n) #the data, shape (m, n)
w1 = np.random.rand(n, k) #weights from input layer to hidden layer, shape (n, k)
b1 = np.random.rand(m, k) #bias term from input layer to hidden layer (m, k)
z1 = np.dot(x,w1)+b1 #output of the input layer, shape (m, k)
h1 = g(z1) #input of hidden layer, shape (m, k)
w2 = np.random.rand(k, n) #weights from hidden layer to output layer of the autoencoder, shape (k, n)
b2 = np.random.rand(m, n) #bias term from hidden layer to output layer of autoencoder, shape (m, n)
z2 = np.dot(h1, w2)+b2 #output of the hidden layer, shape (m, n)
h2 = g(z2) #Output of the entire autoencoder. The output layer of the autoencoder. shape (m, n)
w3 = np.random.rand(n, n) #weights from output layer of autoencoder to entire output of the machine, shape (n, n)
b3 = np.random.rand(m, n) #bias term from output layer of autoencoder to entire output of the machine, shape (m, n)
xhat = np.dot(h2, w3)+b3 #the output of the machine, which hopefully resembles the original data x, shape (m, n)
好的,这是一个建议。 在向量的情况下,如果你有x作为长度为n
的向量,那么g(x)
也是长度为n
的向量。 然而, g'(x)
不是矢量,它是雅可比矩阵 ,并且其大小为n X n
。 类似地,在小批量情况下,其中X是大小为m X n
的矩阵, g(X)
是m X n
但g'(X)
是n X n
。 尝试:
def gGradient(x): #gradient of sigmoid
return np.dot(g(x).T, 1 - g(x))
@Paul是对的,偏见项应该是向量,而不是矩阵。 你应该有:
b1 = np.random.rand(k) #bias term from input layer to hidden layer (k,)
b2 = np.random.rand(n) #bias term from hidden layer to output layer of autoencoder, shape (n,)
b3 = np.random.rand(n) #bias term from output layer of autoencoder to entire output of the machine, shape (n,)
Numpy的广播意味着您无需更改xhat
的计算。
然后(我想!)你可以像这样计算衍生物:
dSdxhat = (1/float(m)) * (xhat-x)
dSdw3 = np.dot(h2.T,dSdxhat)
dSdb3 = dSdxhat.mean(axis=0)
dSdh2 = np.dot(dSdxhat, w3.T)
dSdz2 = np.dot(dSdh2, gGradient(z2))
dSdb2 = dSdz2.mean(axis=0)
dSdw2 = np.dot(h1.T,dSdz2)
dSdh1 = np.dot(dSdz2, w2.T)
dSdz1 = np.dot(dSdh1, gGradient(z1))
dSdb1 = dSdz1.mean(axis=0)
dSdw1 = np.dot(x.T,dSdz1)
这对你有用吗?
编辑
我已经决定,我完全不确定gGradient
应该是一个矩阵。 怎么样:
dSdxhat = (xhat-x) / m
dSdw3 = np.dot(h2.T,dSdxhat)
dSdb3 = dSdxhat.sum(axis=0)
dSdh2 = np.dot(dSdxhat, w3.T)
dSdz2 = h2 * (1-h2) * dSdh2
dSdb2 = dSdz2.sum(axis=0)
dSdw2 = np.dot(h1.T,dSdz2)
dSdh1 = np.dot(dSdz2, w2.T)
dSdz1 = h1 * (1-h1) * dSdh1
dSdb1 = dSdz1.sum(axis=0)
dSdw1 = np.dot(x.T,dSdz1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.