1

I used to use this code to train variational autoencoder (I found the code on a forum and adapted it to my needs) :

import pickle
from pylab import mpl,plt

#lecture des résultats

filename=r'XXX.pic'

data_file=open(filename,'rb')
X_sec = pickle.load(data_file)#[:,3000:]
data_file.close()

size=X_sec.shape[0]
prop=0.75

cut=int(size*prop)

X_train=X_sec[:cut]
X_test=X_sec[cut:]

std=X_train.std()

X_train /= std
X_test /= std

import keras
from keras import layers
from keras import backend as K
from keras.models import Model
import numpy as np

#encoding_dim = 12

sig_shape = (3600,)
batch_size = 128
latent_dim = 12
input_sig = keras.Input(shape=sig_shape)

x = layers.Dense(128, activation='relu')(input_sig)
x = layers.Dense(64, activation='relu')(x)
shape_before_flattening = K.int_shape(x)

x = layers.Dense(32, activation='relu')(x)

z_mean = layers.Dense(latent_dim)(x)
z_log_var = layers.Dense(latent_dim)(x)

encoder=Model(input_sig,[z_mean,z_log_var])



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(z_log_var) * epsilon

z = layers.Lambda(sampling)([z_mean, z_log_var])

decoder_input = layers.Input(K.int_shape(z)[1:])
x = layers.Dense(np.prod(shape_before_flattening[1:]),activation='relu')(decoder_input)
x = layers.Reshape(shape_before_flattening[1:])(x)
x = layers.Dense(128, activation='relu')(x)
x = layers.Dense(3600, activation='linear')(x)

decoder = Model(decoder_input, x)
z_decoded = decoder(z)

class CustomVariationalLayer(keras.layers.Layer):
    def vae_loss(self, x, z_decoded):
        x = K.flatten(x)
        z_decoded = K.flatten(z_decoded)
        xent_loss = keras.metrics.mae(x, z_decoded)
        kl_loss = -5e-4 * K.mean(
        1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
        return K.mean(xent_loss + kl_loss)
    def call(self, inputs):
        x = inputs[0]
        z_decoded = inputs[1]
        loss = self.vae_loss(x, z_decoded)
        self.add_loss(loss, inputs=inputs)
        return x

y = CustomVariationalLayer()([input_sig, z_decoded])

vae = Model(input_sig, y)
vae.compile(optimizer='rmsprop', loss=None)
vae.summary()
vae.fit(x=X_train, y=None,shuffle=True,epochs=100,batch_size=batch_size,validation_data=(X_test, None))

it used to work smoothly but I have updated my librairies and now I get this error :

File "C:\Users\user\AppData\Local\conda\conda\envs\my_root\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1619, in _create_c_op c_op = c_api.TF_FinishOperation(op_desc)

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

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "I:\Documents\Nico\Python\finance\travail_amont\autoencoder_variationnel_bruit.py", line 74, in z = layers.Lambda(sampling)([z_mean, z_log_var])

File "C:\Users\user\AppData\Local\conda\conda\envs\my_root\lib\site-packages\keras\backend\tensorflow_backend.py", line 75, in symbolic_fn_wrapper return func(*args, **kwargs)

File "C:\Users\user\AppData\Local\conda\conda\envs\my_root\lib\site-packages\keras\engine\base_layer.py", line 506, in call output_shape = self.compute_output_shape(input_shape)

File "C:\Users\user\AppData\Local\conda\conda\envs\my_root\lib\site-packages\keras\layers\core.py", line 674, in compute_output_shape x = self.call(xs)

File "C:\Users\user\AppData\Local\conda\conda\envs\my_root\lib\site-packages\keras\layers\core.py", line 716, in call return self.function(inputs, **arguments)

File "I:\Documents\Nico\Python\finance\travail_amont\autoencoder_variationnel_bruit.py", line 71, in sampling mean=0., stddev=1.)

File "C:\Users\user\AppData\Local\conda\conda\envs\my_root\lib\site-packages\keras\backend\tensorflow_backend.py", line 4329, in random_normal shape, mean=mean, stddev=stddev, dtype=dtype, seed=seed)

File "C:\Users\user\AppData\Local\conda\conda\envs\my_root\lib\site-packages\tensorflow_core\python\keras\backend.py", line 5602, in random_normal shape, mean=mean, stddev=stddev, dtype=dtype, seed=seed)

File "C:\Users\user\AppData\Local\conda\conda\envs\my_root\lib\site-packages\tensorflow_core\python\ops\random_ops.py", line 69, in random_normal shape_tensor = tensor_util.shape_tensor(shape)

File "C:\Users\user\AppData\Local\conda\conda\envs\my_root\lib\site-packages\tensorflow_core\python\framework\tensor_util.py", line 994, in shape_tensor return ops.convert_to_tensor(shape, dtype=dtype, name="shape")

File "C:\Users\user\AppData\Local\conda\conda\envs\my_root\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1314, in convert_to_tensor ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)

File "C:\Users\user\AppData\Local\conda\conda\envs\my_root\lib\site-packages\tensorflow_core\python\ops\array_ops.py", line 1368, in _autopacking_conversion_function return _autopacking_helper(v, dtype, name or "packed")

File "C:\Users\user\AppData\Local\conda\conda\envs\my_root\lib\site-packages\tensorflow_core\python\ops\array_ops.py", line 1304, in _autopacking_helper return gen_array_ops.pack(elems_as_tensors, name=scope)

File "C:\Users\user\AppData\Local\conda\conda\envs\my_root\lib\site-packages\tensorflow_core\python\ops\gen_array_ops.py", line 5704, in pack "Pack", values=values, axis=axis, name=name)

File "C:\Users\user\AppData\Local\conda\conda\envs\my_root\lib\site-packages\tensorflow_core\python\framework\op_def_library.py", line 742, in _apply_op_helper attrs=attr_protos, op_def=op_def)

File "C:\Users\user\AppData\Local\conda\conda\envs\my_root\lib\site-packages\tensorflow_core\python\framework\func_graph.py", line 595, in _create_op_internal compute_device)

File "C:\Users\user\AppData\Local\conda\conda\envs\my_root\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3322, in _create_op_internal op_def=op_def)

File "C:\Users\user\AppData\Local\conda\conda\envs\my_root\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1786, in init control_input_ops)

File "C:\Users\user\AppData\Local\conda\conda\envs\my_root\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1622, in _create_c_op raise ValueError(str(e))

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

I do not know this error : "Duplicate node name in graph". Does anyone has a clue ? Thanks.

0

1 Answer 1

2

If you're using tf 2.x, then import your keras modules as follows.

from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.kerasimport backend as K
from tensorflow.keras.models import Model

More related on this, #36509, #130

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, it does solve one part of the issue but then I run into a "TypeError: An op outside of the function building code is being passed a "Graph" tensor.". My script was OK with tf 1.14 but it seems it does not work anymore with tf 2.1. The error is raised at the "fit" step.
OK, I have the answer even if I do not fully undersatnd it : it works alright with only the change in the imports for the line : from tensorflow.keras import backend as K, the rest should be kept as before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.