Text níže je vybrán, stiskněte Ctrl+C pro zkopírování do schránky. (⌘+C na Mac) Nebudou zkopírována žádná čísla řádků.
Guest
Keras machine learning
Od Guest on 18th September 2022 12:56:07 PM | Syntaxe: PYTHON | Zobrazeno: 18



Nový kód/text | Stáhnout | Zobrazit/Skrýt čísla řádků | Zkopírujte text do schránky
  1. import numpy as np
  2. import pandas as pd
  3. from tensorflow import keras
  4. from tensorflow.keras import layers
  5. from matplotlib import pyplot as plt
  6.  
  7. master_url_root = "https://raw.githubusercontent.com/numenta/NAB/master/data/"
  8.  
  9. df_small_noise_url_suffix = "artificialNoAnomaly/art_daily_small_noise.csv"
  10. df_small_noise_url = master_url_root + df_small_noise_url_suffix
  11. df_small_noise = pd.read_csv(
  12.     df_small_noise_url, parse_dates=True, index_col="timestamp"
  13. )
  14.  
  15. df_daily_jumpsup_url_suffix = "artificialWithAnomaly/art_daily_jumpsup.csv"
  16. df_daily_jumpsup_url = master_url_root + df_daily_jumpsup_url_suffix
  17. df_daily_jumpsup = pd.read_csv(
  18.     df_daily_jumpsup_url, parse_dates=True, index_col="timestamp"
  19. )
  20.  
  21. print(df_small_noise.head())
  22.  
  23. print(df_daily_jumpsup.head())
  24.  
  25. fig, ax = plt.subplots()
  26. df_small_noise.plot(legend=False, ax=ax)
  27. plt.show()
  28.  
  29. fig, ax = plt.subplots()
  30. df_daily_jumpsup.plot(legend=False, ax=ax)
  31. plt.show()
  32.  
  33. # Normalize and save the mean and std we get,
  34. # for normalizing test data.
  35. training_mean = df_small_noise.mean()
  36. training_std = df_small_noise.std()
  37. df_training_value = (df_small_noise - training_mean) / training_std
  38. print("Number of training samples:", len(df_training_value))
  39.  
  40. TIME_STEPS = 288
  41.  
  42. # Generated training sequences for use in the model.
  43. def create_sequences(values, time_steps=TIME_STEPS):
  44.     output = []
  45.     for i in range(len(values) - time_steps + 1):
  46.         output.append(values[i : (i + time_steps)])
  47.     return np.stack(output)
  48.  
  49.  
  50. x_train = create_sequences(df_training_value.values)
  51. print("Training input shape: ", x_train.shape)
  52.  
  53. model = keras.Sequential(
  54.     [
  55.         layers.Input(shape=(x_train.shape[1], x_train.shape[2])),
  56.         layers.Conv1D(
  57.             filters=32, kernel_size=7, padding="same", strides=2, activation="relu"
  58.         ),
  59.         layers.Dropout(rate=0.2),
  60.         layers.Conv1D(
  61.             filters=16, kernel_size=7, padding="same", strides=2, activation="relu"
  62.         ),
  63.         layers.Conv1DTranspose(
  64.             filters=16, kernel_size=7, padding="same", strides=2, activation="relu"
  65.         ),
  66.         layers.Dropout(rate=0.2),
  67.         layers.Conv1DTranspose(
  68.             filters=32, kernel_size=7, padding="same", strides=2, activation="relu"
  69.         ),
  70.         layers.Conv1DTranspose(filters=1, kernel_size=7, padding="same"),
  71.     ]
  72. )
  73. model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.001), loss="mse")
  74. model. Summary()
  75.  
  76. history = model.fit(
  77.     x_train,
  78.     x_train,
  79.     epochs=50,
  80.     batch_size=128,
  81.     validation_split=0.1,
  82.     callbacks=[
  83.         keras.callbacks.EarlyStopping(monitor="val_loss", patience=5, mode="min")
  84.     ],
  85. )
  86.  
  87. plt.plot(history.history["loss"], label="Training Loss")
  88. plt.plot(history.history["val_loss"], label="Validation Loss")
  89. plt.legend()
  90. plt.show()
  91.  
  92. # Get train MAE loss.
  93. x_train_pred = model.predict(x_train)
  94. train_mae_loss = np.mean(np.abs(x_train_pred - x_train), axis=1)
  95.  
  96. plt.hist(train_mae_loss, bins=50)
  97. plt.xlabel("Train MAE loss")
  98. plt.ylabel("No of samples")
  99. plt.show()
  100.  
  101. # Get reconstruction loss threshold.
  102. threshold = np.max(train_mae_loss)
  103. print("Reconstruction error threshold: ", threshold)
  104.  
  105. # Checking how the first sequence is learnt
  106. plt.plot(x_train[0])
  107. plt.plot(x_train_pred[0])
  108. plt.show()
  109.  
  110. df_test_value = (df_daily_jumpsup - training_mean) / training_std
  111. fig, ax = plt.subplots()
  112. df_test_value.plot(legend=False, ax=ax)
  113. plt.show()
  114.  
  115. # Create sequences from test values.
  116. x_test = create_sequences(df_test_value.values)
  117. print("Test input shape: ", x_test.shape)
  118.  
  119. # Get test MAE loss.
  120. x_test_pred = model.predict(x_test)
  121. test_mae_loss = np.mean(np.abs(x_test_pred - x_test), axis=1)
  122. test_mae_loss = test_mae_loss.reshape((-1))
  123.  
  124. plt.hist(test_mae_loss, bins=50)
  125. plt.xlabel("test MAE loss")
  126. plt.ylabel("No of samples")
  127. plt.show()
  128.  
  129. # Detect all the samples which are anomalies.
  130. anomalies = test_mae_loss > threshold
  131. print("Number of anomaly samples: ", np.sum(anomalies))
  132. print("Indices of anomaly samples: ", np.where(anomalies))
  133.  
  134. # data i is an anomaly if samples [(i - timesteps + 1) to (i)] are anomalies
  135. anomalous_data_indices = []
  136. for data_idx in range(TIME_STEPS - 1, len(df_test_value) - TIME_STEPS + 1):
  137.     if np.all(anomalies[data_idx - TIME_STEPS + 1 : data_idx]):
  138.         anomalous_data_indices.append(data_idx)
  139.  
  140. df_subset = df_daily_jumpsup.iloc[anomalous_data_indices]
  141. fig, ax = plt.subplots()
  142. df_daily_jumpsup.plot(legend=False, ax=ax)
  143. df_subset.plot(legend=False, ax=ax, color="r")
  144. plt.show()



  • Nedávné příspěvky

Počitadlo návštěv