davidADSP / GDL_code

The official code repository for examples in the O'Reilly book 'Generative Deep Learning'
GNU General Public License v3.0
1.47k stars 739 forks source link

03_03_vae_digits_train #103

Open Arindam75 opened 2 years ago

Arindam75 commented 2 years ago

Getting the following error , when trying to run vae.train

` TypeError: in user code:

File "C:\Users\arind\anaconda3\envs\tf\lib\site-packages\keras\engine\training.py", line 1051, in train_function  *
    return step_function(self, iterator)
File "C:\Users\arind\anaconda3\envs\tf\lib\site-packages\keras\engine\training.py", line 1040, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\arind\anaconda3\envs\tf\lib\site-packages\keras\engine\training.py", line 1030, in run_step  **
    outputs = model.train_step(data)
File "C:\Users\arind\anaconda3\envs\tf\lib\site-packages\keras\engine\training.py", line 890, in train_step
    loss = self.compute_loss(x, y, y_pred, sample_weight)
File "C:\Users\arind\anaconda3\envs\tf\lib\site-packages\keras\engine\training.py", line 948, in compute_loss
    return self.compiled_loss(
File "C:\Users\arind\anaconda3\envs\tf\lib\site-packages\keras\engine\compile_utils.py", line 239, in __call__
    self._loss_metric.update_state(
File "C:\Users\arind\anaconda3\envs\tf\lib\site-packages\keras\utils\metrics_utils.py", line 70, in decorated
    update_op = update_state_fn(*args, **kwargs)
File "C:\Users\arind\anaconda3\envs\tf\lib\site-packages\keras\metrics\base_metric.py", line 140, in update_state_fn
    return ag_update_state(*args, **kwargs)
File "C:\Users\arind\anaconda3\envs\tf\lib\site-packages\keras\metrics\base_metric.py", line 449, in update_state  **
    sample_weight = tf.__internal__.ops.broadcast_weights(
File "C:\Users\arind\anaconda3\envs\tf\lib\site-packages\keras\engine\keras_tensor.py", line 254, in __array__
    raise TypeError(

TypeError: You are passing KerasTensor(type_spec=TensorSpec(shape=(), dtype=tf.float32, name=None), name='Placeholder:0', description="created by layer 'tf.cast_2'"), an intermediate Keras symbolic input/output, to a TF API that does not allow registering custom dispatchers, such as `tf.cond`, `tf.function`, gradient tapes, or `tf.map_fn`. Keras Functional model construction only supports TF API calls that *do* support dispatching, such as `tf.math.add` or `tf.reshape`. Other APIs cannot be called directly on symbolic Kerasinputs/outputs. You can work around this limitation by putting the operation in a custom Keras layer `call` and calling that layer on this symbolic input/output.

` Edit-1 25-Aug-22

I am certain that the error is coming from the custom KL Loss function. In the compile method , if I use only the vae_r_loss, the code works

` def vae_r_loss(y_true, y_pred): r_loss = K.mean(K.square(y_true - y_pred), axis = [1,2,3]) return r_loss_factor * r_loss

def vae_kl_loss(y_true, y_pred):
    kl_loss =  -0.5 * K.sum(1 + self.log_var - K.square(self.mu) - K.exp(self.log_var), axis = 1)
    return kl_loss

def vae_loss(y_true, y_pred):
    r_loss = vae_r_loss(y_true, y_pred)
    kl_loss = vae_kl_loss(y_true, y_pred)
    return  r_loss + kl_loss

optimizer = Adam(lr=learning_rate)
self.model.compile(optimizer=optimizer, loss = vae_r_loss,  metrics = [vae_r_loss])

`

gkonto commented 1 year ago

from tensorflow.python.framework.ops import disable_eager_execution disable_eager_execution()

This code will possibly fix your issue.