Tensorflow自定义层错误-变量不存在渐变

时间:2020-08-19 03:54:00

标签: python tensorflow tf.keras

我正在尝试运行在此链接中找到的教程ES-RNN代码。 timeseries forcasting tutorial

当我在安装了必需软件包(tensorflow = 1.12.0,keras == 2.2.4等)的conda环境中运行此程序时,我能够成功运行model.fit函数。

但是我想在最新的tensorflow版本(2.2)中运行它,所以我创建了另一个conda环境。另外,我在ES课上做了一些小的改动。例如,我使用from keras import backend as K

而不是from tensorflow.keras import backend as K

也没有K.slice函数,所以我改用tf.slice

一旦进行了更改,代码将无法运行,并且错误消息如下:

Train on 23328 samples, validate on 1488 samples
Epoch 1/1000
WARNING:tensorflow:Gradients do not exist for variables ['es/init_seasonality:0'] when minimizing the loss.
WARNING:tensorflow:Gradients do not exist for variables ['es/init_seasonality:0'] when minimizing the loss.
   48/23328 [..............................] - ETA: 47:29WARNING:tensorflow:Early stopping conditioned on metric `val_loss` which is not available. Available metrics are:  


###
###
###

TypeError: An op outside of the function building code is being passed
a "Graph" tensor. It is possible to have Graph tensors
leak out of the function building context by including a
tf.init_scope in your function building code.
For example, the following function will fail:
  @tf.function
  def has_init_scope():
    my_constant = tf.constant(1.)
    with tf.init_scope():
      added = my_constant * 2
The graph tensor has name: model/es/add_59:0


不仅如此,model.summary结果也与教程中显示的结果不同。

Model: "model"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            [(None, None, 1)]    0                                            
__________________________________________________________________________________________________
es (ES)                         [(48, 6, 1), (48, 3, 26          input_1[0][0]                    
__________________________________________________________________________________________________
gru (GRU)                       (48, 5)              120         es[0][0]                         
__________________________________________________________________________________________________
dense (Dense)                   (48, 3)              18          gru[0][0]                        
__________________________________________________________________________________________________
denormalization (Denormalizatio (48, 3)              0           dense[0][0]                      
                                                                 es[0][1]                         
==================================================================================================
Total params: 164
Trainable params: 164
Non-trainable params: 0

能否请您帮我解决此问题?我不确定应该修改ES类中的哪些代码以在tensorflow 2.2版本中工作?

谢谢

0 个答案:

没有答案