简体   繁体   English

如何在ANN中设置偏置并将Sigmoid更改为ReLU功能?

[英]How can I set Bias and change Sigmoid to ReLU function in ANN?

I'm trying to create a data prediction model through artificial neural networks. 我正在尝试通过人工神经网络创建数据预测模型。 The following code is part of the Python-based ANN code created through many books. 以下代码是通过许多书籍创建的基于Python的ANN代码的一部分。 Also, the error rate between the predicted value and the actual value doesn't meet below 19%. 此外,预测值和实际值之间的错误率也不会低于19%。 I tried to increase the number of hidden layers, but it did not tremendously affect the error rate. 我试图增加隐藏层的数量,但是并没有极大地影响错误率。 I think this is probably a limitation of Sigmoid function and not considering Bias. 我认为这可能是Sigmoid函数的局限性,而不考虑Bias。 I looked around for a month and found out how to build ReLU and Bias, but I could not find the range of Bias and ReLU. 我环顾了一个月,发现了如何构建ReLU和Bias,但是找不到Bias和ReLU的范围。

Q1 = How do I convert Sigmoid to ReLU and Q2 = how to add Bias to my code? Q1 =如何将Sigmoid转换为ReLU和Q2 =如何将Bias添加到我的代码中?

Q3 = Also, If I change Sigmoid to ReLU, do I have to make my dataset 0.0~1.0 range? Q3 =另外,如果我将Sigmoid更改为ReLU,是否必须将数据集的范围设置为0.0〜1.0? This is because Sigmoid function accepts 0.0~1.0 range of data, but I don't know what range ReLU allows. 这是因为Sigmoid函数接受0.0〜1.0范围的数据,但是我不知道ReLU允许的范围。

I'm sorry to ask an elementary question. 很抱歉问一个基本问题。

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

Your question is too broad and there are lots of concept behind ReLU vs sigmoid. 您的问题太笼统了,ReLU和Sigmoid背后有很多概念。

But in short: 简而言之:
Sigmoid Saturate and kill gradients (look at Gradient descent ) sigmoid are not zero centered because output of sigmoid is 0<output<1 . 乙状结肠饱和和终止梯度(请参见Gradient descent )不以零为中心,因为乙状结肠的输出为0<output<1 I can see for sigmoid you are using scipy but for ReLU its easy. 我可以看到对于Sigmoid,您正在使用scipy但对于ReLU来说,它很容易。 Relu is defined by the following function Relu由以下功能定义

f(x) = max(0,x)

This means if the input is greater then zero return input else return 0. And ReLU is prefered for hidden layers and other like softmax for output layers. 这意味着如果输入大于0,则返回零,否则返回0。对于隐藏层,首选ReLU,对于输出层,则首选softmax

I would say, look different activation function and why we need activation functions on neural net. 我会说,看起来不同的激活函数以及为什么我们需要神经网络上的激活函数。 How sigmoid kills gradients and why they slow converge. 乙状结肠如何杀死梯度,以及为什么它们会收敛。

Q1 = How do I convert Sigmoid to ReLU and Q2 = how to add Bias to my code? Q1 =如何将Sigmoid转换为ReLU和Q2 =如何将Bias添加到我的代码中?
simply write a method on your own based on the ReLU function above and update the following line 只需根据上面的ReLU函数自行编写一个方法,然后更新以下行

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

Q3 = Also, If I change Sigmoid to ReLU, do I have to make my dataset 0.0~1.0 range? Q3 =另外,如果我将Sigmoid更改为ReLU,是否必须将数据集的范围设置为0.0〜1.0? This is because Sigmoid function accepts 0.0~1.0 range of data, but I don't know what range ReLU allows. 这是因为Sigmoid函数接受0.0〜1.0范围的数据,但是我不知道ReLU允许的范围。

Answer of this question depends on your network and your data, but yes you normalize the data. 该问题的答案取决于您的网络和数据,但是可以,您可以对数据进行规范化。 And there is no such range that you need to make your data in. Because for ReLU: If the input is less than zero, it will return 0 and if input is >= 0, it will return the input . 并没有需要input数据的范围。因为对于ReLU:如果input小于零,则它将返回0 ,如果input > = 0,则将返回input So no such range like in sigmoid. 因此,没有像乙状结肠这样的范围。 Answer of this question 这个问题的答案

If you wanna look at how ReLU works and can be used, following detailed example will help though these examples are written using framework (PyTorch) to build the network and train. 如果您想了解ReLU的工作原理和使用方法,尽管使用框架(PyTorch)编写了这些示例以构建网络并进行培训,但下面的详细示例将有所帮助。

  • PyTorch Basic Projects Link PyTorch基础项目链接
  • ReLU vs sigmoid vs TanH Video ReLU vs乙状结肠vs TanH 视频

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM