Error-tf.function-decorated function tried to create variables on non-first call

ValueError: tf.function-decorated function tried to create variables on non-first call

在tensorflow2中如下保存模型的总是报错,很是头疼:

model.save(weight_file)

错误的位置定格在:

out_data = tf.keras.layers.Conv2D(filters, [3,3])(concat_data)

最终找到原因,需要先在__init__函数体中定义好,才能在call中进行调用:

    def __init__(self,**kwargs):
        super(Up_sampling, self).__init__(**kwargs)
        self.Conv2D_256 = tf.keras.layers.Conv2D(256, [3, 3])
    def call(self, input,filters):
        if filters==256:
            out_data = self.Conv2D_256(input)

这样就很好的解决问题了,错误消失!

你可能感兴趣的:(Tensorflow,Code-error,tensorflow,人工智能,python)