我最近一直在探索神经网络及其在应用程序中的应用。就在不久前,我看到一个教程,描述了一个神经网络,它可以学习如何对从0到9的手写数字进行分类(MNIST)。教程中我感到困惑的代码部分如下(https://pythonprogramming.net/tensorflow-neural-network-session-machine-learning-tutorial/)
def neural_network_model(data): hidden_1_layer = {'weights':tf.Variable(tf.random_normal([784, nodes_hl1])), 'biases':tf.Variable(tf.random_normal([nodes_hl1]))} hidden_2_layer = {'weights':tf.Variable(tf.random_normal([nodes_hl1, nodes_hl2])), 'biases':tf.Variable(tf.random_normal([nodes_hl2]))} hidden_3_layer = {'weights':tf.Variable(tf.random_normal([nodes_hl2, nodes_hl3])), 'biases':tf.Variable(tf.random_normal([nodes_hl3]))} output_layer = {'weights':tf.Variable(tf.random_normal([nodes_hl3, n_classes])), 'biases':tf.Variable(tf.random_normal([n_classes])),} l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']), hidden_1_layer['biases']) l1 = tf.nn.relu(l1) l2 = tf.add(tf.matmul(l1,hidden_2_layer['weights']), hidden_2_layer['biases']) l2 = tf.nn.relu(l2) l3 = tf.add(tf.matmul(l2,hidden_3_layer['weights']), hidden_3_layer['biases']) l3 = tf.nn.relu(l3) output = tf.matmul(l3,output_layer['weights']) + output_layer['biases'] return output
我对其中的基本操作有了一定的了解。这三个隐藏层各是一组由偏置和权重连接的节点。最终的输出层是神经网络的结果。我理解这里的所有内容,除了包括tf.nn.relu()的代码行。查看了TensorFlow的文档后,它只提到该函数计算特征的整流线性。我对这个函数的作用及其在神经网络中的重要性感到相当困惑。
回答:
整流线性单元(ReLU)的优势包括
- 计算成本较低(因此性能更好)
- 其他一些函数如sigmoid容易饱和
- 它们的导数易于计算(记住训练过程依赖于导数)