Integrating two VAEs from keras v1 & 2 problem

Hello all.

I know that this problem could be solved from a version perspective but it seems to me that can be quickly solved through optimization. I’m having some problems integrating two VAEs (one for image and other for audio) in the same py file.

When running in separate files (with tf 2.5 and CUDA) and with the eager execution option in the older code, it trains and generates without any problem. But when I mix them I have a problem. The VAE that is causing problems is the same as in and the bug is as follows:

Traceback (most recent call last):
  File "/home/luis/Desktop/keras/full/", line 745, in <module>
    audio_autoencoder = VAE(
  File "/home/luis/Desktop/keras/full/", line 539, in __init__
  File "/home/luis/Desktop/keras/full/", line 624, in _build
  File "/home/luis/Desktop/keras/full/", line 689, in _build_encoder
    bottleneck = self._add_bottleneck(conv_layers)
  File "/home/luis/Desktop/keras/full/", line 738, in _add_bottleneck
    x = Lambda(sample_point_from_normal_distribution, name="encoder_output")([, self.log_variance])
  File "/home/luis/anaconda3/envs/tensorflow/lib/python3.9/site-packages/tensorflow/python/keras/engine/", line 969, in __call__
    return self._functional_construction_call(inputs, args, kwargs,
  File "/home/luis/anaconda3/envs/tensorflow/lib/python3.9/site-packages/tensorflow/python/keras/engine/", line 1107, in _functional_construction_call
    outputs = self._keras_tensor_symbolic_call(
  File "/home/luis/anaconda3/envs/tensorflow/lib/python3.9/site-packages/tensorflow/python/keras/engine/", line 840, in _keras_tensor_symbolic_call
    return self._infer_output_signature(inputs, args, kwargs, input_masks)
  File "/home/luis/anaconda3/envs/tensorflow/lib/python3.9/site-packages/tensorflow/python/keras/engine/", line 885, in _infer_output_signature
    outputs = nest.map_structure(
  File "/home/luis/anaconda3/envs/tensorflow/lib/python3.9/site-packages/tensorflow/python/util/", line 867, in map_structure
    structure[0], [func(*x) for x in entries],
  File "/home/luis/anaconda3/envs/tensorflow/lib/python3.9/site-packages/tensorflow/python/util/", line 867, in <listcomp>
    structure[0], [func(*x) for x in entries],
  File "/home/luis/anaconda3/envs/tensorflow/lib/python3.9/site-packages/tensorflow/python/keras/engine/", line 590, in keras_tensor_from_tensor
    out = keras_tensor_cls.from_tensor(tensor)
  File "/home/luis/anaconda3/envs/tensorflow/lib/python3.9/site-packages/tensorflow/python/keras/engine/", line 182, in from_tensor
    type_spec = type_spec_module.type_spec_from_value(tensor)
  File "/home/luis/anaconda3/envs/tensorflow/lib/python3.9/site-packages/tensorflow/python/framework/", line 579, in type_spec_from_value
    raise TypeError("Could not build a TypeSpec for %r with type %s" %
TypeError: Could not build a TypeSpec for <KerasTensor: shape=(None, 128) dtype=float32 (created by layer 'tf.__operators__.add')> with type KerasTensor

It seems the lambda layer is causing some problems, any tf/keras expert to help on this?

Hi @kashik

Welcome to the TensorFlow Forum!

This code is running fine with the latest TensorFlow and Keras version 2.15. Attached the working gist here for your reference. Could you please try again the same code with the latest TensorFlow and Keras version and let us know if the issue still persists. Thank you.