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 https://github.com/musikalkemist/generating-sound-with-neural-networks/blob/main/14%20Sound%20generation%20with%20VAE/code/autoencoder.py and the bug is as follows:

Traceback (most recent call last):
  File "/home/luis/Desktop/keras/full/rt.py", line 745, in <module>
    audio_autoencoder = VAE(
  File "/home/luis/Desktop/keras/full/rt.py", line 539, in __init__
    self._build()
  File "/home/luis/Desktop/keras/full/rt.py", line 624, in _build
    self._build_encoder()
  File "/home/luis/Desktop/keras/full/rt.py", line 689, in _build_encoder
    bottleneck = self._add_bottleneck(conv_layers)
  File "/home/luis/Desktop/keras/full/rt.py", line 738, in _add_bottleneck
    x = Lambda(sample_point_from_normal_distribution, name="encoder_output")([self.mu, self.log_variance])
  File "/home/luis/anaconda3/envs/tensorflow/lib/python3.9/site-packages/tensorflow/python/keras/engine/base_layer.py", 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/base_layer.py", 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/base_layer.py", 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/base_layer.py", line 885, in _infer_output_signature
    outputs = nest.map_structure(
  File "/home/luis/anaconda3/envs/tensorflow/lib/python3.9/site-packages/tensorflow/python/util/nest.py", 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/nest.py", 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/keras_tensor.py", 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/keras_tensor.py", 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/type_spec.py", 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?
Thanks!

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.