I'm new to TensorFlow. I'm trying to run the following github repo for my classifier. https://github.com/hfawaz/ijcnn19attacks/blob/master/src/cleverhans_tutorials/tsc_tutorial_keras_tf.py
I understand running this old code needs to disable TensorFlow v2 behavior, so I added these two lines: import tensorflow.compact.v1 as tf tf.disable_v2_behavior()
But now I recieve the following error for the model load function: model = keras.models.load_model(file_path), "NotImplementedError: numpy() is only available when eager execution is enabled." which requires enabling eager execution mode. In enabled eager execution mode, and get many new errors (one's from the deprecated placeholder function).
What do I do with code that needs eager execution mode enabled in some parts and disabled in others? Or what can I do to change my classifier model to work without requiring eager execution? My classifier is the following code:
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import KFold
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
from keras.models import load_model
from sklearn.model_selection import TimeSeriesSplit
from sklearn.utils import compute_class_weight
from keras import backend as K
from tensorflow.keras.optimizers.legacy import Adam
import csv
import numpy as np
import tensorflow as tf
np.random.seed(42)
tf.random.set_seed(42)
# Load the dataset from CSV file
df = pd.read_csv('dataset.csv')
# dropping the first colomn - Timestaps colomn
df = df.drop(df.columns[0],axis=1)
# Normalize the dataset
# select all columns except the first one
cols_to_normalize = df.columns[1:]
scaler = MinMaxScaler(feature_range=(0, 1))
df[cols_to_normalize] = scaler.fit_transform(df[cols_to_normalize])
#spliting the last week of data as a seprate section for test
l=[266417,383714]
l_mod = [0] + l + [max(l)+1]
print(l_mod)
list_of_dfs = [df.iloc[l_mod[n]:l_mod[n+1]] for n in range(len(l_mod)-1)]
dfTrain = list_of_dfs[0]
# dfTrain= dfTrain.rename(columns={'activity':'0'})
dfTest = list_of_dfs[1]
# dfTest= dfTest.rename(columns={'activity':'0'})
# dfTrain.to_csv('O4H_TRAIN', header=False, index=False)
# dfTest.to_csv('O4H_TEST', header=False, index=False)
# Split the dataset into input (X) and output (y)
X = dfTrain.iloc[:, 1:].values # features are in columns 2 to 197
y = dfTrain.iloc[:, 0].values # activity label is in column 1
# Define the LSTM model
n_timesteps, n_features, n_outputs = 1, X.shape[1], len(np.unique(y))
model = Sequential()
model.add(LSTM(100, input_shape=(n_features, n_timesteps)))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
# Perform 5-fold Time series validation
tscv = TimeSeriesSplit()
print(tscv)
TimeSeriesSplit(gap=0, max_train_size=None, n_splits=5, test_size=None)
for fold, (train_index, test_index) in enumerate(tscv.split(X)):
print(f"Fold {fold}:")
print(f" Train: index={train_index}")
print(f" Test: index={test_index}")
X_train, y_train = X[train_index], y[train_index]
X_test, y_test = X[test_index], y[test_index]
# Reshape the input data for LSTM model
X_train = X_train.reshape((X_train.shape[0], n_features, n_timesteps))
X_test = X_test.reshape((X_test.shape[0], n_features,n_timesteps))
# Convert the output labels to one-hot encoding
y_train = np.eye(n_outputs)[y_train]
y_test = np.eye(n_outputs)[y_test]
# model.fit(X_train, y_train, epochs=10, batch_size=32, class_weight=class_weights_dict, validation_data=(X_test, y_test))
# Train the LSTM model on this fold
model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_test, y_test))
# Evaluate the trained model on the test set
# loss, accuracy, f1_score, precision, recall = model.evaluate(X_test, y_test, verbose=0)
# print(f'Fold {fold+1} - Loss: {loss:.4f} - Accuracy: {accuracy:.4f}- F1-Score: {f1_score:.4f}- Precision: {precision:.4f}- Recall: {recall:.4f}')
loss, accuracy = model.evaluate(X_test, y_test, verbose=0)
print(f'Fold {fold+1} - Loss: {loss:.4f} - Accuracy: {accuracy:.4f}')
model.save('TSC_model7.hdf5')
print("*********************************************************************")
#load seprate test section
# Split the dataset into input (X) and output (y)
XX = dfTest.iloc[:, 1:].values # features are in columns 2 to 197
YY = dfTest.iloc[:, 0].values # activity label is in column 1
# Reshape the input data for LSTM model
XX = XX.reshape((XX.shape[0], n_features, n_timesteps))
# Convert the output labels to one-hot encoding
YY = np.eye(n_outputs)[YY]
# Evaluate the trained model on the test set
# loss, accuracy, f1_score, precision, recall = model.evaluate(XX,YY, verbose=0)
loss, accuracy = model.evaluate(XX,YY, verbose=0)
print("Test last week of data")
print(f'Loss: {loss:.4f} - Accuracy: {accuracy:.4f}')`