在 TensorFlow2.0.0 中创建自定义激活

Creating custom activations in TensorFlow2.0.0

我正在 Colab 上做一个项目。我想创建一个自定义激活以在 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)
])

训练时我看到以下内容:

Epoch 1/100
      9/Unknown - 6s 674ms/step - loss: nan - mae: nan

为什么是亏和湄南? 根据我的理解,TF2.0.0 启用了急切执行。那么这是否意味着我可以在不设置会话的情况下评估 tf.math.log(x) ?自定义激活似乎适用于其他变体,例如 tf.math.abs(x)。知道我在这里做错了什么吗?是因为 Colab 还是我选择的激活方式?任何帮助表示赞赏。提前致谢。

你不能真的只使用对数作为激活函数,因为它没有为值 x <= 0.0 定义,所以如果在任何时候密集层产生负值或零值,对数将产生 nan,然后传播到损失。

您可以轻松地进行测试:

import tensorflow as tf
print(tf.math.log(-1.0))

产生:

<tf.Tensor: id=1, shape=(), dtype=float32, numpy=nan>

所以这不是编程问题,而是数学理解问题。