在 TensorFlow 2.4.1 中将 PRelu 激活实现为函数
Implementing PRelu activation as a function in TensorFlow 2.4.1
我正在尝试在此处给出的 tensorflow 2.4.1 中实现 PReLU 激活
出现以下错误
ValueError: Variable alpha already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?
这是我的代码
def prelu(_x):
alphas = tf.compat.v1.get_variable('alpha', _x.get_shape()[-1],
initializer=tf.constant_initializer(0.0), dtype=tf.float32)
pos = tf.nn.relu(_x)
neg = alphas * (_x - abs(_x)) * 0.5
return pos + neg
感谢任何帮助。
注:
我不想使用层接口 tf.keras.layers.PReLU,因为它不能作为参数传递给 conv2D,如下所示
Conv2D(filters, (3, 3), padding='same', activation='prelu')
这是 tensorflow as a function rather than layer which is available as a built-in activation layer and (I think that should be used), PRelu 中的 PRelu
实现。
def prelu_advanced(scope=None):
def prelu_plus(x):
with tf.compat.v1.variable_scope(name_or_scope=scope,
default_name="prelu", reuse=True):
alpha = tf.compat.v1.get_variable("prelu", shape=x.get_shape()[-1],
dtype=x.dtype, initializer=tf.constant_initializer(0.0))
pos = tf.nn.relu(x)
neg = alpha * (x - abs(x)) * 0.5
return tf.maximum(0.0, x) + alpha * tf.minimum(0.0, x)
return prelu_plus
检查
foo = tf.constant([1.0, -0.1, -1.0, 0.5, 0.5], dtype = tf.float32)
# layer (built-in)
tf.keras.layers.PReLU(alpha_initializer="zeros", alpha_regularizer=None,
alpha_constraint=None, shared_axes=None)(foo).numpy()
array([1. , 0. , 0. , 0.5, 0.5], dtype=float32)
# function
x = prelu_advanced(scope='prelu1')
x(foo).numpy()
array([1. , 0. , 0. , 0.5, 0.5], dtype=float32)
假人训练
input = tf.keras.Input(shape=(28, 28, 1))
x = tf.keras.layers.Flatten()(input)
x = tf.keras.layers.Dense(128, activation=prelu_advanced(scope='prelu'))(x)
y = tf.keras.layers.Dense(units=10, activation='softmax')(x)
func_model = tf.keras.Model(inputs=[input], outputs=[y])
func_model.compile(
loss = tf.keras.losses.CategoricalCrossentropy(),
metrics = tf.keras.metrics.CategoricalAccuracy(),
optimizer = tf.keras.optimizers.Adam())
func_model.fit(x_train, y_train)
4s 2ms/step - loss: 0.2690 - categorical_accuracy: 0.9242
我正在尝试在此处给出的 tensorflow 2.4.1 中实现 PReLU 激活
出现以下错误
ValueError: Variable alpha already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?
这是我的代码
def prelu(_x):
alphas = tf.compat.v1.get_variable('alpha', _x.get_shape()[-1],
initializer=tf.constant_initializer(0.0), dtype=tf.float32)
pos = tf.nn.relu(_x)
neg = alphas * (_x - abs(_x)) * 0.5
return pos + neg
感谢任何帮助。
注:
我不想使用层接口 tf.keras.layers.PReLU,因为它不能作为参数传递给 conv2D,如下所示
Conv2D(filters, (3, 3), padding='same', activation='prelu')
这是 tensorflow as a function rather than layer which is available as a built-in activation layer and (I think that should be used), PRelu 中的 PRelu
实现。
def prelu_advanced(scope=None):
def prelu_plus(x):
with tf.compat.v1.variable_scope(name_or_scope=scope,
default_name="prelu", reuse=True):
alpha = tf.compat.v1.get_variable("prelu", shape=x.get_shape()[-1],
dtype=x.dtype, initializer=tf.constant_initializer(0.0))
pos = tf.nn.relu(x)
neg = alpha * (x - abs(x)) * 0.5
return tf.maximum(0.0, x) + alpha * tf.minimum(0.0, x)
return prelu_plus
检查
foo = tf.constant([1.0, -0.1, -1.0, 0.5, 0.5], dtype = tf.float32)
# layer (built-in)
tf.keras.layers.PReLU(alpha_initializer="zeros", alpha_regularizer=None,
alpha_constraint=None, shared_axes=None)(foo).numpy()
array([1. , 0. , 0. , 0.5, 0.5], dtype=float32)
# function
x = prelu_advanced(scope='prelu1')
x(foo).numpy()
array([1. , 0. , 0. , 0.5, 0.5], dtype=float32)
假人训练
input = tf.keras.Input(shape=(28, 28, 1))
x = tf.keras.layers.Flatten()(input)
x = tf.keras.layers.Dense(128, activation=prelu_advanced(scope='prelu'))(x)
y = tf.keras.layers.Dense(units=10, activation='softmax')(x)
func_model = tf.keras.Model(inputs=[input], outputs=[y])
func_model.compile(
loss = tf.keras.losses.CategoricalCrossentropy(),
metrics = tf.keras.metrics.CategoricalAccuracy(),
optimizer = tf.keras.optimizers.Adam())
func_model.fit(x_train, y_train)
4s 2ms/step - loss: 0.2690 - categorical_accuracy: 0.9242