【问题标题】:How to determine the size of bias matrices in a neural network?如何确定神经网络中偏差矩阵的大小?
【发布时间】:2018-08-30 09:03:45
【问题描述】:

我是机器学习领域的新手。我的问题是如何确定神经网络中偏差的大小(使用反向传播算法)?目前,我有一个 2 层神经网络(1 个隐藏层和 1 个输出层)。代码如下:

import numpy as np
from matplotlib import pyplot as plt 

sigmoid = lambda x : 1 / (1 + np.exp(-x))
dsigmoid = lambda y: y * (1 - sigmoid(y))

# This function performs the given function (func) to the whole numpy array
def mapFunc(array, func) :
    newArray = array.copy()
    for element in np.nditer(newArray, op_flags=['readwrite']) :
        element[...] = func(element)
    return newArray

class NeuralNetwork :

def __init__(self, input_nodes, hidden_nodes, output_nodes) :
    self.input_nodes = input_nodes
    self.hidden_nodes = hidden_nodes
    self.output_nodes = output_nodes

    self.W_ih = np.random.rand(hidden_nodes, input_nodes)
    self.W_ho = np.random.rand(output_nodes, hidden_nodes)

    self.B_ih = np.random.rand(hidden_nodes, 1)
    self.B_ho = np.random.rand(output_nodes, 1)

    self.learningRate = 0.1

def predict(self, inputs) :
    # Calculate hidden's output
    H_output = np.dot(self.W_ih, inputs)
    H_output += self.B_ih
    H_output = mapFunc(H_output, sigmoid) # Activation

    # Calculate output's output
    O_output = np.dot(self.W_ho, H_output)
    O_output += self.B_ho
    O_output = mapFunc(O_output, sigmoid) # Activation

    return O_output

def train(self, inputs, target) :
    # Calculate hidden's output
    H_output = np.dot(self.W_ih, inputs)
    H_output += self.B_ih
    H_output = mapFunc(H_output, sigmoid) # Activation

    # Calculate output's output
    O_output = np.dot(self.W_ho, H_output)
    O_output += self.B_ho
    O_output = mapFunc(O_output, sigmoid) # Activation

    # Calculate output error :
    O_error = O_output - target

    # Calculate output delta
    O_gradient = mapFunc(O_output, dsigmoid)
    O_gradient = np.dot(O_gradient, np.transpose(O_error)) * self.learningRate

    W_ho_delta = np.dot(O_gradient, np.transpose(H_output))

    self.W_ho -= W_ho_delta
    self.B_ho -= O_gradient

    # Calculate hidden error :
    W_ho_t = np.transpose(self.W_ho)
    H_error = np.dot(W_ho_t, O_error)

    # Calculate hidden delta :
    H_gradient = mapFunc(H_output, dsigmoid)
    H_gradient = np.dot(H_gradient, np.transpose(H_error)) * self.learningRate

    W_ih_delta = np.dot(H_gradient, inputs)

    self.W_ih -= W_ih_delta
    self.B_ih += H_gradient

    return O_output


n = NeuralNetwork(2, 2, 1)

inputs = np.matrix([[1], [0], [1], [1], [0], [1], [0], [0]])

input_list = []
input_list.append([[1], [0]])
input_list.append([[0], [1]])
input_list.append([[1], [1]])
input_list.append([[0], [0]])

target = np.matrix([[0], [0], [1], [1]])

outputs = []
for i in range(50000) :
    ind = np.random.randint(len(input_list))
    inp = input_list[ind]
    out = n.train(inp, target[ind]).tolist()
    outputs.append(out[0][0])

print outputs
plt.plot(outputs)

plt.show()

newInput = [[1], [1]]
print (n.predict(newInput))

在 train 函数中,self.B_ih += H_gradient 行向我抛出一个关于它们的大小不相等的错误。我什至试图将偏差仅设为一个数字,但这并没有帮助,因为它被H_gradient 更改为矩阵。那么,偏见本身有问题还是我做错了其他步骤?

【问题讨论】:

  • 您需要与即将到来的层中的神经元一样多的偏差值。
  • @JahKnows 但是它并没有被添加到H_output,因为它们的大小变得不同了。

标签: python numpy machine-learning artificial-intelligence backpropagation


【解决方案1】:

你的偏见的形状是正确的。最后从偏差中减去的渐变形状应该是(2,1)而不是(2,2) - 这是您的问题(H_gradient的形状不应该是(2,2))。

还有,

#Calculate output error : O_error = O_output - target

在这里,您应该使用均方误差(分类时的交叉熵损失),而不仅仅是差异。

最后,梯度是根据权重和偏差分别计算的。所以 dL/dW 是单独计算并从权重中减去,dL/db 是单独计算并从偏差中减去(其中 L 是损失)。您只是对两者使用相同的渐变。

【讨论】:

  • 您能否更具体一些并告诉我应该如何更改我的代码?我计算H_gradient 错误吗?因为梯度的大小是由它的计算决定的
猜你喜欢
  • 1970-01-01
  • 2011-08-13
  • 2016-09-08
  • 1970-01-01
  • 2016-08-12
  • 1970-01-01
  • 2010-12-02
  • 1970-01-01
  • 2017-12-06
相关资源
最近更新 更多