my loss goes to be negative, check and let me know about my mistakes, the loss is cosine sim. import pandas as pd
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, roc_auc_score
from sklearn.metrics import mean_absolute_error, explained_variance_score
import matplotlib.pyplot as plt
from sklearn.cross_decomposition import CCA
# Convert all elements to numeric, forcing non-numeric to NaN
df_numeric = df.apply(pd.to_numeric, errors='coerce')
# Replace NaN values with 0
df_numeric.fillna(0, inplace=True)
# Replace empty strings with 0
df_numeric.replace("", 0, inplace=True)
# remove the row with all zeros besiders the first column
if drop_zero_rows:
df_numeric = df_numeric.loc[(df_numeric.iloc[:, 1:] != 0).any(axis=1)]
return df_numeric
df_list_processed = [process_dataframe(df, patterns, drop_zero_rows=True) for df in df_list]
drop rows with NaN
df_list_processed = [df.dropna() for df in df_list_processed]
Assistant
merge all df on User code
#merge all df on User code
merged_df = df_list_processed[0]
for i, df in enumerate(df_list_processed, start=2):
merged_df = pd.merge(merged_df, df, on='User code', suffixes=('', f'_df{i}'))
drop all row of data_df besides the first and the last
Note: Since this is an autoencoder, calculating binary accuracy or AUC might not be meaningful in a reconstruction context
It seems you haven't defined the cosine similarity loss function explicitly when compiling your Autoencoder model. Instead, you declared it separately but didn't use it. You're using LogCosh as the loss function, which might not be what you intended.
Here are some corrections and suggestions:
Define and use Cosine Similarity Loss Correctly:
If you want to use CosineSimilarity as the loss function, you should pass it when compiling the model.
Debug Negative Loss Values:
The CosineSimilarity loss is designed to return values in the range ([-1, 1]), where -1 indicates that the vectors are as different as possible. Thus, negative values are expected but not typical in a training loss context.
Ensure Proper Loss Function:
Here is your corrected code to use the CosineSimilarity loss function:
import pandas as pd
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, roc_auc_score
from sklearn.metrics import mean_absolute_error, explained_variance_score
import matplotlib.pyplot as plt
from sklearn.cross_decomposition import CCA
# Load data
d1 = pd.read_csv("/path/to/data1.csv")
d2 = pd.read_csv("/path/to/data2.csv")
# ... include other data files as needed
df_list = [d1, d2, ...] # Ensure all your dataframes are included here
data_df = pd.read_csv('/path/to/main_data.csv')
# Preprocess data
d7["User code"] = d7["User code"].str.replace("-I", "-C")
patterns = [
"adrs", "ANXDX", "audit", "BIS", "CAPE42", "item",
"CTQ", "EDEQ", "SCID", "IRI", "JVQ", "PAAQ",
"RRS", "tci", "TFEQ", "User code"
]
def process_dataframe(df, patterns, drop_zero_rows=False):
df = df[df.columns[df.columns.str.contains('|'.join(patterns))]]
df.loc[:, "User code"] = df.loc[:, "User code"].str.replace("-C", "")
df.loc[:, "User code"] = df.loc[:, "User code"].str.lstrip("0")
df_numeric = df.apply(pd.to_numeric, errors='coerce')
df_numeric.fillna(0, inplace=True)
df_numeric.replace("", 0, inplace=True)
if drop_zero_rows:
df_numeric = df_numeric.loc[(df_numeric.iloc[:, 1:] != 0).any(axis=1)]
return df_numeric
df_list_processed = [process_dataframe(df, patterns, drop_zero_rows=True) for df in df_list]
df_list_processed = [df.dropna() for df in df_list_processed]
merged_df = df_list_processed[0]
for i, df in enumerate(df_list_processed, start=2):
merged_df = pd.merge(merged_df, df, on='User code', suffixes=('', f'_df{i}'))
data_df = data_df.drop(data_df.columns[1:-1], axis=1)
data_df.rename(columns={'Unnamed: 0': 'User code'}, inplace=True)
merged_df = pd.merge(data_df, merged_df, on='User code')
# Pre-process dataframe for TensorFlow
df = merged_df.drop(columns=['User code', "FilePath"])
non_binary_columns = [col for col in df.columns if len(df[col].unique()) > 2]
df = pd.get_dummies(df, columns=non_binary_columns, drop_first=True)
df = df.astype(float)
# Split data into train and validation sets
X_train, X_val = train_test_split(df.values, test_size=0.20, random_state=42)
# Define the CNN Autoencoder model
class Autoencoder(tf.keras.Model):
def __init__(self, input_size):
super(Autoencoder, self).__init__()
self.encoder = tf.keras.Sequential([
tf.keras.layers.Input(shape=(input_size, 1)),
tf.keras.layers.Conv1D(128, kernel_size=3, activation="relu", padding="same"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPooling1D(pool_size=3, padding="same"),
tf.keras.layers.Conv1D(64, kernel_size=3, activation="relu", padding="same"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPooling1D(pool_size=3)
])
self.decoder = tf.keras.Sequential([
tf.keras.layers.UpSampling1D(size=3),
tf.keras.layers.Conv1DTranspose(64, kernel_size=3, activation="relu", padding="same"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.UpSampling1D(size=3),
tf.keras.layers.Conv1DTranspose(128, kernel_size=3, activation="relu"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.ZeroPadding1D(padding=2),
tf.keras.layers.Conv1D(1, kernel_size=3, activation="sigmoid")
])
def call(self, x):
x = tf.expand_dims(x, axis=-1) # Add channel dimension
encoded = self.encoder(x)
decoded = self.decoder(encoded)
decoded = tf.squeeze(decoded, axis=-1) # Remove channel dimension
return decoded
# Set up CosineSimilarity loss
cos = tf.keras.losses.CosineSimilarity(axis=-1, reduction='auto', name='cosine_similarity')
input_size = df.shape[1]
autoencoder = Autoencoder(input_size)
autoencoder.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01), loss=cos)
early_stopping = tf.keras.callbacks.EarlyStopping(monitor="val_loss", patience=10, restore_best_weights=True)
history = autoencoder.fit(X_train, X_train,
epochs=100,
batch_size=512,
validation_data=(X_val, X_val),
callbacks=[early_stopping])
# Evaluate the model
train_predictions = autoencoder.predict(X_train)
val_predictions = autoencoder.predict(X_val)
train_mse = np.mean(np.square(train_predictions - X_train))
val_mse = np.mean(np.square(val_predictions - X_val))
train_mae = mean_absolute_error(X_train, train_predictions)
val_mae = mean_absolute_error(X_val, val_predictions)
train_explained_var = explained_variance_score(X_train, train_predictions)
val_explained_var = explained_variance_score(X_val, val_predictions)
print(f"Training MSE: {train_mse:.4f}, Validation MSE: {val_mse:.4f}")
print(f"Training MAE: {train_mae:.4f}, Validation MAE: {val_mae:.4f}")
print(f"Training Explained Variance: {train_explained_var:.4f}, Validation Explained Variance: {val_explained_var:.4f}")
# Compute Canonical Correlations
num_components = min(X_train.shape[0], train_predictions.shape[0])
cca = CCA(n_components=num_components)
cca.fit(X_train, train_predictions)
X_train_c, train_predictions_c = cca.transform(X_train, train_predictions)
# Compute the correlation for each pair of canonical variables
cca_correlations = np.corrcoef(X_train_c.T, train_predictions_c.T)[:X_train_c.shape[1], X_train_c.shape[1]:]
print("Canonical Correlations:")
for i, corr in enumerate(cca_correlations.diagonal()):
print(f"Canonical correlation {i+1}: {corr:.4f}")
# Plot loss curves for training and validation
plt.plot(history.history["loss"], label="Training Loss")
plt.plot(history.history["val_loss"], label="Validation Loss")
plt.legend()
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.title("Training and Validation Loss")
plt.show()
Key Changes:
Passed cos in the compile() function of the model.
Adjusted autoencoder.compile to use Cosine Similarity loss.
Now, the model should use Cosine Similarity as its loss function. Negative values in this context indicate the vectors are inversely related, which might be informative but should prompt you to inspect your data preprocessing and model architecture to ensure everything is aligned as expected.