[英]Creating custom activations in TensorFlow2.0.0
I am working on a project on Colab.我正在 Colab 上做一个项目。 I want to create a custom activation to use in TensorFlow 2.0.0 as follows:我想创建一个自定义激活以在 TensorFlow 2.0.0 中使用,如下所示:
def custom_activation(x):
return tf.math.log(x)
model = tf.keras.models.Sequential([
... # some layers
tf.keras.layers.Dense(10, activation=custom_activation),
tf.keras.layers.Dense(1)
])
While training I see the following:在训练时,我看到以下内容:
Epoch 1/100
9/Unknown - 6s 674ms/step - loss: nan - mae: nan
Why is the loss and mae nan?为什么是亏和湄南? From my understanding TF2.0.0 has eager execution enabled.根据我的理解,TF2.0.0 启用了急切执行。 So wouldn't this mean I could evaluate tf.math.log(x) without setting up a Session?那么这是否意味着我可以在不设置 Session 的情况下评估 tf.math.log(x)? The custom activation seems to work for other variations such as tf.math.abs(x).自定义激活似乎适用于其他变体,例如 tf.math.abs(x)。 Any idea what I'm doing wrong here?知道我在这里做错了什么吗? Is it because of Colab or my choice of activation?是因为 Colab 还是我选择的激活方式? Any help appreciated.任何帮助表示赞赏。 Thanks in advance.提前致谢。
You cannot really just use the logarithm as an activation function, as it is not defined for values x <= 0.0
, so if at any point the Dense layer produces a negative or zero value, the logarithm will produce nan
, which then propagates to the loss.你不能真的只使用对数作为激活 function,因为它没有为值x <= 0.0
定义,所以如果 Dense 层在任何时候产生负值或零值,对数将产生nan
,然后传播到失利。
You can easily test this as:您可以轻松地将其测试为:
import tensorflow as tf
print(tf.math.log(-1.0))
Which produces:哪个产生:
<tf.Tensor: id=1, shape=(), dtype=float32, numpy=nan>
So its not a programming problem, but a mathematical understanding one.所以这不是一个编程问题,而是一个数学理解问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.