使用张量流引入了新的层

时间:2018-12-26 21:40:48

标签: python tensorflow deep-learning reinforcement-learning

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

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].

1 个答案:

答案 0 :(得分: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
)

编辑:

只需在行代码的末尾添加一个括号,就可以了