I made VAE model referred to Keras VAE Tutorials(https://keras.io/examples/generative/vae/) from Conv2D to Conv1D and I fitted the model successfully.
After fitting the model, I used 'predict' method to calculate train_mse_loss.
x_train_pred = vae.predict(x_train)
train_mae_loss = np.mean(np.abs(x_train_pred - x_train), axis=1)
and error came out
NotImplementedError Traceback (most recent call last)
Cell In [98], line 3
1 # Get train MAE Loss
----> 3 x_train_pred = vae.predict(x_train)
4 train_mae_loss = np.mean(np.abs(x_train_pred - x_train), axis=1)
5 # train_mae_loss = np.mean(abs(x_train_pred - x_train), axis=1)
File /notebooks/users/1004831/objupyterlabgpu-1004831/kernels/mytensor2.0.0b/lib/python3.9/site-packages/keras/utils/traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.__traceback__)
68 # To get the full stack trace, call:
69 # `tf.debugging.disable_traceback_filtering()`
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
File /tmp/__autograph_generated_fileau_h0p6f.py:15, in outer_factory.<locals>.inner_factory.<locals>.tf__predict_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
NotImplementedError: in user code:
File "/notebooks/users/1004831/objupyterlabgpu-1004831/kernels/mytensor2.0.0b/lib/python3.9/site-packages/keras/engine/training.py", line 2041, in predict_function *
return step_function(self, iterator)
File "/notebooks/users/1004831/objupyterlabgpu-1004831/kernels/mytensor2.0.0b/lib/python3.9/site-packages/keras/engine/training.py", line 2027, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/notebooks/users/1004831/objupyterlabgpu-1004831/kernels/mytensor2.0.0b/lib/python3.9/site-packages/keras/engine/training.py", line 2015, in run_step **
outputs = model.predict_step(data)
File "/notebooks/users/1004831/objupyterlabgpu-1004831/kernels/mytensor2.0.0b/lib/python3.9/site-packages/keras/engine/training.py", line 1983, in predict_step
return self(x, training=False)
File "/notebooks/users/1004831/objupyterlabgpu-1004831/kernels/mytensor2.0.0b/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/notebooks/users/1004831/objupyterlabgpu-1004831/kernels/mytensor2.0.0b/lib/python3.9/site-packages/keras/engine/training.py", line 584, in call
raise NotImplementedError(
NotImplementedError: Exception encountered when calling layer "vae_6" " f"(type VAE).
Unimplemented `tf.keras.Model.call()`: if you intend to create a `Model` with the Functional API, please provide `inputs` and `outputs` arguments. Otherwise, subclass `Model` with an overridden `call()` method.
Call arguments received by layer "vae_6" " f"(type VAE):
• inputs=tf.Tensor(shape=(None, 20, 1), dtype=float32)
• training=False
• mask=None
My initial code was here,
x.train shape: (7511302, 20, 1)
class Sampling(layers.Layer):
"""Uses (z_mean, z_log_var) to sample z, the vector encoding a digit."""
def call(self, inputs):
z_mean, z_log_var = inputs
batch = tf.shape(z_mean)[0]
dim = tf.shape(z_mean)[1]
epsilon = tf.keras.backend.random_normal(shape=(batch, dim))
return z_mean + tf.exp(0.5 * z_log_var) * epsilon
##### Encoder
latent_dim = 2
encoder_inputs = keras.Input(shape=(x_train.shape[1], x_train.shape[2]))
x = layers.Conv1D(filters=64, kernel_size=3, padding='same', strides=5, kernel_regularizer='l2')(encoder_inputs)
x = layers.Conv1D(filters=32, kernel_size=3, padding='same', strides=2, kernel_regularizer='l2')(x)
x = layers.Flatten()(x)
x = layers.Dense(16, activation="relu")(x)
z_mean = layers.Dense(latent_dim, name="z_mean")(x)
z_log_var = layers.Dense(latent_dim, name="z_log_var")(x)
z = Sampling()([z_mean, z_log_var])
encoder = keras.Model(encoder_inputs, [z_mean, z_log_var, z], name="encoder")
encoder.summary()
##### Decoder
latent_inputs = keras.Input(shape=(latent_dim,))
x = layers.Dense(64, activation="relu")(latent_inputs)
x = layers.Reshape((2, 32))(x)
x = layers.Conv1DTranspose(filters=32, kernel_size=3, padding='same', strides=2, kernel_regularizer='l2')(x)
x = layers.Conv1DTranspose(filters=64, kernel_size=3, padding='same', strides=5, kernel_regularizer='l2')(x)
decoder_outputs = layers.Conv1D(filters=1, kernel_size=3, padding='same', activation='relu')(x)
decoder = keras.Model(latent_inputs, decoder_outputs, name="decoder")
decoder.summary()
##### Define the VAE as a Model with a custom train_step
class VAE(keras.Model):
def __init__(self, encoder, decoder, **kwargs):
super(VAE, self).__init__(**kwargs)
self.encoder = encoder
self.decoder = decoder
self.total_loss_tracker = keras.metrics.Mean(name="total_loss")
self.reconstruction_loss_tracker = keras.metrics.Mean(
name="reconstruction_loss"
)
self.kl_loss_tracker = keras.metrics.Mean(name="kl_loss")
@property
def metrics(self):
return [
self.total_loss_tracker,
self.reconstruction_loss_tracker,
self.kl_loss_tracker,
]
def train_step(self, data):
with tf.GradientTape() as tape:
z_mean, z_log_var, z = self.encoder(data)
reconstruction = self.decoder(z)
reconstruction_loss = tf.reduce_mean(
tf.reduce_sum(
keras.losses.mse(data, reconstruction), axis=1
)
)
kl_loss = -0.5 * (1 + z_log_var - tf.square(z_mean) - tf.exp(z_log_var))
kl_loss = tf.reduce_mean(tf.reduce_sum(kl_loss, axis=1))
total_loss = reconstruction_loss + kl_loss
grads = tape.gradient(total_loss, self.trainable_weights)
self.optimizer.apply_gradients(zip(grads, self.trainable_weights))
self.total_loss_tracker.update_state(total_loss)
self.reconstruction_loss_tracker.update_state(reconstruction_loss)
self.kl_loss_tracker.update_state(kl_loss)
return {
"loss": self.total_loss_tracker.result(),
"reconstruction_loss": self.reconstruction_loss_tracker.result(),
"kl_loss": self.kl_loss_tracker.result(),
}
vae = VAE(encoder, decoder)
vae.compile(optimizer=keras.optimizers.Adam())
vae.fit(x_train, epochs=30, batch_size=128)
It means subclass, API issue, but I couldn't understand the problem. How can I do to fix my code to calculate train loss?