【问题标题】:How can I set Bias and change Sigmoid to ReLU function in ANN?如何在 ANN 中设置 Bias 并将 Sigmoid 更改为 ReLU 函数?
【发布时间】:2018-12-21 00:12:09
【问题描述】:

我正在尝试通过人工神经网络创建数据预测模型。以下代码是通过许多书籍创建的基于 Python 的 ANN 代码的一部分。此外,预测值与实际值之间的误差率不会低于 19%。我尝试增加隐藏层的数量,但并没有对错误率产生太大影响。我认为这可能是 Sigmoid 函数的限制,没有考虑 Bias。环顾了一个月,找到了如何构建 ReLU 和 Bias,但找不到 Bias 和 ReLU 的范围。

Q1 = 如何将 Sigmoid 转换为 ReLU,Q2 = 如何将 Bias 添加到我的代码中?

Q3 = 另外,如果我将 Sigmoid 更改为 ReLU,我是否必须将我的数据集设置为 0.0~1.0 范围?这是因为 Sigmoid 函数接受 0.0~1.0 范围的数据,但我不知道 ReLU 允许什么范围。

很抱歉提出一个基本问题。

class neuralNetwork:
# initialize the neural network
def __init__(self, input_nodes, hidden_nodes, output_nodes, learning_rate):

#
    self.inodes = input_nodes
    self.hnodes = hidden_nodes
    self.onodes = output_nodes

    # link weight matrices, wih and who
    self.wih = numpy.random.normal(0.0, pow(self.hnodes, -0.5), (self.hnodes, self.inodes))
    self.who = numpy.random.normal(0.0, pow(self.onodes, -0.5), (self.onodes, self.hnodes))

    # learning rate
    self.lr = learning_rate

    # activation function is the sigmoid function
    self.activation_function = lambda x: scipy.special.expit(x)

    pass

# train the neural network
def train(self, inputs_list, targets_list):
    # convert inputs list to 2d array
    inputs = numpy.array(inputs_list, ndmin=2).T
    targets = numpy.array(targets_list, ndmin=2).T

    # calculate signals into hidden layer
    hidden_inputs = numpy.dot(self.wih, inputs)
    # calculate the signals emerging from hidden layer
    hidden_outputs = self.activation_function(hidden_inputs)

    # calculate signals into final output layer
    final_inputs = numpy.dot(self.who, hidden_outputs)
    # calculate the signals emerging from final output layer
    final_outputs = self.activation_function(final_inputs)

    # output layer error is the (target - actual)
    output_errors = targets - final_outputs
    # hidden layer error is the output_errors, split by weights, recombined at hidden nodes
    hidden_errors = numpy.dot(self.who.T, output_errors)

    # update the weights for the links between the hidden and output layers
    self.who += self.lr*numpy.dot((output_errors*final_outputs*(1.0-final_outputs)), numpy.transpose(hidden_outputs))
    # update the weights for the links between the input and output layers
    self.wih += self.lr*numpy.dot((hidden_errors*hidden_outputs*(1.0-hidden_outputs)), numpy.transpose(inputs))

    pass

# query the neural network
def query(self, inputs_list) :

    inputs = numpy.array(inputs_list, ndmin=2).T

    # convert hidden list to 2d array
    hidden_inputs = numpy.dot(self.wih, inputs)
    # calculate signals into hidden layer
    hidden_outputs = self.activation_function(hidden_inputs)


    final_inputs = numpy.dot(self.who, hidden_outputs)
    final_outputs = self.activation_function(final_inputs)
    return final_outputs        
    pass

【问题讨论】:

    标签: python neural-network deep-learning artificial-intelligence


    【解决方案1】:

    你的问题太宽泛了,ReLU vs sigmoid 背后有很多概念。

    但总之:
    Sigmoid 饱和并杀死梯度(看Gradient descent)sigmoid 不是以零为中心的,因为sigmoid 的输出是0<output<1。我可以看到你正在使用的 sigmoid scipy 但对于 ReLU 来说很容易。 Relu由以下函数定义

    f(x) = max(0,x)
    

    这意味着如果输入大于零,则返回输入,否则返回 0。ReLU 更适用于隐藏层,其他类似 softmax 的输出层。

    我想说,看看不同的激活函数以及为什么我们需要神经网络上的激活函数。 sigmoid 如何杀死梯度以及它们收敛缓慢的原因。

    Q1 = 如何将 Sigmoid 转换为 ReLU,Q2 = 如何将 Bias 添加到我的代码中?
    只需根据上面的 ReLU 函数自己写一个方法,然后更新下面这行

    self.activation_function = max(0,x) # instead of lambda x: scipy.special.expit(x)
    

    Q3 = 另外,如果我将 Sigmoid 更改为 ReLU,我是否必须使我的数据集在 0.0~1.0 范围内?这是因为 Sigmoid 函数接受 0.0~1.0 范围的数据,但我不知道 ReLU 允许什么范围。

    这个问题的答案取决于您的网络和数据,但是您可以对数据进行标准化。并且没有需要将数据放入的范围。因为对于 ReLU:如果 input 小于零,它将返回 0,如果 input >= 0,它将返回 @ 987654333@。所以没有像 sigmoid 这样的范围。 Answer of this question

    如果您想了解 ReLU 的工作原理和使用方法,以下详细示例将有所帮助,尽管这些示例是使用框架 (PyTorch) 编写的,用于构建网络和训练。

    • PyTorch 基础项目Link
    • ReLU vs sigmoid vs TanH Video

    【讨论】:

      猜你喜欢
      • 2017-10-08
      • 2021-07-16
      • 2011-11-02
      • 2015-11-09
      • 2021-09-30
      • 2020-04-11
      • 2018-08-08
      • 2020-04-23
      • 2013-09-13
      相关资源
      最近更新 更多