将Tensorflow 2.x与1.x代码一起使用时,会出现以下错误:
代码:
# load VAE model
import tensorflow.compat.v1 as tf #version - 2.3.0
tf.disable_v2_behavior()
tf.Variable
tf.enable_eager_execution
tf.variable_scope
config = process_config('NAB_config.json')
create_dirs([config['result_dir'], config['checkpoint_dir']])
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
data = DataGenerator(config)
model_vae = VAEmodel(config)
trainer_vae = vaeTrainer(sess, model_vae, data, config)
model_vae.load(sess)错误:
AttributeError Traceback (most recent call last)
<ipython-input-61-30d2f020e3b6> in <module>
16 data = DataGenerator(config)
17 # create a CNN model
---> 18 model_vae = VAEmodel(config)
19 # create a CNN model
20 trainer_vae = vaeTrainer(sess, model_vae, data, config)
~\VAE-LSTM-for-anomaly-detection-master\VAE-LSTM-for-anomaly-detection-master\codes\models.py in __init__(self, config)
12 class VAEmodel(BaseModel):
13 def __init__(self, config):
---> 14 super(VAEmodel, self).__init__(config)
15 self.input_dims = self.config['l_win'] * self.config['n_channel']
16
~\VAE-LSTM-for-anomaly-detection-master\VAE-LSTM-for-anomaly-detection-master\codes\base.py in __init__(self, config)
27 self.config = config
28 # init the global step
---> 29 self.init_global_step()
30 # init the epoch counter
31 self.init_cur_epoch()
~\VAE-LSTM-for-anomaly-detection-master\VAE-LSTM-for-anomaly-detection-master\codes\base.py in init_global_step(self)
60 def init_global_step(self):
61 # DON'T forget to add the global step tensor to the tensorflow trainer
---> 62 with tf.variable_scope('global_step'):
63 self.global_step_tensor = tf.Variable(0, trainable=False, name='global_step')
64 self.increment_global_step_tensor = tf.assign(发布于 2020-11-09 12:20:27
TensorFlow 2.0中没有tf.variable_scope()
每个v1.variable_scope都应该转换成一个Python对象。通常,这将是以下之一:
tf.keras.layers.Layer
tf.keras.Model
tf.Module请按照此处的迁移指南进行操作Migrate your TensorFlow 1 code to TensorFlow 2
发布于 2020-11-16 23:38:37
已从TensorFlow 2.0中删除了tf.variable_scope()。
发布于 2021-04-07 16:06:39
安装v2包后调用v1功能的解决方案如下:
使用tf.compat.v1.variable_scope()而不是tf.variable_scope()
还有一个名为tf_upgrade_v2的工具,可以将任何Tensorflow v1代码迁移到v2。
https://stackoverflow.com/questions/64743987
复制相似问题