<jupyter_start><jupyter_text><jupyter_code>datalink = 'https://github.com/ttruongatl/ml-lstm/blob/master/data/29297294_02-08-2019.csv'

!wget --no-check-certificate \
    https://raw.githubusercontent.com/ttruongatl/ml-lstm/master/data/mil-week-labeled-train.csv \
    -O /tmp/mil-week-labeled-train.csv
!pip install tensorflow==2.0.0
import json
import tensorflow as tf
import csv
import random
import numpy as np

from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.utils import to_categorical
from tensorflow.keras import regularizers
num_sentences = 0
datetimes = []
datas = []
labels = []
colume = [1,2,3,4,5,6,7,8]
header = ['timestamp', 'CylinderBorePressure', 'CylinderRodPressure', 'RotationCWPressure', 'RotationCCWPressure', 'MotionX', 'MotionY', 'MotionZ', 'StringPotV', 'event', 'start']
label_dict = {}
count = 0
with open("/tmp/mil-week-labeled-train.csv") as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    next(reader)
    for row in reader:
      data = []
      for col in colume:
        data.append(float(row[col]))
      datas.append(data)
      datetimes.append(row[0])
      labels.append(row[9])
      if row[9] not in label_dict:
        label_dict[row[9]] = count
        count += 1
datas = np.array(datas)
labels = np.array(list(label_dict[x] for x in labels))
print(label_dict)
print(datas.shape)
def windowed_dataset(data_in, windows = 20):
  data_extend = np.zeros((data_in.shape[0] + windows, data_in.shape[1]))
  data_extend[windows:,:] = data_in[:,:]
  data_extend = data_extend.T
  data_out = np.zeros((data_in.shape[1], data_in.shape[0], windows))
  print(data_extend.shape)
  print(data_out.shape)
  for i in range(data_in.shape[0]):  
    data_out[:,i,:] = data_extend[:,i:i+windows]
  return data_out
from keras.utils import to_categorical

window_size = 40
n_samples = 10000

datas_new = windowed_dataset(datas, windows = window_size)
data_samples = datas_new[:, :n_samples, :] #Get 20 k
labels = labels[:n_samples]

split_time = int(n_samples *0.8)

#max_X = np.max(datas_new)
X_train = data_samples[:,:split_time,:]
y_train = to_categorical(labels[:split_time])

X_valid = data_samples[:,split_time:,:]
y_valid = to_categorical(labels[split_time:])


X_train = np.transpose(X_train, (1, 2, 0)) / 6000
X_valid = np.transpose(X_valid, (1, 2, 0)) / 6000

print(X_train.shape)
print(X_valid.shape)

print(X_train.shape)
print(X_valid.shape)
np.max(X_train)


def separate_block(input_tensor, n_LSTMs = 16, n_Dense = 16):
  x = tf.keras.layers.Lambda(lambda x: tf.expand_dims(x, axis=-1))(input_tensor)
  x = tf.keras.layers.Conv1D(n_LSTMs, 5, activation = 'relu') (x) # test with Conv1D
# x = tf.keras.layers.Dropout(0.2)(x)
  x = tf.keras.layers.LSTM(n_LSTMs, return_sequences=True)(x)
  x = tf.keras.layers.LSTM(n_LSTMs)(x)
  x = tf.keras.layers.Dense(n_Dense, activation="relu")(x)
  return x
class myCallback(tf.keras.callbacks.Callback):
  def on_epoch_end(self, epoch, logs={}):
    if(logs.get('acc')>0.99 and logs.get('val_acc')>0.95):
      print("\nReached 99% accuracy so cancelling training!")
      self.model.stop_training = True
tf.keras.backend.clear_session()
#tf.random.set_seed(51)
#np.random.seed(51)
#window_size = 64
#batch_size = 256
window_size = 40
batch_size = 32
epochs = 200
callback = myCallback()


input_data = tf.keras.layers.Input(shape = [None, 8])
print(input_data)

x0 = separate_block(input_data[:,:, 0], 32, 32)
x1 = separate_block(input_data[:,:, 1], 32, 32)
x2 = separate_block(input_data[:,:, 2], 32, 32)
x3 = separate_block(input_data[:,:, 3], 32, 32)
x4 = separate_block(input_data[:,:, 4], 16, 16)
x5 = separate_block(input_data[:,:, 5], 16, 16)
x6 = separate_block(input_data[:,:, 6], 16, 16)
x7 = separate_block(input_data[:,:, 7], 16, 16)
x = tf.keras.layers.concatenate([x0, x1, x2, x3, x4, x5, x6, x7])
x = tf.keras.layers.Dense(128, activation="relu")(x)
x = tf.keras.layers.Dense(3, activation = 'softmax')(x)

model = tf.keras.models.Model(input_data, x)

#model = tf.keras.models.Sequential([
#  tf.keras.layers.Lambda(lambda x: tf.expand_dims(x, axis=-1),
#                      input_shape=[None]),
#  tf.keras.layers.Conv1D(32, 5, activation = 'relu'),  # test with Conv1D
#  tf.keras.layers.Dropout(0.2),
#  tf.keras.layers.LSTM(32, return_sequences=True),
#  tf.keras.layers.LSTM(32),
#  tf.keras.layers.Dense(16, activation="relu"),
#  tf.keras.layers.Dense(3, activation = 'softmax')
#])
model.summary()
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=["acc"])
history = model.fit(X_train, y_train, epochs=epochs, batch_size= batch_size,
                    validation_data=(X_valid, y_valid), verbose = 1, callbacks = [callback])
import matplotlib.pyplot as plt
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(len(acc))

plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()

plt.plot(epochs, loss, 'r', label='Training Loss')
plt.plot(epochs, val_loss, 'b', label='Validation Loss')
plt.title('Training and validation loss')
plt.legend()

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16334883720930232, 'fraction_numerical': 0.03293023255813954, 'mean_word_length': 4.92723263506064, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '540184', 'license': 'MIT', 'n_tokens_mistral': 2212, 'n_tokens_neox': 2067, 'n_words': 391, 'path': 'khiem_test_LSTMs.ipynb', 'repo_name': 'dnkhiem/course-resources-ml-with-experts-budgets', 'size': '5375'}