I am trying to train an autoencoder using a custom hamming loss function. The code is as follows,
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping
def hamming_loss(y_true, y_pred):
def smooth_heaviside(x, k=20):
return 1 / (1 + tf.exp(-k * x))
y_true_bin = smooth_heaviside(tf.cast(y_true, tf.float32))
y_pred_bin = smooth_heaviside(tf.cast(y_pred, tf.float32))
loss = y_true_bin * (1 - y_pred_bin) + (1 - y_true_bin) * y_pred_bin
return tf.reduce_mean(loss)
# Create a simple fully connected autoencoder
def create_autoencoder(input_dim, encoding_dim):
input_layer = Input(shape=(input_dim,))
encoder = Dense(encoding_dim, activation='relu')(input_layer)
decoder = Dense(input_dim, activation='sigmoid')(encoder)
autoencoder = Model(inputs=input_layer, outputs=decoder)
autoencoder.compile(optimizer='adam', loss=hamming_loss)
return autoencoder
# Example data (binary vectors)
import numpy as np
# Generate some random binary data
data = np.random.randint(0, 20, size=(1000, 32))
# Split into train and validation sets
train_data = data[:800]
val_data = data[800:]
# Define early stopping
early_stopping = EarlyStopping(
monitor='val_loss', # Quantity to monitor.
min_delta=0.0001, # Minimum change to qualify as an improvement.
patience=10, # Number of epochs with no improvement after which training will be stopped.
verbose=1, # Verbosity mode.
mode='min', # In 'min' mode, training will stop when the quantity monitored has stopped decreasing.
restore_best_weights=True # Restores model weights from the epoch with the best value of the monitored quantity.
)
# Create and train the autoencoder
input_dim = 32 # Dimension of input data
encoding_dim = 16 # Dimension of the encoded representation
autoencoder = create_autoencoder(input_dim, encoding_dim)
# Fit the autoencoder with early stopping
history = autoencoder.fit(train_data, train_data,
epochs=200,
batch_size=32,
shuffle=True,
validation_data=(val_data, val_data),
callbacks=[early_stopping])
# Print the number of epochs the model actually ran
print(f"Stopped training after {len(history.history['loss'])} epochs.")
# Plot training and validation loss
plt.figure(figsize=(8, 6))
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Training and Validation Loss')
plt.legend()
plt.grid(True)
plt.show()
The issue is that I don’t think my model is learning anything. The Early Stopping criteria is always trigerred. I require help with two things:
(1) Confirming that my custom hamming loss function is correct
(2) Help with how can I get my model to learn
New contributor
stevemadden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.