{ "cells": [ { "cell_type": "markdown", "id": "c1d7ea96-5551-4bfe-885c-cc70bdeee9b9", "metadata": {}, "source": [ "Книга для обучения GCAEC классификатора с использованием Tensorflow из статьи https://gitlab.com/protsenkovi/efd_nn/" ] }, { "cell_type": "code", "execution_count": 1, "id": "c32ace82-fff8-4b9f-8eb9-0945079f3776", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Num GPUs Available: 1\n", "1 Physical GPUs, 1 Logical GPUs\n", "2.12.0\n" ] } ], "source": [ "import os\n", "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' \n", "import random\n", "import tensorflow as tf\n", "# tf.autograph.set_verbosity(1)\n", "# tf.get_logger().setLevel('INFO')\n", "import tensorflow_addons as tfa\n", "from IPython.display import display, clear_output\n", "import seaborn as sns\n", "import pandas as pd\n", "import numpy as np\n", "import warnings\n", "\n", "from tensorflow.keras import regularizers\n", "from tensorflow.keras.callbacks import LearningRateScheduler\n", "# os.environ[\"CUDA_VISIBLE_DEVICES\"]=\"-1\"\n", "from tensorflow.keras.models import Sequential\n", "from tensorflow.keras.layers import Dense, Dropout, Reshape, SimpleRNN, GRU, LSTM, PReLU, MaxPooling1D, Flatten, AveragePooling1D, \\\n", " GaussianNoise\n", "from tensorflow.keras.layers import Conv1D, BatchNormalization\n", "from tensorflow.keras.optimizers import Adam, Adamax, SGD\n", "from tensorflow.keras import losses \n", "from tensorflow.keras import metrics as kmetrics\n", "from tensorflow.keras.saving import load_model\n", "from tensorflow.keras.utils import plot_model\n", "from sklearn.neural_network import MLPClassifier\n", "\n", "warnings.filterwarnings('ignore')\n", "from sklearn.metrics import confusion_matrix\n", "import matplotlib.pyplot as plt\n", "from sklearn.model_selection import train_test_split, StratifiedKFold\n", "from time import time\n", "\n", "np.random.seed(42)\n", "random.seed(42)\n", "clear_output()\n", "\n", "tf.keras.utils.set_random_seed(42)\n", "tf.config.experimental.enable_op_determinism()\n", "print(\"Num GPUs Available: \", len(tf.config.list_physical_devices('GPU')))\n", "\n", "gpus = tf.config.list_physical_devices('GPU')\n", "if gpus:\n", " try:\n", " # Currently, memory growth needs to be the same across GPUs\n", " for gpu in gpus:\n", " tf.config.experimental.set_memory_growth(gpu, True)\n", " logical_gpus = tf.config.list_logical_devices('GPU')\n", " print(len(gpus), \"Physical GPUs,\", len(logical_gpus), \"Logical GPUs\")\n", " except RuntimeError as e:\n", " # Memory growth must be set before GPUs have been initialized\n", " print(e)\n", " \n", "print(tf.__version__)" ] }, { "cell_type": "code", "execution_count": 2, "id": "050e0c0a-629f-41d1-8c9f-9fcf72720bca", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((116600, 24, 6), (29150, 24, 6), (11054, 24, 6))" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "window = 24\n", "\n", "X_train = np.load(f\"../assets/X_train_{window}.npy\")\n", "y_train = np.load(f\"../assets/y_train_{window}.npy\")\n", "X_val = np.load(f\"../assets/X_val_{window}.npy\")\n", "y_val = np.load(f\"../assets/y_val_{window}.npy\")\n", "X_test = np.load(f\"../assets/X_test_{window}.npy\")\n", "y_test = np.load(f\"../assets/y_test_{window}.npy\")\n", "\n", "shuffled_index = np.arange(X_train.shape[0])\n", "np.random.shuffle(shuffled_index)\n", "X_train = X_train[shuffled_index]\n", "y_train = y_train[shuffled_index]\n", "\n", "shuffled_index = np.arange(X_val.shape[0])\n", "np.random.shuffle(shuffled_index)\n", "X_val = X_val[shuffled_index]\n", "y_val = y_val[shuffled_index]\n", "\n", "shuffled_index = np.arange(X_test.shape[0])\n", "np.random.shuffle(shuffled_index)\n", "X_test = X_test[shuffled_index]\n", "y_test = y_test[shuffled_index]\n", "\n", "X_train.shape, X_val.shape, X_test.shape" ] }, { "cell_type": "code", "execution_count": 3, "id": "89fef4d0-bc58-4aa1-847a-26278045e8a5", "metadata": { "tags": [] }, "outputs": [], "source": [ "import tensorflow.keras.layers as kl\n", "import tensorflow.keras as k\n", "\n", "def new_pred(predict):\n", " pred = []\n", " for i in predict:\n", " if i < 0.5:\n", " pred.append(0)\n", " else:\n", " pred.append(1)\n", " return pred\n", "\n", "conv_num = -1\n", "def conv_block_name():\n", " global conv_num\n", " conv_num = conv_num + 1\n", " return 'cb_{}'.format(conv_num)\n", "\n", "def conv_block(output_timesteps, output_channels, kernel_size, name, activity_regu=None):\n", " def f(preceding_layer):\n", " input_timesteps, input_channels = preceding_layer.get_shape().as_list()[1:]\n", " \n", " inputs = k.Input(shape=(input_timesteps, input_channels))\n", " \n", " act = kl.Conv1D(output_channels, kernel_size=kernel_size, activation='linear', padding='same', name=name+'_conv_features1', activity_regularizer=activity_regu)(inputs)\n", " gate = kl.Conv1D(output_channels, kernel_size=kernel_size, activation='sigmoid', padding='same', name=name+'_conv_memory')(inputs)\n", " gated_act = kl.Multiply()([tfa.layers.InstanceNormalization()(kl.PReLU()(act)), gate]) \n", " \n", " a = kl.Permute((2,1))(gated_act)\n", " b = kl.Dense(output_timesteps, use_bias=False)(a)\n", " c = kl.Permute((2,1))(b)\n", " \n", " m = k.Model(inputs=inputs, outputs=c, name=name)\n", "# m.summary()\n", " return m(preceding_layer)\n", " return f\n", "\n", "def model_1D(input_shape):\n", " inputs = k.Input(shape=input_shape)\n", " e = conv_block(output_timesteps=window//2, output_channels=128, kernel_size=3, name=conv_block_name())(inputs)\n", " e = conv_block(output_timesteps=2, output_channels=128, kernel_size=3, name=conv_block_name(), activity_regu=regularizers.l1(1e-3))(e)\n", " c = kl.Flatten()(e)\n", " c = Dense(1, activation='sigmoid', name='sigmoid_layer')(c)\n", " d = conv_block(output_timesteps=window//2, output_channels=128, kernel_size=3, name=conv_block_name())(e)\n", " d = conv_block(output_timesteps=input_shape[0], output_channels=input_shape[1], kernel_size=3, name=conv_block_name())(d)\n", " decoder_output = kl.GaussianNoise(1e-2, name='decoder_output')(d)\n", " \n", " classifier_model = k.Model(inputs=inputs, outputs=c, name='classifier')\n", " model = k.Model(inputs=inputs, outputs=[decoder_output, c], name='autoencoder')\n", " return model, classifier_model\n", "\n", "class ProgressCallback(k.callbacks.Callback):\n", " def on_epoch_end(self, epoch, logs={}):\n", " results = pd.DataFrame(data=np.array([v for k,v in logs.items()]).reshape(2,4), \n", " columns=['loss', 'ae_loss', 'classifier_loss', 'accuracy'],\n", " index=['train','val'])\n", " results = results.style.set_caption(f\"{epoch}\")\n", " clear_output(wait=True)\n", " display(results)\n", "\n", "\n", "def train(X_train, y_train, X_val, y_val, model, epochs=50, lr=1e-2):\n", " model.compile(loss={\n", " 'decoder_output':losses.MSE,\n", " 'sigmoid_layer':losses.BinaryCrossentropy()\n", " }, \n", " optimizer=Adam(learning_rate=lr), \n", " metrics={\n", " 'sigmoid_layer': kmetrics.BinaryAccuracy(),\n", " })\n", " try:\n", " history = model.fit(X_train, \n", " [X_train, y_train], \n", " epochs=epochs,\n", " batch_size=4096, \n", " verbose=0,\n", " validation_data=(X_val, (X_val, y_val)),\n", " callbacks=[ProgressCallback()])\n", " except KeyboardInterrupt as e:\n", " history = []\n", " return history" ] }, { "cell_type": "code", "execution_count": 4, "id": "650486b7-542b-4fc1-a38f-028b74d61e42", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model: \"autoencoder\"\n", "__________________________________________________________________________________________________\n", " Layer (type) Output Shape Param # Connected to \n", "==================================================================================================\n", " input_1 (InputLayer) [(None, 24, 6)] 0 [] \n", " \n", " cb_0 (Functional) (None, 12, 128) 8480 ['input_1[0][0]'] \n", " \n", " cb_1 (Functional) (None, 2, 128) 100376 ['cb_0[0][0]'] \n", " \n", " cb_2 (Functional) (None, 12, 128) 99096 ['cb_1[0][0]'] \n", " \n", " cb_3 (Functional) (None, 24, 6) 4992 ['cb_2[0][0]'] \n", " \n", " flatten (Flatten) (None, 256) 0 ['cb_1[0][0]'] \n", " \n", " decoder_output (GaussianNoise) (None, 24, 6) 0 ['cb_3[0][0]'] \n", " \n", " sigmoid_layer (Dense) (None, 1) 257 ['flatten[0][0]'] \n", " \n", "==================================================================================================\n", "Total params: 213,201\n", "Trainable params: 213,201\n", "Non-trainable params: 0\n", "__________________________________________________________________________________________________\n" ] } ], "source": [ "gcaec, classifier = model_1D(input_shape=X_train.shape[1:])\n", "gcaec.summary()" ] }, { "cell_type": "code", "execution_count": 5, "id": "d59ed374-3085-4a36-94dd-cc8670d9d6e3", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " | loss | \n", "ae_loss | \n", "classifier_loss | \n", "accuracy | \n", "
---|---|---|---|---|
train | \n", "0.003677 | \n", "0.002197 | \n", "0.000171 | \n", "1.000000 | \n", "
val | \n", "4.735565 | \n", "0.002891 | \n", "4.731774 | \n", "0.619776 | \n", "