from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow import keras
def plot_learning_curves(loss, val_loss):
plt.plot(np.arange(len(loss)) + 0.5, loss, "b.-", label="Training loss")
plt.plot(np.arange(len(val_loss)) + 1, val_loss, "r.-", label="Validation loss")
plt.gca().xaxis.set_major_locator(mpl.ticker.MaxNLocator(integer=True))
plt.axis([1, 20, 0, 0.05])
plt.legend(fontsize=14)
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.grid(True)
training_data_sets
training_data_store = pd.read_csv('cut_training_data_file/cut_training_data.csv')
training_data_store.head()
ax | ay | az | gx | gy | gz | class_ | |
---|---|---|---|---|---|---|---|
0 | 3.536667 | -8.764157 | 8.219295 | -0.426663 | 0.376293 | 0.468237 | running |
1 | 3.972804 | -12.131095 | 11.188315 | -0.133465 | -0.126467 | 0.777247 | running |
2 | 3.418738 | -14.802985 | 12.352308 | 0.195966 | -0.441429 | 0.986390 | running |
3 | 1.820551 | -16.844654 | 8.020269 | 0.370743 | -0.536392 | 1.180035 | running |
4 | 0.771038 | -16.254522 | 3.320959 | -0.066549 | -0.279340 | 1.371567 | running |
pd.api.types.is_string_dtype(training_data_store['class_'])
True
for label, content in training_data_store.items():
if pd.api.types.is_string_dtype(content):
print(label)
class_
for label, content in training_data_store.items():
if pd.api.types.is_string_dtype(content):
training_data_store[label] = content.astype('category').cat.as_ordered()
training_data_store['class_'].cat.codes
0 2 1 2 2 2 3 2 4 2 .. 17820 0 17821 0 17822 0 17823 0 17824 0 Length: 17825, dtype: int8
training_data_store['class_'] = training_data_store['class_'].cat.codes
training_data_store
ax | ay | az | gx | gy | gz | class_ | |
---|---|---|---|---|---|---|---|
0 | 3.536667 | -8.764157 | 8.219295 | -0.426663 | 0.376293 | 0.468237 | 2 |
1 | 3.972804 | -12.131095 | 11.188315 | -0.133465 | -0.126467 | 0.777247 | 2 |
2 | 3.418738 | -14.802985 | 12.352308 | 0.195966 | -0.441429 | 0.986390 | 2 |
3 | 1.820551 | -16.844654 | 8.020269 | 0.370743 | -0.536392 | 1.180035 | 2 |
4 | 0.771038 | -16.254522 | 3.320959 | -0.066549 | -0.279340 | 1.371567 | 2 |
... | ... | ... | ... | ... | ... | ... | ... |
17820 | -4.225421 | -7.347326 | 2.834419 | 1.315053 | 0.989951 | 0.788295 | 0 |
17821 | -3.888998 | -6.893213 | 2.058209 | 1.380154 | 1.288926 | 0.862489 | 0 |
17822 | -4.683547 | -6.589226 | 1.510789 | 1.444906 | 1.497039 | 0.967244 | 0 |
17823 | -5.238657 | -6.579218 | 0.651558 | 1.428796 | 1.569016 | 1.079469 | 0 |
17824 | -5.052723 | -6.786039 | -0.916491 | 1.325403 | 1.399684 | 1.295959 | 0 |
17825 rows × 7 columns
X = training_data_store.drop(['class_'], axis='columns')
y = np.ravel(training_data_store['class_'])
X_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size=0.4, random_state=0)
X_valid, X_test, y_valid, y_test = train_test_split(X_temp, y_temp, test_size=0.5, random_state=0)
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler().fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
import tensorflow as tf
from keras.models import Sequential
import pandas as pd
from keras.layers import Dense
model = Sequential()
model.add(Dense(8, activation='relu', input_shape=(None, 1,6)))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
history = model.fit(X_train, y_train,epochs=8, batch_size=1, verbose=1)
Epoch 1/8 WARNING:tensorflow:Model was constructed with shape (None, None, 1, 6) for input KerasTensor(type_spec=TensorSpec(shape=(None, None, 1, 6), dtype=tf.float32, name='dense_59_input'), name='dense_59_input', description="created by layer 'dense_59_input'"), but it was called on an input with incompatible shape (1, 6). WARNING:tensorflow:Model was constructed with shape (None, None, 1, 6) for input KerasTensor(type_spec=TensorSpec(shape=(None, None, 1, 6), dtype=tf.float32, name='dense_59_input'), name='dense_59_input', description="created by layer 'dense_59_input'"), but it was called on an input with incompatible shape (1, 6). 10695/10695 [==============================] - 9s 777us/step - loss: 654609.3750 - accuracy: 0.2510 Epoch 2/8 10695/10695 [==============================] - 9s 827us/step - loss: -37.6062 - accuracy: 0.2511 Epoch 3/8 10695/10695 [==============================] - 9s 880us/step - loss: -62.5471 - accuracy: 0.2511 Epoch 4/8 10695/10695 [==============================] - 11s 1ms/step - loss: -87.4949 - accuracy: 0.2511 Epoch 5/8 10695/10695 [==============================] - 10s 954us/step - loss: -112.4436 - accuracy: 0.2511 Epoch 6/8 10695/10695 [==============================] - 9s 846us/step - loss: -137.3822 - accuracy: 0.2511 Epoch 7/8 10695/10695 [==============================] - 9s 865us/step - loss: -162.3128 - accuracy: 0.2511 Epoch 8/8 10695/10695 [==============================] - 9s 834us/step - loss: -187.2427 - accuracy: 0.2511
<keras.callbacks.History at 0x1d2658156c0>
plot_learning_curves(history.history["loss"], history.history["val_loss"])
plt.show()
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import Dropout
X = training_data_store.drop(['class_'], axis='columns')
y = np.ravel(training_data_store['class_'])
X_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size=0.4, random_state=0)
X_valid, X_test, y_valid, y_test = train_test_split(X_temp, y_temp, test_size=0.5, random_state=0)
x_train, y_train = X_train , y_train
x_test, y_test = X_test, y_test
batch_size = 64
epochs = 5
x_train.shape, y_train.shape
((10695, 6), (10695,))
model = Sequential()
model.add(keras.Input(shape=(6,1)))
model.add(layers.SimpleRNN(128,return_sequences=False, activation='relu')) # N, 128
model.add(layers.Dense(10))
# model.add(LSTM(units = 50, return_sequences=True, input_shape=(x_train.shape[1], 1)))
# loss and optimizer
loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True)
optim = keras.optimizers.Adam(learning_rate=0.001)
metrics = ['accuracy']
model.compile(loss=loss, optimizer=optim, metrics=metrics)
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=2)
Epoch 1/5 168/168 - 1s - loss: 1.0913 - accuracy: 0.5783 - 1s/epoch - 7ms/step Epoch 2/5 168/168 - 0s - loss: 0.7169 - accuracy: 0.7406 - 402ms/epoch - 2ms/step Epoch 3/5 168/168 - 0s - loss: 0.5904 - accuracy: 0.7821 - 355ms/epoch - 2ms/step Epoch 4/5 168/168 - 0s - loss: 0.5399 - accuracy: 0.8013 - 359ms/epoch - 2ms/step Epoch 5/5 168/168 - 0s - loss: 0.5042 - accuracy: 0.8162 - 410ms/epoch - 2ms/step
<keras.callbacks.History at 0x1d265922290>
model.evaluate(x_test, y_test, batch_size=batch_size, verbose=2)
56/56 - 0s - loss: 0.4919 - accuracy: 0.8208 - 249ms/epoch - 4ms/step
[0.49192091822624207, 0.8207573890686035]
model = keras.models.Sequential()
model.add(keras.Input(shape=(6, 1))) # seq_length, input_size
# model.add(layers.SimpleRNN(128,return_sequences=True, activation='relu')) # N, 28, 128
model.add(layers.GRU(128,return_sequences=False, activation='relu')) # N, 128
model.add(layers.Dense(10))
# loss and optimizer
loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True)
optim = keras.optimizers.Adam(learning_rate=0.001)
metrics = ['accuracy']
model.compile(loss=loss, optimizer=optim, metrics=metrics)
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=2)
# evaluate
model.evaluate(x_test, y_test, batch_size=batch_size, verbose=2)
Epoch 1/5 168/168 - 2s - loss: 1.1838 - accuracy: 0.5154 - 2s/epoch - 11ms/step Epoch 2/5 168/168 - 1s - loss: 0.7972 - accuracy: 0.6970 - 778ms/epoch - 5ms/step Epoch 3/5 168/168 - 1s - loss: 0.6352 - accuracy: 0.7640 - 846ms/epoch - 5ms/step Epoch 4/5 168/168 - 1s - loss: 0.5438 - accuracy: 0.8012 - 860ms/epoch - 5ms/step Epoch 5/5 168/168 - 1s - loss: 0.4953 - accuracy: 0.8204 - 796ms/epoch - 5ms/step 56/56 - 0s - loss: 0.5216 - accuracy: 0.8098 - 337ms/epoch - 6ms/step
[0.521604061126709, 0.8098176717758179]
model = keras.models.Sequential()
model.add(keras.Input(shape=(6, 1))) # seq_length, input_size
# model.add(layers.SimpleRNN(128,return_sequences=True, activation='relu')) # N, 28, 128
model.add(layers.LSTM(128,return_sequences=False, activation='relu')) # N, 128
model.add(layers.Dense(10))
# loss and optimizer
loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True)
optim = keras.optimizers.Adam(learning_rate=0.001)
metrics = ['accuracy']
model.compile(loss=loss, optimizer=optim, metrics=metrics)
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=2)
# evaluate
model.evaluate(x_test, y_test, batch_size=batch_size, verbose=2)
Epoch 1/5 168/168 - 2s - loss: 1.2294 - accuracy: 0.4914 - 2s/epoch - 11ms/step Epoch 2/5 168/168 - 1s - loss: 0.9171 - accuracy: 0.6347 - 879ms/epoch - 5ms/step Epoch 3/5 168/168 - 1s - loss: 0.7524 - accuracy: 0.7096 - 981ms/epoch - 6ms/step Epoch 4/5 168/168 - 1s - loss: 0.6591 - accuracy: 0.7525 - 917ms/epoch - 5ms/step Epoch 5/5 168/168 - 1s - loss: 0.6043 - accuracy: 0.7765 - 909ms/epoch - 5ms/step 56/56 - 0s - loss: 0.5745 - accuracy: 0.7835 - 334ms/epoch - 6ms/step
[0.5744776725769043, 0.783450186252594]
model = tf.keras.Sequential([
tf.keras.layers.Dense(16, activation='relu'), # if x <= 0, --> 0, x > 0 --> x
tf.keras.layers.Dense(16, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
loss=tf.keras.losses.BinaryCrossentropy(),
metrics=['accuracy'])
model.evaluate(X_train, y_train)
335/335 [==============================] - 0s 809us/step - loss: 8.8606 - accuracy: 0.2745
[8.860629081726074, 0.2745208144187927]
model.evaluate(X_valid, y_valid)
112/112 [==============================] - 0s 1ms/step - loss: 8.8132 - accuracy: 0.2805
[8.813237190246582, 0.28050491213798523]
model.fit(X_train, y_train, batch_size=16, epochs=20, validation_data=(X_valid, y_valid))
Epoch 1/20 669/669 [==============================] - 4s 6ms/step - loss: -235.1996 - accuracy: 0.2428 - val_loss: -1019.5363 - val_accuracy: 0.2496 Epoch 2/20 669/669 [==============================] - 1s 1ms/step - loss: -4164.0693 - accuracy: 0.2443 - val_loss: -8750.5283 - val_accuracy: 0.2415 Epoch 3/20 669/669 [==============================] - 1s 1ms/step - loss: -17297.6328 - accuracy: 0.2389 - val_loss: -27263.3320 - val_accuracy: 0.2384 Epoch 4/20 669/669 [==============================] - 1s 1ms/step - loss: -42248.3164 - accuracy: 0.2367 - val_loss: -58239.5977 - val_accuracy: 0.2359 Epoch 5/20 669/669 [==============================] - 1s 1ms/step - loss: -80429.0391 - accuracy: 0.2379 - val_loss: -102664.6016 - val_accuracy: 0.2432 Epoch 6/20 669/669 [==============================] - 1s 1ms/step - loss: -132948.6719 - accuracy: 0.2433 - val_loss: -161977.0156 - val_accuracy: 0.2457 Epoch 7/20 669/669 [==============================] - 1s 1ms/step - loss: -201032.5938 - accuracy: 0.2434 - val_loss: -237127.0938 - val_accuracy: 0.2446 Epoch 8/20 669/669 [==============================] - 1s 1ms/step - loss: -285669.6875 - accuracy: 0.2455 - val_loss: -328911.4688 - val_accuracy: 0.2435 Epoch 9/20 669/669 [==============================] - 1s 1ms/step - loss: -387987.5625 - accuracy: 0.2453 - val_loss: -438936.5000 - val_accuracy: 0.2435 Epoch 10/20 669/669 [==============================] - 1s 1ms/step - loss: -508355.7812 - accuracy: 0.2449 - val_loss: -566644.9375 - val_accuracy: 0.2424 Epoch 11/20 669/669 [==============================] - 1s 1ms/step - loss: -649278.3750 - accuracy: 0.2449 - val_loss: -715668.8750 - val_accuracy: 0.2449 Epoch 12/20 669/669 [==============================] - 1s 1ms/step - loss: -811134.1250 - accuracy: 0.2456 - val_loss: -885734.7500 - val_accuracy: 0.2496 Epoch 13/20 669/669 [==============================] - 1s 1ms/step - loss: -994793.5625 - accuracy: 0.2453 - val_loss: -1077977.1250 - val_accuracy: 0.2496 Epoch 14/20 669/669 [==============================] - 1s 1ms/step - loss: -1200795.3750 - accuracy: 0.2459 - val_loss: -1291846.5000 - val_accuracy: 0.2454 Epoch 15/20 669/669 [==============================] - 1s 1ms/step - loss: -1430086.2500 - accuracy: 0.2457 - val_loss: -1530435.3750 - val_accuracy: 0.2454 Epoch 16/20 669/669 [==============================] - 1s 1ms/step - loss: -1684317.0000 - accuracy: 0.2459 - val_loss: -1793811.7500 - val_accuracy: 0.2452 Epoch 17/20 669/669 [==============================] - 1s 1ms/step - loss: -1967338.5000 - accuracy: 0.2460 - val_loss: -2087851.1250 - val_accuracy: 0.2460 Epoch 18/20 669/669 [==============================] - 1s 1ms/step - loss: -2279071.5000 - accuracy: 0.2464 - val_loss: -2408506.0000 - val_accuracy: 0.2452 Epoch 19/20 669/669 [==============================] - 1s 1ms/step - loss: -2620246.5000 - accuracy: 0.2453 - val_loss: -2758454.7500 - val_accuracy: 0.2471 Epoch 20/20 669/669 [==============================] - 1s 1ms/step - loss: -2989409.2500 - accuracy: 0.2468 - val_loss: -3137055.0000 - val_accuracy: 0.2482
<keras.callbacks.History at 0x1d26d9c8c10>
model.evaluate(X_test, y_test)
112/112 [==============================] - 0s 906us/step - loss: -3185349.7500 - accuracy: 0.2494
[-3185349.75, 0.24936886131763458]
testing_data_sets
testing_data_store = pd.read_csv('cut_testing_data_file/cut_testing_data.csv')
testing_data_store.head()
ax | ay | az | gx | gy | gz | class_ | |
---|---|---|---|---|---|---|---|
0 | -1.303614 | -6.411490 | 4.077556 | -0.421829 | -0.733195 | -0.274802 | running |
1 | -0.998066 | -6.549138 | 4.557804 | -0.496127 | -0.755675 | -0.248011 | running |
2 | -0.687327 | -6.553696 | 4.994870 | -0.511329 | -0.774472 | -0.243334 | running |
3 | -0.329969 | -6.566999 | 6.440067 | -0.487907 | -0.773408 | -0.255150 | running |
4 | -0.195836 | -6.753056 | 6.938913 | -0.338978 | -0.745797 | -0.273982 | running |
pd.api.types.is_string_dtype(testing_data_store['class_'])
True
for label, content in testing_data_store.items():
if pd.api.types.is_string_dtype(content):
print(label)
class_
for label, content in testing_data_store.items():
if pd.api.types.is_string_dtype(content):
testing_data_store[label] = content.astype('category').cat.as_ordered()
testing_data_store['class_'].cat.codes
0 2 1 2 2 2 3 2 4 2 .. 3595 0 3596 0 3597 0 3598 0 3599 0 Length: 3600, dtype: int8
testing_data_store['class_'] = testing_data_store['class_'].cat.codes
X = testing_data_store.drop(['class_'], axis='columns')
y = np.ravel(testing_data_store['class_'])
X_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size=0.4, random_state=0)
X_valid, X_test, y_valid, y_test = train_test_split(X_temp, y_temp, test_size=0.5, random_state=0)
x_train, y_train = X_train , y_train
x_test, y_test = X_test, y_test
batch_size = 64
epochs = 5
x_train.shape, y_train.shape
((2160, 6), (2160,))
model = Sequential()
model.add(keras.Input(shape=(6,1)))
model.add(layers.SimpleRNN(128,return_sequences=False, activation='relu')) # N, 128
model.add(layers.Dense(10))
# loss and optimizer
loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True)
optim = keras.optimizers.Adam(learning_rate=0.001)
metrics = ['accuracy']
model.compile(loss=loss, optimizer=optim, metrics=metrics)
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=2)
model.evaluate(x_test, y_test, batch_size=batch_size, verbose=2)
Epoch 1/5 34/34 - 1s - loss: 1.3755 - accuracy: 0.4579 - 689ms/epoch - 20ms/step Epoch 2/5 34/34 - 0s - loss: 1.0177 - accuracy: 0.5944 - 101ms/epoch - 3ms/step Epoch 3/5 34/34 - 0s - loss: 0.9299 - accuracy: 0.6514 - 92ms/epoch - 3ms/step Epoch 4/5 34/34 - 0s - loss: 0.8803 - accuracy: 0.6662 - 110ms/epoch - 3ms/step Epoch 5/5 34/34 - 0s - loss: 0.8263 - accuracy: 0.6972 - 92ms/epoch - 3ms/step 12/12 - 0s - loss: 0.8438 - accuracy: 0.6694 - 178ms/epoch - 15ms/step
[0.8437779545783997, 0.6694444417953491]
model = keras.models.Sequential()
model.add(keras.Input(shape=(6, 1))) # seq_length, input_size
# model.add(layers.SimpleRNN(128,return_sequences=True, activation='relu')) # N, 28, 128
model.add(layers.GRU(128,return_sequences=False, activation='relu')) # N, 128
model.add(layers.Dense(10))
# loss and optimizer
loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True)
optim = keras.optimizers.Adam(learning_rate=0.001)
metrics = ['accuracy']
model.compile(loss=loss, optimizer=optim, metrics=metrics)
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=2)
# evaluate
model.evaluate(x_test, y_test, batch_size=batch_size, verbose=2)
Epoch 1/5 34/34 - 1s - loss: 1.7664 - accuracy: 0.3546 - 1s/epoch - 37ms/step Epoch 2/5 34/34 - 0s - loss: 1.1162 - accuracy: 0.4995 - 192ms/epoch - 6ms/step Epoch 3/5 34/34 - 0s - loss: 0.9772 - accuracy: 0.5940 - 195ms/epoch - 6ms/step Epoch 4/5 34/34 - 0s - loss: 0.9165 - accuracy: 0.6315 - 194ms/epoch - 6ms/step Epoch 5/5 34/34 - 0s - loss: 0.8649 - accuracy: 0.6579 - 207ms/epoch - 6ms/step 12/12 - 0s - loss: 0.8343 - accuracy: 0.6625 - 245ms/epoch - 20ms/step
[0.8342746496200562, 0.6625000238418579]
model = keras.models.Sequential()
model.add(keras.Input(shape=(6, 1))) # seq_length, input_size
# model.add(layers.SimpleRNN(128,return_sequences=True, activation='relu')) # N, 28, 128
model.add(layers.LSTM(128,return_sequences=False, activation='relu')) # N, 128
model.add(layers.Dense(10))
# loss and optimizer
loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True)
optim = keras.optimizers.Adam(learning_rate=0.001)
metrics = ['accuracy']
model.compile(loss=loss, optimizer=optim, metrics=metrics)
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=2)
# evaluate
model.evaluate(x_test, y_test, batch_size=batch_size, verbose=2)
Epoch 1/5 34/34 - 1s - loss: 1.6878 - accuracy: 0.3597 - 1s/epoch - 34ms/step Epoch 2/5 34/34 - 0s - loss: 1.1969 - accuracy: 0.4644 - 213ms/epoch - 6ms/step Epoch 3/5 34/34 - 0s - loss: 1.0142 - accuracy: 0.5681 - 212ms/epoch - 6ms/step Epoch 4/5 34/34 - 0s - loss: 0.9779 - accuracy: 0.6014 - 220ms/epoch - 6ms/step Epoch 5/5 34/34 - 0s - loss: 0.9279 - accuracy: 0.6352 - 242ms/epoch - 7ms/step 12/12 - 0s - loss: 0.9007 - accuracy: 0.6472 - 222ms/epoch - 19ms/step
[0.9006998538970947, 0.6472222208976746]