ValueError: Variable attention/f_conv/conv2d/kernel already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
文章解释来源于
https://www.cnblogs.com/zymei/p/10721931.html
调用了两次attention,在第二次调用的时候报了上面这个错误。主要是因为第二次的变量名和第一次的变量名一样,导致了变量命名相同的冲突
在attention的这个函数里用到了tf.variable_scope()
说到这就要了解tensorflow里生成变量的两个方法:
tf.get_variable()和tf.Variable()
在tf.name_scope()的框架下使用这两种方法,使用tf.Variable(),尽管name一样,但为了不重复变量名,Tensorflow输出的变量名并不一样,所以本质上是不一样的变量;使用tf.get_variable()定义的变量虽然不会被tf.name_scope()中的名字影响,但在未指定共享变量时,如果重名了会报错。要实现变量共享,可以使用tf.variable_scope(reuse=tf.AUTO_REUSE)创建具有相同名称的作用域。
相关链接: