LSTM
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.layers import LSTM
from tensorflow.keras.utils import to_categorical
def load_file(filepath):
dataframe = pd.read_csv(filepath, header=None, delim_whitespace=True)
return dataframe.values
def load_dataset(data_rootdir, dirname, group):
'''
该函数实现将训练数据或测试数据文件列表堆叠为三维数组
'''
filename_list = []
filepath_list = []
X = []
# os.walk() 方法是一个简单易用的文件、目录遍历器,可以高效的处理文件、目录。
for rootdir, dirnames, filenames in os.walk(data_rootdir + dirname):
for filename in filenames:
filename_list.append(filename)
filepath_list.append(os.path.join(rootdir, filename))
#print(filename_list)
#print(filepath_list)
# 遍历根目录下的文件,并读取为DataFrame格式;
for filepath in filepath_list:
X.append(load_file(filepath))
X = np.dstack(X) # dstack沿第三个维度叠加,两个二维数组叠加后,前两个维度尺寸不变,第三个维度增加;
y = load_file(data_rootdir+'/y_'+group+'.txt')
# one-hot编码。这个之前的文章中提到了,因为原数据集标签从1开始,而one-hot编码从0开始,所以要先减去1
y = to_categorical(y-1)
print('{}_X.shape:{},{}_y.shape:{}\n'.format(group,X.shape,group,y.shape))
return X, y
def evaluate_model(trainX, trainy, testX, testy):
verbose, epochs, batch_size = 0, 15, 64
n_timesteps, n_features, n_outputs = trainX.shape[1], trainX.shape[2], trainy.shape[1]
model = Sequential()
model.add(LSTM(100, input_shape=(n_timesteps,n_features)))
model.add(Dropout(0.5))
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size, verbose=verbose)
_, accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=0)
return accuracy
def run_experiment(trainX, trainy, testX, testy, repeats=10):
scores = list()
for r in range(repeats):
score = evaluate_model(trainX, trainy, testX, testy)
score = score * 100.0
print('>#%d: %.3f' % (r+1, score))
scores.append(score)
m, s = np.mean(scores), np.std(scores)
print('Accuracy: %.3f%% (+/-%.3f)' % (m, s))
if __name__ == '__main__':
train_dir = 'D:/GraduationCode/01 Datasets/UCI HAR Dataset/train/'
test_dir = 'D:/GraduationCode/01 Datasets/UCI HAR Dataset/test/'
dirname = '/Inertial Signals/'
trainX, trainy = load_dataset(train_dir, dirname, 'train')
testX, testy = load_dataset(test_dir, dirname, 'test')
run_experiment(trainX, trainy, testX, testy, repeats=10)
train_X.shape:(7352, 128, 9),train_y.shape:(7352, 6) test_X.shape:(2947, 128, 9),test_y.shape:(2947, 6) Model: "sequential_1" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= lstm_1 (LSTM) (None, 100) 44000 _________________________________________________________________ dropout_1 (Dropout) (None, 100) 0 _________________________________________________________________ dense_2 (Dense) (None, 100) 10100 _________________________________________________________________ dense_3 (Dense) (None, 6) 606 ================================================================= Total params: 54,706 Trainable params: 54,706 Non-trainable params: 0 _________________________________________________________________ None >#1: 90.770 Model: "sequential_2" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= lstm_2 (LSTM) (None, 100) 44000 _________________________________________________________________ dropout_2 (Dropout) (None, 100) 0 _________________________________________________________________ dense_4 (Dense) (None, 100) 10100 _________________________________________________________________ dense_5 (Dense) (None, 6) 606 ================================================================= Total params: 54,706 Trainable params: 54,706 Non-trainable params: 0 _________________________________________________________________ None >#2: 90.804 Model: "sequential_3" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= lstm_3 (LSTM) (None, 100) 44000 _________________________________________________________________ dropout_3 (Dropout) (None, 100) 0 _________________________________________________________________ dense_6 (Dense) (None, 100) 10100 _________________________________________________________________ dense_7 (Dense) (None, 6) 606 ================================================================= Total params: 54,706 Trainable params: 54,706 Non-trainable params: 0 _________________________________________________________________ None >#3: 88.768 Model: "sequential_4" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= lstm_4 (LSTM) (None, 100) 44000 _________________________________________________________________ dropout_4 (Dropout) (None, 100) 0 _________________________________________________________________ dense_8 (Dense) (None, 100) 10100 _________________________________________________________________ dense_9 (Dense) (None, 6) 606 ================================================================= Total params: 54,706 Trainable params: 54,706 Non-trainable params: 0 _________________________________________________________________ None >#4: 88.870 Model: "sequential_5" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= lstm_5 (LSTM) (None, 100) 44000 _________________________________________________________________ dropout_5 (Dropout) (None, 100) 0 _________________________________________________________________ dense_10 (Dense) (None, 100) 10100 _________________________________________________________________ dense_11 (Dense) (None, 6) 606 ================================================================= Total params: 54,706 Trainable params: 54,706 Non-trainable params: 0 _________________________________________________________________ None >#5: 90.227 Model: "sequential_6" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= lstm_6 (LSTM) (None, 100) 44000 _________________________________________________________________ dropout_6 (Dropout) (None, 100) 0 _________________________________________________________________ dense_12 (Dense) (None, 100) 10100 _________________________________________________________________ dense_13 (Dense) (None, 6) 606 ================================================================= Total params: 54,706 Trainable params: 54,706 Non-trainable params: 0 _________________________________________________________________ None >#6: 87.615 Model: "sequential_7" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= lstm_7 (LSTM) (None, 100) 44000 _________________________________________________________________ dropout_7 (Dropout) (None, 100) 0 _________________________________________________________________ dense_14 (Dense) (None, 100) 10100 _________________________________________________________________ dense_15 (Dense) (None, 6) 606 ================================================================= Total params: 54,706 Trainable params: 54,706 Non-trainable params: 0 _________________________________________________________________ None >#7: 91.313 Model: "sequential_8" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= lstm_8 (LSTM) (None, 100) 44000 _________________________________________________________________ dropout_8 (Dropout) (None, 100) 0 _________________________________________________________________ dense_16 (Dense) (None, 100) 10100 _________________________________________________________________ dense_17 (Dense) (None, 6) 606 ================================================================= Total params: 54,706 Trainable params: 54,706 Non-trainable params: 0 _________________________________________________________________ None >#8: 87.479 Model: "sequential_9" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= lstm_9 (LSTM) (None, 100) 44000 _________________________________________________________________ dropout_9 (Dropout) (None, 100) 0 _________________________________________________________________ dense_18 (Dense) (None, 100) 10100 _________________________________________________________________ dense_19 (Dense) (None, 6) 606 ================================================================= Total params: 54,706 Trainable params: 54,706 Non-trainable params: 0 _________________________________________________________________ None
>#9: 91.144 Model: "sequential_10" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= lstm_10 (LSTM) (None, 100) 44000 _________________________________________________________________ dropout_10 (Dropout) (None, 100) 0 _________________________________________________________________ dense_20 (Dense) (None, 100) 10100 _________________________________________________________________ dense_21 (Dense) (None, 6) 606 ================================================================= Total params: 54,706 Trainable params: 54,706 Non-trainable params: 0 _________________________________________________________________ None >#10: 90.092 Accuracy: 89.708% (+/-1.354)
CNN-LSTM
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv1D, MaxPooling1D
from tensorflow.keras.layers import LSTM, TimeDistributed, ConvLSTM2D
from tensorflow.keras.utils import to_categorical
def load_file(filepath):
dataframe = pd.read_csv(filepath, header=None, delim_whitespace=True)
return dataframe.values
def load_dataset(data_rootdir, dirname, group):
'''
该函数实现将训练数据或测试数据文件列表堆叠为三维数组
'''
filename_list = []
filepath_list = []
X = []
# os.walk() 方法是一个简单易用的文件、目录遍历器,可以高效的处理文件、目录。
for rootdir, dirnames, filenames in os.walk(data_rootdir + dirname):
for filename in filenames:
filename_list.append(filename)
filepath_list.append(os.path.join(rootdir, filename))
#print(filename_list)
#print(filepath_list)
# 遍历根目录下的文件,并读取为DataFrame格式;
for filepath in filepath_list:
X.append(load_file(filepath))
X = np.dstack(X) # dstack沿第三个维度叠加,两个二维数组叠加后,前两个维度尺寸不变,第三个维度增加;
y = load_file(data_rootdir+'/y_'+group+'.txt')
# one-hot编码。这个之前的文章中提到了,因为原数据集标签从1开始,而one-hot编码从0开始,所以要先减去1
y = to_categorical(y-1)
print('{}_X.shape:{},{}_y.shape:{}\n'.format(group,X.shape,group,y.shape))
return X, y
def evaluate_model(trainX, trainy, testX, testy):
verbose, epochs, batch_size = 0, 25, 64
n_timesteps, n_features, n_outputs = trainX.shape[1], trainX.shape[2], trainy.shape[1]
n_steps, n_length = 4, 32
trainX = trainX.reshape((trainX.shape[0], n_steps, n_length, n_features))
testX = testX.reshape((testX.shape[0], n_steps, n_length, n_features))
model = Sequential()
model.add(TimeDistributed(Conv1D(filters=64, kernel_size=3, activation='relu'),
input_shape=(None, n_length, n_features)))
model.add(TimeDistributed(Conv1D(filters=64, kernel_size=3, activation='relu')))
model.add(TimeDistributed(Dropout(0.5)))
model.add(TimeDistributed(MaxPooling1D(pool_size=2)))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(100))
model.add(Dropout(0.5))
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size, verbose=verbose)
_, accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=0)
return accuracy
def run_experiment(trainX, trainy, testX, testy, repeats=10):
scores = list()
for r in range(repeats):
score = evaluate_model(trainX, trainy, testX, testy)
score = score * 100.0
print('>#%d: %.3f' % (r+1, score))
scores.append(score)
m, s = np.mean(scores), np.std(scores)
print('Accuracy: %.3f%% (+/-%.3f)' % (m, s))
if __name__ == '__main__':
train_dir = 'D:/GraduationCode/01 Datasets/UCI HAR Dataset/train/'
test_dir = 'D:/GraduationCode/01 Datasets/UCI HAR Dataset/test/'
dirname = '/Inertial Signals/'
trainX, trainy = load_dataset(train_dir, dirname, 'train')
testX, testy = load_dataset(test_dir, dirname, 'test')
run_experiment(trainX, trainy, testX, testy, repeats=2)
train_X.shape:(7352, 128, 9),train_y.shape:(7352, 6) test_X.shape:(2947, 128, 9),test_y.shape:(2947, 6) >#1: 90.126 >#2: 90.872 Accuracy: 90.499% (+/-0.373)
ConvLSTM
import numpy as np
import matplotlib.pyplot as plt
import os
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.layers import LSTM, TimeDistributed, ConvLSTM2D
from tensorflow.keras.utils import to_categorical
def load_file(filepath):
dataframe = pd.read_csv(filepath, header=None, delim_whitespace=True)
return dataframe.values
def load_dataset(data_rootdir, dirname, group):
'''
该函数实现将训练数据或测试数据文件列表堆叠为三维数组
'''
filename_list = []
filepath_list = []
X = []
# os.walk() 方法是一个简单易用的文件、目录遍历器,可以高效的处理文件、目录。
for rootdir, dirnames, filenames in os.walk(data_rootdir + dirname):
for filename in filenames:
filename_list.append(filename)
filepath_list.append(os.path.join(rootdir, filename))
#print(filename_list)
#print(filepath_list)
# 遍历根目录下的文件,并读取为DataFrame格式;
for filepath in filepath_list:
X.append(load_file(filepath))
X = np.dstack(X) # dstack沿第三个维度叠加,两个二维数组叠加后,前两个维度尺寸不变,第三个维度增加;
y = load_file(data_rootdir+'/y_'+group+'.txt')
# one-hot编码。这个之前的文章中提到了,因为原数据集标签从1开始,而one-hot编码从0开始,所以要先减去1
y = to_categorical(y-1)
print('{}_X.shape:{},{}_y.shape:{}\n'.format(group,X.shape,group,y.shape))
return X, y
def evaluate_model(trainX, trainy, testX, testy):
verbose, epochs, batch_size = 0, 25, 64
n_timesteps, n_features, n_outputs = trainX.shape[1], trainX.shape[2], trainy.shape[1]
n_steps, n_length = 4, 32
trainX = trainX.reshape((trainX.shape[0], n_steps, 1, n_length, n_features))
testX = testX.reshape((testX.shape[0], n_steps, 1, n_length, n_features))
model = Sequential()
model.add(ConvLSTM2D(filters=64, kernel_size=(1,3), activation='relu', input_shape=(n_steps, 1, n_length, n_features)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size, verbose=verbose)
_, accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=0)
return accuracy
def run_experiment(trainX, trainy, testX, testy, repeats=10):
scores = list()
for r in range(repeats):
score = evaluate_model(trainX, trainy, testX, testy)
score = score * 100.0
print('>#%d: %.3f' % (r+1, score))
scores.append(score)
m, s = np.mean(scores), np.std(scores)
print('Accuracy: %.3f%% (+/-%.3f)' % (m, s))
if __name__ == '__main__':
train_dir = 'D:/GraduationCode/01 Datasets/UCI HAR Dataset/train/'
test_dir = 'D:/GraduationCode/01 Datasets/UCI HAR Dataset/test/'
dirname = '/Inertial Signals/'
trainX, trainy = load_dataset(train_dir, dirname,'train'')
testX, testy = load_dataset(test_dir, dirname, 'test')
run_experiment(trainX, trainy, testX, testy, repeats=2)
train_X.shape:(7352, 128, 9),train_y.shape:(7352, 6) test_X.shape:(2947, 128, 9),test_y.shape:(2947, 6) >#1: 90.159 >#2: 91.347 Accuracy: 90.753% (+/-0.594)