很抱歉问这个愚蠢的问题。我正在试验 Keras 框架,由于更复杂的设置中存在收敛问题,我现在正在逐步进行。
我用 relu 建立了一个非常简单的 1 节点神经网络。然而,根据我的设置方式,relu 的行为符合预期,或者错误地作为线性恒等映射。
解决方案 1:输入节点 -> 身份通过 relu 激活传递到节点 -> 身份传递到输出节点 [
解决方案 2:输入节点 -> 身份通过 ReLU 激活传递到输出节点 [
解决方案 3:输入节点 -> 身份传递 -> relu 激活 -> 身份传递到输出节点 [蓝色 =
有什么线索可以解释为什么解决方案 1 不起作用吗?
[红色和蓝色曲线重叠在
我担心如果将 RELU 函数放入网络的不同位置或以不同的方式,其功能会有所不同。
注意:GELU/SIGMOID/等似乎不受此问题的影响;只需在下面设置
mm = "sigmoid"
或 mm = "gelu"
。
#### load libraries
library(tensorflow)
library(keras)
#### define a simple test grid
x = as_tensor(-5+10*(1:1e3)/1e3, dtype = tf$float32)
#### direct pass through of the input to output
dum1 = list(matrix(1,1,1), as.array(0, dim = 1))
mm = "relu"
#### does not work as planned; yields linear, not RELU ####
model <- keras_model_sequential(input_shape = c(1, 1)) %>%
layer_flatten() %>%
layer_dense(1, activation = mm, weights = dum1) %>%
layer_dense(1, weights = dum1)
plot(x,predict(model, x), type = "l", col = "black")
#### works as planned ####
model <- keras_model_sequential(input_shape = c(1, 1)) %>%
layer_flatten() %>%
layer_dense(1, activation = mm, weights = dum1)
lines(x,predict(model, x), type = "l", col = "red")
#### works as planned ####
model <- keras_model_sequential(input_shape = c(1, 1)) %>%
layer_flatten() %>%
layer_activation_relu() %>%
layer_dense(1, weights = dum1)
lines(x,predict(model, x), type = "l", col = "blue")
我用谷歌搜索不同的答案和手册,但无济于事。以上是我的问题,只剩下熊的必需品。
对于所有权重均为 1 的情况,这意味着神经元的输出(ReLU 之前)只是输入的总和。
现在,让我们考虑 ReLU 对输入总和的影响:
鉴于这种行为,如果输入的总和(即 x,并且由于某些 x 是负数)是负数,ReLU 会将其设置为零,但是 如果总和为正数,ReLU 会将其保持不变。
因此,当权重设置为 1 时,此场景中的 ReLU 激活表现为正输入或零输入的线性变换,并将负输入设置为零,这可能会在输入值范围内呈现线性响应。
注意:我的计算机上没有 R,您可以通过监视输入的总和来检查这是否是原因吗?