├── README.md ├── calclulated_reconstruction_performance ├── ae_reconstruction_corr_Rvalue.mat ├── ica_reconstruction_corr_Rvalue.mat ├── pca_reconstruction_corr_Rvalue.mat ├── rbm_reconstruction_corr_Rvalue.mat └── vae_reconstruction_corr_Rvalue.mat ├── data_normalization_zscore.m ├── get_ica_encoded.m ├── get_pca_encoded.m ├── performance_ae_Rvalue_encoded_decoded.m ├── performance_ica_Rvalue_encoded_decoded.m ├── performance_pca_Rvalue_encoded_decoded.m ├── performance_rbm_Rvalue_encoded_decoded.m ├── performance_vae_Rvalue_encoded_decoded.m ├── plot_Rvalue_latentdims_each_method.m ├── plot_Rvalue_latentdims_each_subject.m ├── prediction_LSTM-RNN.py ├── reconstruction_AE.py ├── reconstruction_RBM.m └── reconstruction_VAE.py /README.md: -------------------------------------------------------------------------------- 1 | # LatentFactorDecodingEEG 2 | 3 | These codes are shared with the publication of the "Latent Factor Decoding of Multi-channel EEG for Emotion Recognition through AE-like Neural Networks". 4 | 5 | 0. The file named 'data_normalization_zscore' is used for preprocessing the multichannel EEGs. 6 | 7 | 1. The file named 'reconstruction_XXX.py' is used for training the AE-like models. 8 | After training, the mined latent factors and the decoded data are saved into pre-defined local file path. 9 | 10 | 2. The file named 'get_ica_encoded.m' and 'get_pca_encoded.m' are used for get the encoded data of ICA and PCA based methods. 11 | The mined latent factors and the decoded data are saved into pre-defined local file path. 12 | 13 | 3. The file named 'performance_XXX_Rvalue_encoded_decoded.m' is used for calcluate and measure the reconstruction performance. 14 | The obtained results are stored in the folder 'calclulated_reconstruction_performance' 15 | 16 | 4. The file named 'plot_Rvalue_latentdims_each_method' and 'plot_Rvalue_latentdims_each_subject' are used for 17 | plotting the reconstruction performance. 18 | 19 | 5. The file named 'prediction_LSTM-RNN' is used to constructing training sequences from the mined latent factors, training the LSTM based 20 | sequence modeling method and conducting emotion recognition. 21 | -------------------------------------------------------------------------------- /calclulated_reconstruction_performance/ae_reconstruction_corr_Rvalue.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muzixiang/LatentFactorDecodingEEG/1c76ed5161296210b91af1ffb3096113fed68059/calclulated_reconstruction_performance/ae_reconstruction_corr_Rvalue.mat -------------------------------------------------------------------------------- /calclulated_reconstruction_performance/ica_reconstruction_corr_Rvalue.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muzixiang/LatentFactorDecodingEEG/1c76ed5161296210b91af1ffb3096113fed68059/calclulated_reconstruction_performance/ica_reconstruction_corr_Rvalue.mat -------------------------------------------------------------------------------- /calclulated_reconstruction_performance/pca_reconstruction_corr_Rvalue.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muzixiang/LatentFactorDecodingEEG/1c76ed5161296210b91af1ffb3096113fed68059/calclulated_reconstruction_performance/pca_reconstruction_corr_Rvalue.mat -------------------------------------------------------------------------------- /calclulated_reconstruction_performance/rbm_reconstruction_corr_Rvalue.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muzixiang/LatentFactorDecodingEEG/1c76ed5161296210b91af1ffb3096113fed68059/calclulated_reconstruction_performance/rbm_reconstruction_corr_Rvalue.mat -------------------------------------------------------------------------------- /calclulated_reconstruction_performance/vae_reconstruction_corr_Rvalue.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muzixiang/LatentFactorDecodingEEG/1c76ed5161296210b91af1ffb3096113fed68059/calclulated_reconstruction_performance/vae_reconstruction_corr_Rvalue.mat -------------------------------------------------------------------------------- /data_normalization_zscore.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muzixiang/LatentFactorDecodingEEG/1c76ed5161296210b91af1ffb3096113fed68059/data_normalization_zscore.m -------------------------------------------------------------------------------- /get_ica_encoded.m: -------------------------------------------------------------------------------- 1 | 2 | subNum=32; 3 | channelNum=32; 4 | zscore=1; 5 | 6 | latdim = 16; 7 | 8 | for subNo=1:subNum 9 | zscore_eegs_file = load(strcat('D:\Processed DEAP DATA\normalize_zscore\sub',num2str(subNo),'.mat')); 10 | zscore_eegs = zscore_eegs_file.zscore_data(:,1:channelNum)'; 11 | disp(strcat('subNo: ',num2str(subNo),' latentdim: ', num2str(latdim))); 12 | %fast ICA A:Mixing matrix W: Seperating matrix 13 | [ICs, A, W] = fastica(zscore_eegs,'numOfIC',latdim,'verbose','off'); 14 | decoded_eegs = A*ICs; 15 | encoded_eegs = ICs; 16 | %save data 17 | fileName = strcat('D:\VAE Experiment\DEAP\encoded_eegs_ica\encoded_eegs_ica_sub',num2str(subNo),'_latedtdim',num2str(latdim)); 18 | save(fileName,'encoded_eegs','-v7.3'); 19 | end 20 | -------------------------------------------------------------------------------- /get_pca_encoded.m: -------------------------------------------------------------------------------- 1 | 2 | subNum=32; 3 | channelNum=32; 4 | zscore=1; 5 | 6 | latdim = 16; 7 | 8 | for subNo=1:subNum 9 | zscore_eegs_file = load(strcat('D:\Processed DEAP DATA\normalize_zscore\sub',num2str(subNo),'.mat')); 10 | zscore_eegs = zscore_eegs_file.zscore_data(:,1:channelNum)'; 11 | disp(strcat('subNo: ',num2str(subNo),' latentdim: ', num2str(latdim))); 12 | 13 | X = zscore_eegs.'; 14 | [coeff, score, latent] = princomp(X); 15 | encoded_eegs = score(:,1:latdim); 16 | % [pcaData, coeff] = fastPCA(X,latdim); 17 | % encoded_eegs = pcaData; 18 | %save data 19 | fileName = strcat('D:\VAE Experiment\DEAP\encoded_eegs_pca\encoded_eegs_pca_sub',num2str(subNo),'_latedtdim',num2str(latdim)); 20 | save(fileName,'encoded_eegs','-v7.3'); 21 | end 22 | -------------------------------------------------------------------------------- /performance_ae_Rvalue_encoded_decoded.m: -------------------------------------------------------------------------------- 1 | subNum=32; 2 | channelNum=32; 3 | latdimNum=16; 4 | 5 | 6 | corr_chs = zeros(latdimNum,subNum,channelNum); 7 | for latdim=1:latdimNum 8 | for subNo=1:subNum 9 | filename1 = strcat(strcat('D:\VAE Experiment\DEAP\decoded_eegs_1ae\decoded_eegs_1ae_sub',num2str(subNo),'_latentdim',num2str(latdim),'.mat')); 10 | decoded_eegs_ae_file = load(filename1); 11 | decoded_eegs_ae = decoded_eegs_ae_file.decoded_eegs; 12 | zscore_eegs_file = load(strcat('D:\Processed DEAP DATA\normalize_zscore\sub',num2str(subNo),'.mat')); 13 | zscore_eegs = zscore_eegs_file.zscore_data; 14 | for chno=1:channelNum 15 | disp(strcat('latdim: ', num2str(latdim), ' subNo: ', num2str(subNo), ' chNo: ', num2str(chno))); 16 | R=corrcoef(decoded_eegs_ae(:,chno), zscore_eegs(:,chno)); 17 | corr_chs(latdim,subNo,chno)=R(1,2); 18 | end 19 | end 20 | end 21 | 22 | ms = zeros(1,latdimNum); 23 | for latdim=1:latdimNum 24 | latdim_performance = squeeze(corr_chs(latdim,:,:)); 25 | m = mean(mean(latdim_performance)); 26 | ms(1,latdim)=m; 27 | end 28 | -------------------------------------------------------------------------------- /performance_ica_Rvalue_encoded_decoded.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muzixiang/LatentFactorDecodingEEG/1c76ed5161296210b91af1ffb3096113fed68059/performance_ica_Rvalue_encoded_decoded.m -------------------------------------------------------------------------------- /performance_pca_Rvalue_encoded_decoded.m: -------------------------------------------------------------------------------- 1 | subNum=32; 2 | channelNum=32; 3 | testNum=10; 4 | latdimNum=16; 5 | 6 | corr_chs = zeros(subNum,latdimNum,channelNum); 7 | 8 | 9 | for subNo=1:subNum 10 | zscore_eegs_file = load(strcat('D:\Processed DEAP DATA\normalize_zscore\sub',num2str(subNo),'.mat')); 11 | zscore_eegs = zscore_eegs_file.zscore_data(:,1:32)'; 12 | for latdim = 1:latdimNum 13 | disp(strcat(' subNo: ', num2str(subNo), ' latdim: ', num2str(latdim))); 14 | [residuals, reconstructed] = pcares(zscore_eegs, latdim); 15 | decoded_eegs_pca = reconstructed; 16 | for chno1=1:channelNum 17 | max_r = 0; 18 | for chno2=1:channelNum 19 | R=corrcoef(decoded_eegs_pca(chno1,:), zscore_eegs(chno2,:)); 20 | r=R(1,2); 21 | if r>max_r 22 | max_r=r; 23 | end 24 | end 25 | corr_chs(subNo,latdim,chno1)=max_r; 26 | end 27 | corr_chs(subNo,latdim, :) 28 | end 29 | end 30 | 31 | %mean(corr_chs) -------------------------------------------------------------------------------- /performance_rbm_Rvalue_encoded_decoded.m: -------------------------------------------------------------------------------- 1 | subNum=32; 2 | channelNum=32; 3 | 4 | 5 | corr_chs = zeros(subNum,channelNum); 6 | for subNo=1:subNum 7 | filename1 = strcat(strcat('D:\VAE Experiment\decoded_eegs_rbm\decoded_eegs_rbm_sub',num2str(subNo),'_latentdim',num2str(latent_dims(subNo)),'_interdim',num2str(inter_dims(subNo)),'.mat')); 8 | decoded_eegs_rbm_file = load(filename1); 9 | decoded_eegs_rbm = decoded_eegs_rbm_file.decoded_eegs; 10 | zscore_eegs_file = load(strcat('D:\Processed DEAP DATA\normalize_zscore\sub',num2str(subNo),'.mat')); 11 | zscore_eegs = zscore_eegs_file.zscore_data; 12 | for chno=1:channelNum 13 | R=corrcoef(decoded_eegs_rbm(:,chno), zscore_eegs(:,chno)); 14 | corr_chs(subNo,chno)=R(1,2); 15 | end 16 | corr_chs(subNo,:) 17 | end 18 | mean(corr_chs) -------------------------------------------------------------------------------- /performance_vae_Rvalue_encoded_decoded.m: -------------------------------------------------------------------------------- 1 | subNum=32; 2 | channelNum=32; 3 | latdimNum=16; 4 | 5 | 6 | corr_chs = zeros(latdimNum,subNum,channelNum); 7 | for latdim=1:latdimNum 8 | for subNo=1:subNum 9 | filename1 = strcat(strcat('D:\VAE Experiment\DEAP\decoded_eegs_vae\decoded_eegs_vae_sub',num2str(subNo),'_latentdim',num2str(latdim),'.mat')); 10 | decoded_eegs_ae_file = load(filename1); 11 | decoded_eegs_ae = decoded_eegs_ae_file.decoded_eegs; 12 | zscore_eegs_file = load(strcat('D:\Processed DEAP DATA\normalize_zscore\sub',num2str(subNo),'.mat')); 13 | zscore_eegs = zscore_eegs_file.zscore_data; 14 | for chno=1:channelNum 15 | disp(strcat('latdim: ', num2str(latdim), ' subNo: ', num2str(subNo), ' chNo: ', num2str(chno))); 16 | R=corrcoef(decoded_eegs_ae(:,chno), zscore_eegs(:,chno)); 17 | corr_chs(latdim,subNo,chno)=R(1,2); 18 | end 19 | end 20 | end 21 | 22 | ms = zeros(1,latdimNum); 23 | for latdim=1:latdimNum 24 | latdim_performance = squeeze(corr_chs(latdim,:,:)); 25 | m = mean(mean(latdim_performance)); 26 | ms(1,latdim)=m; 27 | end 28 | -------------------------------------------------------------------------------- /plot_Rvalue_latentdims_each_method.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muzixiang/LatentFactorDecodingEEG/1c76ed5161296210b91af1ffb3096113fed68059/plot_Rvalue_latentdims_each_method.m -------------------------------------------------------------------------------- /plot_Rvalue_latentdims_each_subject.m: -------------------------------------------------------------------------------- 1 | 2 | aefile = load('ae_reconstruction_corr_Rvalue.mat'); 3 | ae_corr_chs = aefile.corr_chs; 4 | 5 | vaefile = load('vae_reconstruction_corr_Rvalue.mat'); 6 | vae_corr_chs = vaefile.corr_chs; 7 | 8 | latentdim_num=16; 9 | sub_num =32; 10 | 11 | corr_chs = ae_corr_chs; 12 | %corr_chs = vae_corr_chs; 13 | mean_latentdim_corrs = zeros(sub_num,latentdim_num); 14 | 15 | for subNo = 1:32 16 | for latent_dim=1:latentdim_num 17 | latdim_corr_chs = squeeze(corr_chs(latent_dim,subNo,:)); %获取到该sub该latdim下各个channel的重构表现 18 | mean_latentdim_corrs(subNo,latent_dim) = mean(latdim_corr_chs); 19 | end 20 | end 21 | 22 | 23 | startdim=2; 24 | x=startdim:1:16; 25 | for subNo = 1:32 26 | y = mean_latentdim_corrs(subNo,startdim:latentdim_num); 27 | plot(x,y,'-*','Color',[rand rand rand]);hold on 28 | end 29 | 30 | xlim([1,17]); 31 | %设置中间间隔的刻度,修改1即可 32 | set( gca, 'xtick', [1:1:17]); 33 | 34 | xlabel('Number of latent factors'); 35 | ylabel('R-value'); 36 | title('Mean Correlation between Orignial and Reconstructed Channel Signals: AE based Method'); 37 | 38 | grid on; 39 | %legend('RBM-BP','RBM-CD','ICA','AE','VAE',4); 40 | legend({'sub01','sub02','sub03','sub04','sub05','sub06','sub07','sub08','sub09','sub10',... 41 | 'sub11','sub12','sub13','sub14','sub15','sub16','sub17','sub18','sub19','sub20',... 42 | 'sub21','sub22','sub23','sub24','sub25','sub26','sub27','sub28','sub29','sub30',... 43 | 'sub31','sub32'},4); 44 | -------------------------------------------------------------------------------- /prediction_LSTM-RNN.py: -------------------------------------------------------------------------------- 1 | from keras.layers import Input, LSTM, Dense, Dropout 2 | from keras.models import Model, Sequential 3 | import h5py 4 | import scipy.io as sio 5 | import numpy as np 6 | from sklearn import metrics 7 | 8 | trialL=63*128 9 | trialNum=40 10 | latdim=16 11 | step=128*3 #控制序列长度 12 | method='1ae' 13 | emodim=1 #valence 0 arousal 1 14 | batch=40 15 | epoch_num=5 16 | 17 | def ZscoreNormalization(x): 18 | """Z-score normaliaztion""" 19 | x = (x - np.mean(x)) / np.std(x) 20 | return x 21 | 22 | 23 | #file = h5py.File('trial_labels_general_valence_arousal_dominance.mat','r') 24 | file = h5py.File('trial_labels_personal_valence_arousal_dominance.mat','r') 25 | l_data = file['trial_labels'] 26 | l_data = np.transpose(l_data) 27 | print(l_data.shape) 28 | 29 | pred_test_f1=[] 30 | pred_train_f1=[] 31 | pred_test_acc=[] 32 | pred_train_acc=[] 33 | 34 | for testSubNo in range(1,33): 35 | X_test = [] 36 | X_train = [] # list类型 37 | if testSubNo==1: 38 | Y_test = l_data[0:testSubNo * trialNum, emodim] 39 | Y_train = l_data[testSubNo*trialNum:32*trialNum,emodim] 40 | elif testSubNo==32: 41 | Y_test = l_data[(testSubNo-1)*trialNum:testSubNo * trialNum, emodim] 42 | Y_train = l_data[0:(testSubNo-1)*trialNum, emodim] 43 | else: 44 | Y_test = l_data[(testSubNo - 1) * trialNum:testSubNo * trialNum, emodim] 45 | Y_train1 = l_data[0:(testSubNo - 1) * trialNum, emodim] 46 | Y_train2 = l_data[testSubNo * trialNum:32*trialNum, emodim] 47 | Y_train = np.concatenate((Y_train1,Y_train2)) 48 | 49 | print('test subNo: '+str(testSubNo)) 50 | file1 = sio.loadmat('D:\\VAE Experiment\\DEAP\\encoded_eegs_' + method + '\\encoded_eegs_'+ method +'_sub' + str(testSubNo) + '_latentdim' + str(latdim) + '.mat') 51 | 52 | testSubData = file1['encoded_eegs'] 53 | testSubData = ZscoreNormalization(testSubData) 54 | #print(t_data.shape) 55 | #构造X_test序列集 56 | for trialNo in range(0,40): 57 | trial_data = testSubData[trialNo*trialL:(trialNo+1)*trialL,:] 58 | #以step等间隔采样 59 | trial_data = trial_data[0:trialL:step,:] 60 | #print(trial_data.shape) 61 | #将序列插入list,list中每个元素即一个序列 62 | X_test.append(trial_data) 63 | #print(len(X_test)) 64 | #print(len(Y_test)) 65 | 66 | # 构造X_train序列集 67 | for trainSubNo in range(1,33): 68 | if trainSubNo==testSubNo: 69 | continue 70 | file2 = sio.loadmat('D:\\VAE Experiment\\DEAP\\encoded_eegs_' + method + '\\encoded_eegs_'+ method + '_sub' + str( 71 | trainSubNo) + '_latentdim' + str(latdim) + '.mat') 72 | trainSubData = file2['encoded_eegs'] 73 | trainSubData = ZscoreNormalization(trainSubData) 74 | for trialNo in range(0, 40): 75 | trial_data = trainSubData[trialNo * trialL:(trialNo + 1) * trialL, :] 76 | # 以step等间隔采样 77 | trial_data = trial_data[0:trialL:step, :] 78 | # 将序列插入list,list中每个元素即一个序列 79 | X_train.append(trial_data) 80 | print(len(X_train)) 81 | print(len(Y_train)) 82 | 83 | #构造RNN模型 84 | seqL = trial_data.shape[0] 85 | print('seqence length: '+str(seqL)) 86 | model = Sequential() 87 | model.add(LSTM(200, input_shape=(seqL, latdim))) 88 | model.add(Dropout(0.5)) 89 | model.add(Dense(100, activation='relu')) 90 | model.add(Dropout(0.5)) 91 | model.add(Dense(1, activation='sigmoid')) 92 | 93 | model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) 94 | model.fit([X_train], Y_train, validation_data=([X_test], Y_test), epochs=epoch_num, batch_size=batch, shuffle=True) 95 | # predict on the training data after training... 96 | Y_test_predict = model.predict_classes([X_test]) 97 | Y_train_predict = model.predict_classes([X_train]) 98 | 99 | print(metrics.f1_score(Y_test, Y_test_predict)) 100 | print(metrics.accuracy_score(Y_test, Y_test_predict)) 101 | print(metrics.f1_score(Y_train, Y_train_predict)) 102 | print(metrics.accuracy_score(Y_train, Y_train_predict)) 103 | 104 | pred_test_f1.append(metrics.f1_score(Y_test, Y_test_predict)) 105 | pred_test_acc.append(metrics.accuracy_score(Y_test, Y_test_predict)) 106 | pred_train_f1.append(metrics.f1_score(Y_train, Y_train_predict)) 107 | pred_train_acc.append(metrics.accuracy_score(Y_train, Y_train_predict)) 108 | 109 | if emodim==0: 110 | sio.savemat('lstm_personal_performance_ae_valence_subcross.mat', 111 | {'test_f1': pred_test_f1, 'test_acc': pred_test_acc, 'train_f1': pred_train_f1, 112 | 'train_acc': pred_train_acc}) 113 | else: 114 | sio.savemat('lstm_personal_performance_ae_arousal_subcross.mat', 115 | {'test_f1': pred_test_f1, 'test_acc': pred_test_acc, 'train_f1': pred_train_f1, 116 | 'train_acc': pred_train_acc}) -------------------------------------------------------------------------------- /reconstruction_AE.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | import numpy as np 4 | from keras.layers import Input, Dense, LeakyReLU 5 | from keras.models import Model 6 | import scipy.io as sio 7 | import h5py 8 | import tensorflow as tf 9 | 10 | batch_size = 500 11 | original_dim = 32 12 | epochs = 10 13 | zscore = True 14 | 15 | for subNo in range(1,33): #subNo 从1到32 16 | for latent_dim in range(1,17): 17 | print('subNo: '+str(subNo)+' latend_dim: '+str(latent_dim)) 18 | # encoder 19 | eeg_input = Input(shape=(original_dim,)) 20 | dense1 = Dense(latent_dim, activation='sigmoid')(eeg_input) 21 | encoder = Model(eeg_input, dense1) 22 | # decoder 23 | eeg_output = Dense(original_dim)(dense1) 24 | 25 | ae = Model(eeg_input, eeg_output) 26 | ae.compile(optimizer='adam', loss='mean_squared_error') 27 | 28 | #### train the VAE on normalized (z-score) multi-channel EEG data 29 | if zscore: 30 | sub_data_file = h5py.File('D:\\Processed DEAP DATA\\normalize_zscore\\sub'+str(subNo)+'.mat', 'r') 31 | #sub_data_file = h5py.File('D:\\Processed DEAP DATA\\normalize_zscore_bandpass\\sub' + str(subNo+1) + '.mat', 'r') 32 | x_train = sub_data_file['zscore_data'] 33 | else: 34 | sub_data_file = h5py.File('D:\\Processed DEAP DATA\\normalize_minmax\\sub'+str(subNo)+'.mat', 'r') 35 | #sub_data_file = h5py.File('D:\\Processed DEAP DATA\\normalize_minmax_bandpass\\sub' + str(subNo+1) + '.mat', 'r') 36 | x_train = sub_data_file['minmax_data'] 37 | 38 | x_train = np.transpose(x_train)[:, 0:32] 39 | x_test = x_train 40 | 41 | ae.fit(x_train, x_train, 42 | shuffle=True, 43 | epochs=epochs, 44 | batch_size=batch_size, 45 | validation_data=(x_test, x_test)) 46 | 47 | # build a model to project inputs 48 | decoded_x = ae.predict(x_train) 49 | encoded_x = encoder.predict(x_train) 50 | 51 | 52 | sio.savemat('D:\\VAE Experiment\\DEAP\\encoded_eegs_1ae\\encoded_eegs_1ae_sub' + 53 | str(subNo) + '_latentdim' + str(latent_dim) + '.mat', {'encoded_eegs': encoded_x}) 54 | 55 | sio.savemat('D:\\VAE Experiment\\DEAP\\decoded_eegs_1ae\\decoded_eegs_1ae_sub' + 56 | str(subNo) + '_latentdim' + str(latent_dim) + '.mat', {'decoded_eegs': decoded_x}) -------------------------------------------------------------------------------- /reconstruction_RBM.m: -------------------------------------------------------------------------------- 1 | %Test getFeature function for autoEncoder DBN in MNIST data set 2 | % clc 3 | % clear; 4 | % addpath('DeeBNet'); 5 | % 6 | % This code needs the DeeBNet Toolbox V3.0 for runing 7 | % 8 | 9 | subNum=32; 10 | %batch_size = 500; 11 | %original_dim = 32; 12 | epochs = 50; 13 | 14 | for subNo=1:subNum 15 | for latent_dim = 1:16 16 | zscore_eegs_file = load(strcat('D:\Processed DEAP DATA\normalize_zscore\sub',num2str(subNo),'.mat')); 17 | zscore_eegs = zscore_eegs_file.zscore_data(:,1:32); 18 | 19 | data = DataClasses.DataStore(); 20 | data.valueType = ValueType.gaussian; 21 | data.trainData = zscore_eegs; 22 | data.testData = zscore_eegs; 23 | data.validationData=zscore_eegs; 24 | 25 | dbn=DBN('autoEncoder'); 26 | %dbn.dbnType='autoEncoder'; 27 | % RBM1 28 | % rbmParams=RbmParameters(inter_dims(subNo),ValueType.gaussian); 29 | % rbmParams.maxEpoch=epochs; 30 | % rbmParams.gpu=1; 31 | % rbmParams.samplingMethodType=SamplingClasses.SamplingMethodType.CD; 32 | % rbmParams.performanceMethod='reconstruction'; 33 | % dbn.addRBM(rbmParams); 34 | % RBM2 35 | rbmParams=RbmParameters(latent_dim,ValueType.gaussian); 36 | rbmParams.maxEpoch=epochs; 37 | rbmParams.gpu=1; 38 | rbmParams.samplingMethodType=SamplingClasses.SamplingMethodType.CD; 39 | rbmParams.performanceMethod='reconstruction'; 40 | dbn.addRBM(rbmParams); 41 | 42 | dbn.train(data); 43 | useGPU='yes'; 44 | dbn.backpropagation(data,useGPU); 45 | 46 | encoded_eegs=dbn.getFeature(zscore_eegs); 47 | decoded_eegs=dbn.reconstructData(zscore_eegs); 48 | 49 | %save data 50 | fileName1 = strcat('D:\VAE Experiment\encoded_eegs_1rbm\encoded_eegs_1rbm_sub',num2str(subNo),'_latentdim',num2str(latent_dim)); 51 | save(fileName1,'encoded_eegs','-v7.3'); 52 | fileName2 = strcat('D:\VAE Experiment\decoded_eegs_1rbm\decoded_eegs_1rbm_sub',num2str(subNo),'_latentdim',num2str(latent_dim)); 53 | save(fileName2,'decoded_eegs','-v7.3'); 54 | disp(strcat('ends!subject ',num2str(subNo))); 55 | end 56 | end 57 | 58 | -------------------------------------------------------------------------------- /reconstruction_VAE.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | from __future__ import absolute_import 4 | from __future__ import division 5 | from __future__ import print_function 6 | 7 | from keras.layers import Lambda, Input, Dense, LeakyReLU 8 | from keras.models import Model 9 | from keras.datasets import mnist 10 | from keras.losses import mse, binary_crossentropy, mean_squared_error 11 | from keras.utils import plot_model 12 | from keras import backend as K 13 | from keras import optimizers 14 | 15 | import numpy as np 16 | import matplotlib.pyplot as plt 17 | import argparse 18 | import os 19 | import h5py 20 | import scipy.io as sio 21 | 22 | # reparameterization trick 23 | # instead of sampling from Q(z|X), sample eps = N(0,I) 24 | # z = z_mean + sqrt(var)*eps 25 | def sampling(args): 26 | """Reparameterization trick by sampling fr an isotropic unit Gaussian. 27 | # Arguments: 28 | args (tensor): mean and log of variance of Q(z|X) 29 | # Returns: 30 | z (tensor): sampled latent vector 31 | """ 32 | z_mean, z_log_var = args 33 | batch = K.shape(z_mean)[0] 34 | dim = K.int_shape(z_mean)[1] 35 | # by default, random_normal has mean=0 and std=1.0 36 | epsilon = K.random_normal(shape=(batch, dim)) 37 | return z_mean + K.exp(0.5 * z_log_var) * epsilon 38 | 39 | 40 | batch_size = 512 41 | original_dim = 32 42 | epochs = 10 43 | subNum = 32 44 | zscore = True 45 | 46 | 47 | for subNo in range(25,33): #subNo 从1到32 48 | for latent_dim in range(1,17): #latent_dim 从2到11/16/ 49 | #### train the VAE on normalized (z-score) multi-channel EEG data 50 | if zscore: 51 | sub_data_file = h5py.File('D:\\Processed DEAP DATA\\normalize_zscore\\sub' + str(subNo) + '.mat', 'r') 52 | x_train = sub_data_file['zscore_data'] 53 | else: 54 | sub_data_file = h5py.File('D:\\Processed DEAP DATA\\normalize_minmax\\sub' + str(subNo) + '.mat', 'r') 55 | x_train = sub_data_file['minmax_data'] 56 | 57 | x_train = np.transpose(x_train)[:, 0:32] 58 | x_test = x_train 59 | 60 | # VAE model = encoder + decoder 61 | 62 | # build encoder model 63 | inputs = Input(shape=(original_dim, ), name='encoder_input') 64 | h = Dense(128, activation=LeakyReLU(alpha=0.3))(inputs) 65 | z_mean = Dense(latent_dim, name='z_mean')(h) 66 | z_log_var = Dense(latent_dim, name='z_log_var')(h) 67 | # use reparameterization trick to push the sampling out as input 68 | # note that "output_shape" isn't necessary with the TensorFlow backend 69 | z = Lambda(sampling, output_shape=(latent_dim,), name='z')([z_mean, z_log_var]) 70 | # instantiate encoder model 71 | encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder') 72 | #encoder.summary() 73 | #plot_model(encoder, to_file='vae_mlp_encoder.png', show_shapes=True) 74 | 75 | # build decoder model 76 | latent_inputs = Input(shape=(latent_dim,), name='z_sampling') 77 | h_decoded = Dense(128, activation=LeakyReLU(alpha=0.3))(latent_inputs) 78 | inputs_decoded = Dense(original_dim)(h_decoded) 79 | # instantiate decoder model 80 | decoder = Model(latent_inputs, inputs_decoded, name='decoder') 81 | #decoder.summary() 82 | #plot_model(decoder, to_file='vae_mlp_decoder.png', show_shapes=True) 83 | 84 | # instantiate VAE model 85 | outputs = decoder(encoder(inputs)[2]) 86 | vae = Model(inputs, outputs, name='vae_mlp') 87 | 88 | # VAE loss = mse_loss or xent_loss + kl_loss 89 | #reconstruction_loss = binary_crossentropy(inputs, outputs) 90 | reconstruction_loss = mean_squared_error(inputs, outputs) 91 | reconstruction_loss *= original_dim 92 | kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var) 93 | kl_loss = K.sum(kl_loss, axis=-1) 94 | kl_loss *= -0.5 95 | vae_loss = K.mean(reconstruction_loss + kl_loss) 96 | vae.add_loss(vae_loss) 97 | 98 | rmsprop = optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=None, decay=0.0) 99 | vae.compile(optimizer=rmsprop) 100 | #vae.summary() 101 | #plot_model(vae, to_file='vae_mlp.png', show_shapes=True) 102 | 103 | # train the autoencoder 104 | vae.fit(x_train, 105 | epochs=epochs, 106 | batch_size=batch_size, 107 | validation_data=(x_test, None)) 108 | # save the model 109 | # vae.save_weights('vae_mlp_mnist.h5') 110 | 111 | # build a model to project inputs 112 | encoded_x_zmean = encoder.predict(x_train)[0] 113 | encoded_x_z = encoder.predict(x_train)[2] 114 | 115 | sio.savemat('D:\\VAE Experiment\\DEAP\\encoded_eegs_2vae\\encoded_eegs_2vae_zmean_sub' + 116 | str(subNo) + '_latentdim' + str(latent_dim) + '.mat', 117 | {'encoded_eegs_zmean': encoded_x_zmean}) 118 | sio.savemat('D:\\VAE Experiment\\DEAP\\encoded_eegs_2vae\\encoded_eegs_2vae_z_sub' + 119 | str(subNo) + '_latentdim' + str(latent_dim) + '.mat', 120 | {'encoded_eegs_z': encoded_x_z}) 121 | 122 | decoded_x = vae.predict(x_train) 123 | sio.savemat('D:\\VAE Experiment\\DEAP\\decoded_eegs_2vae\\decoded_eegs_2vae_sub' + 124 | str(subNo) + '_latentdim' + str(latent_dim) + '.mat', 125 | {'decoded_eegs': decoded_x}) --------------------------------------------------------------------------------