Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: Duplicate node name in graph #36509

Closed
AIChuY opened this issue Feb 6, 2020 · 4 comments
Closed

ValueError: Duplicate node name in graph #36509

AIChuY opened this issue Feb 6, 2020 · 4 comments
Assignees
Labels
comp:keras Keras related issues TF 2.1 for tracking issues in 2.1 release type:support Support issues

Comments

@AIChuY
Copy link

AIChuY commented Feb 6, 2020

I am trying to run following code(which comes from Chollet's Deep Learning with Python) on a kaggle kernel,but get an error

import keras
from keras import layers
from keras import backend as K
from keras.models import Model
import numpy as np
img_shape=(28,28,1)
batch_size=16
latent_dim=2
input_img=keras.Input(shape=img_shape)
x=layers.Conv2D(32,3,padding='same',activation='relu')(input_img)
x=layers.Conv2D(64,3,padding='same',activation='relu',strides=(2,2))(x)
x=layers.Conv2D(64,3,padding='same',activation='relu')(x)
x=layers.Conv2D(64,3,padding='same',activation='relu')(x)
shape_before_flattening=K.int_shape(x)
x=layers.Flatten()(x)
x=layers.Dense(32,activation='relu')(x)
z_mean=layers.Dense(latent_dim)(x)
z_log_var=layers.Dense(latent_dim)(x)
def sampling(args):
    z_mean,z_log_var=args
    epsilon=K.random_normal(shape=(K.shape(z_mean)[0],latent_dim),mean=0,stddev=1)
    return z_mean+K.exp(0.5*z_log_var)*epsilon
z=layers.Lambda(sampling)([z_mean,z_log_var])

the trace back is following

InvalidArgumentError                      Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
   1618   try:
-> 1619     c_op = c_api.TF_FinishOperation(op_desc)
   1620   except errors.InvalidArgumentError as e:

InvalidArgumentError: Duplicate node name in graph: 'lambda_7/random_normal/shape'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-12-6c91b1ce5964> in <module>
      3     epsilon=K.random_normal(shape=(K.shape(z_mean)[0],latent_dim),mean=0,stddev=1)
      4     return z_mean+K.exp(0.5*z_log_var)*epsilon
----> 5 z=layers.Lambda(sampling)([z_mean,z_log_var])

/opt/conda/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in symbolic_fn_wrapper(*args, **kwargs)
     73         if _SYMBOLIC_SCOPE.value:
     74             with get_graph().as_default():
---> 75                 return func(*args, **kwargs)
     76         else:
     77             return func(*args, **kwargs)

/opt/conda/lib/python3.6/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
    504             if all([s is not None
    505                     for s in to_list(input_shape)]):
--> 506                 output_shape = self.compute_output_shape(input_shape)
    507             else:
    508                 if isinstance(input_shape, list):

/opt/conda/lib/python3.6/site-packages/keras/layers/core.py in compute_output_shape(self, input_shape)
    672                     xs = [K.placeholder(shape=shape, dtype=dtype)
    673                           for shape, dtype in zip(input_shape, self._input_dtypes)]
--> 674                     x = self.call(xs)
    675                 else:
    676                     x = K.placeholder(shape=input_shape, dtype=self._input_dtypes)

/opt/conda/lib/python3.6/site-packages/keras/layers/core.py in call(self, inputs, mask)
    714         else:
    715             self._input_dtypes = K.dtype(inputs)
--> 716         return self.function(inputs, **arguments)
    717 
    718     def compute_mask(self, inputs, mask=None):

<ipython-input-12-6c91b1ce5964> in sampling(args)
      1 def sampling(args):
      2     z_mean,z_log_var=args
----> 3     epsilon=K.random_normal(shape=(K.shape(z_mean)[0],latent_dim),mean=0,stddev=1)
      4     return z_mean+K.exp(0.5*z_log_var)*epsilon
      5 z=layers.Lambda(sampling)([z_mean,z_log_var])

/opt/conda/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in random_normal(shape, mean, stddev, dtype, seed)
   4327     with tf_ops.init_scope():
   4328         return tf_keras_backend.random_normal(
-> 4329             shape, mean=mean, stddev=stddev, dtype=dtype, seed=seed)
   4330 
   4331 

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/keras/backend.py in random_normal(shape, mean, stddev, dtype, seed)
   5600     seed = np.random.randint(10e6)
   5601   return random_ops.random_normal(
-> 5602       shape, mean=mean, stddev=stddev, dtype=dtype, seed=seed)
   5603 
   5604 

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/ops/random_ops.py in random_normal(shape, mean, stddev, dtype, seed, name)
     67   """
     68   with ops.name_scope(name, "random_normal", [shape, mean, stddev]) as name:
---> 69     shape_tensor = tensor_util.shape_tensor(shape)
     70     mean_tensor = ops.convert_to_tensor(mean, dtype=dtype, name="mean")
     71     stddev_tensor = ops.convert_to_tensor(stddev, dtype=dtype, name="stddev")

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/tensor_util.py in shape_tensor(shape)
    992       # not convertible to Tensors becasue of mixed content.
    993       shape = tuple(map(tensor_shape.dimension_value, shape))
--> 994   return ops.convert_to_tensor(shape, dtype=dtype, name="shape")
    995 
    996 

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
   1312 
   1313     if ret is None:
-> 1314       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
   1315 
   1316     if ret is NotImplemented:

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/ops/array_ops.py in _autopacking_conversion_function(v, dtype, name, as_ref)
   1366   elif dtype != inferred_dtype:
   1367     v = nest.map_structure(_cast_nested_seqs_to_dtype(dtype), v)
-> 1368   return _autopacking_helper(v, dtype, name or "packed")
   1369 
   1370 

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/ops/array_ops.py in _autopacking_helper(list_or_tuple, dtype, name)
   1302           elems_as_tensors.append(
   1303               constant_op.constant(elem, dtype=dtype, name=str(i)))
-> 1304       return gen_array_ops.pack(elems_as_tensors, name=scope)
   1305     else:
   1306       return converted_elems

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/ops/gen_array_ops.py in pack(values, axis, name)
   5702   axis = _execute.make_int(axis, "axis")
   5703   _, _, _op, _outputs = _op_def_library._apply_op_helper(
-> 5704         "Pack", values=values, axis=axis, name=name)
   5705   _result = _outputs[:]
   5706   if _execute.must_record_gradient():

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/op_def_library.py in _apply_op_helper(op_type_name, name, **keywords)
    740       op = g._create_op_internal(op_type_name, inputs, dtypes=None,
    741                                  name=scope, input_types=input_types,
--> 742                                  attrs=attr_protos, op_def=op_def)
    743 
    744     # `outputs` is returned as a separate return value so that the output

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/func_graph.py in _create_op_internal(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_device)
    593     return super(FuncGraph, self)._create_op_internal(  # pylint: disable=protected-access
    594         op_type, inputs, dtypes, input_types, name, attrs, op_def,
--> 595         compute_device)
    596 
    597   def capture(self, tensor, name=None, shape=None):

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in _create_op_internal(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_device)
   3320           input_types=input_types,
   3321           original_op=self._default_original_op,
-> 3322           op_def=op_def)
   3323       self._create_op_helper(ret, compute_device=compute_device)
   3324     return ret

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in __init__(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def)
   1784           op_def, inputs, node_def.attr)
   1785       self._c_op = _create_c_op(self._graph, node_def, grouped_inputs,
-> 1786                                 control_input_ops)
   1787       name = compat.as_str(node_def.name)
   1788     # pylint: enable=protected-access

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
   1620   except errors.InvalidArgumentError as e:
   1621     # Convert to ValueError for backwards compatibility.
-> 1622     raise ValueError(str(e))
   1623 
   1624   return c_op

ValueError: Duplicate node name in graph: 'lambda_7/random_normal/shape'

This is quite strange as I can run these codes normally on my laptop.I wonder whether it comes
from difference of environment,but I don't know how to check the environment of kaggle kernel

@ravikyram
Copy link
Contributor

@AIChuY

Which version of TensorFlow you are using. I am not seeing any issue with Tensor Flow 1.14 and 1.15. Please, find the gist here.If you are using TF 2.X you should use tf.keras instead.Thanks!

@ravikyram ravikyram added the stat:awaiting response Status - Awaiting response from author label Feb 7, 2020
@AIChuY
Copy link
Author

AIChuY commented Feb 7, 2020

@AIChuY

Which version of TensorFlow you are using. I am not seeing any issue with Tensor Flow 1.14 and 1.15. Please, find the gist here.If you are using TF 2.X you should use tf.keras instead.Thanks!

It seems that kaggle kernel is using a tensorflow 2.1.0. I have managed to rewrite the codes and
everything is ok now.
Thanks for your help!

@AIChuY AIChuY closed this as completed Feb 7, 2020
@ravikyram ravikyram added comp:keras Keras related issues TF 2.1 for tracking issues in 2.1 release type:support Support issues and removed stat:awaiting response Status - Awaiting response from author labels Feb 7, 2020
@dorafan
Copy link

dorafan commented Apr 8, 2020

@AIChuY
Which version of TensorFlow you are using. I am not seeing any issue with Tensor Flow 1.14 and 1.15. Please, find the gist here.If you are using TF 2.X you should use tf.keras instead.Thanks!

It seems that kaggle kernel is using a tensorflow 2.1.0. I have managed to rewrite the codes and
everything is ok now.
Thanks for your help!

I have the seem question as you. Could you please post your new codes?

@Melvin-Var
Copy link

@AIChuY
Which version of TensorFlow you are using. I am not seeing any issue with Tensor Flow 1.14 and 1.15. Please, find the gist here.If you are using TF 2.X you should use tf.keras instead.Thanks!

It seems that kaggle kernel is using a tensorflow 2.1.0. I have managed to rewrite the codes and
everything is ok now.
Thanks for your help!

I have the seem question as you. Could you please post your new codes?

You need to replace the line you import the keras backend with:

from tensorflow.keras import backend as K

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:keras Keras related issues TF 2.1 for tracking issues in 2.1 release type:support Support issues
Projects
None yet
Development

No branches or pull requests

4 participants