Я новичок в TensorFlow. Я пытаюсь запустить следующий репозиторий GitHub для моего классификатора. https://github.com/hfawaz/ijcnn19attack ... eras_tf.py
Я понимаю, что для запуска этого старого кода необходимо отключить поведение TensorFlow v2, поэтому я добавил эти две строки:
import tensorflow.compact.v1 как tf
tf.disable_v2_behavior()
Но теперь я получаю следующую ошибку для функции загрузки модели:model = keras.models.load_model(file_path),
"NotImplementedError: numpy() доступен только тогда, когда включено активное выполнение." что требует включения режима активного выполнения.
В включенном режиме активного выполнения я получаю много новых ошибок (одна из устаревшей функции-заполнителя).
Что мне делать с кодом, который требует режим нетерпеливого выполнения включен в некоторых частях и отключен в других?
Или что я могу сделать, чтобы изменить мою модель классификатора, чтобы она работала без необходимости нетерпеливого выполнения?
Мой классификатор представляет собой следующий код:
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 column - Timestamps column
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}')`
Подробнее здесь: https://stackoverflow.com/questions/759 ... ow-problem