坚持实施简单的神经网络

时间:2016-10-01 08:59:04

标签: python algorithm numpy matrix derivative

我一直在用这个砖墙砸我的头看似永恒,我似乎无法绕过它。我正在尝试仅使用numpy和矩阵乘法来实现自动编码器。不允许使用theano或keras技巧。

我将描述问题及其所有细节。它起初有点复杂,因为有很多变量,但它确实非常简单。

我们所知道的

1)Xmn矩阵,这是我们的输入。输入是该矩阵的行。每个输入都是n维行向量,我们有m个。

2)我们(单个)隐藏层中的神经元数量,即k

3)我们神经元的激活功能(sigmoid,将表示为g(x))及其衍生物g'(x)

我们不知道并希望找到的内容

总体而言,我们的目标是找到6个矩阵:w1 n kb1 m kw2 k nm nw3 n n {}}和b3 m n。{/ p>

它们随机初始化,我们找到了使用梯度下降的最佳解决方案。

流程

整个过程看起来像这样 enter image description here

首先我们计算z1 = Xw1+b1。它是m k,是隐藏图层的输入。然后我们计算h1 = g(z1),它只是将sigmoid函数应用于z1的所有元素。自然它也是m k并且是隐藏图层的输出。

然后,我们计算z2 = h1w2+b2 m n,并且是神经网络输出层的输入。然后我们计算h2 = g(z2),它m自然也是n,并且是神经网络的输出。

最后,我们采用此输出并在其上执行一些线性运算符:Xhat = h2w3+b3m也是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)

1 个答案:

答案 0 :(得分:4)

好的,这是一个建议。在向量的情况下,如果您将 x 作为长度为n的向量,则g(x)也是长度为n的向量。但是,g'(x)不是向量,而是Jacobian matrix,大小为n X n。同样,在小批量案例中, X 是一个大小为m X n的矩阵,g(X)m X ng'(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)