简体   繁体   English

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

[英]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.

相关问题 TensorFlow2.0.0 Alpha-模块'tensorflow'没有属性'gfile' - TensorFlow2.0.0 Alpha- module 'tensorflow' has no attribute 'gfile' Tensorflow 2.0.0 中的自定义损失 - Custom loss in Tensorflow 2.0.0 TensorFlow 2.0.0 Keras:使用自定义指标进行验证 - TensorFlow 2.0.0 Keras: Validation using custom metrics TensorFlow:指定层激活的存储 - TensorFlow: Specifying storage of layer activations 在张量流中创建自定义层时出错 - Error creating a custom layer in tensorflow 获取具有可变序列长度的激活时的Tensorflow GRU单元错误 - Tensorflow GRU cell error when fetching activations with variable sequence length 无法在“tensorflow.python.keras.activations”中导入“swish” - Cannot import 'swish' in 'tensorflow.python.keras.activations' 从Tensorflow中的图像的自定义数据集创建批次 - Creating batches from custom dataset of images in Tensorflow Tensorflow a2.0.0:将CSV转换为tfrecord,创建一个Keras模型,该模型使用来自大型来源的管道数据并将权重存储到CSV文件? - Tensorflow a2.0.0: Converting CSV to a tfrecord, creating a Keras model that uses pipelined data from a large source, storing weights to a CSV file? 在 tensorflow keras 中创建熵作为自定义损失函数 - creating entropy as a custom loss function in tensorflow keras
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM