【问题标题】:ReLU() returns an error but Activation('relu') works fine on TensorFlow Functional APIReLU() 返回错误,但 Activation(\'relu\') 在 TensorFlow Functional API 上运行良好
【发布时间】:2022-12-20 22:16:24
【问题描述】:

根据文档 Activation('relu')ReLU() 应该产生类似的结果,除了 ReLU() 中的附加参数。

然而,

X = Activation('relu')(X)

工作正常。但

X=ReLU()(X)

给出以下错误:

NameError: name 'ReLU' is not defined

为什么是这样? ReLU() 不应该与函数式 API 一起使用吗?

【问题讨论】:

  • 需要先导入ReLU类:from tensorflow.keras.layers import ReLU

标签: tensorflow keras tensorflow2.0


【解决方案1】:

您需要了解激活函数和 ReLU。

它们不会总是返回相同的值,但 RelU 是 Rectified Linear Unit 激活函数,而激活 ReLU 是目标层激活 Fn。

[ 样本 ]:

import tensorflow as tf

layer = tf.keras.layers.ReLU()
output = layer([-3.0, -1.0, 0.0, 2.0])
print(output.numpy())
print( "================" )

print( tf.keras.layers.Dense(1, activation='relu')(tf.constant([-3.0, -1.0, 0.0, 2.0], shape=( 4, 1 )).numpy()) )

[ 输出 ]:

F:	empPython>python test_tf_ReLU.py
2022-05-10 12:38:02.190099: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-05-10 12:38:02.770833: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 4634 MB memory:  -> device: 0, name: NVIDIA GeForce GTX 1060 6GB, pci bus id: 0000:01:00.0, compute capability: 6.1
[0. 0. 0. 2.]
================
(None, 4, 1)
[[[0.       ]
  [0.       ]
  [0.       ]
  [2.0980666]]]

F:	empPython>

【讨论】:

    猜你喜欢
    • 2016-12-03
    • 2022-11-14
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    • 2017-12-31
    • 2021-01-18
    • 1970-01-01
    相关资源
    最近更新 更多