tensorflow2.0使用GELU激活函数

首先,tensorflow2.0没有GELU激活函数,所以必须自定义GELU激活函数。

其次,自定义代码如下:

第一步:import tensorflow as tf

第二步:class GELU(layers.Layer):
    def __init__(self):
        super(GELU, self).__init__()

    def call(self, x):
        cdf = 0.5 * (1.0 + tf.tanh((np.sqrt(2 / np.pi) * (x + 0.044715 * tf.pow(x, 3)))))
        return x * cdf

第三步:

self.act=GRLU()

第四步:(call中使用)

x = self.act(x)

猜你喜欢

转载自blog.csdn.net/weiyuangong/article/details/125110681