使用tensorflow引入了一个新层

Introduced a new layer using tensorflow

我想在 tensorflow 中引入一个新层作为激活函数。但是,存在无法解决的错误。 这是新层的代码。

def smooth_relu(tensor):
    e=0.15
    alpha=0.005

    def smooth(tensor):

            smoothtensor=tf.cond(tensor<(e+alpha) ,lambda: (tensor-alpha)*(tensor-alpha),lambda:e*((tensor-alpha)-self.e*0.5),    tf.cond(
                        pred,
                        true_fn=None,
                        false_fn=None,
                        strict=False,
                        name=None,
                        fn1=None,
                        fn2=None
                        ))


            return (smoothtensor)



    newtensor=tf.cond(tensor<0 ,lambda :0, lambda:smooth(tensor))
    # In addition to return the result, we return my_random for initializing on each
    # iteration and alpha to check the final value used.

    return (newtensor)

这是错误。

ValueError: Shape must be rank 0 but is rank 2 for 'cond/Switch' (op: 'Switch') with input shapes: [1,1], [1,1].

那是因为 smoothtensor 没有属性可以分配给 dtype

你的错在这一行:

def smooth(tensor): `smoothtensor=tf.cond(tensor<(e+alpha) ,lambda: (tensor-alpha)*(tensor-alpha),lambda:e*((tensor-alpha)-self.e*0.5),dtype=tf.float32)`

您可以分配的可用属性:

tf.cond(
    pred,
    true_fn=None,
    false_fn=None,
    strict=False,
    name=None,
    fn1=None,
    fn2=None
)

编辑:

只需在行代码的末尾添加一个括号...它丢失了