{"id":13454687,"url":"https://github.com/guillaume-chevalier/LSTM-Human-Activity-Recognition","last_synced_at":"2025-03-24T06:31:07.175Z","repository":{"id":41109555,"uuid":"59073471","full_name":"guillaume-chevalier/LSTM-Human-Activity-Recognition","owner":"guillaume-chevalier","description":"Human Activity Recognition example using TensorFlow on smartphone sensors dataset and an LSTM RNN. Classifying the type of movement amongst six activity categories - Guillaume Chevalier","archived":false,"fork":false,"pushed_at":"2022-11-06T17:53:06.000Z","size":1442,"stargazers_count":3379,"open_issues_count":22,"forks_count":939,"subscribers_count":160,"default_branch":"master","last_synced_at":"2025-03-23T17:04:19.060Z","etag":null,"topics":["activity-recognition","deep-learning","human-activity-recognition","lstm","machine-learning","neural-network","recurrent-neural-networks","rnn","tensorflow"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/guillaume-chevalier.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-18T02:00:21.000Z","updated_at":"2025-03-21T16:12:26.000Z","dependencies_parsed_at":"2022-07-12T18:17:27.438Z","dependency_job_id":null,"html_url":"https://github.com/guillaume-chevalier/LSTM-Human-Activity-Recognition","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guillaume-chevalier%2FLSTM-Human-Activity-Recognition","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guillaume-chevalier%2FLSTM-Human-Activity-Recognition/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guillaume-chevalier%2FLSTM-Human-Activity-Recognition/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guillaume-chevalier%2FLSTM-Human-Activity-Recognition/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/guillaume-chevalier","download_url":"https://codeload.github.com/guillaume-chevalier/LSTM-Human-Activity-Recognition/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245222402,"owners_count":20580150,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activity-recognition","deep-learning","human-activity-recognition","lstm","machine-learning","neural-network","recurrent-neural-networks","rnn","tensorflow"],"created_at":"2024-07-31T08:00:56.857Z","updated_at":"2025-03-24T06:31:07.149Z","avatar_url":"https://github.com/guillaume-chevalier.png","language":"Jupyter Notebook","funding_links":[],"categories":["Table of Contents","Tutorials","Jupyter Notebook","Practical Resources","教程"],"sub_categories":["Tutorials","Librairies and Implementations","Misc","微信群"],"readme":"\n# \u003ca title=\"Activity Recognition\" href=\"https://github.com/guillaume-chevalier/LSTM-Human-Activity-Recognition\" \u003e LSTMs for Human Activity Recognition\u003c/a\u003e\n\nHuman Activity Recognition (HAR) using smartphones dataset and an LSTM RNN. Classifying the type of movement amongst six categories:\n- WALKING,\n- WALKING_UPSTAIRS,\n- WALKING_DOWNSTAIRS,\n- SITTING,\n- STANDING,\n- LAYING.\n\nCompared to a classical approach, using a Recurrent Neural Networks (RNN) with Long Short-Term Memory cells (LSTMs) require no or almost no feature engineering. Data can be fed directly into the neural network who acts like a black box, modeling the problem correctly. [Other research](https://archive.ics.uci.edu/ml/machine-learning-databases/00240/UCI%20HAR%20Dataset.names) on the activity recognition dataset can use a big amount of feature engineering, which is rather a signal processing approach combined with classical data science techniques. The approach here is rather very simple in terms of how much was the data preprocessed.\n\nLet's use Google's neat Deep Learning library, TensorFlow, demonstrating the usage of an LSTM, a type of Artificial Neural Network that can process sequential data / time series.\n\n## Video dataset overview\n\nFollow this link to see a video of the 6 activities recorded in the experiment with one of the participants:\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"http://www.youtube.com/watch?feature=player_embedded\u0026v=XOEN9W05_4A\n\" target=\"_blank\"\u003e\u003cimg src=\"http://img.youtube.com/vi/XOEN9W05_4A/0.jpg\"\nalt=\"Video of the experiment\" width=\"400\" height=\"300\" border=\"10\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://youtu.be/XOEN9W05_4A\"\u003e\u003ccenter\u003e[Watch video]\u003c/center\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n## Details about the input data\n\nI will be using an LSTM on the data to learn (as a cellphone attached on the waist) to recognise the type of activity that the user is doing. The dataset's description goes like this:\n\n\u003e The sensor signals (accelerometer and gyroscope) were pre-processed by applying noise filters and then sampled in fixed-width sliding windows of 2.56 sec and 50% overlap (128 readings/window). The sensor acceleration signal, which has gravitational and body motion components, was separated using a Butterworth low-pass filter into body acceleration and gravity. The gravitational force is assumed to have only low frequency components, therefore a filter with 0.3 Hz cutoff frequency was used.\n\nThat said, I will use the almost raw data: only the gravity effect has been filtered out of the accelerometer  as a preprocessing step for another 3D feature as an input to help learning. If you'd ever want to extract the gravity by yourself, you could fork my code on using a [Butterworth Low-Pass Filter (LPF) in Python](https://github.com/guillaume-chevalier/filtering-stft-and-laplace-transform) and edit it to have the right cutoff frequency of 0.3 Hz which is a good frequency for activity recognition from body sensors.\n\n## What is an RNN?\n\nAs explained in [this article](http://karpathy.github.io/2015/05/21/rnn-effectiveness/), an RNN takes many input vectors to process them and output other vectors. It can be roughly pictured like in the image below, imagining each rectangle has a vectorial depth and other special hidden quirks in the image below. **In our case, the \"many to one\" architecture is used**: we accept time series of [feature vectors](https://www.quora.com/What-do-samples-features-time-steps-mean-in-LSTM/answer/Guillaume-Chevalier-2) (one vector per [time step](https://www.quora.com/What-do-samples-features-time-steps-mean-in-LSTM/answer/Guillaume-Chevalier-2)) to convert them to a probability vector at the output for classification. Note that a \"one to one\" architecture would be a standard feedforward neural network.\n\n\u003e [![RNN Architectures](https://raw.githubusercontent.com/Neuraxio/Machine-Learning-Figures/master/rnn-architectures.png)](https://www.dl-rnn-course.neuraxio.com/start?utm_source=github_lstm)\n\u003e [Learn more on RNNs](https://www.dl-rnn-course.neuraxio.com/start?utm_source=github_lstm)\n\n## What is an LSTM?\n\nAn LSTM is an improved RNN. It is more complex, but easier to train, avoiding what is called the vanishing gradient problem. I recommend [this course](https://www.dl-rnn-course.neuraxio.com/start?utm_source=github_lstm) for you to learn more on LSTMs.\n\n\u003e [Learn more on LSTMs](https://www.dl-rnn-course.neuraxio.com/start?utm_source=github_lstm)\n\n## Results\n\nScroll on! Nice visuals awaits.\n\n\n```python\n# All Includes\n\nimport numpy as np\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport tensorflow as tf  # Version 1.0.0 (some previous versions are used in past commits)\nfrom sklearn import metrics\n\nimport os\n```\n\n\n```python\n# Useful Constants\n\n# Those are separate normalised input features for the neural network\nINPUT_SIGNAL_TYPES = [\n    \"body_acc_x_\",\n    \"body_acc_y_\",\n    \"body_acc_z_\",\n    \"body_gyro_x_\",\n    \"body_gyro_y_\",\n    \"body_gyro_z_\",\n    \"total_acc_x_\",\n    \"total_acc_y_\",\n    \"total_acc_z_\"\n]\n\n# Output classes to learn how to classify\nLABELS = [\n    \"WALKING\",\n    \"WALKING_UPSTAIRS\",\n    \"WALKING_DOWNSTAIRS\",\n    \"SITTING\",\n    \"STANDING\",\n    \"LAYING\"\n]\n\n```\n\n## Let's start by downloading the data:\n\n\n```python\n# Note: Linux bash commands start with a \"!\" inside those \"ipython notebook\" cells\n\nDATA_PATH = \"data/\"\n\n!pwd \u0026\u0026 ls\nos.chdir(DATA_PATH)\n!pwd \u0026\u0026 ls\n\n!python download_dataset.py\n\n!pwd \u0026\u0026 ls\nos.chdir(\"..\")\n!pwd \u0026\u0026 ls\n\nDATASET_PATH = DATA_PATH + \"UCI HAR Dataset/\"\nprint(\"\\n\" + \"Dataset is now located at: \" + DATASET_PATH)\n\n```\n\n    /home/ubuntu/pynb/LSTM-Human-Activity-Recognition\n    data\t LSTM_files  LSTM_OLD.ipynb  README.md\n    LICENSE  LSTM.ipynb  lstm.py\t     screenlog.0\n    /home/ubuntu/pynb/LSTM-Human-Activity-Recognition/data\n    download_dataset.py  source.txt\n\n    Downloading...\n    --2017-05-24 01:49:53--  https://archive.ics.uci.edu/ml/machine-learning-databases/00240/UCI%20HAR%20Dataset.zip\n    Resolving archive.ics.uci.edu (archive.ics.uci.edu)... 128.195.10.249\n    Connecting to archive.ics.uci.edu (archive.ics.uci.edu)|128.195.10.249|:443... connected.\n    HTTP request sent, awaiting response... 200 OK\n    Length: 60999314 (58M) [application/zip]\n    Saving to: ‘UCI HAR Dataset.zip’\n\n    100%[======================================\u003e] 60,999,314  1.69MB/s   in 38s    \n\n    2017-05-24 01:50:31 (1.55 MB/s) - ‘UCI HAR Dataset.zip’ saved [60999314/60999314]\n\n    Downloading done.\n\n    Extracting...\n    Extracting successfully done to /home/ubuntu/pynb/LSTM-Human-Activity-Recognition/data/UCI HAR Dataset.\n    /home/ubuntu/pynb/LSTM-Human-Activity-Recognition/data\n    download_dataset.py  __MACOSX  source.txt  UCI HAR Dataset  UCI HAR Dataset.zip\n    /home/ubuntu/pynb/LSTM-Human-Activity-Recognition\n    data\t LSTM_files  LSTM_OLD.ipynb  README.md\n    LICENSE  LSTM.ipynb  lstm.py\t     screenlog.0\n\n    Dataset is now located at: data/UCI HAR Dataset/\n\n\n## Preparing dataset:\n\n\n```python\nTRAIN = \"train/\"\nTEST = \"test/\"\n\n\n# Load \"X\" (the neural network's training and testing inputs)\n\ndef load_X(X_signals_paths):\n    X_signals = []\n\n    for signal_type_path in X_signals_paths:\n        file = open(signal_type_path, 'r')\n        # Read dataset from disk, dealing with text files' syntax\n        X_signals.append(\n            [np.array(serie, dtype=np.float32) for serie in [\n                row.replace('  ', ' ').strip().split(' ') for row in file\n            ]]\n        )\n        file.close()\n\n    return np.transpose(np.array(X_signals), (1, 2, 0))\n\nX_train_signals_paths = [\n    DATASET_PATH + TRAIN + \"Inertial Signals/\" + signal + \"train.txt\" for signal in INPUT_SIGNAL_TYPES\n]\nX_test_signals_paths = [\n    DATASET_PATH + TEST + \"Inertial Signals/\" + signal + \"test.txt\" for signal in INPUT_SIGNAL_TYPES\n]\n\nX_train = load_X(X_train_signals_paths)\nX_test = load_X(X_test_signals_paths)\n\n\n# Load \"y\" (the neural network's training and testing outputs)\n\ndef load_y(y_path):\n    file = open(y_path, 'r')\n    # Read dataset from disk, dealing with text file's syntax\n    y_ = np.array(\n        [elem for elem in [\n            row.replace('  ', ' ').strip().split(' ') for row in file\n        ]],\n        dtype=np.int32\n    )\n    file.close()\n\n    # Substract 1 to each output class for friendly 0-based indexing\n    return y_ - 1\n\ny_train_path = DATASET_PATH + TRAIN + \"y_train.txt\"\ny_test_path = DATASET_PATH + TEST + \"y_test.txt\"\n\ny_train = load_y(y_train_path)\ny_test = load_y(y_test_path)\n\n```\n\n## Additionnal Parameters:\n\nHere are some core parameter definitions for the training.\n\nFor example, the whole neural network's structure could be summarised by enumerating those parameters and the fact that two LSTM are used one on top of another (stacked) output-to-input as hidden layers through time steps.\n\n\n```python\n# Input Data\n\ntraining_data_count = len(X_train)  # 7352 training series (with 50% overlap between each serie)\ntest_data_count = len(X_test)  # 2947 testing series\nn_steps = len(X_train[0])  # 128 timesteps per series\nn_input = len(X_train[0][0])  # 9 input parameters per timestep\n\n\n# LSTM Neural Network's internal structure\n\nn_hidden = 32 # Hidden layer num of features\nn_classes = 6 # Total classes (should go up, or should go down)\n\n\n# Training\n\nlearning_rate = 0.0025\nlambda_loss_amount = 0.0015\ntraining_iters = training_data_count * 300  # Loop 300 times on the dataset\nbatch_size = 1500\ndisplay_iter = 30000  # To show test set accuracy during training\n\n\n# Some debugging info\n\nprint(\"Some useful info to get an insight on dataset's shape and normalisation:\")\nprint(\"(X shape, y shape, every X's mean, every X's standard deviation)\")\nprint(X_test.shape, y_test.shape, np.mean(X_test), np.std(X_test))\nprint(\"The dataset is therefore properly normalised, as expected, but not yet one-hot encoded.\")\n\n```\n\n    Some useful info to get an insight on dataset's shape and normalisation:\n    (X shape, y shape, every X's mean, every X's standard deviation)\n    (2947, 128, 9) (2947, 1) 0.0991399 0.395671\n    The dataset is therefore properly normalised, as expected, but not yet one-hot encoded.\n\n\n## Utility functions for training:\n\n\n```python\ndef LSTM_RNN(_X, _weights, _biases):\n    # Function returns a tensorflow LSTM (RNN) artificial neural network from given parameters.\n    # Moreover, two LSTM cells are stacked which adds deepness to the neural network.\n    # Note, some code of this notebook is inspired from an slightly different\n    # RNN architecture used on another dataset, some of the credits goes to\n    # \"aymericdamien\" under the MIT license.\n\n    # (NOTE: This step could be greatly optimised by shaping the dataset once\n    # input shape: (batch_size, n_steps, n_input)\n    _X = tf.transpose(_X, [1, 0, 2])  # permute n_steps and batch_size\n    # Reshape to prepare input to hidden activation\n    _X = tf.reshape(_X, [-1, n_input])\n    # new shape: (n_steps*batch_size, n_input)\n\n    # ReLU activation, thanks to Yu Zhao for adding this improvement here:\n    _X = tf.nn.relu(tf.matmul(_X, _weights['hidden']) + _biases['hidden'])\n    # Split data because rnn cell needs a list of inputs for the RNN inner loop\n    _X = tf.split(_X, n_steps, 0)\n    # new shape: n_steps * (batch_size, n_hidden)\n\n    # Define two stacked LSTM cells (two recurrent layers deep) with tensorflow\n    lstm_cell_1 = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True)\n    lstm_cell_2 = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True)\n    lstm_cells = tf.contrib.rnn.MultiRNNCell([lstm_cell_1, lstm_cell_2], state_is_tuple=True)\n    # Get LSTM cell output\n    outputs, states = tf.contrib.rnn.static_rnn(lstm_cells, _X, dtype=tf.float32)\n\n    # Get last time step's output feature for a \"many-to-one\" style classifier,\n    # as in the image describing RNNs at the top of this page\n    lstm_last_output = outputs[-1]\n\n    # Linear activation\n    return tf.matmul(lstm_last_output, _weights['out']) + _biases['out']\n\n\ndef extract_batch_size(_train, step, batch_size):\n    # Function to fetch a \"batch_size\" amount of data from \"(X|y)_train\" data.\n\n    shape = list(_train.shape)\n    shape[0] = batch_size\n    batch_s = np.empty(shape)\n\n    for i in range(batch_size):\n        # Loop index\n        index = ((step-1)*batch_size + i) % len(_train)\n        batch_s[i] = _train[index]\n\n    return batch_s\n\n\ndef one_hot(y_, n_classes=n_classes):\n    # Function to encode neural one-hot output labels from number indexes\n    # e.g.:\n    # one_hot(y_=[[5], [0], [3]], n_classes=6):\n    #     return [[0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0]]\n\n    y_ = y_.reshape(len(y_))\n    return np.eye(n_classes)[np.array(y_, dtype=np.int32)]  # Returns FLOATS\n\n```\n\n## Let's get serious and build the neural network:\n\n\n```python\n\n# Graph input/output\nx = tf.placeholder(tf.float32, [None, n_steps, n_input])\ny = tf.placeholder(tf.float32, [None, n_classes])\n\n# Graph weights\nweights = {\n    'hidden': tf.Variable(tf.random_normal([n_input, n_hidden])), # Hidden layer weights\n    'out': tf.Variable(tf.random_normal([n_hidden, n_classes], mean=1.0))\n}\nbiases = {\n    'hidden': tf.Variable(tf.random_normal([n_hidden])),\n    'out': tf.Variable(tf.random_normal([n_classes]))\n}\n\npred = LSTM_RNN(x, weights, biases)\n\n# Loss, optimizer and evaluation\nl2 = lambda_loss_amount * sum(\n    tf.nn.l2_loss(tf_var) for tf_var in tf.trainable_variables()\n) # L2 loss prevents this overkill neural network to overfit the data\ncost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=pred)) + l2 # Softmax loss\noptimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) # Adam Optimizer\n\ncorrect_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))\naccuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))\n\n```\n\n## Hooray, now train the neural network:\n\n\n```python\n# To keep track of training's performance\ntest_losses = []\ntest_accuracies = []\ntrain_losses = []\ntrain_accuracies = []\n\n# Launch the graph\nsess = tf.InteractiveSession(config=tf.ConfigProto(log_device_placement=True))\ninit = tf.global_variables_initializer()\nsess.run(init)\n\n# Perform Training steps with \"batch_size\" amount of example data at each loop\nstep = 1\nwhile step * batch_size \u003c= training_iters:\n    batch_xs =         extract_batch_size(X_train, step, batch_size)\n    batch_ys = one_hot(extract_batch_size(y_train, step, batch_size))\n\n    # Fit training using batch data\n    _, loss, acc = sess.run(\n        [optimizer, cost, accuracy],\n        feed_dict={\n            x: batch_xs,\n            y: batch_ys\n        }\n    )\n    train_losses.append(loss)\n    train_accuracies.append(acc)\n\n    # Evaluate network only at some steps for faster training:\n    if (step*batch_size % display_iter == 0) or (step == 1) or (step * batch_size \u003e training_iters):\n\n        # To not spam console, show training accuracy/loss in this \"if\"\n        print(\"Training iter #\" + str(step*batch_size) + \\\n              \":   Batch Loss = \" + \"{:.6f}\".format(loss) + \\\n              \", Accuracy = {}\".format(acc))\n\n        # Evaluation on the test set (no learning made here - just evaluation for diagnosis)\n        loss, acc = sess.run(\n            [cost, accuracy],\n            feed_dict={\n                x: X_test,\n                y: one_hot(y_test)\n            }\n        )\n        test_losses.append(loss)\n        test_accuracies.append(acc)\n        print(\"PERFORMANCE ON TEST SET: \" + \\\n              \"Batch Loss = {}\".format(loss) + \\\n              \", Accuracy = {}\".format(acc))\n\n    step += 1\n\nprint(\"Optimization Finished!\")\n\n# Accuracy for test data\n\none_hot_predictions, accuracy, final_loss = sess.run(\n    [pred, accuracy, cost],\n    feed_dict={\n        x: X_test,\n        y: one_hot(y_test)\n    }\n)\n\ntest_losses.append(final_loss)\ntest_accuracies.append(accuracy)\n\nprint(\"FINAL RESULT: \" + \\\n      \"Batch Loss = {}\".format(final_loss) + \\\n      \", Accuracy = {}\".format(accuracy))\n\n```\n\n    WARNING:tensorflow:From \u003cipython-input-19-3339689e51f6\u003e:9: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.\n    Instructions for updating:\n    Use `tf.global_variables_initializer` instead.\n    Training iter #1500:   Batch Loss = 5.416760, Accuracy = 0.15266665816307068\n    PERFORMANCE ON TEST SET: Batch Loss = 4.880829811096191, Accuracy = 0.05632847175002098\n    Training iter #30000:   Batch Loss = 3.031930, Accuracy = 0.607333242893219\n    PERFORMANCE ON TEST SET: Batch Loss = 3.0515167713165283, Accuracy = 0.6067186594009399\n    Training iter #60000:   Batch Loss = 2.672764, Accuracy = 0.7386666536331177\n    PERFORMANCE ON TEST SET: Batch Loss = 2.780435085296631, Accuracy = 0.7027485370635986\n    Training iter #90000:   Batch Loss = 2.378301, Accuracy = 0.8366667032241821\n    PERFORMANCE ON TEST SET: Batch Loss = 2.6019773483276367, Accuracy = 0.7617915868759155\n    Training iter #120000:   Batch Loss = 2.127290, Accuracy = 0.9066667556762695\n    PERFORMANCE ON TEST SET: Batch Loss = 2.3625404834747314, Accuracy = 0.8116728663444519\n    Training iter #150000:   Batch Loss = 1.929805, Accuracy = 0.9380000233650208\n    PERFORMANCE ON TEST SET: Batch Loss = 2.306251049041748, Accuracy = 0.8276212215423584\n    Training iter #180000:   Batch Loss = 1.971904, Accuracy = 0.9153333902359009\n    PERFORMANCE ON TEST SET: Batch Loss = 2.0835530757904053, Accuracy = 0.8771631121635437\n    Training iter #210000:   Batch Loss = 1.860249, Accuracy = 0.8613333702087402\n    PERFORMANCE ON TEST SET: Batch Loss = 1.9994492530822754, Accuracy = 0.8788597583770752\n    Training iter #240000:   Batch Loss = 1.626292, Accuracy = 0.9380000233650208\n    PERFORMANCE ON TEST SET: Batch Loss = 1.879166603088379, Accuracy = 0.8944689035415649\n    Training iter #270000:   Batch Loss = 1.582758, Accuracy = 0.9386667013168335\n    PERFORMANCE ON TEST SET: Batch Loss = 2.0341007709503174, Accuracy = 0.8361043930053711\n    Training iter #300000:   Batch Loss = 1.620352, Accuracy = 0.9306666851043701\n    PERFORMANCE ON TEST SET: Batch Loss = 1.8185184001922607, Accuracy = 0.8639293313026428\n    Training iter #330000:   Batch Loss = 1.474394, Accuracy = 0.9693333506584167\n    PERFORMANCE ON TEST SET: Batch Loss = 1.7638503313064575, Accuracy = 0.8747878670692444\n    Training iter #360000:   Batch Loss = 1.406998, Accuracy = 0.9420000314712524\n    PERFORMANCE ON TEST SET: Batch Loss = 1.5946787595748901, Accuracy = 0.902273416519165\n    Training iter #390000:   Batch Loss = 1.362515, Accuracy = 0.940000057220459\n    PERFORMANCE ON TEST SET: Batch Loss = 1.5285792350769043, Accuracy = 0.9046487212181091\n    Training iter #420000:   Batch Loss = 1.252860, Accuracy = 0.9566667079925537\n    PERFORMANCE ON TEST SET: Batch Loss = 1.4635565280914307, Accuracy = 0.9107565879821777\n    Training iter #450000:   Batch Loss = 1.190078, Accuracy = 0.9553333520889282\n    ...\n    PERFORMANCE ON TEST SET: Batch Loss = 0.42567864060401917, Accuracy = 0.9324736595153809\n    Training iter #2070000:   Batch Loss = 0.342763, Accuracy = 0.9326667189598083\n    PERFORMANCE ON TEST SET: Batch Loss = 0.4292983412742615, Accuracy = 0.9273836612701416\n    Training iter #2100000:   Batch Loss = 0.259442, Accuracy = 0.9873334169387817\n    PERFORMANCE ON TEST SET: Batch Loss = 0.44131210446357727, Accuracy = 0.9273836612701416\n    Training iter #2130000:   Batch Loss = 0.284630, Accuracy = 0.9593333601951599\n    PERFORMANCE ON TEST SET: Batch Loss = 0.46982717514038086, Accuracy = 0.9093992710113525\n    Training iter #2160000:   Batch Loss = 0.299012, Accuracy = 0.9686667323112488\n    PERFORMANCE ON TEST SET: Batch Loss = 0.48389002680778503, Accuracy = 0.9138105511665344\n    Training iter #2190000:   Batch Loss = 0.287106, Accuracy = 0.9700000286102295\n    PERFORMANCE ON TEST SET: Batch Loss = 0.4670214056968689, Accuracy = 0.9216151237487793\n    Optimization Finished!\n    FINAL RESULT: Batch Loss = 0.45611169934272766, Accuracy = 0.9165252447128296\n\n\n## Training is good, but having visual insight is even better:\n\nOkay, let's plot this simply in the notebook for now.\n\n\n```python\n# (Inline plots: )\n%matplotlib inline\n\nfont = {\n    'family' : 'Bitstream Vera Sans',\n    'weight' : 'bold',\n    'size'   : 18\n}\nmatplotlib.rc('font', **font)\n\nwidth = 12\nheight = 12\nplt.figure(figsize=(width, height))\n\nindep_train_axis = np.array(range(batch_size, (len(train_losses)+1)*batch_size, batch_size))\nplt.plot(indep_train_axis, np.array(train_losses),     \"b--\", label=\"Train losses\")\nplt.plot(indep_train_axis, np.array(train_accuracies), \"g--\", label=\"Train accuracies\")\n\nindep_test_axis = np.append(\n    np.array(range(batch_size, len(test_losses)*display_iter, display_iter)[:-1]),\n    [training_iters]\n)\nplt.plot(indep_test_axis, np.array(test_losses),     \"b-\", label=\"Test losses\")\nplt.plot(indep_test_axis, np.array(test_accuracies), \"g-\", label=\"Test accuracies\")\n\nplt.title(\"Training session's progress over iterations\")\nplt.legend(loc='upper right', shadow=True)\nplt.ylabel('Training Progress (Loss or Accuracy values)')\nplt.xlabel('Training iteration')\n\nplt.show()\n```\n\n\n![LSTM Training Testing Comparison Curve](LSTM_files/LSTM_16_0.png)\n\n\n## And finally, the multi-class confusion matrix and metrics!\n\n\n```python\n# Results\n\npredictions = one_hot_predictions.argmax(1)\n\nprint(\"Testing Accuracy: {}%\".format(100*accuracy))\n\nprint(\"\")\nprint(\"Precision: {}%\".format(100*metrics.precision_score(y_test, predictions, average=\"weighted\")))\nprint(\"Recall: {}%\".format(100*metrics.recall_score(y_test, predictions, average=\"weighted\")))\nprint(\"f1_score: {}%\".format(100*metrics.f1_score(y_test, predictions, average=\"weighted\")))\n\nprint(\"\")\nprint(\"Confusion Matrix:\")\nconfusion_matrix = metrics.confusion_matrix(y_test, predictions)\nprint(confusion_matrix)\nnormalised_confusion_matrix = np.array(confusion_matrix, dtype=np.float32)/np.sum(confusion_matrix)*100\n\nprint(\"\")\nprint(\"Confusion matrix (normalised to % of total test data):\")\nprint(normalised_confusion_matrix)\nprint(\"Note: training and testing data is not equally distributed amongst classes, \")\nprint(\"so it is normal that more than a 6th of the data is correctly classifier in the last category.\")\n\n# Plot Results:\nwidth = 12\nheight = 12\nplt.figure(figsize=(width, height))\nplt.imshow(\n    normalised_confusion_matrix,\n    interpolation='nearest',\n    cmap=plt.cm.rainbow\n)\nplt.title(\"Confusion matrix \\n(normalised to % of total test data)\")\nplt.colorbar()\ntick_marks = np.arange(n_classes)\nplt.xticks(tick_marks, LABELS, rotation=90)\nplt.yticks(tick_marks, LABELS)\nplt.tight_layout()\nplt.ylabel('True label')\nplt.xlabel('Predicted label')\nplt.show()\n```\n\n    Testing Accuracy: 91.65252447128296%\n\n    Precision: 91.76286479743305%\n    Recall: 91.65252799457076%\n    f1_score: 91.6437546304815%\n\n    Confusion Matrix:\n    [[466   2  26   0   2   0]\n     [  5 441  25   0   0   0]\n     [  1   0 419   0   0   0]\n     [  1   1   0 396  87   6]\n     [  2   1   0  87 442   0]\n     [  0   0   0   0   0 537]]\n\n    Confusion matrix (normalised to % of total test data):\n    [[ 15.81269073   0.06786563   0.88225317   0.           0.06786563   0.        ]\n     [  0.16966406  14.96437073   0.84832031   0.           0.           0.        ]\n     [  0.03393281   0.          14.21784878   0.           0.           0.        ]\n     [  0.03393281   0.03393281   0.          13.43739319   2.95215464\n        0.20359688]\n     [  0.06786563   0.03393281   0.           2.95215464  14.99830341   0.        ]\n     [  0.           0.           0.           0.           0.          18.22192001]]\n    Note: training and testing data is not equally distributed amongst classes,\n    so it is normal that more than a 6th of the data is correctly classifier in the last category.\n\n\n\n![Confusion Matrix](LSTM_files/LSTM_18_1.png)\n\n\n\n```python\nsess.close()\n```\n\n## Conclusion\n\nOutstandingly, **the final accuracy is of 91%**! And it can peak to values such as 93.25%, at some moments of luck during the training, depending on how the neural network's weights got initialized at the start of the training, randomly.\n\nThis means that the neural networks is almost always able to correctly identify the movement type! Remember, the phone is attached on the waist and each series to classify has just a 128 sample window of two internal sensors (a.k.a. 2.56 seconds at 50 FPS), so it amazes me how those predictions are extremely accurate given this small window of context and raw data. I've validated and re-validated that there is no important bug, and the community used and tried this code a lot. (Note: be sure to report something in the issue tab if you find bugs, otherwise [Quora](https://www.quora.com/), [StackOverflow](https://stackoverflow.com/questions/tagged/tensorflow?sort=votes\u0026pageSize=50), and other [StackExchange](https://stackexchange.com/sites#science) sites are the places for asking questions.)\n\nI specially did not expect such good results for guessing between the labels \"SITTING\" and \"STANDING\". Those are seemingly almost the same thing from the point of view of a device placed at waist level according to how the dataset was originally gathered. Thought, it is still possible to see a little cluster on the matrix between those classes, which drifts away just a bit from the identity. This is great.\n\nIt is also possible to see that there was a slight difficulty in doing the difference between \"WALKING\", \"WALKING_UPSTAIRS\" and \"WALKING_DOWNSTAIRS\". Obviously, those activities are quite similar in terms of movements.\n\nI also tried my code without the gyroscope, using only the 3D accelerometer's 6 features (and not changing the training hyperparameters), and got an accuracy of 87%. In general, gyroscopes consumes more power than accelerometers, so it is preferable to turn them off.\n\n\n## Improvements\n\nIn [another open-source repository of mine](https://github.com/guillaume-chevalier/HAR-stacked-residual-bidir-LSTMs), the accuracy is pushed up to nearly 94% using a special deep LSTM architecture which combines the concepts of bidirectional RNNs, residual connections, and stacked cells. This architecture is also tested on another similar activity dataset. It resembles the nice architecture used in \"[Google’s Neural Machine Translation System: Bridging the Gap between Human and Machine Translation](https://arxiv.org/pdf/1609.08144.pdf)\", without an attention mechanism, and with just the encoder part - as a \"many to one\" architecture instead of a \"many to many\" to be adapted to the Human Activity Recognition (HAR) problem. I also worked more on the problem and came up with the [LARNN](https://github.com/guillaume-chevalier/Linear-Attention-Recurrent-Neural-Network), however it's complicated for just a little gain. Thus the current, original activity recognition project is simply better to use for its simplicity. We've also coded a [non-deep learning machine learning pipeline](https://github.com/Neuraxio/Kata-Clean-Machine-Learning-From-Dirty-Code) on the same datasets using classical featurization techniques and older machine learning algorithms.\n\nIf you want to learn more about deep learning, I have also built a list of the learning ressources for deep learning which have revealed to be the most useful to me [here](https://github.com/guillaume-chevalier/Awesome-Deep-Learning-Resources). \n\n\n## References\n\nThe [dataset](https://archive.ics.uci.edu/ml/datasets/Human+Activity+Recognition+Using+Smartphones) can be found on the UCI Machine Learning Repository:\n\n\u003e Davide Anguita, Alessandro Ghio, Luca Oneto, Xavier Parra and Jorge L. Reyes-Ortiz. A Public Domain Dataset for Human Activity Recognition Using Smartphones. 21th European Symposium on Artificial Neural Networks, Computational Intelligence and Machine Learning, ESANN 2013. Bruges, Belgium 24-26 April 2013.\n\n\n## Citation\n\nCopyright (c) 2016 Guillaume Chevalier. To cite my code, you can point to the URL of the GitHub repository, for example:\n\n\u003e Guillaume Chevalier, LSTMs for Human Activity Recognition, 2016,\n\u003e https://github.com/guillaume-chevalier/LSTM-Human-Activity-Recognition\n\nMy code is available for free and even for private usage for anyone under the [MIT License](https://github.com/guillaume-chevalier/LSTM-Human-Activity-Recognition/blob/master/LICENSE), however I ask to cite for using the code.\n\nHere is the BibTeX citation code: \n```\n@misc{chevalier2016lstms,\n  title={LSTMs for human activity recognition},\n  author={Chevalier, Guillaume},\n  year={2016}\n}\n```\n\nI've also published a second paper, with contributors, regarding a [second iteration as an improvement of this work](https://github.com/guillaume-chevalier/HAR-stacked-residual-bidir-LSTMs), with deeper neural networks. The paper is available on [arXiv](https://arxiv.org/abs/1708.08989). Here is the BibTeX citation code for this newer piece of work based on this project: \n```\n@article{DBLP:journals/corr/abs-1708-08989,\n  author    = {Yu Zhao and\n               Rennong Yang and\n               Guillaume Chevalier and\n               Maoguo Gong},\n  title     = {Deep Residual Bidir-LSTM for Human Activity Recognition Using Wearable\n               Sensors},\n  journal   = {CoRR},\n  volume    = {abs/1708.08989},\n  year      = {2017},\n  url       = {http://arxiv.org/abs/1708.08989},\n  archivePrefix = {arXiv},\n  eprint    = {1708.08989},\n  timestamp = {Mon, 13 Aug 2018 16:46:48 +0200},\n  biburl    = {https://dblp.org/rec/bib/journals/corr/abs-1708-08989},\n  bibsource = {dblp computer science bibliography, https://dblp.org}\n}\n```\n\n## Extra links\n\n### Connect with me\n\n- [GitHub](https://github.com/guillaume-chevalier/)\n- [LinkedIn](https://ca.linkedin.com/in/chevalierg)\n- [YouTube](https://www.youtube.com/c/GuillaumeChevalier)\n\n### Liked this project? Did it help you? Leave a [star](https://github.com/guillaume-chevalier/LSTM-Human-Activity-Recognition/stargazers), [fork](https://github.com/guillaume-chevalier/LSTM-Human-Activity-Recognition/network/members) and share the love!\n\nThis activity recognition project has been seen in:\n\n- [Hacker News 1st page](https://news.ycombinator.com/item?id=13049143)\n- [Awesome TensorFlow](https://github.com/jtoy/awesome-tensorflow#tutorials)\n- [TensorFlow World](https://github.com/astorfi/TensorFlow-World#some-useful-tutorials)\n- And more.\n\n---\n\n\n\n```python\n# Let's convert this notebook to a README automatically for the GitHub project's title page:\n!jupyter nbconvert --to markdown LSTM.ipynb\n!mv LSTM.md README.md\n```\n\n    [NbConvertApp] Converting notebook LSTM.ipynb to markdown\n    [NbConvertApp] Support files will be in LSTM_files/\n    [NbConvertApp] Making directory LSTM_files\n    [NbConvertApp] Making directory LSTM_files\n    [NbConvertApp] Writing 38654 bytes to LSTM.md\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguillaume-chevalier%2FLSTM-Human-Activity-Recognition","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fguillaume-chevalier%2FLSTM-Human-Activity-Recognition","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguillaume-chevalier%2FLSTM-Human-Activity-Recognition/lists"}