我们什么时候不需要激活函数?

when do we not need activation function?

我写了一个非常基本的张量流模型,我想在其中预测一个数字:

import tensorflow as tf
import numpy as np


def HW_numbers(x):
    y = (2 * x) + 1
    return y

x = np.array([1.0,2.0,3.0,4.0,5.0,6.0,7.0], dtype=float)
y = np.array(HW_numbers(x))

model = tf.keras.models.Sequential([tf.keras.layers.Dense(units=1,input_shape=[1])])
model.compile(optimizer='sgd',loss='mean_squared_error')
model.fit(x,y,epochs = 30)

print(model.predict([10.0])) 

上面的代码工作正常。但是如果我在 Dense 层中添加一个激活函数,预测就会变得很奇怪。我试过'relu'、'sigmoid'、'tanh'等

我的问题是,这是为什么?激活函数在扰乱预测的单层中到底做了什么? 我用过 Tensorflow 2.0

您的网络仅包含一个神经元。所以它在没有激活函数的情况下所做的是将你的输入乘以神经元权重。这个权重最终会收敛到 2.1 左右。

  • 但是使用 relu 作为激活函数,只有正数会通过您的网络传播。所以如果你的神经元的权重是用负数初始化的,你将总是得到零作为输出。所以有了relu,你就有50:50的机会得到好的结果。
  • 使用激活函数tanhsigmoid,神经元的输出被限制为[-1,1 ] 和 [0, 1],所以你的输出不能超过一个。

所以对于这么小的神经元网络,这些激活函数不匹配问题。

目前,您正在学习线性函数。因为它可以用单个神经元来描述,所以你只需要一个神经元来学习这个功能。另一方面 activation function 是:

to learn and make sense of something really complicated and Non-linear complex functional mappings between the inputs and response variable. It introduces non-linear properties to our Network. Their main purpose is to convert an input signal of a node in an A-NN to an output signal. That output signal now is used as an input in the next layer in the stack.

因此,由于这里只有一个神经元(特定情况),因此不需要将值传递给下一层。换句话说,所有隐藏层、输入层和输出层都合并在一起。因此,激活功能对您的情况没有帮助。除非你想根据神经元的输出做出决定。