使用tensorflow引入了一个新层

问题描述 投票:0回答:1

我想在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].
python tensorflow deep-learning reinforcement-learning
1个回答
0
投票

那是因为没有为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
)

编辑:

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

© www.soinside.com 2019 - 2024. All rights reserved.