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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"nbformat": 4,
|
3 |
+
"nbformat_minor": 0,
|
4 |
+
"metadata": {
|
5 |
+
"colab": {
|
6 |
+
"provenance": []
|
7 |
+
},
|
8 |
+
"kernelspec": {
|
9 |
+
"name": "python3",
|
10 |
+
"display_name": "Python 3"
|
11 |
+
},
|
12 |
+
"language_info": {
|
13 |
+
"name": "python"
|
14 |
+
},
|
15 |
+
"accelerator": "GPU",
|
16 |
+
"gpuClass": "standard"
|
17 |
+
},
|
18 |
+
"cells": [
|
19 |
+
{
|
20 |
+
"cell_type": "markdown",
|
21 |
+
"source": [
|
22 |
+
"# Applying LSTM Models on Raw Data"
|
23 |
+
],
|
24 |
+
"metadata": {
|
25 |
+
"id": "jUkGXVGfU1xN"
|
26 |
+
}
|
27 |
+
},
|
28 |
+
{
|
29 |
+
"cell_type": "code",
|
30 |
+
"execution_count": null,
|
31 |
+
"metadata": {
|
32 |
+
"id": "7USnX2QTSuKt"
|
33 |
+
},
|
34 |
+
"outputs": [],
|
35 |
+
"source": [
|
36 |
+
"# Importing Libraries\n",
|
37 |
+
"\n",
|
38 |
+
"import pandas as pd\n",
|
39 |
+
"import numpy as np\n",
|
40 |
+
"\n",
|
41 |
+
"# Import Keras\n",
|
42 |
+
"from keras import backend as K\n",
|
43 |
+
"from keras.models import Sequential\n",
|
44 |
+
"from keras.layers import LSTM\n",
|
45 |
+
"from keras.layers.core import Dense, Dropout\n",
|
46 |
+
"from keras.layers import BatchNormalization\n",
|
47 |
+
"from keras.regularizers import L1L2"
|
48 |
+
]
|
49 |
+
},
|
50 |
+
{
|
51 |
+
"cell_type": "code",
|
52 |
+
"source": [
|
53 |
+
"# Activities are the class labels\n",
|
54 |
+
"# It is a 6 class classification\n",
|
55 |
+
"ACTIVITIES = {\n",
|
56 |
+
" 0: 'WALKING',\n",
|
57 |
+
" 1: 'WALKING_UPSTAIRS',\n",
|
58 |
+
" 2: 'WALKING_DOWNSTAIRS',\n",
|
59 |
+
" 3: 'SITTING',\n",
|
60 |
+
" 4: 'STANDING',\n",
|
61 |
+
" 5: 'LAYING',\n",
|
62 |
+
"}"
|
63 |
+
],
|
64 |
+
"metadata": {
|
65 |
+
"id": "UklmP7-eU9Wm"
|
66 |
+
},
|
67 |
+
"execution_count": null,
|
68 |
+
"outputs": []
|
69 |
+
},
|
70 |
+
{
|
71 |
+
"cell_type": "code",
|
72 |
+
"source": [
|
73 |
+
"import matplotlib.pyplot as plt\n",
|
74 |
+
"import seaborn as sns\n",
|
75 |
+
"\n",
|
76 |
+
"# function to print the confusion matrix\n",
|
77 |
+
"\n",
|
78 |
+
"def confusion_matrix(Y_true, Y_pred):\n",
|
79 |
+
" \n",
|
80 |
+
" Y_true = pd.Series([ACTIVITIES[y] for y in np.argmax(Y_true, axis=1)])\n",
|
81 |
+
" Y_pred = pd.Series([ACTIVITIES[y] for y in np.argmax(Y_pred, axis=1)])\n",
|
82 |
+
"\n",
|
83 |
+
" return pd.crosstab(Y_true, Y_pred, rownames=['True'], colnames=['Pred'])\n",
|
84 |
+
"\n",
|
85 |
+
" \n",
|
86 |
+
" result = confusion_matrix(Y_true, Y_pred)\n",
|
87 |
+
"\n",
|
88 |
+
" plt.figure(figsize=(10, 8))\n",
|
89 |
+
" sns.heatmap(result, \n",
|
90 |
+
" xticklabels= list(ACTIVITIES.values()), \n",
|
91 |
+
" yticklabels=list(ACTIVITIES.values()), \n",
|
92 |
+
" annot=True, fmt=\"d\");\n",
|
93 |
+
" plt.title(\"Confusion matrix\")\n",
|
94 |
+
" plt.ylabel('True label')\n",
|
95 |
+
" plt.xlabel('Predicted label')\n",
|
96 |
+
" plt.show() "
|
97 |
+
],
|
98 |
+
"metadata": {
|
99 |
+
"id": "cG79tQGXVASE"
|
100 |
+
},
|
101 |
+
"execution_count": null,
|
102 |
+
"outputs": []
|
103 |
+
},
|
104 |
+
{
|
105 |
+
"cell_type": "markdown",
|
106 |
+
"source": [
|
107 |
+
"### Loading Data"
|
108 |
+
],
|
109 |
+
"metadata": {
|
110 |
+
"id": "dfbEhMvGVG4K"
|
111 |
+
}
|
112 |
+
},
|
113 |
+
{
|
114 |
+
"cell_type": "code",
|
115 |
+
"source": [
|
116 |
+
"# Data directory\n",
|
117 |
+
"DATADIR = 'UCI_HAR_Dataset'\n",
|
118 |
+
"\n",
|
119 |
+
"# Raw data signals\n",
|
120 |
+
"# Signals are from Accelerometer and Gyroscope\n",
|
121 |
+
"# The signals are in x,y,z directions\n",
|
122 |
+
"# Sensor signals are filtered to have only body acceleration\n",
|
123 |
+
"# excluding the acceleration due to gravity\n",
|
124 |
+
"# Triaxial acceleration from the accelerometer is total acceleration\n",
|
125 |
+
"SIGNALS = [\n",
|
126 |
+
" \"body_acc_x\",\n",
|
127 |
+
" \"body_acc_y\",\n",
|
128 |
+
" \"body_acc_z\",\n",
|
129 |
+
" \"body_gyro_x\",\n",
|
130 |
+
" \"body_gyro_y\",\n",
|
131 |
+
" \"body_gyro_z\",\n",
|
132 |
+
" \"total_acc_x\",\n",
|
133 |
+
" \"total_acc_y\",\n",
|
134 |
+
" \"total_acc_z\"\n",
|
135 |
+
" ]"
|
136 |
+
],
|
137 |
+
"metadata": {
|
138 |
+
"id": "W6E6gq2tVJjl"
|
139 |
+
},
|
140 |
+
"execution_count": null,
|
141 |
+
"outputs": []
|
142 |
+
},
|
143 |
+
{
|
144 |
+
"cell_type": "code",
|
145 |
+
"source": [
|
146 |
+
"# define a function to read the data from csv file\n",
|
147 |
+
"def _read_csv(filename):\n",
|
148 |
+
" return pd.read_csv(filename, delim_whitespace=True, header=None)\n",
|
149 |
+
"\n",
|
150 |
+
"# function to load the load\n",
|
151 |
+
"def load_signals(subset):\n",
|
152 |
+
" signals_data = []\n",
|
153 |
+
"\n",
|
154 |
+
" for signal in SIGNALS:\n",
|
155 |
+
" filename = f'/content/drive/MyDrive/UCI_HAR_Dataset/{subset}/Inertial Signals/{signal}_{subset}.txt'\n",
|
156 |
+
" signals_data.append(\n",
|
157 |
+
" _read_csv(filename).to_numpy()\n",
|
158 |
+
" ) \n",
|
159 |
+
"\n",
|
160 |
+
" # Transpose is used to change the dimensionality of the output,\n",
|
161 |
+
" # aggregating the signals by combination of sample/timestep.\n",
|
162 |
+
" # Resultant shape is (7352 train/2947 test samples, 128 timesteps, 9 signals)\n",
|
163 |
+
" return np.transpose(signals_data, (1, 2, 0))"
|
164 |
+
],
|
165 |
+
"metadata": {
|
166 |
+
"id": "Gbp0kyOLVO13"
|
167 |
+
},
|
168 |
+
"execution_count": null,
|
169 |
+
"outputs": []
|
170 |
+
},
|
171 |
+
{
|
172 |
+
"cell_type": "code",
|
173 |
+
"source": [
|
174 |
+
"def load_y(subset):\n",
|
175 |
+
" \"\"\"\n",
|
176 |
+
" The objective that we are trying to predict is a integer, from 1 to 6,\n",
|
177 |
+
" that represents a human activity. We return a binary representation of \n",
|
178 |
+
" every sample objective as a 6 bits vector using One Hot Encoding\n",
|
179 |
+
" (https://pandas.pydata.org/pandas-docs/stable/generated/pandas.get_dummies.html)\n",
|
180 |
+
" \"\"\"\n",
|
181 |
+
" filename = f'/content/drive/MyDrive/UCI_HAR_Dataset/{subset}/y_{subset}.txt'\n",
|
182 |
+
" y = _read_csv(filename)[0]\n",
|
183 |
+
"\n",
|
184 |
+
" return pd.get_dummies(y).to_numpy()"
|
185 |
+
],
|
186 |
+
"metadata": {
|
187 |
+
"id": "MOXrPORRVRcJ"
|
188 |
+
},
|
189 |
+
"execution_count": null,
|
190 |
+
"outputs": []
|
191 |
+
},
|
192 |
+
{
|
193 |
+
"cell_type": "code",
|
194 |
+
"source": [
|
195 |
+
"def load_data():\n",
|
196 |
+
" \"\"\"\n",
|
197 |
+
" Obtain the dataset from multiple files.\n",
|
198 |
+
" Returns: X_train, X_test, y_train, y_test\n",
|
199 |
+
" \"\"\"\n",
|
200 |
+
" X_train, X_test = load_signals('train'), load_signals('test')\n",
|
201 |
+
" y_train, y_test = load_y('train'), load_y('test')\n",
|
202 |
+
"\n",
|
203 |
+
" return X_train, X_test, y_train, y_test"
|
204 |
+
],
|
205 |
+
"metadata": {
|
206 |
+
"id": "MEf5hg9lVTun"
|
207 |
+
},
|
208 |
+
"execution_count": null,
|
209 |
+
"outputs": []
|
210 |
+
},
|
211 |
+
{
|
212 |
+
"cell_type": "code",
|
213 |
+
"source": [
|
214 |
+
"# Importing tensorflow\n",
|
215 |
+
"np.random.seed(42)\n",
|
216 |
+
"import tensorflow as tf\n",
|
217 |
+
"tf.random.set_seed(42)"
|
218 |
+
],
|
219 |
+
"metadata": {
|
220 |
+
"id": "Edexn_grVWug"
|
221 |
+
},
|
222 |
+
"execution_count": null,
|
223 |
+
"outputs": []
|
224 |
+
},
|
225 |
+
{
|
226 |
+
"cell_type": "code",
|
227 |
+
"source": [
|
228 |
+
"# Initializing parameters\n",
|
229 |
+
"epochs = 30\n",
|
230 |
+
"batch_size = 16\n",
|
231 |
+
"n_hidden = 32"
|
232 |
+
],
|
233 |
+
"metadata": {
|
234 |
+
"id": "B4dT-A4bVapl"
|
235 |
+
},
|
236 |
+
"execution_count": null,
|
237 |
+
"outputs": []
|
238 |
+
},
|
239 |
+
{
|
240 |
+
"cell_type": "code",
|
241 |
+
"source": [
|
242 |
+
"#function to count the number of classes\n",
|
243 |
+
"def _count_classes(y):\n",
|
244 |
+
" return len(set([tuple(category) for category in y]))"
|
245 |
+
],
|
246 |
+
"metadata": {
|
247 |
+
"id": "5D6pcPuVVbyl"
|
248 |
+
},
|
249 |
+
"execution_count": null,
|
250 |
+
"outputs": []
|
251 |
+
},
|
252 |
+
{
|
253 |
+
"cell_type": "code",
|
254 |
+
"source": [
|
255 |
+
"# Loading the train and test data\n",
|
256 |
+
"X_train, X_test, y_train, y_test = load_data()"
|
257 |
+
],
|
258 |
+
"metadata": {
|
259 |
+
"id": "VUoSMvSfVga3"
|
260 |
+
},
|
261 |
+
"execution_count": null,
|
262 |
+
"outputs": []
|
263 |
+
},
|
264 |
+
{
|
265 |
+
"cell_type": "code",
|
266 |
+
"source": [
|
267 |
+
"timesteps = len(X_train[0])\n",
|
268 |
+
"input_dim = len(X_train[0][0])\n",
|
269 |
+
"n_classes = _count_classes(y_train)\n",
|
270 |
+
"\n",
|
271 |
+
"print(timesteps)\n",
|
272 |
+
"print(input_dim)\n",
|
273 |
+
"print(len(X_train))"
|
274 |
+
],
|
275 |
+
"metadata": {
|
276 |
+
"colab": {
|
277 |
+
"base_uri": "https://localhost:8080/"
|
278 |
+
},
|
279 |
+
"id": "MXby1ubyVjBV",
|
280 |
+
"outputId": "8f44a692-57c1-4df3-b11f-251c4979e19e"
|
281 |
+
},
|
282 |
+
"execution_count": null,
|
283 |
+
"outputs": [
|
284 |
+
{
|
285 |
+
"output_type": "stream",
|
286 |
+
"name": "stdout",
|
287 |
+
"text": [
|
288 |
+
"128\n",
|
289 |
+
"9\n",
|
290 |
+
"7352\n"
|
291 |
+
]
|
292 |
+
}
|
293 |
+
]
|
294 |
+
},
|
295 |
+
{
|
296 |
+
"cell_type": "markdown",
|
297 |
+
"source": [
|
298 |
+
" 1. Defining the Architecture of 1-Layer of LSTM"
|
299 |
+
],
|
300 |
+
"metadata": {
|
301 |
+
"id": "m182sLnyVl0K"
|
302 |
+
}
|
303 |
+
},
|
304 |
+
{
|
305 |
+
"cell_type": "code",
|
306 |
+
"source": [
|
307 |
+
"# Initiliazing the sequential model\n",
|
308 |
+
"model = Sequential()\n",
|
309 |
+
"# Configuring the parameters\n",
|
310 |
+
"model.add(LSTM(n_hidden, input_shape=(timesteps, input_dim)))\n",
|
311 |
+
"# Adding a dropout layer\n",
|
312 |
+
"model.add(Dropout(0.5))\n",
|
313 |
+
"# Adding a dense output layer with sigmoid activation\n",
|
314 |
+
"model.add(Dense(n_classes, activation='sigmoid'))\n",
|
315 |
+
"model.summary()"
|
316 |
+
],
|
317 |
+
"metadata": {
|
318 |
+
"colab": {
|
319 |
+
"base_uri": "https://localhost:8080/"
|
320 |
+
},
|
321 |
+
"id": "BGD2Lt3MVnK5",
|
322 |
+
"outputId": "f7ece6dd-85c8-4d41-a713-540bea33ddf0"
|
323 |
+
},
|
324 |
+
"execution_count": null,
|
325 |
+
"outputs": [
|
326 |
+
{
|
327 |
+
"output_type": "stream",
|
328 |
+
"name": "stdout",
|
329 |
+
"text": [
|
330 |
+
"Model: \"sequential_3\"\n",
|
331 |
+
"_________________________________________________________________\n",
|
332 |
+
" Layer (type) Output Shape Param # \n",
|
333 |
+
"=================================================================\n",
|
334 |
+
" lstm_5 (LSTM) (None, 32) 5376 \n",
|
335 |
+
" \n",
|
336 |
+
" dropout_5 (Dropout) (None, 32) 0 \n",
|
337 |
+
" \n",
|
338 |
+
" dense_3 (Dense) (None, 6) 198 \n",
|
339 |
+
" \n",
|
340 |
+
"=================================================================\n",
|
341 |
+
"Total params: 5,574\n",
|
342 |
+
"Trainable params: 5,574\n",
|
343 |
+
"Non-trainable params: 0\n",
|
344 |
+
"_________________________________________________________________\n"
|
345 |
+
]
|
346 |
+
}
|
347 |
+
]
|
348 |
+
},
|
349 |
+
{
|
350 |
+
"cell_type": "code",
|
351 |
+
"source": [
|
352 |
+
"# Compiling the model\n",
|
353 |
+
"model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])"
|
354 |
+
],
|
355 |
+
"metadata": {
|
356 |
+
"id": "MIX8hyRoVrSs"
|
357 |
+
},
|
358 |
+
"execution_count": null,
|
359 |
+
"outputs": []
|
360 |
+
},
|
361 |
+
{
|
362 |
+
"cell_type": "code",
|
363 |
+
"source": [
|
364 |
+
"# Training the model\n",
|
365 |
+
"model.fit(X_train, y_train, batch_size=batch_size, validation_data=(X_test, y_test),epochs=epochs)"
|
366 |
+
],
|
367 |
+
"metadata": {
|
368 |
+
"colab": {
|
369 |
+
"base_uri": "https://localhost:8080/"
|
370 |
+
},
|
371 |
+
"id": "RxoV_8fdVt7u",
|
372 |
+
"outputId": "fc88555b-3bcc-47b6-f10e-1aa6d752887e"
|
373 |
+
},
|
374 |
+
"execution_count": null,
|
375 |
+
"outputs": [
|
376 |
+
{
|
377 |
+
"output_type": "stream",
|
378 |
+
"name": "stdout",
|
379 |
+
"text": [
|
380 |
+
"Epoch 1/30\n",
|
381 |
+
"460/460 [==============================] - 8s 11ms/step - loss: 1.0781 - accuracy: 0.5423 - val_loss: 0.9016 - val_accuracy: 0.6213\n",
|
382 |
+
"Epoch 2/30\n",
|
383 |
+
"460/460 [==============================] - 4s 10ms/step - loss: 0.7431 - accuracy: 0.6632 - val_loss: 0.6547 - val_accuracy: 0.7255\n",
|
384 |
+
"Epoch 3/30\n",
|
385 |
+
"460/460 [==============================] - 5s 12ms/step - loss: 0.5874 - accuracy: 0.7629 - val_loss: 0.5316 - val_accuracy: 0.7906\n",
|
386 |
+
"Epoch 4/30\n",
|
387 |
+
"460/460 [==============================] - 4s 8ms/step - loss: 0.4666 - accuracy: 0.8320 - val_loss: 1.4815 - val_accuracy: 0.6155\n",
|
388 |
+
"Epoch 5/30\n",
|
389 |
+
"460/460 [==============================] - 4s 8ms/step - loss: 0.3551 - accuracy: 0.8863 - val_loss: 0.4364 - val_accuracy: 0.8483\n",
|
390 |
+
"Epoch 6/30\n",
|
391 |
+
"460/460 [==============================] - 6s 12ms/step - loss: 0.2936 - accuracy: 0.9067 - val_loss: 0.3727 - val_accuracy: 0.8765\n",
|
392 |
+
"Epoch 7/30\n",
|
393 |
+
"460/460 [==============================] - 4s 8ms/step - loss: 0.2671 - accuracy: 0.9174 - val_loss: 0.4289 - val_accuracy: 0.8639\n",
|
394 |
+
"Epoch 8/30\n",
|
395 |
+
"460/460 [==============================] - 4s 8ms/step - loss: 0.2423 - accuracy: 0.9266 - val_loss: 0.3437 - val_accuracy: 0.8711\n",
|
396 |
+
"Epoch 9/30\n",
|
397 |
+
"460/460 [==============================] - 5s 11ms/step - loss: 0.2158 - accuracy: 0.9293 - val_loss: 0.3924 - val_accuracy: 0.8826\n",
|
398 |
+
"Epoch 10/30\n",
|
399 |
+
"460/460 [==============================] - 4s 10ms/step - loss: 0.2087 - accuracy: 0.9343 - val_loss: 0.3207 - val_accuracy: 0.8856\n",
|
400 |
+
"Epoch 11/30\n",
|
401 |
+
"460/460 [==============================] - 4s 10ms/step - loss: 0.1990 - accuracy: 0.9344 - val_loss: 0.3968 - val_accuracy: 0.8704\n",
|
402 |
+
"Epoch 12/30\n",
|
403 |
+
"460/460 [==============================] - 5s 11ms/step - loss: 0.1726 - accuracy: 0.9376 - val_loss: 0.2942 - val_accuracy: 0.8931\n",
|
404 |
+
"Epoch 13/30\n",
|
405 |
+
"460/460 [==============================] - 4s 9ms/step - loss: 0.1755 - accuracy: 0.9414 - val_loss: 0.2492 - val_accuracy: 0.9002\n",
|
406 |
+
"Epoch 14/30\n",
|
407 |
+
"460/460 [==============================] - 4s 10ms/step - loss: 0.1585 - accuracy: 0.9465 - val_loss: 0.3617 - val_accuracy: 0.8904\n",
|
408 |
+
"Epoch 15/30\n",
|
409 |
+
"460/460 [==============================] - 5s 11ms/step - loss: 0.1629 - accuracy: 0.9468 - val_loss: 0.5414 - val_accuracy: 0.8724\n",
|
410 |
+
"Epoch 16/30\n",
|
411 |
+
"460/460 [==============================] - 4s 8ms/step - loss: 0.1647 - accuracy: 0.9479 - val_loss: 0.3329 - val_accuracy: 0.8979\n",
|
412 |
+
"Epoch 17/30\n",
|
413 |
+
"460/460 [==============================] - 4s 8ms/step - loss: 0.1531 - accuracy: 0.9486 - val_loss: 0.3276 - val_accuracy: 0.9043\n",
|
414 |
+
"Epoch 18/30\n",
|
415 |
+
"460/460 [==============================] - 4s 10ms/step - loss: 0.1623 - accuracy: 0.9459 - val_loss: 0.2336 - val_accuracy: 0.9125\n",
|
416 |
+
"Epoch 19/30\n",
|
417 |
+
"460/460 [==============================] - 4s 9ms/step - loss: 0.1494 - accuracy: 0.9489 - val_loss: 0.3656 - val_accuracy: 0.9013\n",
|
418 |
+
"Epoch 20/30\n",
|
419 |
+
"460/460 [==============================] - 4s 8ms/step - loss: 0.1543 - accuracy: 0.9480 - val_loss: 0.3468 - val_accuracy: 0.9016\n",
|
420 |
+
"Epoch 21/30\n",
|
421 |
+
"460/460 [==============================] - 4s 9ms/step - loss: 0.1398 - accuracy: 0.9490 - val_loss: 0.3211 - val_accuracy: 0.9057\n",
|
422 |
+
"Epoch 22/30\n",
|
423 |
+
"460/460 [==============================] - 5s 10ms/step - loss: 0.1520 - accuracy: 0.9479 - val_loss: 0.2789 - val_accuracy: 0.9067\n",
|
424 |
+
"Epoch 23/30\n",
|
425 |
+
"460/460 [==============================] - 4s 8ms/step - loss: 0.1479 - accuracy: 0.9475 - val_loss: 0.2733 - val_accuracy: 0.9074\n",
|
426 |
+
"Epoch 24/30\n",
|
427 |
+
"460/460 [==============================] - 4s 10ms/step - loss: 0.1425 - accuracy: 0.9493 - val_loss: 0.3381 - val_accuracy: 0.9036\n",
|
428 |
+
"Epoch 25/30\n",
|
429 |
+
"460/460 [==============================] - 5s 12ms/step - loss: 0.1437 - accuracy: 0.9504 - val_loss: 0.3459 - val_accuracy: 0.8921\n",
|
430 |
+
"Epoch 26/30\n",
|
431 |
+
"460/460 [==============================] - 4s 8ms/step - loss: 0.1390 - accuracy: 0.9497 - val_loss: 0.3192 - val_accuracy: 0.9074\n",
|
432 |
+
"Epoch 27/30\n",
|
433 |
+
"460/460 [==============================] - 4s 10ms/step - loss: 0.1361 - accuracy: 0.9520 - val_loss: 0.3324 - val_accuracy: 0.9189\n",
|
434 |
+
"Epoch 28/30\n",
|
435 |
+
"460/460 [==============================] - 6s 12ms/step - loss: 0.1469 - accuracy: 0.9509 - val_loss: 0.3501 - val_accuracy: 0.9097\n",
|
436 |
+
"Epoch 29/30\n",
|
437 |
+
"460/460 [==============================] - 5s 10ms/step - loss: 0.1439 - accuracy: 0.9438 - val_loss: 0.2556 - val_accuracy: 0.9308\n",
|
438 |
+
"Epoch 30/30\n",
|
439 |
+
"460/460 [==============================] - 4s 10ms/step - loss: 0.1434 - accuracy: 0.9486 - val_loss: 0.3141 - val_accuracy: 0.9165\n"
|
440 |
+
]
|
441 |
+
},
|
442 |
+
{
|
443 |
+
"output_type": "execute_result",
|
444 |
+
"data": {
|
445 |
+
"text/plain": [
|
446 |
+
"<keras.callbacks.History at 0x7f78fa564e80>"
|
447 |
+
]
|
448 |
+
},
|
449 |
+
"metadata": {},
|
450 |
+
"execution_count": 52
|
451 |
+
}
|
452 |
+
]
|
453 |
+
},
|
454 |
+
{
|
455 |
+
"cell_type": "code",
|
456 |
+
"source": [
|
457 |
+
"# Confusion Matrix\n",
|
458 |
+
"confusion_matrix(y_test, model.predict(X_test))"
|
459 |
+
],
|
460 |
+
"metadata": {
|
461 |
+
"colab": {
|
462 |
+
"base_uri": "https://localhost:8080/",
|
463 |
+
"height": 286
|
464 |
+
},
|
465 |
+
"id": "Pl6Y6R3zVwzE",
|
466 |
+
"outputId": "d50b38f5-1af7-4cd8-c5f5-771190191675"
|
467 |
+
},
|
468 |
+
"execution_count": null,
|
469 |
+
"outputs": [
|
470 |
+
{
|
471 |
+
"output_type": "stream",
|
472 |
+
"name": "stdout",
|
473 |
+
"text": [
|
474 |
+
"93/93 [==============================] - 1s 4ms/step\n"
|
475 |
+
]
|
476 |
+
},
|
477 |
+
{
|
478 |
+
"output_type": "execute_result",
|
479 |
+
"data": {
|
480 |
+
"text/plain": [
|
481 |
+
"Pred LAYING SITTING STANDING WALKING WALKING_DOWNSTAIRS \\\n",
|
482 |
+
"True \n",
|
483 |
+
"LAYING 511 0 26 0 0 \n",
|
484 |
+
"SITTING 1 409 76 4 0 \n",
|
485 |
+
"STANDING 0 95 433 4 0 \n",
|
486 |
+
"WALKING 0 0 0 480 15 \n",
|
487 |
+
"WALKING_DOWNSTAIRS 0 0 0 0 419 \n",
|
488 |
+
"WALKING_UPSTAIRS 0 1 0 12 9 \n",
|
489 |
+
"\n",
|
490 |
+
"Pred WALKING_UPSTAIRS \n",
|
491 |
+
"True \n",
|
492 |
+
"LAYING 0 \n",
|
493 |
+
"SITTING 1 \n",
|
494 |
+
"STANDING 0 \n",
|
495 |
+
"WALKING 1 \n",
|
496 |
+
"WALKING_DOWNSTAIRS 1 \n",
|
497 |
+
"WALKING_UPSTAIRS 449 "
|
498 |
+
],
|
499 |
+
"text/html": [
|
500 |
+
"\n",
|
501 |
+
" <div id=\"df-ebce1837-967c-4ffc-872b-b24d45f84c89\">\n",
|
502 |
+
" <div class=\"colab-df-container\">\n",
|
503 |
+
" <div>\n",
|
504 |
+
"<style scoped>\n",
|
505 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
506 |
+
" vertical-align: middle;\n",
|
507 |
+
" }\n",
|
508 |
+
"\n",
|
509 |
+
" .dataframe tbody tr th {\n",
|
510 |
+
" vertical-align: top;\n",
|
511 |
+
" }\n",
|
512 |
+
"\n",
|
513 |
+
" .dataframe thead th {\n",
|
514 |
+
" text-align: right;\n",
|
515 |
+
" }\n",
|
516 |
+
"</style>\n",
|
517 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
518 |
+
" <thead>\n",
|
519 |
+
" <tr style=\"text-align: right;\">\n",
|
520 |
+
" <th>Pred</th>\n",
|
521 |
+
" <th>LAYING</th>\n",
|
522 |
+
" <th>SITTING</th>\n",
|
523 |
+
" <th>STANDING</th>\n",
|
524 |
+
" <th>WALKING</th>\n",
|
525 |
+
" <th>WALKING_DOWNSTAIRS</th>\n",
|
526 |
+
" <th>WALKING_UPSTAIRS</th>\n",
|
527 |
+
" </tr>\n",
|
528 |
+
" <tr>\n",
|
529 |
+
" <th>True</th>\n",
|
530 |
+
" <th></th>\n",
|
531 |
+
" <th></th>\n",
|
532 |
+
" <th></th>\n",
|
533 |
+
" <th></th>\n",
|
534 |
+
" <th></th>\n",
|
535 |
+
" <th></th>\n",
|
536 |
+
" </tr>\n",
|
537 |
+
" </thead>\n",
|
538 |
+
" <tbody>\n",
|
539 |
+
" <tr>\n",
|
540 |
+
" <th>LAYING</th>\n",
|
541 |
+
" <td>511</td>\n",
|
542 |
+
" <td>0</td>\n",
|
543 |
+
" <td>26</td>\n",
|
544 |
+
" <td>0</td>\n",
|
545 |
+
" <td>0</td>\n",
|
546 |
+
" <td>0</td>\n",
|
547 |
+
" </tr>\n",
|
548 |
+
" <tr>\n",
|
549 |
+
" <th>SITTING</th>\n",
|
550 |
+
" <td>1</td>\n",
|
551 |
+
" <td>409</td>\n",
|
552 |
+
" <td>76</td>\n",
|
553 |
+
" <td>4</td>\n",
|
554 |
+
" <td>0</td>\n",
|
555 |
+
" <td>1</td>\n",
|
556 |
+
" </tr>\n",
|
557 |
+
" <tr>\n",
|
558 |
+
" <th>STANDING</th>\n",
|
559 |
+
" <td>0</td>\n",
|
560 |
+
" <td>95</td>\n",
|
561 |
+
" <td>433</td>\n",
|
562 |
+
" <td>4</td>\n",
|
563 |
+
" <td>0</td>\n",
|
564 |
+
" <td>0</td>\n",
|
565 |
+
" </tr>\n",
|
566 |
+
" <tr>\n",
|
567 |
+
" <th>WALKING</th>\n",
|
568 |
+
" <td>0</td>\n",
|
569 |
+
" <td>0</td>\n",
|
570 |
+
" <td>0</td>\n",
|
571 |
+
" <td>480</td>\n",
|
572 |
+
" <td>15</td>\n",
|
573 |
+
" <td>1</td>\n",
|
574 |
+
" </tr>\n",
|
575 |
+
" <tr>\n",
|
576 |
+
" <th>WALKING_DOWNSTAIRS</th>\n",
|
577 |
+
" <td>0</td>\n",
|
578 |
+
" <td>0</td>\n",
|
579 |
+
" <td>0</td>\n",
|
580 |
+
" <td>0</td>\n",
|
581 |
+
" <td>419</td>\n",
|
582 |
+
" <td>1</td>\n",
|
583 |
+
" </tr>\n",
|
584 |
+
" <tr>\n",
|
585 |
+
" <th>WALKING_UPSTAIRS</th>\n",
|
586 |
+
" <td>0</td>\n",
|
587 |
+
" <td>1</td>\n",
|
588 |
+
" <td>0</td>\n",
|
589 |
+
" <td>12</td>\n",
|
590 |
+
" <td>9</td>\n",
|
591 |
+
" <td>449</td>\n",
|
592 |
+
" </tr>\n",
|
593 |
+
" </tbody>\n",
|
594 |
+
"</table>\n",
|
595 |
+
"</div>\n",
|
596 |
+
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-ebce1837-967c-4ffc-872b-b24d45f84c89')\"\n",
|
597 |
+
" title=\"Convert this dataframe to an interactive table.\"\n",
|
598 |
+
" style=\"display:none;\">\n",
|
599 |
+
" \n",
|
600 |
+
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
|
601 |
+
" width=\"24px\">\n",
|
602 |
+
" <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
|
603 |
+
" <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
|
604 |
+
" </svg>\n",
|
605 |
+
" </button>\n",
|
606 |
+
" \n",
|
607 |
+
" <style>\n",
|
608 |
+
" .colab-df-container {\n",
|
609 |
+
" display:flex;\n",
|
610 |
+
" flex-wrap:wrap;\n",
|
611 |
+
" gap: 12px;\n",
|
612 |
+
" }\n",
|
613 |
+
"\n",
|
614 |
+
" .colab-df-convert {\n",
|
615 |
+
" background-color: #E8F0FE;\n",
|
616 |
+
" border: none;\n",
|
617 |
+
" border-radius: 50%;\n",
|
618 |
+
" cursor: pointer;\n",
|
619 |
+
" display: none;\n",
|
620 |
+
" fill: #1967D2;\n",
|
621 |
+
" height: 32px;\n",
|
622 |
+
" padding: 0 0 0 0;\n",
|
623 |
+
" width: 32px;\n",
|
624 |
+
" }\n",
|
625 |
+
"\n",
|
626 |
+
" .colab-df-convert:hover {\n",
|
627 |
+
" background-color: #E2EBFA;\n",
|
628 |
+
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
|
629 |
+
" fill: #174EA6;\n",
|
630 |
+
" }\n",
|
631 |
+
"\n",
|
632 |
+
" [theme=dark] .colab-df-convert {\n",
|
633 |
+
" background-color: #3B4455;\n",
|
634 |
+
" fill: #D2E3FC;\n",
|
635 |
+
" }\n",
|
636 |
+
"\n",
|
637 |
+
" [theme=dark] .colab-df-convert:hover {\n",
|
638 |
+
" background-color: #434B5C;\n",
|
639 |
+
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
|
640 |
+
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
|
641 |
+
" fill: #FFFFFF;\n",
|
642 |
+
" }\n",
|
643 |
+
" </style>\n",
|
644 |
+
"\n",
|
645 |
+
" <script>\n",
|
646 |
+
" const buttonEl =\n",
|
647 |
+
" document.querySelector('#df-ebce1837-967c-4ffc-872b-b24d45f84c89 button.colab-df-convert');\n",
|
648 |
+
" buttonEl.style.display =\n",
|
649 |
+
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
|
650 |
+
"\n",
|
651 |
+
" async function convertToInteractive(key) {\n",
|
652 |
+
" const element = document.querySelector('#df-ebce1837-967c-4ffc-872b-b24d45f84c89');\n",
|
653 |
+
" const dataTable =\n",
|
654 |
+
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
|
655 |
+
" [key], {});\n",
|
656 |
+
" if (!dataTable) return;\n",
|
657 |
+
"\n",
|
658 |
+
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
|
659 |
+
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
|
660 |
+
" + ' to learn more about interactive tables.';\n",
|
661 |
+
" element.innerHTML = '';\n",
|
662 |
+
" dataTable['output_type'] = 'display_data';\n",
|
663 |
+
" await google.colab.output.renderOutput(dataTable, element);\n",
|
664 |
+
" const docLink = document.createElement('div');\n",
|
665 |
+
" docLink.innerHTML = docLinkHtml;\n",
|
666 |
+
" element.appendChild(docLink);\n",
|
667 |
+
" }\n",
|
668 |
+
" </script>\n",
|
669 |
+
" </div>\n",
|
670 |
+
" </div>\n",
|
671 |
+
" "
|
672 |
+
]
|
673 |
+
},
|
674 |
+
"metadata": {},
|
675 |
+
"execution_count": 53
|
676 |
+
}
|
677 |
+
]
|
678 |
+
},
|
679 |
+
{
|
680 |
+
"cell_type": "code",
|
681 |
+
"source": [
|
682 |
+
"score = model.evaluate(X_test, y_test)\n",
|
683 |
+
"\n",
|
684 |
+
"print(\"\\n categorical_crossentropy || accuracy \")\n",
|
685 |
+
"print(\" ____________________________________\")\n",
|
686 |
+
"print(score)"
|
687 |
+
],
|
688 |
+
"metadata": {
|
689 |
+
"colab": {
|
690 |
+
"base_uri": "https://localhost:8080/"
|
691 |
+
},
|
692 |
+
"id": "hwFZ64M7V0dN",
|
693 |
+
"outputId": "7d42242a-fe62-4172-ca6b-f328c8e9412a"
|
694 |
+
},
|
695 |
+
"execution_count": null,
|
696 |
+
"outputs": [
|
697 |
+
{
|
698 |
+
"output_type": "stream",
|
699 |
+
"name": "stdout",
|
700 |
+
"text": [
|
701 |
+
"93/93 [==============================] - 0s 5ms/step - loss: 0.3141 - accuracy: 0.9165\n",
|
702 |
+
"\n",
|
703 |
+
" categorical_crossentropy || accuracy \n",
|
704 |
+
" ____________________________________\n",
|
705 |
+
"[0.3141055107116699, 0.9165253043174744]\n"
|
706 |
+
]
|
707 |
+
}
|
708 |
+
]
|
709 |
+
},
|
710 |
+
{
|
711 |
+
"cell_type": "markdown",
|
712 |
+
"source": [
|
713 |
+
" 2. Defining the Architecture of 2-Layer of LSTM with more hyperparameter tunning"
|
714 |
+
],
|
715 |
+
"metadata": {
|
716 |
+
"id": "EFsp0FcXV9HG"
|
717 |
+
}
|
718 |
+
},
|
719 |
+
{
|
720 |
+
"cell_type": "markdown",
|
721 |
+
"source": [
|
722 |
+
"#### 2.1 First Model for 2-Layer of LSTM with more hyperparameter tunning"
|
723 |
+
],
|
724 |
+
"metadata": {
|
725 |
+
"id": "8p4j41aSV_Uv"
|
726 |
+
}
|
727 |
+
},
|
728 |
+
{
|
729 |
+
"cell_type": "code",
|
730 |
+
"source": [
|
731 |
+
"# Initializing parameters\n",
|
732 |
+
"n_epochs = 30\n",
|
733 |
+
"n_batch = 16\n",
|
734 |
+
"n_classes = _count_classes(y_train)\n",
|
735 |
+
"\n",
|
736 |
+
"# Bias regularizer value - we will use elasticnet\n",
|
737 |
+
"reg = L1L2(0.01, 0.01)"
|
738 |
+
],
|
739 |
+
"metadata": {
|
740 |
+
"id": "O594yNV2WSRj"
|
741 |
+
},
|
742 |
+
"execution_count": null,
|
743 |
+
"outputs": []
|
744 |
+
},
|
745 |
+
{
|
746 |
+
"cell_type": "code",
|
747 |
+
"source": [
|
748 |
+
"# Model execution\n",
|
749 |
+
"model = Sequential()\n",
|
750 |
+
"model.add(LSTM(48, input_shape=(timesteps, input_dim), return_sequences=True,bias_regularizer=reg ))\n",
|
751 |
+
"model.add(BatchNormalization())\n",
|
752 |
+
"model.add(Dropout(0.50))\n",
|
753 |
+
"model.add(LSTM(32))\n",
|
754 |
+
"model.add(Dropout(0.50))\n",
|
755 |
+
"model.add(Dense(n_classes, activation='sigmoid'))\n",
|
756 |
+
"print(\"Model Summary: \")\n",
|
757 |
+
"model.summary()"
|
758 |
+
],
|
759 |
+
"metadata": {
|
760 |
+
"colab": {
|
761 |
+
"base_uri": "https://localhost:8080/"
|
762 |
+
},
|
763 |
+
"id": "17yKQHdAWVWs",
|
764 |
+
"outputId": "48627f4b-09b1-465c-d1ab-cd2142af8642"
|
765 |
+
},
|
766 |
+
"execution_count": null,
|
767 |
+
"outputs": [
|
768 |
+
{
|
769 |
+
"output_type": "stream",
|
770 |
+
"name": "stdout",
|
771 |
+
"text": [
|
772 |
+
"Model Summary: \n",
|
773 |
+
"Model: \"sequential_4\"\n",
|
774 |
+
"_________________________________________________________________\n",
|
775 |
+
" Layer (type) Output Shape Param # \n",
|
776 |
+
"=================================================================\n",
|
777 |
+
" lstm_6 (LSTM) (None, 128, 48) 11136 \n",
|
778 |
+
" \n",
|
779 |
+
" batch_normalization_2 (Batc (None, 128, 48) 192 \n",
|
780 |
+
" hNormalization) \n",
|
781 |
+
" \n",
|
782 |
+
" dropout_6 (Dropout) (None, 128, 48) 0 \n",
|
783 |
+
" \n",
|
784 |
+
" lstm_7 (LSTM) (None, 32) 10368 \n",
|
785 |
+
" \n",
|
786 |
+
" dropout_7 (Dropout) (None, 32) 0 \n",
|
787 |
+
" \n",
|
788 |
+
" dense_4 (Dense) (None, 6) 198 \n",
|
789 |
+
" \n",
|
790 |
+
"=================================================================\n",
|
791 |
+
"Total params: 21,894\n",
|
792 |
+
"Trainable params: 21,798\n",
|
793 |
+
"Non-trainable params: 96\n",
|
794 |
+
"_________________________________________________________________\n"
|
795 |
+
]
|
796 |
+
}
|
797 |
+
]
|
798 |
+
},
|
799 |
+
{
|
800 |
+
"cell_type": "code",
|
801 |
+
"source": [
|
802 |
+
"model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])"
|
803 |
+
],
|
804 |
+
"metadata": {
|
805 |
+
"id": "HfXE9HTbWYMc"
|
806 |
+
},
|
807 |
+
"execution_count": null,
|
808 |
+
"outputs": []
|
809 |
+
},
|
810 |
+
{
|
811 |
+
"cell_type": "code",
|
812 |
+
"source": [
|
813 |
+
"# Training the model\n",
|
814 |
+
"model.fit(X_train, y_train, batch_size=n_batch, validation_data=(X_test, y_test), epochs=n_epochs)"
|
815 |
+
],
|
816 |
+
"metadata": {
|
817 |
+
"colab": {
|
818 |
+
"base_uri": "https://localhost:8080/"
|
819 |
+
},
|
820 |
+
"id": "TQxn8R_VWbJA",
|
821 |
+
"outputId": "b9186f7f-87a8-4e68-f35e-63fd06ca7efb"
|
822 |
+
},
|
823 |
+
"execution_count": null,
|
824 |
+
"outputs": [
|
825 |
+
{
|
826 |
+
"output_type": "stream",
|
827 |
+
"name": "stdout",
|
828 |
+
"text": [
|
829 |
+
"Epoch 1/30\n",
|
830 |
+
"460/460 [==============================] - 14s 17ms/step - loss: 1.5191 - accuracy: 0.6938 - val_loss: 0.9805 - val_accuracy: 0.8310\n",
|
831 |
+
"Epoch 2/30\n",
|
832 |
+
"460/460 [==============================] - 7s 16ms/step - loss: 0.7182 - accuracy: 0.8690 - val_loss: 0.5508 - val_accuracy: 0.8904\n",
|
833 |
+
"Epoch 3/30\n",
|
834 |
+
"460/460 [==============================] - 6s 13ms/step - loss: 0.3774 - accuracy: 0.9128 - val_loss: 0.3188 - val_accuracy: 0.8965\n",
|
835 |
+
"Epoch 4/30\n",
|
836 |
+
"460/460 [==============================] - 8s 17ms/step - loss: 0.2595 - accuracy: 0.9225 - val_loss: 0.4659 - val_accuracy: 0.8405\n",
|
837 |
+
"Epoch 5/30\n",
|
838 |
+
"460/460 [==============================] - 6s 14ms/step - loss: 0.2368 - accuracy: 0.9187 - val_loss: 0.4207 - val_accuracy: 0.8565\n",
|
839 |
+
"Epoch 6/30\n",
|
840 |
+
"460/460 [==============================] - 8s 17ms/step - loss: 0.2269 - accuracy: 0.9234 - val_loss: 0.2667 - val_accuracy: 0.9036\n",
|
841 |
+
"Epoch 7/30\n",
|
842 |
+
"460/460 [==============================] - 6s 14ms/step - loss: 0.1687 - accuracy: 0.9374 - val_loss: 0.2469 - val_accuracy: 0.9125\n",
|
843 |
+
"Epoch 8/30\n",
|
844 |
+
"460/460 [==============================] - 7s 16ms/step - loss: 0.1691 - accuracy: 0.9387 - val_loss: 0.3808 - val_accuracy: 0.8884\n",
|
845 |
+
"Epoch 9/30\n",
|
846 |
+
"460/460 [==============================] - 7s 14ms/step - loss: 0.1535 - accuracy: 0.9422 - val_loss: 0.2922 - val_accuracy: 0.9060\n",
|
847 |
+
"Epoch 10/30\n",
|
848 |
+
"460/460 [==============================] - 7s 16ms/step - loss: 0.1839 - accuracy: 0.9350 - val_loss: 0.3028 - val_accuracy: 0.8935\n",
|
849 |
+
"Epoch 11/30\n",
|
850 |
+
"460/460 [==============================] - 7s 15ms/step - loss: 0.1648 - accuracy: 0.9408 - val_loss: 0.3396 - val_accuracy: 0.8860\n",
|
851 |
+
"Epoch 12/30\n",
|
852 |
+
"460/460 [==============================] - 7s 16ms/step - loss: 0.1487 - accuracy: 0.9442 - val_loss: 0.2619 - val_accuracy: 0.9148\n",
|
853 |
+
"Epoch 13/30\n",
|
854 |
+
"460/460 [==============================] - 8s 18ms/step - loss: 0.1564 - accuracy: 0.9393 - val_loss: 0.2611 - val_accuracy: 0.9131\n",
|
855 |
+
"Epoch 14/30\n",
|
856 |
+
"460/460 [==============================] - 7s 14ms/step - loss: 0.1492 - accuracy: 0.9418 - val_loss: 0.3017 - val_accuracy: 0.9155\n",
|
857 |
+
"Epoch 15/30\n",
|
858 |
+
"460/460 [==============================] - 7s 16ms/step - loss: 0.1759 - accuracy: 0.9370 - val_loss: 0.3169 - val_accuracy: 0.9182\n",
|
859 |
+
"Epoch 16/30\n",
|
860 |
+
"460/460 [==============================] - 7s 14ms/step - loss: 0.1700 - accuracy: 0.9396 - val_loss: 0.3099 - val_accuracy: 0.9030\n",
|
861 |
+
"Epoch 17/30\n",
|
862 |
+
"460/460 [==============================] - 8s 17ms/step - loss: 0.1506 - accuracy: 0.9403 - val_loss: 0.3593 - val_accuracy: 0.8965\n",
|
863 |
+
"Epoch 18/30\n",
|
864 |
+
"460/460 [==============================] - 6s 13ms/step - loss: 0.1462 - accuracy: 0.9461 - val_loss: 0.3433 - val_accuracy: 0.9074\n",
|
865 |
+
"Epoch 19/30\n",
|
866 |
+
"460/460 [==============================] - 8s 17ms/step - loss: 0.1372 - accuracy: 0.9459 - val_loss: 0.2816 - val_accuracy: 0.9206\n",
|
867 |
+
"Epoch 20/30\n",
|
868 |
+
"460/460 [==============================] - 6s 13ms/step - loss: 0.1436 - accuracy: 0.9429 - val_loss: 0.2907 - val_accuracy: 0.9196\n",
|
869 |
+
"Epoch 21/30\n",
|
870 |
+
"460/460 [==============================] - 8s 17ms/step - loss: 0.1311 - accuracy: 0.9480 - val_loss: 0.2638 - val_accuracy: 0.9223\n",
|
871 |
+
"Epoch 22/30\n",
|
872 |
+
"460/460 [==============================] - 6s 13ms/step - loss: 0.1498 - accuracy: 0.9410 - val_loss: 0.3071 - val_accuracy: 0.9030\n",
|
873 |
+
"Epoch 23/30\n",
|
874 |
+
"460/460 [==============================] - 8s 16ms/step - loss: 0.1399 - accuracy: 0.9472 - val_loss: 0.3322 - val_accuracy: 0.9060\n",
|
875 |
+
"Epoch 24/30\n",
|
876 |
+
"460/460 [==============================] - 6s 14ms/step - loss: 0.1619 - accuracy: 0.9397 - val_loss: 0.2797 - val_accuracy: 0.9179\n",
|
877 |
+
"Epoch 25/30\n",
|
878 |
+
"460/460 [==============================] - 7s 16ms/step - loss: 0.1452 - accuracy: 0.9446 - val_loss: 0.2839 - val_accuracy: 0.9148\n",
|
879 |
+
"Epoch 26/30\n",
|
880 |
+
"460/460 [==============================] - 7s 14ms/step - loss: 0.1296 - accuracy: 0.9494 - val_loss: 0.3043 - val_accuracy: 0.8982\n",
|
881 |
+
"Epoch 27/30\n",
|
882 |
+
"460/460 [==============================] - 7s 16ms/step - loss: 0.1334 - accuracy: 0.9460 - val_loss: 0.2795 - val_accuracy: 0.9080\n",
|
883 |
+
"Epoch 28/30\n",
|
884 |
+
"460/460 [==============================] - 7s 14ms/step - loss: 0.1310 - accuracy: 0.9463 - val_loss: 0.2821 - val_accuracy: 0.8945\n",
|
885 |
+
"Epoch 29/30\n",
|
886 |
+
"460/460 [==============================] - 7s 16ms/step - loss: 0.1253 - accuracy: 0.9489 - val_loss: 0.2743 - val_accuracy: 0.9165\n",
|
887 |
+
"Epoch 30/30\n",
|
888 |
+
"460/460 [==============================] - 7s 14ms/step - loss: 0.1407 - accuracy: 0.9406 - val_loss: 0.2640 - val_accuracy: 0.9131\n"
|
889 |
+
]
|
890 |
+
},
|
891 |
+
{
|
892 |
+
"output_type": "execute_result",
|
893 |
+
"data": {
|
894 |
+
"text/plain": [
|
895 |
+
"<keras.callbacks.History at 0x7f78fa3f00a0>"
|
896 |
+
]
|
897 |
+
},
|
898 |
+
"metadata": {},
|
899 |
+
"execution_count": 58
|
900 |
+
}
|
901 |
+
]
|
902 |
+
},
|
903 |
+
{
|
904 |
+
"cell_type": "code",
|
905 |
+
"source": [
|
906 |
+
"# Confusion Matrix\n",
|
907 |
+
"confusion_matrix(y_test, model.predict(X_test))"
|
908 |
+
],
|
909 |
+
"metadata": {
|
910 |
+
"colab": {
|
911 |
+
"base_uri": "https://localhost:8080/",
|
912 |
+
"height": 286
|
913 |
+
},
|
914 |
+
"id": "abLtI6JsWeM1",
|
915 |
+
"outputId": "1e5b55ea-0793-463a-df25-7596e18e36c8"
|
916 |
+
},
|
917 |
+
"execution_count": null,
|
918 |
+
"outputs": [
|
919 |
+
{
|
920 |
+
"output_type": "stream",
|
921 |
+
"name": "stdout",
|
922 |
+
"text": [
|
923 |
+
"93/93 [==============================] - 2s 7ms/step\n"
|
924 |
+
]
|
925 |
+
},
|
926 |
+
{
|
927 |
+
"output_type": "execute_result",
|
928 |
+
"data": {
|
929 |
+
"text/plain": [
|
930 |
+
"Pred LAYING SITTING STANDING WALKING WALKING_DOWNSTAIRS \\\n",
|
931 |
+
"True \n",
|
932 |
+
"LAYING 537 0 0 0 0 \n",
|
933 |
+
"SITTING 2 419 67 0 0 \n",
|
934 |
+
"STANDING 0 128 404 0 0 \n",
|
935 |
+
"WALKING 0 0 0 472 19 \n",
|
936 |
+
"WALKING_DOWNSTAIRS 0 0 0 2 415 \n",
|
937 |
+
"WALKING_UPSTAIRS 0 0 0 11 16 \n",
|
938 |
+
"\n",
|
939 |
+
"Pred WALKING_UPSTAIRS \n",
|
940 |
+
"True \n",
|
941 |
+
"LAYING 0 \n",
|
942 |
+
"SITTING 3 \n",
|
943 |
+
"STANDING 0 \n",
|
944 |
+
"WALKING 5 \n",
|
945 |
+
"WALKING_DOWNSTAIRS 3 \n",
|
946 |
+
"WALKING_UPSTAIRS 444 "
|
947 |
+
],
|
948 |
+
"text/html": [
|
949 |
+
"\n",
|
950 |
+
" <div id=\"df-03249097-2495-4177-b033-477f5b7ebb57\">\n",
|
951 |
+
" <div class=\"colab-df-container\">\n",
|
952 |
+
" <div>\n",
|
953 |
+
"<style scoped>\n",
|
954 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
955 |
+
" vertical-align: middle;\n",
|
956 |
+
" }\n",
|
957 |
+
"\n",
|
958 |
+
" .dataframe tbody tr th {\n",
|
959 |
+
" vertical-align: top;\n",
|
960 |
+
" }\n",
|
961 |
+
"\n",
|
962 |
+
" .dataframe thead th {\n",
|
963 |
+
" text-align: right;\n",
|
964 |
+
" }\n",
|
965 |
+
"</style>\n",
|
966 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
967 |
+
" <thead>\n",
|
968 |
+
" <tr style=\"text-align: right;\">\n",
|
969 |
+
" <th>Pred</th>\n",
|
970 |
+
" <th>LAYING</th>\n",
|
971 |
+
" <th>SITTING</th>\n",
|
972 |
+
" <th>STANDING</th>\n",
|
973 |
+
" <th>WALKING</th>\n",
|
974 |
+
" <th>WALKING_DOWNSTAIRS</th>\n",
|
975 |
+
" <th>WALKING_UPSTAIRS</th>\n",
|
976 |
+
" </tr>\n",
|
977 |
+
" <tr>\n",
|
978 |
+
" <th>True</th>\n",
|
979 |
+
" <th></th>\n",
|
980 |
+
" <th></th>\n",
|
981 |
+
" <th></th>\n",
|
982 |
+
" <th></th>\n",
|
983 |
+
" <th></th>\n",
|
984 |
+
" <th></th>\n",
|
985 |
+
" </tr>\n",
|
986 |
+
" </thead>\n",
|
987 |
+
" <tbody>\n",
|
988 |
+
" <tr>\n",
|
989 |
+
" <th>LAYING</th>\n",
|
990 |
+
" <td>537</td>\n",
|
991 |
+
" <td>0</td>\n",
|
992 |
+
" <td>0</td>\n",
|
993 |
+
" <td>0</td>\n",
|
994 |
+
" <td>0</td>\n",
|
995 |
+
" <td>0</td>\n",
|
996 |
+
" </tr>\n",
|
997 |
+
" <tr>\n",
|
998 |
+
" <th>SITTING</th>\n",
|
999 |
+
" <td>2</td>\n",
|
1000 |
+
" <td>419</td>\n",
|
1001 |
+
" <td>67</td>\n",
|
1002 |
+
" <td>0</td>\n",
|
1003 |
+
" <td>0</td>\n",
|
1004 |
+
" <td>3</td>\n",
|
1005 |
+
" </tr>\n",
|
1006 |
+
" <tr>\n",
|
1007 |
+
" <th>STANDING</th>\n",
|
1008 |
+
" <td>0</td>\n",
|
1009 |
+
" <td>128</td>\n",
|
1010 |
+
" <td>404</td>\n",
|
1011 |
+
" <td>0</td>\n",
|
1012 |
+
" <td>0</td>\n",
|
1013 |
+
" <td>0</td>\n",
|
1014 |
+
" </tr>\n",
|
1015 |
+
" <tr>\n",
|
1016 |
+
" <th>WALKING</th>\n",
|
1017 |
+
" <td>0</td>\n",
|
1018 |
+
" <td>0</td>\n",
|
1019 |
+
" <td>0</td>\n",
|
1020 |
+
" <td>472</td>\n",
|
1021 |
+
" <td>19</td>\n",
|
1022 |
+
" <td>5</td>\n",
|
1023 |
+
" </tr>\n",
|
1024 |
+
" <tr>\n",
|
1025 |
+
" <th>WALKING_DOWNSTAIRS</th>\n",
|
1026 |
+
" <td>0</td>\n",
|
1027 |
+
" <td>0</td>\n",
|
1028 |
+
" <td>0</td>\n",
|
1029 |
+
" <td>2</td>\n",
|
1030 |
+
" <td>415</td>\n",
|
1031 |
+
" <td>3</td>\n",
|
1032 |
+
" </tr>\n",
|
1033 |
+
" <tr>\n",
|
1034 |
+
" <th>WALKING_UPSTAIRS</th>\n",
|
1035 |
+
" <td>0</td>\n",
|
1036 |
+
" <td>0</td>\n",
|
1037 |
+
" <td>0</td>\n",
|
1038 |
+
" <td>11</td>\n",
|
1039 |
+
" <td>16</td>\n",
|
1040 |
+
" <td>444</td>\n",
|
1041 |
+
" </tr>\n",
|
1042 |
+
" </tbody>\n",
|
1043 |
+
"</table>\n",
|
1044 |
+
"</div>\n",
|
1045 |
+
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-03249097-2495-4177-b033-477f5b7ebb57')\"\n",
|
1046 |
+
" title=\"Convert this dataframe to an interactive table.\"\n",
|
1047 |
+
" style=\"display:none;\">\n",
|
1048 |
+
" \n",
|
1049 |
+
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
|
1050 |
+
" width=\"24px\">\n",
|
1051 |
+
" <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
|
1052 |
+
" <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
|
1053 |
+
" </svg>\n",
|
1054 |
+
" </button>\n",
|
1055 |
+
" \n",
|
1056 |
+
" <style>\n",
|
1057 |
+
" .colab-df-container {\n",
|
1058 |
+
" display:flex;\n",
|
1059 |
+
" flex-wrap:wrap;\n",
|
1060 |
+
" gap: 12px;\n",
|
1061 |
+
" }\n",
|
1062 |
+
"\n",
|
1063 |
+
" .colab-df-convert {\n",
|
1064 |
+
" background-color: #E8F0FE;\n",
|
1065 |
+
" border: none;\n",
|
1066 |
+
" border-radius: 50%;\n",
|
1067 |
+
" cursor: pointer;\n",
|
1068 |
+
" display: none;\n",
|
1069 |
+
" fill: #1967D2;\n",
|
1070 |
+
" height: 32px;\n",
|
1071 |
+
" padding: 0 0 0 0;\n",
|
1072 |
+
" width: 32px;\n",
|
1073 |
+
" }\n",
|
1074 |
+
"\n",
|
1075 |
+
" .colab-df-convert:hover {\n",
|
1076 |
+
" background-color: #E2EBFA;\n",
|
1077 |
+
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
|
1078 |
+
" fill: #174EA6;\n",
|
1079 |
+
" }\n",
|
1080 |
+
"\n",
|
1081 |
+
" [theme=dark] .colab-df-convert {\n",
|
1082 |
+
" background-color: #3B4455;\n",
|
1083 |
+
" fill: #D2E3FC;\n",
|
1084 |
+
" }\n",
|
1085 |
+
"\n",
|
1086 |
+
" [theme=dark] .colab-df-convert:hover {\n",
|
1087 |
+
" background-color: #434B5C;\n",
|
1088 |
+
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
|
1089 |
+
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
|
1090 |
+
" fill: #FFFFFF;\n",
|
1091 |
+
" }\n",
|
1092 |
+
" </style>\n",
|
1093 |
+
"\n",
|
1094 |
+
" <script>\n",
|
1095 |
+
" const buttonEl =\n",
|
1096 |
+
" document.querySelector('#df-03249097-2495-4177-b033-477f5b7ebb57 button.colab-df-convert');\n",
|
1097 |
+
" buttonEl.style.display =\n",
|
1098 |
+
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
|
1099 |
+
"\n",
|
1100 |
+
" async function convertToInteractive(key) {\n",
|
1101 |
+
" const element = document.querySelector('#df-03249097-2495-4177-b033-477f5b7ebb57');\n",
|
1102 |
+
" const dataTable =\n",
|
1103 |
+
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
|
1104 |
+
" [key], {});\n",
|
1105 |
+
" if (!dataTable) return;\n",
|
1106 |
+
"\n",
|
1107 |
+
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
|
1108 |
+
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
|
1109 |
+
" + ' to learn more about interactive tables.';\n",
|
1110 |
+
" element.innerHTML = '';\n",
|
1111 |
+
" dataTable['output_type'] = 'display_data';\n",
|
1112 |
+
" await google.colab.output.renderOutput(dataTable, element);\n",
|
1113 |
+
" const docLink = document.createElement('div');\n",
|
1114 |
+
" docLink.innerHTML = docLinkHtml;\n",
|
1115 |
+
" element.appendChild(docLink);\n",
|
1116 |
+
" }\n",
|
1117 |
+
" </script>\n",
|
1118 |
+
" </div>\n",
|
1119 |
+
" </div>\n",
|
1120 |
+
" "
|
1121 |
+
]
|
1122 |
+
},
|
1123 |
+
"metadata": {},
|
1124 |
+
"execution_count": 59
|
1125 |
+
}
|
1126 |
+
]
|
1127 |
+
},
|
1128 |
+
{
|
1129 |
+
"cell_type": "code",
|
1130 |
+
"source": [
|
1131 |
+
"score = model.evaluate(X_test, y_test)\n",
|
1132 |
+
"\n",
|
1133 |
+
"print(\"\\n categorica_crossentropy || accuracy \")\n",
|
1134 |
+
"print(\" ____________________________________\")\n",
|
1135 |
+
"print(score)"
|
1136 |
+
],
|
1137 |
+
"metadata": {
|
1138 |
+
"colab": {
|
1139 |
+
"base_uri": "https://localhost:8080/"
|
1140 |
+
},
|
1141 |
+
"id": "sYNMpcu_WjB-",
|
1142 |
+
"outputId": "e43835c8-4b1b-4994-9e9e-67cf6f208d59"
|
1143 |
+
},
|
1144 |
+
"execution_count": null,
|
1145 |
+
"outputs": [
|
1146 |
+
{
|
1147 |
+
"output_type": "stream",
|
1148 |
+
"name": "stdout",
|
1149 |
+
"text": [
|
1150 |
+
"93/93 [==============================] - 1s 6ms/step - loss: 0.2640 - accuracy: 0.9131\n",
|
1151 |
+
"\n",
|
1152 |
+
" categorica_crossentropy || accuracy \n",
|
1153 |
+
" ____________________________________\n",
|
1154 |
+
"[0.2640216052532196, 0.9131320118904114]\n"
|
1155 |
+
]
|
1156 |
+
}
|
1157 |
+
]
|
1158 |
+
},
|
1159 |
+
{
|
1160 |
+
"cell_type": "markdown",
|
1161 |
+
"source": [
|
1162 |
+
"#### 2.2 Second Model for 2-Layer of LSTM with more hyperparameter tunning"
|
1163 |
+
],
|
1164 |
+
"metadata": {
|
1165 |
+
"id": "gPw5BDcNWocp"
|
1166 |
+
}
|
1167 |
+
},
|
1168 |
+
{
|
1169 |
+
"cell_type": "code",
|
1170 |
+
"source": [
|
1171 |
+
"# Model execution\n",
|
1172 |
+
"model = Sequential()\n",
|
1173 |
+
"model.add(LSTM(64, input_shape=(timesteps, input_dim), return_sequences=True, bias_regularizer=reg))\n",
|
1174 |
+
"model.add(BatchNormalization())\n",
|
1175 |
+
"model.add(Dropout(0.50))\n",
|
1176 |
+
"model.add(LSTM(48))\n",
|
1177 |
+
"model.add(Dropout(0.50))\n",
|
1178 |
+
"model.add(Dense(n_classes, activation='sigmoid'))\n",
|
1179 |
+
"print(\"Model Summary: \")\n",
|
1180 |
+
"model.summary()"
|
1181 |
+
],
|
1182 |
+
"metadata": {
|
1183 |
+
"colab": {
|
1184 |
+
"base_uri": "https://localhost:8080/"
|
1185 |
+
},
|
1186 |
+
"id": "KgIyrMM0WsEY",
|
1187 |
+
"outputId": "414d300c-10de-40e0-b5e5-73de5ff3e97f"
|
1188 |
+
},
|
1189 |
+
"execution_count": null,
|
1190 |
+
"outputs": [
|
1191 |
+
{
|
1192 |
+
"output_type": "stream",
|
1193 |
+
"name": "stdout",
|
1194 |
+
"text": [
|
1195 |
+
"Model Summary: \n",
|
1196 |
+
"Model: \"sequential_5\"\n",
|
1197 |
+
"_________________________________________________________________\n",
|
1198 |
+
" Layer (type) Output Shape Param # \n",
|
1199 |
+
"=================================================================\n",
|
1200 |
+
" lstm_8 (LSTM) (None, 128, 64) 18944 \n",
|
1201 |
+
" \n",
|
1202 |
+
" batch_normalization_3 (Batc (None, 128, 64) 256 \n",
|
1203 |
+
" hNormalization) \n",
|
1204 |
+
" \n",
|
1205 |
+
" dropout_8 (Dropout) (None, 128, 64) 0 \n",
|
1206 |
+
" \n",
|
1207 |
+
" lstm_9 (LSTM) (None, 48) 21696 \n",
|
1208 |
+
" \n",
|
1209 |
+
" dropout_9 (Dropout) (None, 48) 0 \n",
|
1210 |
+
" \n",
|
1211 |
+
" dense_5 (Dense) (None, 6) 294 \n",
|
1212 |
+
" \n",
|
1213 |
+
"=================================================================\n",
|
1214 |
+
"Total params: 41,190\n",
|
1215 |
+
"Trainable params: 41,062\n",
|
1216 |
+
"Non-trainable params: 128\n",
|
1217 |
+
"_________________________________________________________________\n"
|
1218 |
+
]
|
1219 |
+
}
|
1220 |
+
]
|
1221 |
+
},
|
1222 |
+
{
|
1223 |
+
"cell_type": "code",
|
1224 |
+
"source": [
|
1225 |
+
"model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])"
|
1226 |
+
],
|
1227 |
+
"metadata": {
|
1228 |
+
"id": "cJ4wvkh5WycW"
|
1229 |
+
},
|
1230 |
+
"execution_count": null,
|
1231 |
+
"outputs": []
|
1232 |
+
},
|
1233 |
+
{
|
1234 |
+
"cell_type": "code",
|
1235 |
+
"source": [
|
1236 |
+
"# Training the model\n",
|
1237 |
+
"model.fit(X_train, y_train, batch_size=n_batch, validation_data=(X_test, y_test), epochs=n_epochs)"
|
1238 |
+
],
|
1239 |
+
"metadata": {
|
1240 |
+
"colab": {
|
1241 |
+
"base_uri": "https://localhost:8080/"
|
1242 |
+
},
|
1243 |
+
"id": "VKrGoJiuW3tK",
|
1244 |
+
"outputId": "86011b01-e928-434b-a373-102ff9c27ec2"
|
1245 |
+
},
|
1246 |
+
"execution_count": null,
|
1247 |
+
"outputs": [
|
1248 |
+
{
|
1249 |
+
"output_type": "stream",
|
1250 |
+
"name": "stdout",
|
1251 |
+
"text": [
|
1252 |
+
"Epoch 1/30\n",
|
1253 |
+
"460/460 [==============================] - 13s 19ms/step - loss: 1.6734 - accuracy: 0.7078 - val_loss: 1.5870 - val_accuracy: 0.5945\n",
|
1254 |
+
"Epoch 2/30\n",
|
1255 |
+
"460/460 [==============================] - 6s 14ms/step - loss: 0.7557 - accuracy: 0.8898 - val_loss: 0.4793 - val_accuracy: 0.9179\n",
|
1256 |
+
"Epoch 3/30\n",
|
1257 |
+
"460/460 [==============================] - 8s 16ms/step - loss: 0.4011 - accuracy: 0.9135 - val_loss: 0.3697 - val_accuracy: 0.8761\n",
|
1258 |
+
"Epoch 4/30\n",
|
1259 |
+
"460/460 [==============================] - 6s 14ms/step - loss: 0.2216 - accuracy: 0.9287 - val_loss: 0.3722 - val_accuracy: 0.8829\n",
|
1260 |
+
"Epoch 5/30\n",
|
1261 |
+
"460/460 [==============================] - 8s 17ms/step - loss: 0.1946 - accuracy: 0.9334 - val_loss: 0.4488 - val_accuracy: 0.8446\n",
|
1262 |
+
"Epoch 6/30\n",
|
1263 |
+
"460/460 [==============================] - 6s 13ms/step - loss: 0.1719 - accuracy: 0.9381 - val_loss: 0.2355 - val_accuracy: 0.9128\n",
|
1264 |
+
"Epoch 7/30\n",
|
1265 |
+
"460/460 [==============================] - 7s 16ms/step - loss: 0.1631 - accuracy: 0.9399 - val_loss: 0.2094 - val_accuracy: 0.9325\n",
|
1266 |
+
"Epoch 8/30\n",
|
1267 |
+
"460/460 [==============================] - 6s 13ms/step - loss: 0.1798 - accuracy: 0.9317 - val_loss: 0.2030 - val_accuracy: 0.9189\n",
|
1268 |
+
"Epoch 9/30\n",
|
1269 |
+
"460/460 [==============================] - 8s 17ms/step - loss: 0.1874 - accuracy: 0.9312 - val_loss: 0.2831 - val_accuracy: 0.9165\n",
|
1270 |
+
"Epoch 10/30\n",
|
1271 |
+
"460/460 [==============================] - 6s 14ms/step - loss: 0.1565 - accuracy: 0.9387 - val_loss: 0.2430 - val_accuracy: 0.9192\n",
|
1272 |
+
"Epoch 11/30\n",
|
1273 |
+
"460/460 [==============================] - 7s 16ms/step - loss: 0.1611 - accuracy: 0.9384 - val_loss: 0.2457 - val_accuracy: 0.9080\n",
|
1274 |
+
"Epoch 12/30\n",
|
1275 |
+
"460/460 [==============================] - 6s 14ms/step - loss: 0.1424 - accuracy: 0.9445 - val_loss: 0.3006 - val_accuracy: 0.8992\n",
|
1276 |
+
"Epoch 13/30\n",
|
1277 |
+
"460/460 [==============================] - 7s 16ms/step - loss: 0.1442 - accuracy: 0.9440 - val_loss: 0.2308 - val_accuracy: 0.9162\n",
|
1278 |
+
"Epoch 14/30\n",
|
1279 |
+
"460/460 [==============================] - 6s 13ms/step - loss: 0.1882 - accuracy: 0.9353 - val_loss: 0.2404 - val_accuracy: 0.9199\n",
|
1280 |
+
"Epoch 15/30\n",
|
1281 |
+
"460/460 [==============================] - 8s 16ms/step - loss: 0.1385 - accuracy: 0.9448 - val_loss: 0.2316 - val_accuracy: 0.9094\n",
|
1282 |
+
"Epoch 16/30\n",
|
1283 |
+
"460/460 [==============================] - 6s 14ms/step - loss: 0.1704 - accuracy: 0.9393 - val_loss: 0.2102 - val_accuracy: 0.9257\n",
|
1284 |
+
"Epoch 17/30\n",
|
1285 |
+
"460/460 [==============================] - 7s 16ms/step - loss: 0.1349 - accuracy: 0.9465 - val_loss: 0.2214 - val_accuracy: 0.9165\n",
|
1286 |
+
"Epoch 18/30\n",
|
1287 |
+
"460/460 [==============================] - 6s 13ms/step - loss: 0.1502 - accuracy: 0.9400 - val_loss: 0.2715 - val_accuracy: 0.9101\n",
|
1288 |
+
"Epoch 19/30\n",
|
1289 |
+
"460/460 [==============================] - 7s 16ms/step - loss: 0.1281 - accuracy: 0.9472 - val_loss: 0.2358 - val_accuracy: 0.9179\n",
|
1290 |
+
"Epoch 20/30\n",
|
1291 |
+
"460/460 [==============================] - 6s 14ms/step - loss: 0.1380 - accuracy: 0.9448 - val_loss: 0.2803 - val_accuracy: 0.9080\n",
|
1292 |
+
"Epoch 21/30\n",
|
1293 |
+
"460/460 [==============================] - 7s 16ms/step - loss: 0.1440 - accuracy: 0.9429 - val_loss: 0.2399 - val_accuracy: 0.9253\n",
|
1294 |
+
"Epoch 22/30\n",
|
1295 |
+
"460/460 [==============================] - 7s 14ms/step - loss: 0.1608 - accuracy: 0.9416 - val_loss: 0.2226 - val_accuracy: 0.9216\n",
|
1296 |
+
"Epoch 23/30\n",
|
1297 |
+
"460/460 [==============================] - 7s 16ms/step - loss: 0.1291 - accuracy: 0.9489 - val_loss: 0.2334 - val_accuracy: 0.9257\n",
|
1298 |
+
"Epoch 24/30\n",
|
1299 |
+
"460/460 [==============================] - 7s 14ms/step - loss: 0.1311 - accuracy: 0.9471 - val_loss: 0.2140 - val_accuracy: 0.9267\n",
|
1300 |
+
"Epoch 25/30\n",
|
1301 |
+
"460/460 [==============================] - 8s 16ms/step - loss: 0.1324 - accuracy: 0.9475 - val_loss: 0.2815 - val_accuracy: 0.9165\n",
|
1302 |
+
"Epoch 26/30\n",
|
1303 |
+
"460/460 [==============================] - 6s 13ms/step - loss: 0.1284 - accuracy: 0.9484 - val_loss: 0.2534 - val_accuracy: 0.9325\n",
|
1304 |
+
"Epoch 27/30\n",
|
1305 |
+
"460/460 [==============================] - 7s 16ms/step - loss: 0.1268 - accuracy: 0.9486 - val_loss: 0.2600 - val_accuracy: 0.9220\n",
|
1306 |
+
"Epoch 28/30\n",
|
1307 |
+
"460/460 [==============================] - 6s 13ms/step - loss: 0.1290 - accuracy: 0.9501 - val_loss: 0.2439 - val_accuracy: 0.9192\n",
|
1308 |
+
"Epoch 29/30\n",
|
1309 |
+
"460/460 [==============================] - 8s 17ms/step - loss: 0.1354 - accuracy: 0.9486 - val_loss: 0.5618 - val_accuracy: 0.8320\n",
|
1310 |
+
"Epoch 30/30\n",
|
1311 |
+
"460/460 [==============================] - 7s 14ms/step - loss: 0.1541 - accuracy: 0.9411 - val_loss: 0.3802 - val_accuracy: 0.9125\n"
|
1312 |
+
]
|
1313 |
+
},
|
1314 |
+
{
|
1315 |
+
"output_type": "execute_result",
|
1316 |
+
"data": {
|
1317 |
+
"text/plain": [
|
1318 |
+
"<keras.callbacks.History at 0x7f78f3474a30>"
|
1319 |
+
]
|
1320 |
+
},
|
1321 |
+
"metadata": {},
|
1322 |
+
"execution_count": 63
|
1323 |
+
}
|
1324 |
+
]
|
1325 |
+
},
|
1326 |
+
{
|
1327 |
+
"cell_type": "code",
|
1328 |
+
"source": [
|
1329 |
+
"# Confusion Matrix\n",
|
1330 |
+
"confusion_matrix(y_test, model.predict(X_test))"
|
1331 |
+
],
|
1332 |
+
"metadata": {
|
1333 |
+
"colab": {
|
1334 |
+
"base_uri": "https://localhost:8080/",
|
1335 |
+
"height": 286
|
1336 |
+
},
|
1337 |
+
"id": "65v6IGSdW66a",
|
1338 |
+
"outputId": "e9876301-35b6-4a9e-f364-4580cd009f5d"
|
1339 |
+
},
|
1340 |
+
"execution_count": null,
|
1341 |
+
"outputs": [
|
1342 |
+
{
|
1343 |
+
"output_type": "stream",
|
1344 |
+
"name": "stdout",
|
1345 |
+
"text": [
|
1346 |
+
"93/93 [==============================] - 2s 7ms/step\n"
|
1347 |
+
]
|
1348 |
+
},
|
1349 |
+
{
|
1350 |
+
"output_type": "execute_result",
|
1351 |
+
"data": {
|
1352 |
+
"text/plain": [
|
1353 |
+
"Pred LAYING SITTING STANDING WALKING WALKING_DOWNSTAIRS \\\n",
|
1354 |
+
"True \n",
|
1355 |
+
"LAYING 537 0 0 0 0 \n",
|
1356 |
+
"SITTING 0 407 82 0 0 \n",
|
1357 |
+
"STANDING 0 94 438 0 0 \n",
|
1358 |
+
"WALKING 0 0 0 447 46 \n",
|
1359 |
+
"WALKING_DOWNSTAIRS 0 0 0 1 419 \n",
|
1360 |
+
"WALKING_UPSTAIRS 0 2 0 1 27 \n",
|
1361 |
+
"\n",
|
1362 |
+
"Pred WALKING_UPSTAIRS \n",
|
1363 |
+
"True \n",
|
1364 |
+
"LAYING 0 \n",
|
1365 |
+
"SITTING 2 \n",
|
1366 |
+
"STANDING 0 \n",
|
1367 |
+
"WALKING 3 \n",
|
1368 |
+
"WALKING_DOWNSTAIRS 0 \n",
|
1369 |
+
"WALKING_UPSTAIRS 441 "
|
1370 |
+
],
|
1371 |
+
"text/html": [
|
1372 |
+
"\n",
|
1373 |
+
" <div id=\"df-af1f7d68-479a-4380-870e-0e82389a4389\">\n",
|
1374 |
+
" <div class=\"colab-df-container\">\n",
|
1375 |
+
" <div>\n",
|
1376 |
+
"<style scoped>\n",
|
1377 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
1378 |
+
" vertical-align: middle;\n",
|
1379 |
+
" }\n",
|
1380 |
+
"\n",
|
1381 |
+
" .dataframe tbody tr th {\n",
|
1382 |
+
" vertical-align: top;\n",
|
1383 |
+
" }\n",
|
1384 |
+
"\n",
|
1385 |
+
" .dataframe thead th {\n",
|
1386 |
+
" text-align: right;\n",
|
1387 |
+
" }\n",
|
1388 |
+
"</style>\n",
|
1389 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
1390 |
+
" <thead>\n",
|
1391 |
+
" <tr style=\"text-align: right;\">\n",
|
1392 |
+
" <th>Pred</th>\n",
|
1393 |
+
" <th>LAYING</th>\n",
|
1394 |
+
" <th>SITTING</th>\n",
|
1395 |
+
" <th>STANDING</th>\n",
|
1396 |
+
" <th>WALKING</th>\n",
|
1397 |
+
" <th>WALKING_DOWNSTAIRS</th>\n",
|
1398 |
+
" <th>WALKING_UPSTAIRS</th>\n",
|
1399 |
+
" </tr>\n",
|
1400 |
+
" <tr>\n",
|
1401 |
+
" <th>True</th>\n",
|
1402 |
+
" <th></th>\n",
|
1403 |
+
" <th></th>\n",
|
1404 |
+
" <th></th>\n",
|
1405 |
+
" <th></th>\n",
|
1406 |
+
" <th></th>\n",
|
1407 |
+
" <th></th>\n",
|
1408 |
+
" </tr>\n",
|
1409 |
+
" </thead>\n",
|
1410 |
+
" <tbody>\n",
|
1411 |
+
" <tr>\n",
|
1412 |
+
" <th>LAYING</th>\n",
|
1413 |
+
" <td>537</td>\n",
|
1414 |
+
" <td>0</td>\n",
|
1415 |
+
" <td>0</td>\n",
|
1416 |
+
" <td>0</td>\n",
|
1417 |
+
" <td>0</td>\n",
|
1418 |
+
" <td>0</td>\n",
|
1419 |
+
" </tr>\n",
|
1420 |
+
" <tr>\n",
|
1421 |
+
" <th>SITTING</th>\n",
|
1422 |
+
" <td>0</td>\n",
|
1423 |
+
" <td>407</td>\n",
|
1424 |
+
" <td>82</td>\n",
|
1425 |
+
" <td>0</td>\n",
|
1426 |
+
" <td>0</td>\n",
|
1427 |
+
" <td>2</td>\n",
|
1428 |
+
" </tr>\n",
|
1429 |
+
" <tr>\n",
|
1430 |
+
" <th>STANDING</th>\n",
|
1431 |
+
" <td>0</td>\n",
|
1432 |
+
" <td>94</td>\n",
|
1433 |
+
" <td>438</td>\n",
|
1434 |
+
" <td>0</td>\n",
|
1435 |
+
" <td>0</td>\n",
|
1436 |
+
" <td>0</td>\n",
|
1437 |
+
" </tr>\n",
|
1438 |
+
" <tr>\n",
|
1439 |
+
" <th>WALKING</th>\n",
|
1440 |
+
" <td>0</td>\n",
|
1441 |
+
" <td>0</td>\n",
|
1442 |
+
" <td>0</td>\n",
|
1443 |
+
" <td>447</td>\n",
|
1444 |
+
" <td>46</td>\n",
|
1445 |
+
" <td>3</td>\n",
|
1446 |
+
" </tr>\n",
|
1447 |
+
" <tr>\n",
|
1448 |
+
" <th>WALKING_DOWNSTAIRS</th>\n",
|
1449 |
+
" <td>0</td>\n",
|
1450 |
+
" <td>0</td>\n",
|
1451 |
+
" <td>0</td>\n",
|
1452 |
+
" <td>1</td>\n",
|
1453 |
+
" <td>419</td>\n",
|
1454 |
+
" <td>0</td>\n",
|
1455 |
+
" </tr>\n",
|
1456 |
+
" <tr>\n",
|
1457 |
+
" <th>WALKING_UPSTAIRS</th>\n",
|
1458 |
+
" <td>0</td>\n",
|
1459 |
+
" <td>2</td>\n",
|
1460 |
+
" <td>0</td>\n",
|
1461 |
+
" <td>1</td>\n",
|
1462 |
+
" <td>27</td>\n",
|
1463 |
+
" <td>441</td>\n",
|
1464 |
+
" </tr>\n",
|
1465 |
+
" </tbody>\n",
|
1466 |
+
"</table>\n",
|
1467 |
+
"</div>\n",
|
1468 |
+
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-af1f7d68-479a-4380-870e-0e82389a4389')\"\n",
|
1469 |
+
" title=\"Convert this dataframe to an interactive table.\"\n",
|
1470 |
+
" style=\"display:none;\">\n",
|
1471 |
+
" \n",
|
1472 |
+
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
|
1473 |
+
" width=\"24px\">\n",
|
1474 |
+
" <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
|
1475 |
+
" <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
|
1476 |
+
" </svg>\n",
|
1477 |
+
" </button>\n",
|
1478 |
+
" \n",
|
1479 |
+
" <style>\n",
|
1480 |
+
" .colab-df-container {\n",
|
1481 |
+
" display:flex;\n",
|
1482 |
+
" flex-wrap:wrap;\n",
|
1483 |
+
" gap: 12px;\n",
|
1484 |
+
" }\n",
|
1485 |
+
"\n",
|
1486 |
+
" .colab-df-convert {\n",
|
1487 |
+
" background-color: #E8F0FE;\n",
|
1488 |
+
" border: none;\n",
|
1489 |
+
" border-radius: 50%;\n",
|
1490 |
+
" cursor: pointer;\n",
|
1491 |
+
" display: none;\n",
|
1492 |
+
" fill: #1967D2;\n",
|
1493 |
+
" height: 32px;\n",
|
1494 |
+
" padding: 0 0 0 0;\n",
|
1495 |
+
" width: 32px;\n",
|
1496 |
+
" }\n",
|
1497 |
+
"\n",
|
1498 |
+
" .colab-df-convert:hover {\n",
|
1499 |
+
" background-color: #E2EBFA;\n",
|
1500 |
+
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
|
1501 |
+
" fill: #174EA6;\n",
|
1502 |
+
" }\n",
|
1503 |
+
"\n",
|
1504 |
+
" [theme=dark] .colab-df-convert {\n",
|
1505 |
+
" background-color: #3B4455;\n",
|
1506 |
+
" fill: #D2E3FC;\n",
|
1507 |
+
" }\n",
|
1508 |
+
"\n",
|
1509 |
+
" [theme=dark] .colab-df-convert:hover {\n",
|
1510 |
+
" background-color: #434B5C;\n",
|
1511 |
+
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
|
1512 |
+
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
|
1513 |
+
" fill: #FFFFFF;\n",
|
1514 |
+
" }\n",
|
1515 |
+
" </style>\n",
|
1516 |
+
"\n",
|
1517 |
+
" <script>\n",
|
1518 |
+
" const buttonEl =\n",
|
1519 |
+
" document.querySelector('#df-af1f7d68-479a-4380-870e-0e82389a4389 button.colab-df-convert');\n",
|
1520 |
+
" buttonEl.style.display =\n",
|
1521 |
+
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
|
1522 |
+
"\n",
|
1523 |
+
" async function convertToInteractive(key) {\n",
|
1524 |
+
" const element = document.querySelector('#df-af1f7d68-479a-4380-870e-0e82389a4389');\n",
|
1525 |
+
" const dataTable =\n",
|
1526 |
+
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
|
1527 |
+
" [key], {});\n",
|
1528 |
+
" if (!dataTable) return;\n",
|
1529 |
+
"\n",
|
1530 |
+
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
|
1531 |
+
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
|
1532 |
+
" + ' to learn more about interactive tables.';\n",
|
1533 |
+
" element.innerHTML = '';\n",
|
1534 |
+
" dataTable['output_type'] = 'display_data';\n",
|
1535 |
+
" await google.colab.output.renderOutput(dataTable, element);\n",
|
1536 |
+
" const docLink = document.createElement('div');\n",
|
1537 |
+
" docLink.innerHTML = docLinkHtml;\n",
|
1538 |
+
" element.appendChild(docLink);\n",
|
1539 |
+
" }\n",
|
1540 |
+
" </script>\n",
|
1541 |
+
" </div>\n",
|
1542 |
+
" </div>\n",
|
1543 |
+
" "
|
1544 |
+
]
|
1545 |
+
},
|
1546 |
+
"metadata": {},
|
1547 |
+
"execution_count": 64
|
1548 |
+
}
|
1549 |
+
]
|
1550 |
+
},
|
1551 |
+
{
|
1552 |
+
"cell_type": "code",
|
1553 |
+
"source": [
|
1554 |
+
"score = model.evaluate(X_test, y_test)\n",
|
1555 |
+
"\n",
|
1556 |
+
"print(\"\\n categorical_crossentropy || accuracy \")\n",
|
1557 |
+
"print(\" ____________________________________\")\n",
|
1558 |
+
"print(score)"
|
1559 |
+
],
|
1560 |
+
"metadata": {
|
1561 |
+
"colab": {
|
1562 |
+
"base_uri": "https://localhost:8080/"
|
1563 |
+
},
|
1564 |
+
"id": "N7i84QfGW9Er",
|
1565 |
+
"outputId": "18a8a70f-2896-45a2-a22b-cef28557cfe3"
|
1566 |
+
},
|
1567 |
+
"execution_count": null,
|
1568 |
+
"outputs": [
|
1569 |
+
{
|
1570 |
+
"output_type": "stream",
|
1571 |
+
"name": "stdout",
|
1572 |
+
"text": [
|
1573 |
+
"93/93 [==============================] - 1s 9ms/step - loss: 0.3802 - accuracy: 0.9125\n",
|
1574 |
+
"\n",
|
1575 |
+
" categorical_crossentropy || accuracy \n",
|
1576 |
+
" ____________________________________\n",
|
1577 |
+
"[0.38021206855773926, 0.9124533534049988]\n"
|
1578 |
+
]
|
1579 |
+
}
|
1580 |
+
]
|
1581 |
+
}
|
1582 |
+
]
|
1583 |
+
}
|