andrewgordonwilson / bayesgan

Tensorflow code for the Bayesian GAN (https://arxiv.org/abs/1705.09558) (NIPS 2017)
Other
1.02k stars 176 forks source link

ValueError: Variable discriminator/d_bn1/moving_mean already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? #9

Closed howardgriffin closed 6 years ago

howardgriffin commented 6 years ago

I am using tf1.3,it shows as follow, how to solve this problem? Traceback (most recent call last): File "I:/python3/bayesgan-master/run_bgan_semi.py", line 413, in b_dcgan(dataset, args) File "I:/python3/bayesgan-master/run_bgan_semi.py", line 141, in b_dcgan num_classes=dataset.num_classes) File "I:\python3\bayesgan-master\bgan_semi.py", line 78, in init self.build_bgan_graph() File "I:\python3\bayesgan-master\bgan_semi.py", line 274, in build_bgan_graph self.K, disc_params) File "I:\python3\bayesgan-master\bgan_semi.py", line 401, in discriminator w=disc_params["d_h%i_W" % layer], biases=disc_params["d_h%i_b" % layer]), train=train)) File "I:\python3\bayesgan-master\dcgan_ops.py", line 46, in call scope=self.name) File "D:\ProgramData\tensorflow\lib\site-packages\tensorflow\contrib\framework\python\ops\arg_scope.py", line 181, in func_with_args return func(*args, current_args) File "D:\ProgramData\tensorflow\lib\site-packages\tensorflow\contrib\layers\python\layers\layers.py", line 592, in batch_norm scope=scope) File "D:\ProgramData\tensorflow\lib\site-packages\tensorflow\contrib\layers\python\layers\layers.py", line 373, in _fused_batch_norm collections=moving_mean_collections) File "D:\ProgramData\tensorflow\lib\site-packages\tensorflow\contrib\framework\python\ops\arg_scope.py", line 181, in func_with_args return func(*args, *current_args) File "D:\ProgramData\tensorflow\lib\site-packages\tensorflow\contrib\framework\python\ops\variables.py", line 262, in model_variable use_resource=use_resource) File "D:\ProgramData\tensorflow\lib\site-packages\tensorflow\contrib\framework\python\ops\arg_scope.py", line 181, in func_with_args return func(args, current_args) File "D:\ProgramData\tensorflow\lib\site-packages\tensorflow\contrib\framework\python\ops\variables.py", line 217, in variable use_resource=use_resource) File "D:\ProgramData\tensorflow\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1203, in get_variable constraint=constraint) File "D:\ProgramData\tensorflow\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1092, in get_variable constraint=constraint) File "D:\ProgramData\tensorflow\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 425, in get_variable constraint=constraint) File "D:\ProgramData\tensorflow\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 394, in _true_getter use_resource=use_resource, constraint=constraint) File "D:\ProgramData\tensorflow\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 742, in _get_single_variable name, "".join(traceback.format_list(tb)))) ValueError: Variable discriminator/d_bn1/moving_mean already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:

File "D:\ProgramData\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py", line 1470, in init self._traceback = self._graph._extract_stack() # pylint: disable=protected-access File "D:\ProgramData\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py", line 2956, in create_op op_def=op_def) File "D:\ProgramData\tensorflow\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper op_def=op_def)

rajendraranabhat commented 6 years ago

I am also getting the same error. Has anyone fixed it?

ysaatchi commented 6 years ago

Make sure your tensorflow version is correct (should be 1.0.0 -- see README)

Spandan-Madan commented 6 years ago

I got the same error and this fixed it for me. It's a very silly issue it seems. If you're running your code in a jupyter notebook (as I was), the variables persist in the environment. So if you run once, and the code breaks, and you try to run it again the variable is still in memory and can't be re-created.

Simply restart your kernel and re-run, it'll work fine. Hope this helps! :)

TwistedW commented 6 years ago

If your tensorflow version is greater than 1.0, you need to add reuse to specify whether to repeat the call.like this:

def discriminator(self, image, K, disc_params, train=True, reuse=False):
    with tf.variable_scope("discriminator", reuse=reuse) as scope:
     ...

Of course, you need to add reuse when you use it.

d_probs, d_logits, _ = self.discriminator(self.inputs, self.K, disc_params,reuse=False)
...
d_probs_, d_logits_, _ = self.discriminator(self.generator(self.z[:, :, gi % self.num_gen], gen_params),
                                                            self.K, disc_params, reuse=True
...

The same operation for generator.Hope this helps!