网站网址前的小图标怎么做的,网站代码规范,漂亮的网页设计,建行企业银行app下载简介 本文基于LSTM来完成用户行为识别。数据集来源#xff1a;https://archive.ics.uci.edu/ml/machine-learning-databases/00240/
此数据集一共有6种行为状态#xff1a;
行走#xff1b; 站立#xff1b; 躺下#xff1b; 坐下#xff1b; 上楼#xff1b; 下楼https://archive.ics.uci.edu/ml/machine-learning-databases/00240/
此数据集一共有6种行为状态
行走 站立 躺下 坐下 上楼 下楼 以上6种行为数据是通过传感器进行采集的。
.\data\UCI HAR Dataset\train\Inertial Signals 实现 本次实验实现的是6分类任务。
pip install -i https://pypi.douban.com/simple/ --trusted-hostpypi.douban.com/simple tensorflow1.13.1 import tensorflow as tf
import numpy as np# # 模型好坏主要由数据决定数据决定模型上限模型决定逼近这个上限记录仪上的数据
def load_X(X_signals_paths):X_signals []for signal_type_path in X_signals_paths:file open(signal_type_path, r)X_signals.append([np.array(serie, dtypenp.float32) for serie in[row.replace( , ).strip().split( ) for row in file]])file.close()return np.transpose(np.array(X_signals), (1, 2, 0))def load_y(y_path):file open(y_path, r)y_ np.array([elem for elem in [row.replace( , ).strip().split( ) for row in file]], dtypenp.int32)file.close()return y_ - 1class Config(object):def __init__(self, X_train, X_test):self.train_count len(X_train) # 训练记录self.test_data_count len(X_test)self.n_steps len(X_train[0]) # 步长128步self.learning_rate 0.0025self.lambda_loss_amount 0.0015 # 正则化惩罚粒度self.training_epochs 300self.batch_size 1500self.n_inputs len(X_train[0][0]) # 每个step收集9个数据收集维度self.n_hidden 32 # 隐层神经元个数self.n_classes 6 # 输出6个类别self.W {hidden: tf.Variable(tf.random_normal([self.n_inputs, self.n_hidden])), # 输入到隐层output: tf.Variable(tf.random_normal([self.n_hidden, self.n_classes]))} # 隐层到输出self.biases {hidden: tf.Variable(tf.random_normal([self.n_hidden], mean1.0)),output: tf.Variable(tf.random_normal([self.n_classes]))}# 构造LSTM网络
def LSTM_Network(_X, config):# 数据转换使其满足LSTM网络要求_X tf.transpose(_X, [1, 0, 2]) # 把0 1 2调换成1 0 2调换第一维度和第二维度_X tf.reshape(_X, [-1, config.n_inputs])_X tf.nn.relu(tf.matmul(_X, config.W[hidden]) config.biases[hidden]) # 9个神经元变为32_X tf.split(_X, config.n_steps, 0) # 把每一步放到RNN对应的位置# 两层LSTM堆叠在一起lstm_cell_1 tf.contrib.rnn.BasicLSTMCell(config.n_hidden, forget_bias1.0, state_is_tupleTrue)lstm_cell_2 tf.contrib.rnn.BasicLSTMCell(config.n_hidden, forget_bias1.0, state_is_tupleTrue)lstm_cells tf.contrib.rnn.MultiRNNCell([lstm_cell_1, lstm_cell_2], state_is_tupleTrue)outputs, states tf.contrib.rnn.static_rnn(lstm_cells, _X, dtypetf.float32) # outputs最终结果 states中间结果print(np.array(outputs).shape) # 查看outputs128个输出结果lstm_last_output outputs[-1] # 取最终结果return tf.matmul(lstm_last_output, config.W[output]) config.biases[output] # 分类def one_hot(y_):y_ y_.reshape(len(y_))n_values int(np.max(y_)) 1return np.eye(n_values)[np.array(y_, dtypenp.int32)]if __name__ __main__:# 指定九种不同输入信号即9个文件的文件名前缀INPUT_SIGNAL_TYPES [body_acc_x_,body_acc_y_,body_acc_z_,body_gyro_x_,body_gyro_y_,body_gyro_z_,total_acc_x_,total_acc_y_,total_acc_z_]# 六种行为标签行走 站立 躺下 坐下 上楼 下楼LABELS [WALKING,WALKING_UPSTAIRS,WALKING_DOWNSTAIRS,SITTING,STANDING,LAYING]# 指定数据路径DATA_PATH data/DATASET_PATH DATA_PATH UCI HAR Dataset/print(\n Dataset is now located at: DATASET_PATH)TRAIN train/TEST test/X_train_signals_paths [DATASET_PATH TRAIN Inertial Signals/ signal train.txt for signal in INPUT_SIGNAL_TYPES]X_test_signals_paths [DATASET_PATH TEST Inertial Signals/ signal test.txt for signal inINPUT_SIGNAL_TYPES]X_train load_X(X_train_signals_paths)X_test load_X(X_test_signals_paths)print(X_train:, X_train.shape) # 7352条数据每个数据128窗口序列每个序列记录9个不同指标print(X_test:, X_test.shape)y_train_path DATASET_PATH TRAIN y_train.txty_test_path DATASET_PATH TEST y_test.txty_train one_hot(load_y(y_train_path))y_test one_hot(load_y(y_test_path))print(y_train:, y_train.shape) # 7352条数据6个类别print(y_test:, y_test.shape)config Config(X_train, X_test)print(Some useful info to get an insight on datasets shape and normalisation:)print(features shape, labels shape, each features mean, each features standard deviation)print(X_test.shape, y_test.shape,np.mean(X_test), np.std(X_test))print(the dataset is therefore properly normalised, as expected.)X tf.placeholder(tf.float32, [None, config.n_steps, config.n_inputs])Y tf.placeholder(tf.float32, [None, config.n_classes])pred_Y LSTM_Network(X, config) # 最终预测结果# l2正则惩罚,tf.trainable_variables()(仅可以查看可训练的变量)l2 config.lambda_loss_amount * \sum(tf.nn.l2_loss(tf_var) for tf_var in tf.trainable_variables())# 损失值cost tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labelsY, logitspred_Y)) l2optimizer tf.train.AdamOptimizer(learning_rateconfig.learning_rate).minimize(cost)correct_pred tf.equal(tf.argmax(pred_Y, 1), tf.argmax(Y, 1))accuracy tf.reduce_mean(tf.cast(correct_pred, dtypetf.float32))# tf.InteractiveSession()可以先构建一个session然后再定义操作operation# tf.Session()需要在会话构建之前定义好全部的操作operation然后再构建会话# tf.ConfigProto()获取到 operations 和 Tensor 被指派到哪个设备(几号CPU或几号GPU)上运行# log_device_placementFalse不会在终端打印出各项操作是在哪个设备上运行sess tf.InteractiveSession(configtf.ConfigProto(log_device_placementFalse))init tf.global_variables_initializer()sess.run(init)best_accuracy 0.0for i in range(config.training_epochs):# zip() 函数用于将可迭代的对象作为参数将对象中对应的元素打包成一个个元组然后返回由这些元组组成的列表for start, end in zip(range(0, config.train_count, config.batch_size),range(config.batch_size, config.train_count 1, config.batch_size)):sess.run(optimizer, feed_dict{X: X_train[start:end],Y: y_train[start:end]})# 也可对迭代过程进行可视化展示pred_out, accuracy_out, loss_out sess.run([pred_Y, accuracy, cost], feed_dict{X: X_test, Y: y_test})print(traing iter: {},.format(i) test accuracy: {},.format(accuracy_out) loss:{}.format(loss_out))best_accuracy max(best_accuracy, accuracy_out)print()print(final test accuracy: {}.format(accuracy_out))print(best epochs test accuracy: {}.format(best_accuracy))print() 运行结果
Dataset is now located at:data/UCI HAR Dataset/
X_train: (7352, 128, 9)
X_test: (2947, 128, 9)
y_train: (7352, 6)
y_test: (2947, 6)
Some useful info to get an insight on datasets shape and normalisation:
features shape, labels shape, each features mean, each features standard deviation
(2947, 128, 9) (2947, 6) 0.09913992 0.39567086
the dataset is therefore properly normalised, as expected.
WARNING:tensorflow:From D:/WorkSpace/ai/csdn/lab-lstm-activity-recognition/lstm.py:58: BasicLSTMCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This class is deprecated, please use tf.nn.rnn_cell.LSTMCell, which supports all the feature this cell currently has. Please replace the existing code with tf.nn.rnn_cell.LSTMCell(namebasic_lstm_cell).
(128,)
WARNING:tensorflow:From D:/WorkSpace/ai/csdn/lab-lstm-activity-recognition/lstm.py:141: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Future major versions of TensorFlow will allow gradients to flow
into the labels input on backprop by default.
See tf.nn.softmax_cross_entropy_with_logits_v2.
2019-12-16 19:45:10.908801: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
traing iter: 0,test accuracy: 0.4801492989063263,loss:1.9456521272659302
final test accuracy: 0.4801492989063263
best epochs test accuracy: 0.4801492989063263traing iter: 1,test accuracy: 0.5334238409996033,loss:1.6313532590866089final test accuracy: 0.5334238409996033
best epochs test accuracy: 0.5334238409996033
traing iter: 2,test accuracy: 0.6128265857696533,loss:1.4844205379486084
final test accuracy: 0.6128265857696533
best epochs test accuracy: 0.6128265857696533
任何程序错误以及技术疑问或需要解答的请添加