ValueError:变量嵌入已经存在,不允许。您的意思是在 VarScope 中设置 reuse=True 吗?最初定义

Posted

技术标签:

【中文标题】ValueError:变量嵌入已经存在,不允许。您的意思是在 VarScope 中设置 reuse=True 吗?最初定义【英文标题】:ValueError: Variable Embedding already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined 【发布时间】:2018-05-22 20:58:45 【问题描述】:

基于此 github 链接 https://github.com/brightmart/text_classification/tree/master/a03_TextRNN 当我使用 google_news_wor22vec.bin包含我的文档 + 标签的文本文件 运行 train a03_TextRNN 时,我有这些错误:

我该如何解决这个问题?

    Traceback (most recent call last):
  File "p8_TextRNN_train.py", line 184, in <module>
    tf.app.run()
  File "/home/eslami/anaconda3/lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "p8_TextRNN_train.py", line 68, in main
    vocab_size, FLAGS.embed_size, FLAGS.is_training)
  File "/home/eslami/Downloads/all-kind-text_classification-master/a03_TextRNN/p8_TextRNN_model.py", line 33, in init
    self.instantiate_weights()
  File "/home/eslami/Downloads/all-kind-text_classification-master/a03_TextRNN/p8_TextRNN_model.py", line 45, in instantiate_weights
    self.Embedding = tf.get_variable("Embedding",shape=[self.vocab_size, self.embed_size],initializer=self.initializer) #[vocab_size,embed_size] tf.random_uniform([self.vocab_size, self.embed_size],-1.0,1.0)
  File "/home/eslami/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 1065, in get_variable
    use_resource=use_resource, custom_getter=custom_getter)
  File "/home/eslami/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 962, in get_variable
    use_resource=use_resource, custom_getter=custom_getter)
  File "/home/eslami/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 367, in get_variable
    validate_shape=validate_shape, use_resource=use_resource)
  File "/home/eslami/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 352, in _true_getter
    use_resource=use_resource)
      File "/home/eslami/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 664, in _get_single_variable
        name, "".join(traceback.format_list(tb))))

    ValueError: Variable Embedding already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:
File "/home/eslami/Downloads/all-kind-text_classification-master/a03_TextRNN/p8_TextRNN_model.py", line 45, in instantiate_weights
    self.Embedding = tf.get_variable("Embedding",shape=[self.vocab_size, self.embed_size],initializer=self.initializer) #[vocab_size,embed_size] tf.random_uniform([self.vocab_size, self.embed_size],-1.0,1.0)
  File "/home/eslami/Downloads/all-kind-text_classification-master/a03_TextRNN/p8_TextRNN_model.py", line 33, in init
    self.instantiate_weights()
  File "/home/eslami/Downloads/all-kind-text_classification-master/a03_TextRNN/p8_TextRNN_model.py", line 123, in test
    textRNN=TextRNN(num_classes, learning_rate, batch_size, decay_steps, decay_rate,sequence_length,vocab_size,embed_size,is_training)

【问题讨论】:

【参考方案1】:

这已经很晚了,但是我在寻找解决类似问题时发现了您的帖子。如果其他人在这里找到了方法,请在模型的开头添加此代码将解决问题:

tf.reset_default_graph()

【讨论】:

以上是关于ValueError:变量嵌入已经存在,不允许。您的意思是在 VarScope 中设置 reuse=True 吗?最初定义的主要内容,如果未能解决你的问题,请参考以下文章

如何解决 Python 中的“ValueError:找到样本数量不一致的输入变量”问题

ValueError:`class_weight`必须包含数据中的所有类。类{1,2,3}存在于数据中,但不存在于`class_weight`中

因为该组节的条件字段不存在或无效所以无法打印

带 f 字符串的花括号 - ValueError:字符串格式说明符中不允许符号 [重复]

ValueError:发现样本数量不一致的输入变量:[143, 426]

ValueError:发现样本数量不一致的输入变量:[4, 304]