首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Tensorflow时出错:没有属性“”variable_scope“”

使用Tensorflow时出错:没有属性“”variable_scope“”
EN

Stack Overflow用户
提问于 2020-11-09 07:13:30
回答 3查看 1.3K关注 0票数 0

将Tensorflow 2.x与1.x代码一起使用时,会出现以下错误:

代码:

代码语言:javascript
复制
# 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)

错误:

代码语言:javascript
复制
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(
EN

回答 3

Stack Overflow用户

发布于 2020-11-09 12:20:27

TensorFlow 2.0中没有tf.variable_scope()

每个v1.variable_scope都应该转换成一个Python对象。通常,这将是以下之一:

代码语言:javascript
复制
tf.keras.layers.Layer
tf.keras.Model
tf.Module

请按照此处的迁移指南进行操作Migrate your TensorFlow 1 code to TensorFlow 2

票数 0
EN

Stack Overflow用户

发布于 2020-11-16 23:38:37

已从TensorFlow 2.0中删除了tf.variable_scope()

official doc

票数 0
EN

Stack Overflow用户

发布于 2021-04-07 16:06:39

安装v2包后调用v1功能的解决方案如下:

使用tf.compat.v1.variable_scope()而不是tf.variable_scope()

还有一个名为tf_upgrade_v2的工具,可以将任何Tensorflow v1代码迁移到v2。

更多信息:https://www.tensorflow.org/guide/upgrade

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64743987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档