神经网络:重塑NP数组

时间:2019-10-27 13:49:16

标签: python arrays numpy neural-network

我一直在关注这个神经网络教程:https://medium.com/analytics-vidhya/neural-networks-for-digits-recognition-e11d9dff00d5

该网络具有1个具有25个神经元的隐藏层。我想尝试将隐藏层中不同数量的神经元更改为10。我尝试更改hidden_layer_size并整形theta1theta2

最初theta1的大小为25x401,我将其更改为10x401 theta2的尺寸为10x26,我将其更改为10x11 small diagram

错误是: ValueError: cannot reshape array of size 3690 into shape (10,401)

如何重塑数组以使网络正常运行?(隐藏层中可更改的数量神经元)

代码重塑用于:

def nnCostFunc(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, lmbda):
theta1 = np.reshape(nn_params[:hidden_layer_size*(input_layer_size+1)],
                    (hidden_layer_size, input_layer_size+1), 'F')
theta2 = np.reshape(
    nn_params[hidden_layer_size*(input_layer_size+1):], (num_labels, hidden_layer_size+1), 'F')

m = len(y)
ones = np.ones((m, 1))
a1 = np.hstack((ones, X))
a2 = sigmoid(a1 @ theta1.T)
a2 = np.hstack((ones, a2))
h = sigmoid(a2 @ theta2.T)

y_d = pd.get_dummies(y.flatten())

temp1 = np.multiply(y_d, np.log(h))
temp2 = np.multiply(1-y_d, np.log(1-h))
temp3 = np.sum(temp1 + temp2)

sum1 = np.sum(np.sum(np.power(theta1[:, 1:], 2), axis=1))
sum2 = np.sum(np.sum(np.power(theta2[:, 1:], 2), axis=1))

return np.sum(temp3 / (-m)) + (sum1 + sum2) * lmbda / (2*m)

0 个答案:

没有答案