-
-
Save floschreiber/27582a6dad78da200d4d61e126608143 to your computer and use it in GitHub Desktop.
Code LSTM Keras
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
import collections | |
from sklearn.model_selection import train_test_split | |
from keras.layers import Input, Dense, Dropout, Conv1D, MaxPooling1D,Flatten, LSTM | |
from keras.models import Model, Sequential | |
from keras import metrics | |
import keras | |
import matplotlib.pyplot as plt | |
def LSTM_redshift(X_train,nb_class): | |
if nb_class == 2 : | |
loss='binary_crossentropy' | |
else: | |
loss='categorical_crossentropy' | |
print(loss) | |
dim=(17908,1) | |
model_LSTM = Sequential() | |
model_LSTM.add(LSTM(256, input_shape=dim))# , | |
model_LSTM.add(Dense(nb_class, activation="sigmoid")) | |
model_LSTM.layers[0].trainable = False | |
model_LSTM.compile(loss=loss, | |
optimizer=keras.optimizers.Adam(learning_rate=1e-3), | |
metrics=['acc']) | |
return model_LSTM | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dataset_redshift import dataset_redshift | |
from MLP_redshift import MLP_redshift | |
from CNN1D_redshift import CNN1D_redshift | |
from LSTM_redshift import LSTM_redshift | |
#from visu_data import visu_data | |
import tensorflow as tf | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import collections | |
from sklearn.model_selection import train_test_split | |
from keras.layers import Input, Dense, Dropout, Flatten | |
from keras.models import Model, Sequential | |
from keras import metrics | |
import matplotlib.pyplot as plt | |
from keras.utils import np_utils | |
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay | |
X_train, X_validation ,X_test, target_train, target_validation ,target_test , Y_train , Y_validation, Y_test , nb_class, nom_classes = dataset_redshift('Flag_class',Undersample=True) | |
X_test=np.reshape(X_test, (X_test.shape[0],X_test.shape[1],1)) | |
X_train=np.reshape(X_train, (X_train.shape[0],X_train.shape[1],1)) | |
X_validation=np.reshape(X_validation, (X_validation.shape[0],X_validation.shape[1],1)) | |
model=LSTM_redshift(X_train,nb_class) | |
h = model.fit(X_train, Y_train, | |
epochs=20, | |
batch_size=32, | |
verbose =1, | |
validation_data = (X_validation , Y_validation)) | |
y_pred_test=model.predict(X_test) | |
y_pred_test=np.argmax(y_pred_test, axis=1) | |
conf_matrix_test=confusion_matrix(target_test, y_pred_test) | |
print(conf_matrix_test) | |
y_pred_train=model.predict(X_train) | |
y_pred_train=np.argmax(y_pred_train, axis=1) | |
conf_matrix_train=confusion_matrix(target_train, y_pred_train) | |
print(conf_matrix_train) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment