Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- Created on Mon Oct 21 00:16:21 2019
- @author: jeremiah
- """
- #%% importings
- import numpy as np
- import pandas as pd
- import matplotlib.pyplot as plt
- from keras.models import Sequential
- from keras.layers import Dense, Dropout
- #from keras.wrappers.scikit_learn import KerasClassifier
- from keras.optimizers import SGD
- #from keras.constraints import maxnorm
- from keras.callbacks import ModelCheckpoint
- from scipy.io import arff
- #from sklearn.model_selection import cross_val_score, StratifiedKFold
- from sklearn.preprocessing import LabelEncoder, StandardScaler
- #from sklearn.pipeline import Pipeline
- #%%
- seed = 7
- np.random.seed(7)
- #%% data preparation
- path = '/home/jeremiah/data_analysis/datasets/EEG-Eye-State.arff'
- data = arff.loadarff(path)
- df = pd.DataFrame(data[0])
- dataset = df.to_numpy().astype(float)
- np.random.shuffle(dataset)
- test_per = 0.3
- train_size = int(len(dataset)*(1-test_per))
- X = dataset[:train_size,:-1]
- Y = dataset[:train_size, -1]
- X_test = dataset[train_size:,:-1]
- Y_test = dataset[train_size:,-1]
- #%% defining model
- def create_model():
- #dp_rate = 0.1
- model = Sequential()
- model.add(Dense(20, input_dim=14, kernel_initializer='normal', activation='relu'))
- #model.add(Dropout(rate=dp_rate))
- #model.add(Dense(32, kernel_initializer='normal', activation='relu',
- #kernel_constraint=maxnorm(3)))
- #model.add(Dropout(rate=dp_rate))
- model.add(Dense(10, kernel_initializer='normal', activation='relu'))
- #model.add(Dropout(rate=dp_rate))
- model.add(Dense(1, kernel_initializer='normal', activation='sigmoid'))
- sgd = SGD(lr=0.065, decay=0.002)
- model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
- return model
- #%% training model
- batch_size = 20
- epochs = 300
- model = create_model()
- scaler = StandardScaler()
- scaler.fit(X)
- std_x = scaler.transform(X)
- checkpoint = ModelCheckpoint('/home/jeremiah/trained_models/best.model.h5',
- monitor='val_acc', verbose=1, save_best_only=True, mode='max')
- callback_list = [checkpoint]
- history = model.fit(std_x, Y, validation_split=0.33, batch_size=batch_size, epochs=epochs, verbose=1,
- callbacks=callback_list)
- #%% viewing results
- print(history.history.keys())
- print('acc: %.2f%%\tval_acc: %.2f%%' %(history.history['acc'][-1]*100, history.history['val_acc'][-1]*100))
- plt.figure(figsize=(14, 5))
- plt.subplot(1, 2, 1)
- plt.plot(history.history['acc'], 'b-', history.history['val_acc'], 'r-')
- plt.title('model accuracy')
- plt.ylabel('accuracy')
- plt.xlabel('epoch')
- plt.legend(['train', 'test'], loc='best')
- plt.subplot(1, 2, 2)
- plt.plot(history.history['loss'], 'b-', history.history['val_loss'], 'r-')
- plt.title('model loss')
- plt.ylabel('loss')
- plt.xlabel('epoch')
- plt.legend(['train', 'test'], loc='best')
- plt.savefig('/home/jeremiah/trained_models/EEG-MLP-02.png')
- #%% loading best model weights
- model.load_weights('/home/jeremiah/trained_models/best.model.h5')
- print('model loaded!')
- #%% saving model
- model_json = model.to_json()
- with open('/home/jeremiah/trained_models/EEG-MLP-02.json', 'w') as file:
- file.write(model_json)
- model.save_weights('/home/jeremiah/trained_models/EEG-MLP-02.h5')
- print('model saved successfully!')
- #%%
- scaler.fit(X_test)
- std_x_test = scaler.transform(X_test)
- scores = model.evaluate(std_x_test, Y_test, batch_size=16)
- print('Results:\n%s: %.2f%%' %(model.metrics_names[1], scores[1]*100))
Advertisement
Add Comment
Please, Sign In to add comment