Classification and Regression, Part 2¶
In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sktime.transformations.panel.reduce import Tabularizer
In [2]:
import torch
from torch import nn
import torch.nn.functional as F
from skorch import NeuralNetClassifier
from skorch.callbacks import EpochScoring
from skorch.dataset import ValidSplit
from sklearn.metrics import accuracy_score
from skorch.callbacks import LRScheduler, EarlyStopping, Checkpoint
from torch.optim.lr_scheduler import ReduceLROnPlateau
Load the data¶
Classification¶
In [3]:
from sktime.datasets import load_gunpoint
In [4]:
X_gunpoint, y_gunpoint = load_gunpoint(return_X_y=True, return_type="numpy3D")
X_gunpoint.shape, y_gunpoint.shape
Out[4]:
((200, 1, 150), (200,))
In [5]:
np.unique(y_gunpoint, return_counts=True)
Out[5]:
(array(['1', '2'], dtype='<U1'), array([100, 100]))
In [6]:
from sklearn.preprocessing import LabelEncoder
In [7]:
le = LabelEncoder()
y_gunpoint = le.fit_transform(y_gunpoint)
In [8]:
np.unique(y_gunpoint, return_counts=True)
Out[8]:
(array([0, 1]), array([100, 100]))
Regression¶
In [9]:
X_covid = np.load("data/X_covid.npy")
y_covid = np.load("data/y_covid.npy")
X_covid.shape, y_covid.shape
Out[9]:
((201, 1, 84), (201,))
Split the data¶
In [10]:
from sklearn.model_selection import train_test_split
In [11]:
X_gunpoint_train, X_gunpoint_test, y_gunpoint_train, y_gunpoint_test = train_test_split(X_gunpoint, y_gunpoint, random_state=0)
X_gunpoint_train.shape, X_gunpoint_test.shape, y_gunpoint_train.shape, y_gunpoint_test.shape
Out[11]:
((150, 1, 150), (50, 1, 150), (150,), (50,))
In [12]:
X_train_covid, X_test_covid, y_train_covid, y_test_covid = train_test_split(X_covid, y_covid, random_state=0)
X_train_covid.shape, X_test_covid.shape, y_train_covid.shape, y_test_covid.shape
Out[12]:
((150, 1, 84), (51, 1, 84), (150,), (51,))
Deep Learning based¶
MLP¶
Classification¶
In [13]:
from sklearn.neural_network import MLPClassifier
In [14]:
mlp = Tabularizer() * MLPClassifier(random_state=0)
In [15]:
mlp.fit(X_gunpoint_train, y_gunpoint_train)
/Users/francesco/miniforge3/envs/timeseries_dl/lib/python3.12/site-packages/gluonts/json.py:102: UserWarning: Using `json`-module for json-handling. Consider installing one of `orjson`, `ujson` to speed up serialization and deserialization. warnings.warn( /Users/francesco/miniforge3/envs/timeseries_dl/lib/python3.12/site-packages/sklearn/neural_network/_multilayer_perceptron.py:691: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (200) reached and the optimization hasn't converged yet. warnings.warn(
Out[15]:
SklearnClassifierPipeline(classifier=MLPClassifier(random_state=0),
transformers=[Tabularizer()])Please rerun this cell to show the HTML repr or trust the notebook.SklearnClassifierPipeline(classifier=MLPClassifier(random_state=0),
transformers=[Tabularizer()])MLPClassifier(random_state=0)
MLPClassifier(random_state=0)
In [16]:
mlp.score(X_gunpoint_test, y_gunpoint_test)
Out[16]:
0.98
Regression¶
In [17]:
from sklearn.neural_network import MLPRegressor
In [18]:
mlp = Tabularizer() * MLPRegressor(random_state=0, verbose=True)
In [19]:
mlp.fit(X_train_covid, y_train_covid)
Iteration 1, loss = 119901.71504296 Iteration 2, loss = 73789.36936212 Iteration 3, loss = 40342.14866093 Iteration 4, loss = 18901.53253307 Iteration 5, loss = 7257.18181857 Iteration 6, loss = 3694.82822453 Iteration 7, loss = 5967.27812827 Iteration 8, loss = 11200.74335727 Iteration 9, loss = 16506.50087990 Iteration 10, loss = 19915.56306820 Iteration 11, loss = 20735.24183149 Iteration 12, loss = 19201.77849948 Iteration 13, loss = 16027.60798883 Iteration 14, loss = 12161.52230790 Iteration 15, loss = 8113.06889324 Iteration 16, loss = 4691.02362060 Iteration 17, loss = 2359.80159588 Iteration 18, loss = 1207.02592033 Iteration 19, loss = 1137.63851692 Iteration 20, loss = 1845.38718976 Iteration 21, loss = 2937.13491876 Iteration 22, loss = 4035.56723783 Iteration 23, loss = 4848.22007688 Iteration 24, loss = 5193.99412689 Iteration 25, loss = 5009.25476979 Iteration 26, loss = 4348.85029535 Iteration 27, loss = 3382.22773781 Iteration 28, loss = 2332.13884314 Iteration 29, loss = 1406.29209111 Iteration 30, loss = 746.22307507 Iteration 31, loss = 405.91776190 Iteration 32, loss = 365.55515080 Iteration 33, loss = 586.67272962 Iteration 34, loss = 876.79546825 Iteration 35, loss = 1177.46579618 Iteration 36, loss = 1389.64145151 Iteration 37, loss = 1439.39674368 Iteration 38, loss = 1322.37871561 Iteration 39, loss = 1081.75821565 Iteration 40, loss = 784.58753949 Iteration 41, loss = 498.38610788 Iteration 42, loss = 276.69153826 Iteration 43, loss = 155.93370599 Iteration 44, loss = 133.08856923 Iteration 45, loss = 195.95593502 Iteration 46, loss = 300.27923532 Iteration 47, loss = 400.32752713 Iteration 48, loss = 461.90341143 Iteration 49, loss = 469.47393090 Iteration 50, loss = 424.53306172 Iteration 51, loss = 341.09389462 Iteration 52, loss = 242.28478221 Iteration 53, loss = 153.27694400 Iteration 54, loss = 94.27366394 Iteration 55, loss = 73.27819933 Iteration 56, loss = 85.19315841 Iteration 57, loss = 116.81897939 Iteration 58, loss = 152.88348163 Iteration 59, loss = 180.46244279 Iteration 60, loss = 189.17619697 Iteration 61, loss = 177.78387684 Iteration 62, loss = 150.61252095 Iteration 63, loss = 117.35218877 Iteration 64, loss = 87.90480635 Iteration 65, loss = 68.86753512 Iteration 66, loss = 62.42417376 Iteration 67, loss = 66.92209396 Iteration 68, loss = 76.89431417 Iteration 69, loss = 86.22417323 Iteration 70, loss = 89.84697292 Iteration 71, loss = 86.80977079 Iteration 72, loss = 78.49919333 Iteration 73, loss = 68.23933340 Iteration 74, loss = 59.14144969 Iteration 75, loss = 54.60119888 Iteration 76, loss = 54.64010533 Iteration 77, loss = 57.91684272 Iteration 78, loss = 60.85152088 Iteration 79, loss = 60.73821073 Iteration 80, loss = 57.60191661 Iteration 81, loss = 52.76090883 Iteration 82, loss = 48.34384214 Iteration 83, loss = 45.40016666 Iteration 84, loss = 44.37458354 Iteration 85, loss = 44.71449786 Iteration 86, loss = 45.77601449 Iteration 87, loss = 46.66171178 Iteration 88, loss = 46.74593566 Iteration 89, loss = 45.87724639 Iteration 90, loss = 44.28967455 Iteration 91, loss = 42.27921549 Iteration 92, loss = 40.28900039 Iteration 93, loss = 38.98317312 Iteration 94, loss = 38.37844641 Iteration 95, loss = 38.13459861 Iteration 96, loss = 37.81801386 Iteration 97, loss = 37.16622333 Iteration 98, loss = 36.17769363 Iteration 99, loss = 35.01470007 Iteration 100, loss = 33.96178627 Iteration 101, loss = 33.19851311 Iteration 102, loss = 32.69968508 Iteration 103, loss = 32.28345228 Iteration 104, loss = 31.75516678 Iteration 105, loss = 30.97172635 Iteration 106, loss = 29.76335951 Iteration 107, loss = 28.86556225 Iteration 108, loss = 28.47816707 Iteration 109, loss = 28.31697401 Iteration 110, loss = 28.00398395 Iteration 111, loss = 27.35367380 Iteration 112, loss = 26.31323466 Iteration 113, loss = 25.61578651 Iteration 114, loss = 25.22757282 Iteration 115, loss = 24.94393991 Iteration 116, loss = 24.64983023 Iteration 117, loss = 24.19085405 Iteration 118, loss = 23.73472779 Iteration 119, loss = 23.24474799 Iteration 120, loss = 22.61431966 Iteration 121, loss = 22.09811508 Iteration 122, loss = 21.72389858 Iteration 123, loss = 21.31874679 Iteration 124, loss = 20.92095648 Iteration 125, loss = 20.59779553 Iteration 126, loss = 20.26051561 Iteration 127, loss = 19.87434428 Iteration 128, loss = 19.50584728 Iteration 129, loss = 19.14527509 Iteration 130, loss = 18.76943563 Iteration 131, loss = 18.41546782 Iteration 132, loss = 18.07994843 Iteration 133, loss = 17.74268112 Iteration 134, loss = 17.42508105 Iteration 135, loss = 17.11587528 Iteration 136, loss = 16.79610833 Iteration 137, loss = 16.48261215 Iteration 138, loss = 16.19126585 Iteration 139, loss = 15.89534909 Iteration 140, loss = 15.61317812 Iteration 141, loss = 15.31721530 Iteration 142, loss = 15.03217674 Iteration 143, loss = 14.76020015 Iteration 144, loss = 14.51167238 Iteration 145, loss = 14.27668691 Iteration 146, loss = 14.00670975 Iteration 147, loss = 13.73411520 Iteration 148, loss = 13.46071801 Iteration 149, loss = 13.20257087 Iteration 150, loss = 12.98204905 Iteration 151, loss = 12.74796998 Iteration 152, loss = 12.54864205 Iteration 153, loss = 12.30740886 Iteration 154, loss = 12.08985797 Iteration 155, loss = 11.87413658 Iteration 156, loss = 11.67032807 Iteration 157, loss = 11.46735436 Iteration 158, loss = 11.29179781 Iteration 159, loss = 11.09639842 Iteration 160, loss = 10.89871237 Iteration 161, loss = 10.71522742 Iteration 162, loss = 10.52806528 Iteration 163, loss = 10.34337738 Iteration 164, loss = 10.17135554 Iteration 165, loss = 10.01019321 Iteration 166, loss = 9.83384425 Iteration 167, loss = 9.67244052 Iteration 168, loss = 9.50519070 Iteration 169, loss = 9.34120055 Iteration 170, loss = 9.19798634 Iteration 171, loss = 9.03648042 Iteration 172, loss = 8.89000730 Iteration 173, loss = 8.75318301 Iteration 174, loss = 8.61496937 Iteration 175, loss = 8.47454960 Iteration 176, loss = 8.34027171 Iteration 177, loss = 8.21560574 Iteration 178, loss = 8.08259579 Iteration 179, loss = 7.95334408 Iteration 180, loss = 7.83206583 Iteration 181, loss = 7.71470864 Iteration 182, loss = 7.59613080 Iteration 183, loss = 7.48476927 Iteration 184, loss = 7.36631586 Iteration 185, loss = 7.24859645 Iteration 186, loss = 7.15036164 Iteration 187, loss = 7.04694192 Iteration 188, loss = 6.92634385 Iteration 189, loss = 6.83216064 Iteration 190, loss = 6.72562666 Iteration 191, loss = 6.62909390 Iteration 192, loss = 6.52891342 Iteration 193, loss = 6.43674222 Iteration 194, loss = 6.34371657 Iteration 195, loss = 6.24907761 Iteration 196, loss = 6.16493164 Iteration 197, loss = 6.07652111 Iteration 198, loss = 5.99116787 Iteration 199, loss = 5.91038267 Iteration 200, loss = 5.83351047
/Users/francesco/miniforge3/envs/timeseries_dl/lib/python3.12/site-packages/sklearn/neural_network/_multilayer_perceptron.py:691: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (200) reached and the optimization hasn't converged yet. warnings.warn(
Out[19]:
SklearnRegressorPipeline(regressor=MLPRegressor(random_state=0, verbose=True),
transformers=[Tabularizer()])Please rerun this cell to show the HTML repr or trust the notebook.SklearnRegressorPipeline(regressor=MLPRegressor(random_state=0, verbose=True),
transformers=[Tabularizer()])MLPRegressor(random_state=0, verbose=True)
MLPRegressor(random_state=0, verbose=True)
In [20]:
mlp.score(X_test_covid, y_test_covid)
Out[20]:
-530996.4918941149
CNN¶
In [21]:
from sktime.classification.deep_learning import CNNClassifier
In [21]:
# !pip install sktime[dl]
# !pip install tensorflow
In [22]:
cnn = CNNClassifier(n_conv_layers=2, random_state=0, verbose=True, n_epochs=100)
In [23]:
cnn.fit(X_gunpoint_train, y_gunpoint_train)
Model: "functional"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓ ┃ Layer (type) ┃ Output Shape ┃ Param # ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩ │ input_layer (InputLayer) │ (None, 150, 1) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ conv1d (Conv1D) │ (None, 144, 6) │ 48 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ average_pooling1d │ (None, 48, 6) │ 0 │ │ (AveragePooling1D) │ │ │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ conv1d_1 (Conv1D) │ (None, 42, 12) │ 516 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ average_pooling1d_1 │ (None, 14, 12) │ 0 │ │ (AveragePooling1D) │ │ │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ flatten (Flatten) │ (None, 168) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ dense (Dense) │ (None, 2) │ 338 │ └─────────────────────────────────┴────────────────────────┴───────────────┘
Total params: 902 (3.52 KB)
Trainable params: 902 (3.52 KB)
Non-trainable params: 0 (0.00 B)
Epoch 1/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - accuracy: 0.4415 - loss: 0.7120 Epoch 2/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.4888 - loss: 0.7021 Epoch 3/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.6373 - loss: 0.6746 Epoch 4/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.7857 - loss: 0.6500 Epoch 5/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.7857 - loss: 0.6098 Epoch 6/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.7759 - loss: 0.5636 Epoch 7/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.7857 - loss: 0.5200 Epoch 8/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.7882 - loss: 0.4793 Epoch 9/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.7970 - loss: 0.4463 Epoch 10/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.7951 - loss: 0.4170 Epoch 11/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.8380 - loss: 0.3903 Epoch 12/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.8790 - loss: 0.3614 Epoch 13/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.9029 - loss: 0.3253 Epoch 14/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.9178 - loss: 0.2789 Epoch 15/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9579 - loss: 0.2274 Epoch 16/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9667 - loss: 0.1822 Epoch 17/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9822 - loss: 0.1489 Epoch 18/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9822 - loss: 0.1266 Epoch 19/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9865 - loss: 0.1115 Epoch 20/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.9865 - loss: 0.1007 Epoch 21/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9890 - loss: 0.0926 Epoch 22/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9890 - loss: 0.0863 Epoch 23/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.9890 - loss: 0.0811 Epoch 24/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.9909 - loss: 0.0767 Epoch 25/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9909 - loss: 0.0729 Epoch 26/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.9909 - loss: 0.0695 Epoch 27/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.9927 - loss: 0.0664 Epoch 28/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9927 - loss: 0.0637 Epoch 29/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.9927 - loss: 0.0611 Epoch 30/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.9927 - loss: 0.0588 Epoch 31/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9927 - loss: 0.0566 Epoch 32/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9927 - loss: 0.0546 Epoch 33/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9927 - loss: 0.0527 Epoch 34/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9927 - loss: 0.0509 Epoch 35/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.9927 - loss: 0.0492 Epoch 36/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9927 - loss: 0.0476 Epoch 37/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.9927 - loss: 0.0461 Epoch 38/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9927 - loss: 0.0447 Epoch 39/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9927 - loss: 0.0434 Epoch 40/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9927 - loss: 0.0421 Epoch 41/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.9927 - loss: 0.0409 Epoch 42/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9982 - loss: 0.0397 Epoch 43/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9982 - loss: 0.0386 Epoch 44/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9982 - loss: 0.0375 Epoch 45/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.9982 - loss: 0.0365 Epoch 46/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 0.9982 - loss: 0.0355 Epoch 47/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0346 Epoch 48/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 1.0000 - loss: 0.0337 Epoch 49/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0328 Epoch 50/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0320 Epoch 51/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0312 Epoch 52/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0305 Epoch 53/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0297 Epoch 54/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 1.0000 - loss: 0.0290 Epoch 55/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0283 Epoch 56/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0277 Epoch 57/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0270 Epoch 58/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0264 Epoch 59/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0258 Epoch 60/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0252 Epoch 61/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0246 Epoch 62/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0241 Epoch 63/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0236 Epoch 64/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0231 Epoch 65/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0226 Epoch 66/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0221 Epoch 67/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0216 Epoch 68/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0211 Epoch 69/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0207 Epoch 70/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0203 Epoch 71/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0198 Epoch 72/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0194 Epoch 73/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 1.0000 - loss: 0.0190 Epoch 74/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0186 Epoch 75/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - accuracy: 1.0000 - loss: 0.0183 Epoch 76/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0179 Epoch 77/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0175 Epoch 78/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0172 Epoch 79/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0169 Epoch 80/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0165 Epoch 81/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0162 Epoch 82/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0159 Epoch 83/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0156 Epoch 84/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0153 Epoch 85/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0150 Epoch 86/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0147 Epoch 87/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 1.0000 - loss: 0.0144 Epoch 88/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0141 Epoch 89/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0139 Epoch 90/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0136 Epoch 91/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0134 Epoch 92/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0131 Epoch 93/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0129 Epoch 94/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0127 Epoch 95/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0124 Epoch 96/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0122 Epoch 97/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0120 Epoch 98/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0118 Epoch 99/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0116 Epoch 100/100 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 1.0000 - loss: 0.0114
Out[23]:
CNNClassifier(n_epochs=100, random_state=0, verbose=True)Please rerun this cell to show the HTML repr or trust the notebook.
CNNClassifier(n_epochs=100, random_state=0, verbose=True)
In [24]:
cnn.score(X_gunpoint_test, y_gunpoint_test)
4/4 ━━━━━━━━━━━━━━━━━━━━ 0s 13ms/step
Out[24]:
1.0
In [40]:
cnn.history.history.keys()
Out[40]:
dict_keys(['accuracy', 'loss'])
In [41]:
history = cnn.history.history
plt.plot(history['loss'], label='train_loss')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show()
plt.plot(history["accuracy"], label='train_acc')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.show()
Skorch¶
In [42]:
DEVICE = "mps" if torch.backends.mps.is_available() else "cuda" if torch.cuda.is_available() else "cpu"
In [124]:
class Conv1DClassifier(nn.Module):
def __init__(self, in_channels: int, num_classes: int):
super().__init__()
self.features = nn.Sequential(
nn.Conv1d(
in_channels=in_channels,
out_channels=32,
kernel_size=7,
padding=3
),
nn.BatchNorm1d(32),
nn.ReLU(),
nn.MaxPool1d(kernel_size=2),
nn.Conv1d(
in_channels=32,
out_channels=64,
kernel_size=5,
padding=2
),
nn.BatchNorm1d(64),
nn.ReLU(),
nn.MaxPool1d(kernel_size=2),
nn.Conv1d(
in_channels=64,
out_channels=128,
kernel_size=3,
padding=1
),
nn.BatchNorm1d(128),
nn.ReLU()
)
self.pool = nn.AdaptiveAvgPool1d(output_size=1)
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(128, num_classes)
)
def forward(self, x):
"""
x shape: (batch_size, in_channels, sequence_length)
"""
x = self.features(x)
x = self.pool(x)
x = self.classifier(x)
return x
In [125]:
cnn = NeuralNetClassifier(
Conv1DClassifier,
module__in_channels=X_gunpoint_train.shape[1],
module__num_classes=len(le.classes_),
criterion=nn.CrossEntropyLoss,
optimizer=torch.optim.Adam,
max_epochs=300,
batch_size=128,
lr=1e-4,
callbacks=[
EpochScoring(
scoring='accuracy',
name='train_acc',
on_train=True,
),
Checkpoint(
monitor="valid_acc_best",
load_best=True,
f_history=None,
f_optimizer=None,
)
],
device=DEVICE,
train_split=ValidSplit(cv=0.2, stratified=True)
)
cnn
Out[125]:
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized]( module=<class '__main__.Conv1DClassifier'>, module__in_channels=1, module__num_classes=2, )In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized]( module=<class '__main__.Conv1DClassifier'>, module__in_channels=1, module__num_classes=2, )
In [126]:
cnn.fit(X_gunpoint_train.astype(np.float32), y_gunpoint_train)
epoch train_acc train_loss valid_acc valid_loss cp dur
------- ----------- ------------ ----------- ------------ ---- ------
1 0.3250 0.7017 0.5000 0.6946 + 0.0654
2 0.5417 0.6942 0.5000 0.6948 0.0149
3 0.5917 0.6871 0.5000 0.6947 0.0165
4 0.6333 0.6805 0.5000 0.6947 0.0162
5 0.6500 0.6745 0.5000 0.6939 0.0151
6 0.6500 0.6690 0.5000 0.6930 0.0167
7 0.6667 0.6639 0.5000 0.6921 0.0162
8 0.6667 0.6591 0.5000 0.6913 0.0173
9 0.6667 0.6546 0.5000 0.6904 0.0167
10 0.6750 0.6503 0.5000 0.6896 0.0176
11 0.6833 0.6461 0.5000 0.6886 0.0161
12 0.6917 0.6421 0.5000 0.6876 0.0160
13 0.6917 0.6381 0.5000 0.6864 0.0158
14 0.7000 0.6343 0.5667 0.6850 + 0.0148
15 0.7083 0.6305 0.5667 0.6835 0.0163
16 0.7083 0.6268 0.5667 0.6819 0.0156
17 0.7000 0.6233 0.6000 0.6802 + 0.0160
18 0.7000 0.6199 0.6333 0.6785 + 0.0161
19 0.7000 0.6166 0.6333 0.6767 0.0158
20 0.7000 0.6134 0.6333 0.6747 0.0160
21 0.7000 0.6104 0.6333 0.6725 0.0152
22 0.7000 0.6075 0.6333 0.6700 0.0155
23 0.7000 0.6048 0.6333 0.6674 0.0159
24 0.7000 0.6022 0.6333 0.6645 0.0151
25 0.7000 0.5998 0.6333 0.6613 0.0161
26 0.7000 0.5973 0.6333 0.6579 0.0177
27 0.7000 0.5948 0.6333 0.6541 0.0170
28 0.7000 0.5923 0.6333 0.6501 0.0186
29 0.7000 0.5899 0.6333 0.6457 0.0318
30 0.7000 0.5874 0.6333 0.6411 0.0322
31 0.7083 0.5850 0.6333 0.6362 0.0259
32 0.7083 0.5827 0.6667 0.6311 + 0.0243
33 0.7083 0.5803 0.6667 0.6256 0.0188
34 0.7083 0.5780 0.6667 0.6200 0.0169
35 0.7250 0.5756 0.6667 0.6142 0.0171
36 0.7250 0.5731 0.6667 0.6082 0.0614
37 0.7250 0.5705 0.7000 0.6020 + 0.0158
38 0.7250 0.5677 0.7333 0.5959 + 0.0159
39 0.7250 0.5651 0.7333 0.5897 0.0155
40 0.7250 0.5626 0.7333 0.5834 0.0162
41 0.7250 0.5602 0.7333 0.5773 0.0155
42 0.7250 0.5579 0.7333 0.5714 0.0159
43 0.7250 0.5556 0.7333 0.5657 0.0161
44 0.7250 0.5532 0.7667 0.5601 + 0.0156
45 0.7250 0.5509 0.7667 0.5546 0.0151
46 0.7333 0.5486 0.7667 0.5493 0.0151
47 0.7333 0.5462 0.7667 0.5442 0.0157
48 0.7333 0.5439 0.7667 0.5393 0.0158
49 0.7333 0.5417 0.7667 0.5347 0.0160
50 0.7333 0.5394 0.7667 0.5304 0.0178
51 0.7333 0.5372 0.7667 0.5263 0.0161
52 0.7333 0.5351 0.7667 0.5224 0.0160
53 0.7333 0.5329 0.7667 0.5188 0.0161
54 0.7333 0.5307 0.7667 0.5153 0.0163
55 0.7333 0.5285 0.7667 0.5120 0.0156
56 0.7333 0.5262 0.7667 0.5089 0.0154
57 0.7500 0.5240 0.7667 0.5060 0.0157
58 0.7500 0.5217 0.7667 0.5033 0.0155
59 0.7500 0.5195 0.8000 0.5007 + 0.0156
60 0.7583 0.5173 0.8000 0.4982 0.0156
61 0.7667 0.5152 0.8000 0.4960 0.0160
62 0.7750 0.5131 0.8000 0.4939 0.0155
63 0.7750 0.5111 0.8000 0.4918 0.0157
64 0.7833 0.5090 0.8000 0.4898 0.0153
65 0.7833 0.5069 0.8000 0.4878 0.0154
66 0.7833 0.5048 0.8333 0.4858 + 0.0148
67 0.7917 0.5028 0.8333 0.4836 0.0161
68 0.7917 0.5007 0.8333 0.4814 0.0151
69 0.7917 0.4986 0.8333 0.4792 0.0175
70 0.7917 0.4965 0.8333 0.4770 0.0153
71 0.7917 0.4944 0.8333 0.4750 0.0161
72 0.7917 0.4923 0.8333 0.4731 0.0165
73 0.8000 0.4901 0.8333 0.4713 0.0155
74 0.8000 0.4880 0.8333 0.4696 0.0150
75 0.8000 0.4859 0.8333 0.4679 0.0160
76 0.8167 0.4839 0.8333 0.4661 0.0159
77 0.8167 0.4819 0.8333 0.4643 0.0151
78 0.8167 0.4798 0.8333 0.4623 0.0150
79 0.8167 0.4778 0.8333 0.4602 0.0148
80 0.8167 0.4757 0.8333 0.4582 0.0158
81 0.8167 0.4736 0.8333 0.4562 0.0150
82 0.8167 0.4715 0.8333 0.4544 0.0156
83 0.8250 0.4694 0.8333 0.4526 0.0157
84 0.8333 0.4673 0.8333 0.4509 0.0160
85 0.8333 0.4652 0.8333 0.4492 0.0153
86 0.8333 0.4631 0.8333 0.4474 0.0159
87 0.8333 0.4610 0.8333 0.4457 0.0145
88 0.8417 0.4589 0.8667 0.4439 + 0.0150
89 0.8417 0.4568 0.8667 0.4422 0.0161
90 0.8417 0.4546 0.8667 0.4404 0.0156
91 0.8500 0.4524 0.8667 0.4386 0.0148
92 0.8500 0.4503 0.8667 0.4368 0.0146
93 0.8500 0.4481 0.8667 0.4348 0.0149
94 0.8500 0.4459 0.8667 0.4328 0.0168
95 0.8500 0.4437 0.8667 0.4306 0.0157
96 0.8500 0.4414 0.8667 0.4284 0.0166
97 0.8500 0.4391 0.8667 0.4263 0.0167
98 0.8500 0.4369 0.8667 0.4242 0.0158
99 0.8583 0.4346 0.8667 0.4219 0.0150
100 0.8583 0.4323 0.8667 0.4194 0.0159
101 0.8583 0.4301 0.8667 0.4169 0.0152
102 0.8583 0.4278 0.8667 0.4145 0.0154
103 0.8583 0.4256 0.8667 0.4120 0.0152
104 0.8583 0.4233 0.8667 0.4093 0.0158
105 0.8583 0.4211 0.8667 0.4065 0.0161
106 0.8583 0.4188 0.8667 0.4038 0.0161
107 0.8583 0.4166 0.8667 0.4012 0.0162
108 0.8750 0.4143 0.8667 0.3987 0.0162
109 0.8750 0.4120 0.8333 0.3961 0.0149
110 0.8750 0.4097 0.8333 0.3936 0.0149
111 0.8750 0.4074 0.8333 0.3913 0.0152
112 0.8750 0.4051 0.8333 0.3891 0.0495
113 0.8750 0.4028 0.8333 0.3869 0.0198
114 0.8833 0.4005 0.8333 0.3847 0.0168
115 0.8833 0.3982 0.8333 0.3827 0.0158
116 0.8917 0.3958 0.8333 0.3807 0.0167
117 0.8917 0.3934 0.8333 0.3788 0.0160
118 0.8917 0.3910 0.8333 0.3769 0.0167
119 0.8917 0.3886 0.8333 0.3750 0.0161
120 0.8917 0.3862 0.8333 0.3729 0.0150
121 0.9000 0.3837 0.8333 0.3707 0.0156
122 0.9000 0.3813 0.8667 0.3684 0.0159
123 0.9083 0.3788 0.8667 0.3662 0.0159
124 0.9083 0.3763 0.8667 0.3639 0.0155
125 0.9083 0.3738 0.8667 0.3617 0.0153
126 0.9083 0.3713 0.8667 0.3595 0.0160
127 0.9083 0.3687 0.8667 0.3573 0.0169
128 0.9167 0.3662 0.8667 0.3551 0.0161
129 0.9167 0.3636 0.8667 0.3528 0.0148
130 0.9250 0.3610 0.8667 0.3505 0.0160
131 0.9333 0.3584 0.8667 0.3483 0.0153
132 0.9333 0.3558 0.8667 0.3463 0.0202
133 0.9333 0.3531 0.8667 0.3444 0.0161
134 0.9333 0.3505 0.8667 0.3422 0.0159
135 0.9333 0.3478 0.8667 0.3399 0.0156
136 0.9333 0.3451 0.8667 0.3376 0.0149
137 0.9333 0.3424 0.8667 0.3354 0.0153
138 0.9333 0.3396 0.8667 0.3331 0.0158
139 0.9333 0.3369 0.8667 0.3306 0.0152
140 0.9333 0.3341 0.8667 0.3282 0.0157
141 0.9333 0.3314 0.8667 0.3255 0.0160
142 0.9333 0.3286 0.8667 0.3225 0.0165
143 0.9333 0.3259 0.8667 0.3195 0.0160
144 0.9333 0.3231 0.8667 0.3164 0.0155
145 0.9333 0.3203 0.8667 0.3134 0.0162
146 0.9333 0.3175 0.8667 0.3106 0.0147
147 0.9417 0.3147 0.8667 0.3077 0.0151
148 0.9417 0.3119 0.9000 0.3046 + 0.0158
149 0.9417 0.3090 0.9000 0.3018 0.0157
150 0.9417 0.3061 0.9000 0.2991 0.0153
151 0.9417 0.3032 0.9000 0.2963 0.0159
152 0.9417 0.3002 0.9000 0.2938 0.0163
153 0.9417 0.2973 0.9000 0.2915 0.0201
154 0.9417 0.2943 0.9000 0.2887 0.0166
155 0.9417 0.2914 0.9000 0.2855 0.0162
156 0.9583 0.2884 0.9000 0.2823 0.0351
157 0.9583 0.2855 0.9000 0.2790 0.0158
158 0.9583 0.2825 0.9000 0.2756 0.0155
159 0.9583 0.2796 0.9000 0.2720 0.0165
160 0.9583 0.2767 0.9333 0.2684 + 0.0154
161 0.9583 0.2737 0.9333 0.2649 0.0164
162 0.9583 0.2708 0.9333 0.2619 0.0171
163 0.9667 0.2678 0.9333 0.2591 0.0160
164 0.9667 0.2648 0.9333 0.2563 0.0158
165 0.9667 0.2619 0.9333 0.2537 0.0153
166 0.9667 0.2589 0.9333 0.2512 0.0148
167 0.9667 0.2559 0.9667 0.2485 + 0.0158
168 0.9667 0.2530 0.9667 0.2461 0.0157
169 0.9667 0.2501 0.9667 0.2437 0.0158
170 0.9667 0.2471 0.9667 0.2410 0.0158
171 0.9833 0.2442 0.9667 0.2383 0.0149
172 0.9833 0.2412 0.9667 0.2360 0.0163
173 0.9833 0.2383 0.9667 0.2339 0.0173
174 0.9833 0.2353 0.9667 0.2322 0.0159
175 0.9833 0.2324 0.9333 0.2308 0.0182
176 0.9833 0.2295 0.9333 0.2295 0.0544
177 0.9833 0.2266 0.9333 0.2285 0.0244
178 0.9833 0.2237 0.9333 0.2269 0.0161
179 0.9833 0.2209 0.9333 0.2244 0.0207
180 0.9833 0.2180 0.9333 0.2220 0.0183
181 0.9833 0.2152 0.9333 0.2201 0.0162
182 0.9833 0.2124 0.9333 0.2177 0.0182
183 0.9833 0.2096 0.9333 0.2150 0.0183
184 0.9917 0.2068 0.9333 0.2130 0.0167
185 0.9917 0.2041 0.9333 0.2113 0.0150
186 0.9917 0.2013 0.9333 0.2094 0.0151
187 0.9917 0.1986 0.9333 0.2075 0.0153
188 0.9917 0.1959 0.9333 0.2069 0.0153
189 0.9917 0.1932 0.9333 0.2062 0.0154
190 0.9917 0.1905 0.9333 0.2056 0.0160
191 0.9917 0.1879 0.9333 0.2062 0.0162
192 0.9917 0.1852 0.9333 0.2065 0.0161
193 0.9917 0.1826 0.9333 0.2065 0.0152
194 0.9917 0.1800 0.9333 0.2048 0.0167
195 0.9917 0.1774 0.9333 0.2033 0.0170
196 0.9917 0.1748 0.9333 0.2010 0.0164
197 0.9917 0.1723 0.9333 0.1987 0.0160
198 0.9917 0.1697 0.9333 0.1970 0.0151
199 1.0000 0.1672 0.9333 0.1947 0.0152
200 1.0000 0.1647 0.9333 0.1936 0.0156
201 1.0000 0.1623 0.9333 0.1930 0.0154
202 1.0000 0.1598 0.9333 0.1924 0.0151
203 1.0000 0.1575 0.9333 0.1911 0.0154
204 1.0000 0.1551 0.9333 0.1894 0.0156
205 1.0000 0.1528 0.9333 0.1882 0.0156
206 1.0000 0.1505 0.9667 0.1870 0.0162
207 1.0000 0.1482 0.9667 0.1852 0.0164
208 1.0000 0.1459 0.9667 0.1838 0.0301
209 1.0000 0.1437 0.9667 0.1816 0.0152
210 1.0000 0.1415 0.9667 0.1781 0.0150
211 1.0000 0.1393 0.9667 0.1759 0.0158
212 1.0000 0.1372 0.9667 0.1727 0.0155
213 1.0000 0.1351 0.9667 0.1682 0.0157
214 1.0000 0.1330 0.9667 0.1643 0.0158
215 1.0000 0.1310 0.9667 0.1609 0.0154
216 1.0000 0.1290 1.0000 0.1574 + 0.0205
217 1.0000 0.1270 1.0000 0.1564 0.0152
218 1.0000 0.1250 1.0000 0.1559 0.0157
219 1.0000 0.1231 1.0000 0.1548 0.0155
220 1.0000 0.1212 1.0000 0.1521 0.0157
221 1.0000 0.1194 1.0000 0.1513 0.0156
222 1.0000 0.1176 1.0000 0.1519 0.0155
223 1.0000 0.1159 1.0000 0.1522 0.0151
224 1.0000 0.1142 1.0000 0.1523 0.0147
225 1.0000 0.1125 0.9667 0.1521 0.0153
226 1.0000 0.1108 1.0000 0.1491 0.0154
227 1.0000 0.1092 1.0000 0.1488 0.0152
228 1.0000 0.1076 1.0000 0.1459 0.0159
229 1.0000 0.1060 1.0000 0.1439 0.0164
230 1.0000 0.1044 1.0000 0.1409 0.0154
231 1.0000 0.1028 1.0000 0.1389 0.0157
232 1.0000 0.1013 1.0000 0.1380 0.0149
233 1.0000 0.0998 1.0000 0.1352 0.0149
234 1.0000 0.0984 1.0000 0.1346 0.0152
235 1.0000 0.0969 1.0000 0.1322 0.0152
236 1.0000 0.0955 1.0000 0.1291 0.0207
237 1.0000 0.0941 1.0000 0.1269 0.0159
238 1.0000 0.0927 1.0000 0.1242 0.0167
239 1.0000 0.0914 1.0000 0.1223 0.0163
240 1.0000 0.0901 1.0000 0.1204 0.0161
241 1.0000 0.0888 1.0000 0.1197 0.0151
242 1.0000 0.0875 1.0000 0.1184 0.0156
243 1.0000 0.0862 1.0000 0.1157 0.0155
244 1.0000 0.0851 1.0000 0.1133 0.0155
245 1.0000 0.0839 1.0000 0.1086 0.0156
246 1.0000 0.0827 1.0000 0.1040 0.0161
247 1.0000 0.0815 1.0000 0.1005 0.0173
248 1.0000 0.0804 1.0000 0.0975 0.0157
249 1.0000 0.0793 1.0000 0.0961 0.0158
250 1.0000 0.0783 1.0000 0.0953 0.0150
251 1.0000 0.0772 1.0000 0.0962 0.0157
252 1.0000 0.0761 1.0000 0.0970 0.0155
253 1.0000 0.0751 1.0000 0.0964 0.0156
254 1.0000 0.0741 1.0000 0.0962 0.0161
255 1.0000 0.0731 1.0000 0.0955 0.0158
256 1.0000 0.0721 1.0000 0.0955 0.0165
257 1.0000 0.0712 1.0000 0.0934 0.0152
258 1.0000 0.0703 1.0000 0.0914 0.0159
259 1.0000 0.0693 1.0000 0.0896 0.0158
260 1.0000 0.0684 1.0000 0.0875 0.0153
261 1.0000 0.0675 1.0000 0.0850 0.0158
262 1.0000 0.0666 1.0000 0.0830 0.0154
263 1.0000 0.0658 1.0000 0.0815 0.0155
264 1.0000 0.0649 1.0000 0.0799 0.0210
265 1.0000 0.0641 1.0000 0.0785 0.0206
266 1.0000 0.0633 1.0000 0.0773 0.0154
267 1.0000 0.0625 1.0000 0.0762 0.0150
268 1.0000 0.0617 1.0000 0.0749 0.0152
269 1.0000 0.0609 1.0000 0.0730 0.0150
270 1.0000 0.0602 1.0000 0.0710 0.0156
271 1.0000 0.0594 1.0000 0.0686 0.0150
272 1.0000 0.0587 1.0000 0.0669 0.0145
273 1.0000 0.0580 1.0000 0.0656 0.0147
274 1.0000 0.0573 1.0000 0.0644 0.0148
275 1.0000 0.0566 1.0000 0.0631 0.0149
276 1.0000 0.0559 1.0000 0.0612 0.0155
277 1.0000 0.0552 1.0000 0.0600 0.0157
278 1.0000 0.0546 1.0000 0.0587 0.0151
279 1.0000 0.0540 1.0000 0.0580 0.0147
280 1.0000 0.0533 1.0000 0.0571 0.0152
281 1.0000 0.0527 1.0000 0.0563 0.0152
282 1.0000 0.0521 1.0000 0.0547 0.0153
283 1.0000 0.0515 1.0000 0.0538 0.0147
284 1.0000 0.0509 1.0000 0.0526 0.0145
285 1.0000 0.0504 1.0000 0.0518 0.0149
286 1.0000 0.0498 1.0000 0.0508 0.0144
287 1.0000 0.0492 1.0000 0.0502 0.0150
288 1.0000 0.0487 1.0000 0.0498 0.0152
289 1.0000 0.0482 1.0000 0.0489 0.0151
290 1.0000 0.0477 1.0000 0.0485 0.0147
291 1.0000 0.0471 1.0000 0.0476 0.0149
292 1.0000 0.0466 1.0000 0.0477 0.0157
293 1.0000 0.0462 1.0000 0.0470 0.0154
294 1.0000 0.0457 1.0000 0.0472 0.0149
295 1.0000 0.0452 1.0000 0.0466 0.0146
296 1.0000 0.0447 1.0000 0.0461 0.0191
297 1.0000 0.0442 1.0000 0.0459 0.0148
298 1.0000 0.0438 1.0000 0.0448 0.0146
299 1.0000 0.0433 1.0000 0.0451 0.0147
300 1.0000 0.0429 1.0000 0.0443 0.0151
Out[126]:
<class 'skorch.classifier.NeuralNetClassifier'>[initialized](
module_=Conv1DClassifier(
(features): Sequential(
(0): Conv1d(1, 32, kernel_size=(7,), stride=(1,), padding=(3,))
(1): BatchNorm1d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU()
(3): MaxPool1d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(4): Conv1d(32, 64, kernel_size=(5,), stride=(1,), padding=(2,))
(5): BatchNorm1d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(6): ReLU()
(7): MaxPool1d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(8): Conv1d(64, 128, kernel_size=(3,), stride=(1,), padding=(1,))
(9): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(10): ReLU()
)
(pool): AdaptiveAvgPool1d(output_size=1)
(classifier): Sequential(
(0): Flatten(start_dim=1, end_dim=-1)
(1): Linear(in_features=128, out_features=2, bias=True)
)
),
)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.classifier.NeuralNetClassifier'>[initialized](
module_=Conv1DClassifier(
(features): Sequential(
(0): Conv1d(1, 32, kernel_size=(7,), stride=(1,), padding=(3,))
(1): BatchNorm1d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU()
(3): MaxPool1d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(4): Conv1d(32, 64, kernel_size=(5,), stride=(1,), padding=(2,))
(5): BatchNorm1d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(6): ReLU()
(7): MaxPool1d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(8): Conv1d(64, 128, kernel_size=(3,), stride=(1,), padding=(1,))
(9): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(10): ReLU()
)
(pool): AdaptiveAvgPool1d(output_size=1)
(classifier): Sequential(
(0): Flatten(start_dim=1, end_dim=-1)
(1): Linear(in_features=128, out_features=2, bias=True)
)
),
)In [127]:
y_pred = cnn.predict(X_gunpoint_test.astype(np.float32))
accuracy_score(y_gunpoint_test, y_pred)
Out[127]:
1.0
In [128]:
history = cnn.history
plt.plot(history[:, 'train_loss'], label='train_loss')
plt.plot(history[:, 'valid_loss'], label='valid_loss')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show()
plt.plot(history[:, 'train_acc'], label='train_acc')
plt.plot(history[:, 'valid_acc'], label='valid_acc')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.show()
RNN¶
In [49]:
from sktime.classification.deep_learning import SimpleRNNClassifier
In [50]:
rnn = SimpleRNNClassifier(random_state=0, verbose=True)
In [51]:
rnn.fit(X_gunpoint_train, y_gunpoint_train)
/Users/francesco/miniforge3/envs/timeseries_dl/lib/python3.12/site-packages/keras/src/layers/rnn/rnn.py:200: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead. super().__init__(**kwargs)
Model: "functional_1"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓ ┃ Layer (type) ┃ Output Shape ┃ Param # ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩ │ input_layer_1 (InputLayer) │ (None, 150, 1) │ 0 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ simple_rnn (SimpleRNN) │ (None, 6) │ 42 │ ├─────────────────────────────────┼────────────────────────┼───────────────┤ │ dense_1 (Dense) │ (None, 2) │ 14 │ └─────────────────────────────────┴────────────────────────┴───────────────┘
Total params: 56 (224.00 B)
Trainable params: 56 (224.00 B)
Non-trainable params: 0 (0.00 B)
Epoch 1/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5235 - loss: 0.2811 - learning_rate: 0.0010 Epoch 2/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5772 - loss: 0.2535 - learning_rate: 0.0010 Epoch 3/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5750 - loss: 0.2520 - learning_rate: 0.0010 Epoch 4/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5710 - loss: 0.2514 - learning_rate: 0.0010 Epoch 5/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5645 - loss: 0.2510 - learning_rate: 0.0010 Epoch 6/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5662 - loss: 0.2502 - learning_rate: 0.0010 Epoch 7/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5750 - loss: 0.2499 - learning_rate: 0.0010 Epoch 8/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5786 - loss: 0.2499 - learning_rate: 0.0010 Epoch 9/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5758 - loss: 0.2498 - learning_rate: 0.0010 Epoch 10/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5744 - loss: 0.2498 - learning_rate: 0.0010 Epoch 11/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5744 - loss: 0.2497 - learning_rate: 0.0010 Epoch 12/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5744 - loss: 0.2497 - learning_rate: 0.0010 Epoch 13/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5744 - loss: 0.2497 - learning_rate: 0.0010 Epoch 14/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5826 - loss: 0.2496 - learning_rate: 0.0010 Epoch 15/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5829 - loss: 0.2496 - learning_rate: 0.0010 Epoch 16/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5829 - loss: 0.2496 - learning_rate: 0.0010 Epoch 17/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5829 - loss: 0.2496 - learning_rate: 0.0010 Epoch 18/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5766 - loss: 0.2496 - learning_rate: 0.0010 Epoch 19/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5722 - loss: 0.2496 - learning_rate: 0.0010 Epoch 20/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5692 - loss: 0.2496 - learning_rate: 0.0010 Epoch 21/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5692 - loss: 0.2496 - learning_rate: 0.0010 Epoch 22/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5692 - loss: 0.2496 - learning_rate: 0.0010 Epoch 23/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5692 - loss: 0.2497 - learning_rate: 0.0010 Epoch 24/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5692 - loss: 0.2497 - learning_rate: 0.0010 Epoch 25/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5736 - loss: 0.2497 - learning_rate: 0.0010 Epoch 26/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5736 - loss: 0.2498 - learning_rate: 0.0010 Epoch 27/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5736 - loss: 0.2498 - learning_rate: 0.0010 Epoch 28/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5774 - loss: 0.2498 - learning_rate: 0.0010 Epoch 29/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5774 - loss: 0.2498 - learning_rate: 0.0010 Epoch 30/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5774 - loss: 0.2498 - learning_rate: 0.0010 Epoch 31/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5774 - loss: 0.2499 - learning_rate: 0.0010 Epoch 32/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5743 - loss: 0.2499 - learning_rate: 0.0010 Epoch 33/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5743 - loss: 0.2499 - learning_rate: 0.0010 Epoch 34/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5602 - loss: 0.2499 - learning_rate: 0.0010 Epoch 35/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5633 - loss: 0.2499 - learning_rate: 0.0010 Epoch 36/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5633 - loss: 0.2499 - learning_rate: 0.0010 Epoch 37/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5633 - loss: 0.2500 - learning_rate: 0.0010 Epoch 38/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5694 - loss: 0.2500 - learning_rate: 0.0010 Epoch 39/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5694 - loss: 0.2500 - learning_rate: 0.0010 Epoch 40/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5694 - loss: 0.2500 - learning_rate: 0.0010 Epoch 41/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5694 - loss: 0.2501 - learning_rate: 0.0010 Epoch 42/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5694 - loss: 0.2501 - learning_rate: 0.0010 Epoch 43/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5495 - loss: 0.2501 - learning_rate: 0.0010 Epoch 44/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5495 - loss: 0.2501 - learning_rate: 0.0010 Epoch 45/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5495 - loss: 0.2501 - learning_rate: 0.0010 Epoch 46/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5495 - loss: 0.2501 - learning_rate: 0.0010 Epoch 47/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5443 - loss: 0.2501 - learning_rate: 0.0010 Epoch 48/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5443 - loss: 0.2501 - learning_rate: 0.0010 Epoch 49/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5443 - loss: 0.2501 - learning_rate: 0.0010 Epoch 50/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5439 - loss: 0.2501 - learning_rate: 0.0010 Epoch 51/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5461 - loss: 0.2501 - learning_rate: 0.0010 Epoch 52/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5450 - loss: 0.2501 - learning_rate: 0.0010 Epoch 53/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5450 - loss: 0.2501 - learning_rate: 0.0010 Epoch 54/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5450 - loss: 0.2501 - learning_rate: 0.0010 Epoch 55/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5450 - loss: 0.2501 - learning_rate: 0.0010 Epoch 56/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5450 - loss: 0.2501 - learning_rate: 0.0010 Epoch 57/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5450 - loss: 0.2501 - learning_rate: 0.0010 Epoch 58/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5450 - loss: 0.2501 - learning_rate: 0.0010 Epoch 59/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5477 - loss: 0.2501 - learning_rate: 0.0010 Epoch 60/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5477 - loss: 0.2501 - learning_rate: 0.0010 Epoch 61/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5493 - loss: 0.2501 - learning_rate: 0.0010 Epoch 62/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5472 - loss: 0.2501 - learning_rate: 0.0010 Epoch 63/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5476 - loss: 0.2501 - learning_rate: 0.0010 Epoch 64/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5476 - loss: 0.2501 - learning_rate: 0.0010 Epoch 65/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5475 - loss: 0.2500 - learning_rate: 0.0010 Epoch 66/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5475 - loss: 0.2500 - learning_rate: 0.0010 Epoch 67/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5475 - loss: 0.2500 - learning_rate: 0.0010 Epoch 68/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5475 - loss: 0.2500 - learning_rate: 0.0010 Epoch 69/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5851 - loss: 0.2489 - learning_rate: 5.0000e-04 Epoch 70/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5450 - loss: 0.2486 - learning_rate: 5.0000e-04 Epoch 71/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5540 - loss: 0.2483 - learning_rate: 5.0000e-04 Epoch 72/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5567 - loss: 0.2482 - learning_rate: 5.0000e-04 Epoch 73/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5578 - loss: 0.2481 - learning_rate: 5.0000e-04 Epoch 74/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5831 - loss: 0.2481 - learning_rate: 5.0000e-04 Epoch 75/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5820 - loss: 0.2480 - learning_rate: 5.0000e-04 Epoch 76/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5827 - loss: 0.2480 - learning_rate: 5.0000e-04 Epoch 77/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5628 - loss: 0.2480 - learning_rate: 5.0000e-04 Epoch 78/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5628 - loss: 0.2480 - learning_rate: 5.0000e-04 Epoch 79/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5628 - loss: 0.2480 - learning_rate: 5.0000e-04 Epoch 80/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5628 - loss: 0.2480 - learning_rate: 5.0000e-04 Epoch 81/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5628 - loss: 0.2479 - learning_rate: 5.0000e-04 Epoch 82/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5612 - loss: 0.2479 - learning_rate: 5.0000e-04 Epoch 83/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5612 - loss: 0.2479 - learning_rate: 5.0000e-04 Epoch 84/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5612 - loss: 0.2479 - learning_rate: 5.0000e-04 Epoch 85/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5612 - loss: 0.2479 - learning_rate: 5.0000e-04 Epoch 86/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5612 - loss: 0.2478 - learning_rate: 5.0000e-04 Epoch 87/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5612 - loss: 0.2478 - learning_rate: 5.0000e-04 Epoch 88/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5612 - loss: 0.2478 - learning_rate: 5.0000e-04 Epoch 89/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5612 - loss: 0.2478 - learning_rate: 5.0000e-04 Epoch 90/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5612 - loss: 0.2478 - learning_rate: 5.0000e-04 Epoch 91/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5612 - loss: 0.2478 - learning_rate: 5.0000e-04 Epoch 92/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5612 - loss: 0.2477 - learning_rate: 5.0000e-04 Epoch 93/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5605 - loss: 0.2477 - learning_rate: 5.0000e-04 Epoch 94/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5605 - loss: 0.2477 - learning_rate: 5.0000e-04 Epoch 95/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 6ms/step - accuracy: 0.5605 - loss: 0.2477 - learning_rate: 5.0000e-04 Epoch 96/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5605 - loss: 0.2477 - learning_rate: 5.0000e-04 Epoch 97/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5605 - loss: 0.2476 - learning_rate: 5.0000e-04 Epoch 98/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5666 - loss: 0.2476 - learning_rate: 5.0000e-04 Epoch 99/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.5666 - loss: 0.2476 - learning_rate: 5.0000e-04 Epoch 100/100 150/150 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.5666 - loss: 0.2476 - learning_rate: 5.0000e-04
Out[51]:
SimpleRNNClassifier(verbose=True)Please rerun this cell to show the HTML repr or trust the notebook.
SimpleRNNClassifier(verbose=True)
In [52]:
rnn.score(X_gunpoint_test, y_gunpoint_test)
50/50 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step
Out[52]:
0.5
In [53]:
rnn.summary().keys()
Out[53]:
dict_keys(['accuracy', 'loss', 'learning_rate'])
In [54]:
rnn.summary()
Out[54]:
{'accuracy': [0.4866666793823242,
0.5133333206176758,
0.47333332896232605,
0.46666666865348816,
0.4533333480358124,
0.4533333480358124,
0.46666666865348816,
0.47333332896232605,
0.46666666865348816,
0.46666666865348816,
0.46666666865348816,
0.46666666865348816,
0.46666666865348816,
0.47999998927116394,
0.4866666793823242,
0.4866666793823242,
0.4866666793823242,
0.47999998927116394,
0.47999998927116394,
0.47999998927116394,
0.47999998927116394,
0.47999998927116394,
0.47999998927116394,
0.47999998927116394,
0.4866666793823242,
0.4866666793823242,
0.4866666793823242,
0.4933333396911621,
0.4933333396911621,
0.4933333396911621,
0.4933333396911621,
0.4866666793823242,
0.4866666793823242,
0.47333332896232605,
0.47999998927116394,
0.47999998927116394,
0.47999998927116394,
0.4866666793823242,
0.4866666793823242,
0.4866666793823242,
0.4866666793823242,
0.4866666793823242,
0.47999998927116394,
0.47999998927116394,
0.47999998927116394,
0.47999998927116394,
0.47333332896232605,
0.47333332896232605,
0.47333332896232605,
0.46666666865348816,
0.47333332896232605,
0.47333332896232605,
0.47333332896232605,
0.47333332896232605,
0.47333332896232605,
0.47333332896232605,
0.47333332896232605,
0.47333332896232605,
0.47999998927116394,
0.47999998927116394,
0.4866666793823242,
0.47999998927116394,
0.4866666793823242,
0.4866666793823242,
0.4866666793823242,
0.4866666793823242,
0.4866666793823242,
0.4866666793823242,
0.5066666603088379,
0.5,
0.5,
0.5,
0.5066666603088379,
0.5199999809265137,
0.5133333206176758,
0.5199999809265137,
0.5133333206176758,
0.5133333206176758,
0.5133333206176758,
0.5133333206176758,
0.5133333206176758,
0.5066666603088379,
0.5066666603088379,
0.5066666603088379,
0.5066666603088379,
0.5066666603088379,
0.5066666603088379,
0.5066666603088379,
0.5066666603088379,
0.5066666603088379,
0.5066666603088379,
0.5066666603088379,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5066666603088379,
0.5066666603088379,
0.5066666603088379],
'loss': [0.2774141728878021,
0.25784316658973694,
0.2547556161880493,
0.2535296678543091,
0.25298115611076355,
0.2519757151603699,
0.2515566647052765,
0.2514573931694031,
0.2513832747936249,
0.2513198256492615,
0.25126293301582336,
0.2512091398239136,
0.2511564791202545,
0.2511049509048462,
0.2510550022125244,
0.25101029872894287,
0.2509749233722687,
0.2509461045265198,
0.25092363357543945,
0.25091198086738586,
0.250911682844162,
0.2509240210056305,
0.25094786286354065,
0.2509719729423523,
0.2509922683238983,
0.2510092556476593,
0.25102347135543823,
0.25103577971458435,
0.2510460317134857,
0.251054584980011,
0.25106126070022583,
0.25106653571128845,
0.25107306241989136,
0.2510825991630554,
0.2510966360569,
0.25111594796180725,
0.2511384189128876,
0.2511593699455261,
0.2511741816997528,
0.25118300318717957,
0.25118836760520935,
0.2511923909187317,
0.2511962652206421,
0.25120073556900024,
0.2512059807777405,
0.2512124180793762,
0.25122010707855225,
0.2512294054031372,
0.25124022364616394,
0.25125235319137573,
0.25126609206199646,
0.25128093361854553,
0.25129666924476624,
0.25131306052207947,
0.25132957100868225,
0.25134608149528503,
0.25136229395866394,
0.25137799978256226,
0.25139302015304565,
0.2514074146747589,
0.25142133235931396,
0.25143492221832275,
0.25144851207733154,
0.25146248936653137,
0.25147679448127747,
0.25149133801460266,
0.2515051066875458,
0.2515164911746979,
0.24989812076091766,
0.24986378848552704,
0.24985122680664062,
0.24984575808048248,
0.24984583258628845,
0.24984899163246155,
0.24985259771347046,
0.24985529482364655,
0.24985626339912415,
0.24985533952713013,
0.2498527467250824,
0.24984867870807648,
0.24984359741210938,
0.24983790516853333,
0.24983182549476624,
0.2498255968093872,
0.24981941282749176,
0.24981333315372467,
0.24980764091014862,
0.2498018890619278,
0.24979649484157562,
0.24979138374328613,
0.24978633224964142,
0.2497815191745758,
0.24977688491344452,
0.24977241456508636,
0.24976806342601776,
0.24976392090320587,
0.2497599720954895,
0.24975618720054626,
0.24975261092185974,
0.24974924325942993],
'learning_rate': [0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0010000000474974513,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257,
0.0005000000237487257]} LSTM¶
In [117]:
from skorch.callbacks import LRScheduler, EarlyStopping, Checkpoint
from torch.optim.lr_scheduler import ReduceLROnPlateau
In [118]:
# build a vanilla RNN with a classification head
class LSTM(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(LSTM, self).__init__()
self.rnn = nn.LSTM(input_size, hidden_size, batch_first=True)
self.fc = nn.Linear(hidden_size, output_size)
def forward(self, x):
x = torch.swapaxes(x, 1, 2) # swap the last two dimensions to match RNN input shape
out, _ = self.rnn(x)
out = out[:, -1, :] # take the last output of the sequence
out = self.fc(out)
return out
In [119]:
lstm = NeuralNetClassifier(
LSTM,
module__input_size=X_gunpoint_train.shape[1],
module__hidden_size=64,
module__output_size=len(le.classes_),
criterion=nn.CrossEntropyLoss,
optimizer=torch.optim.Adam,
max_epochs=500,
batch_size=32,
lr=1e-4,
callbacks=[
EpochScoring(
scoring='accuracy',
name='train_acc',
on_train=True,
),
Checkpoint(
monitor="valid_acc_best",
load_best=True,
f_history=None,
f_optimizer=None,
)
],
device=DEVICE,
train_split=ValidSplit(cv=5, stratified=True)
)
lstm
Out[119]:
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized]( module=<class '__main__.LSTM'>, module__hidden_size=64, module__input_size=1, module__output_size=2, )In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized]( module=<class '__main__.LSTM'>, module__hidden_size=64, module__input_size=1, module__output_size=2, )
In [120]:
lstm.fit(X_gunpoint_train.astype(np.float32), y_gunpoint_train)
epoch train_acc train_loss valid_acc valid_loss cp dur
------- ----------- ------------ ----------- ------------ ---- ------
1 0.4917 0.6955 0.5000 0.6944 + 0.1399
2 0.4917 0.6953 0.5000 0.6943 0.0955
3 0.4917 0.6951 0.5000 0.6942 0.0733
4 0.4917 0.6950 0.5000 0.6942 0.0840
5 0.4917 0.6949 0.5000 0.6941 0.1034
6 0.4917 0.6948 0.5000 0.6940 0.1084
7 0.4917 0.6946 0.5000 0.6940 0.0997
8 0.4917 0.6946 0.5000 0.6939 0.0741
9 0.4917 0.6945 0.5000 0.6938 0.0827
10 0.4917 0.6944 0.5000 0.6938 0.1006
11 0.4917 0.6943 0.5000 0.6937 0.1144
12 0.4917 0.6942 0.5000 0.6937 0.1055
13 0.4917 0.6941 0.5000 0.6937 0.0952
14 0.4917 0.6941 0.5000 0.6936 0.0754
15 0.4917 0.6940 0.5000 0.6936 0.0836
16 0.4917 0.6939 0.5000 0.6936 0.0941
17 0.4917 0.6939 0.5000 0.6935 0.1020
18 0.4917 0.6938 0.5000 0.6935 0.1046
19 0.4917 0.6938 0.5000 0.6935 0.1021
20 0.4917 0.6937 0.5000 0.6934 0.1029
21 0.4917 0.6937 0.5000 0.6934 0.1056
22 0.4917 0.6936 0.5000 0.6934 0.1023
23 0.4917 0.6936 0.5000 0.6934 0.1219
24 0.4917 0.6935 0.5000 0.6934 0.1026
25 0.4917 0.6935 0.5000 0.6934 0.1039
26 0.4917 0.6935 0.5000 0.6933 0.1042
27 0.4917 0.6934 0.5000 0.6933 0.1054
28 0.4917 0.6934 0.5000 0.6933 0.1049
29 0.4917 0.6934 0.5000 0.6933 0.1043
30 0.4917 0.6933 0.5000 0.6933 0.1046
31 0.4917 0.6933 0.5000 0.6933 0.1051
32 0.4917 0.6933 0.5000 0.6933 0.1024
33 0.4917 0.6932 0.5000 0.6933 0.1039
34 0.4917 0.6932 0.5000 0.6933 0.1060
35 0.4917 0.6932 0.5000 0.6932 0.1039
36 0.4917 0.6932 0.5000 0.6932 0.1053
37 0.4917 0.6931 0.5000 0.6932 0.1063
38 0.4917 0.6931 0.5000 0.6932 0.1039
39 0.4917 0.6931 0.5000 0.6932 0.1048
40 0.4917 0.6931 0.5000 0.6932 0.1053
41 0.4917 0.6931 0.5000 0.6932 0.1041
42 0.4750 0.6930 0.4000 0.6932 0.1035
43 0.5583 0.6930 0.3667 0.6932 0.1069
44 0.6250 0.6930 0.5000 0.6932 0.1042
45 0.5583 0.6930 0.5000 0.6932 0.1047
46 0.5500 0.6930 0.5000 0.6932 0.1041
47 0.5333 0.6929 0.5000 0.6932 0.1047
48 0.5167 0.6929 0.5000 0.6932 0.1050
49 0.5167 0.6929 0.5000 0.6932 0.1039
50 0.5167 0.6929 0.5000 0.6932 0.1052
51 0.5167 0.6929 0.5000 0.6932 0.1037
52 0.5167 0.6929 0.5000 0.6932 0.1054
53 0.5167 0.6928 0.5000 0.6932 0.1040
54 0.5167 0.6928 0.5000 0.6932 0.1041
55 0.5167 0.6928 0.5000 0.6932 0.1048
56 0.5167 0.6928 0.5000 0.6932 0.1059
57 0.5167 0.6928 0.5000 0.6932 0.1060
58 0.5167 0.6928 0.5000 0.6932 0.1047
59 0.5167 0.6927 0.5000 0.6932 0.1062
60 0.5167 0.6927 0.5000 0.6932 0.1046
61 0.5167 0.6927 0.5000 0.6931 0.1048
62 0.5167 0.6927 0.5000 0.6931 0.1063
63 0.5167 0.6927 0.5000 0.6931 0.1051
64 0.5167 0.6927 0.5000 0.6931 0.1064
65 0.5167 0.6927 0.5000 0.6931 0.1046
66 0.5167 0.6926 0.5000 0.6931 0.1051
67 0.5167 0.6926 0.5000 0.6931 0.1033
68 0.5250 0.6926 0.5000 0.6931 0.1016
69 0.5250 0.6926 0.5000 0.6931 0.1032
70 0.5250 0.6926 0.5000 0.6931 0.1043
71 0.5250 0.6926 0.5000 0.6931 0.1045
72 0.5250 0.6926 0.5000 0.6931 0.1026
73 0.5250 0.6925 0.5000 0.6931 0.1035
74 0.5333 0.6925 0.5000 0.6931 0.1041
75 0.5333 0.6925 0.5000 0.6931 0.1051
76 0.5333 0.6925 0.5000 0.6931 0.1054
77 0.5333 0.6925 0.5000 0.6931 0.1045
78 0.5417 0.6925 0.5000 0.6931 0.1043
79 0.5417 0.6924 0.5000 0.6931 0.1043
80 0.5417 0.6924 0.5000 0.6931 0.1041
81 0.5500 0.6924 0.5000 0.6931 0.1030
82 0.5500 0.6924 0.5000 0.6931 0.1029
83 0.5500 0.6924 0.5000 0.6930 0.1049
84 0.5500 0.6924 0.5000 0.6930 0.1065
85 0.5500 0.6923 0.5000 0.6930 0.1053
86 0.5500 0.6923 0.5000 0.6930 0.1051
87 0.5500 0.6923 0.5000 0.6930 0.1299
88 0.5500 0.6923 0.5000 0.6930 0.1042
89 0.5500 0.6923 0.5000 0.6930 0.1046
90 0.5500 0.6922 0.5000 0.6930 0.1044
91 0.5500 0.6922 0.5000 0.6930 0.1052
92 0.5500 0.6922 0.5000 0.6930 0.1036
93 0.5500 0.6922 0.5000 0.6930 0.1048
94 0.5500 0.6922 0.5000 0.6930 0.1050
95 0.5500 0.6921 0.5000 0.6930 0.1041
96 0.5500 0.6921 0.5000 0.6930 0.1044
97 0.5500 0.6921 0.5000 0.6930 0.1054
98 0.5500 0.6921 0.5000 0.6929 0.1055
99 0.5500 0.6921 0.5000 0.6929 0.1076
100 0.5583 0.6920 0.5000 0.6929 0.1048
101 0.5583 0.6920 0.5000 0.6929 0.1053
102 0.5583 0.6920 0.5000 0.6929 0.1060
103 0.5667 0.6920 0.5000 0.6929 0.1037
104 0.5667 0.6919 0.5000 0.6929 0.0966
105 0.5750 0.6919 0.5000 0.6929 0.0756
106 0.5750 0.6919 0.5000 0.6929 0.0856
107 0.5750 0.6919 0.5000 0.6929 0.1024
108 0.5750 0.6918 0.5000 0.6928 0.1057
109 0.5750 0.6918 0.5000 0.6928 0.1052
110 0.5833 0.6918 0.5000 0.6928 0.1051
111 0.5833 0.6917 0.5000 0.6928 0.1038
112 0.5833 0.6917 0.5000 0.6928 0.1037
113 0.6000 0.6917 0.5000 0.6928 0.1046
114 0.6000 0.6916 0.5000 0.6928 0.1051
115 0.6000 0.6916 0.5000 0.6928 0.1044
116 0.6000 0.6916 0.5000 0.6927 0.1044
117 0.6083 0.6915 0.5000 0.6927 0.1039
118 0.6083 0.6915 0.5000 0.6927 0.1041
119 0.6083 0.6915 0.5000 0.6927 0.1046
120 0.6083 0.6914 0.5000 0.6927 0.1059
121 0.6083 0.6914 0.5000 0.6927 0.1044
122 0.6000 0.6913 0.5000 0.6926 0.1045
123 0.5917 0.6913 0.5000 0.6926 0.1040
124 0.6000 0.6912 0.5000 0.6926 0.1052
125 0.5917 0.6912 0.5000 0.6926 0.1050
126 0.6000 0.6911 0.5000 0.6926 0.1051
127 0.6000 0.6911 0.5000 0.6925 0.1052
128 0.5917 0.6910 0.5000 0.6925 0.1050
129 0.5917 0.6909 0.5000 0.6925 0.1050
130 0.5917 0.6909 0.5000 0.6925 0.1041
131 0.5917 0.6908 0.5000 0.6924 0.1044
132 0.5833 0.6907 0.5000 0.6924 0.1050
133 0.5750 0.6906 0.5000 0.6924 0.1051
134 0.5750 0.6905 0.5000 0.6923 0.1069
135 0.5667 0.6904 0.5000 0.6923 0.1056
136 0.5667 0.6903 0.5333 0.6922 + 0.1071
137 0.5750 0.6902 0.5333 0.6922 0.1042
138 0.5833 0.6900 0.5333 0.6921 0.1051
139 0.5750 0.6899 0.5333 0.6921 0.1035
140 0.5667 0.6897 0.5000 0.6920 0.1023
141 0.5750 0.6895 0.5000 0.6919 0.1045
142 0.5750 0.6893 0.5000 0.6918 0.1050
143 0.5750 0.6890 0.5333 0.6917 0.1062
144 0.5833 0.6887 0.5333 0.6915 0.1043
145 0.5667 0.6883 0.5667 0.6913 + 0.1051
146 0.5667 0.6879 0.5667 0.6911 0.1038
147 0.5917 0.6873 0.6000 0.6907 + 0.1053
148 0.5833 0.6866 0.6000 0.6903 0.1068
149 0.5833 0.6856 0.6000 0.6895 0.1054
150 0.5667 0.6841 0.5667 0.6883 0.1057
151 0.5333 0.6820 0.6000 0.6860 0.1059
152 0.5167 0.6785 0.5667 0.6808 0.1049
153 0.4917 0.6718 0.5333 0.6684 0.1034
154 0.4917 0.6594 0.5667 0.6480 0.1049
155 0.5417 0.6407 0.6667 0.6362 + 0.1047
156 0.7000 0.6109 0.6667 0.6259 0.1042
157 0.7333 0.5714 0.7000 0.5904 + 0.1056
158 0.7167 0.5497 0.6667 0.6162 0.1057
159 0.7417 0.5315 0.7000 0.5815 0.1060
160 0.7417 0.5100 0.7000 0.5780 0.1311
161 0.7500 0.4990 0.6667 0.6315 0.1042
162 0.6833 0.6250 0.5667 0.8212 0.1051
163 0.5583 0.8729 0.5667 0.8084 0.1035
164 0.5000 0.9602 0.5333 0.8458 0.1042
165 0.5000 0.9251 0.5333 0.8195 0.1048
166 0.5000 0.8882 0.5667 0.7485 0.1045
167 0.5167 0.8318 0.5333 0.7787 0.1035
168 0.4917 0.8462 0.5333 0.7754 0.1045
169 0.4917 0.8399 0.5000 0.8088 0.1053
170 0.4083 0.9241 0.3333 0.9811 0.1050
171 0.3917 0.9273 0.3333 0.9627 0.1047
172 0.3833 0.9126 0.3333 0.9390 0.1038
173 0.3833 0.8885 0.3667 0.8887 0.1033
174 0.3917 0.8590 0.3667 0.8701 0.1059
175 0.4167 0.8206 0.4333 0.8059 0.1038
176 0.4167 0.8049 0.4667 0.7730 0.1053
177 0.4333 0.7812 0.5000 0.7458 0.1047
178 0.4583 0.7563 0.5000 0.7418 0.1060
179 0.4750 0.7403 0.5000 0.7386 0.1128
180 0.4750 0.7356 0.5333 0.7187 0.1054
181 0.4667 0.7358 0.5333 0.7174 0.1046
182 0.4667 0.7325 0.5333 0.7162 0.1038
183 0.4667 0.7296 0.5333 0.7152 0.1042
184 0.4667 0.7272 0.5333 0.7143 0.1045
185 0.4667 0.7251 0.5333 0.7135 0.1055
186 0.4667 0.7232 0.5333 0.7128 0.1046
187 0.4667 0.7215 0.5333 0.7121 0.1044
188 0.4667 0.7200 0.5333 0.7115 0.1045
189 0.4667 0.7187 0.5333 0.7110 0.1054
190 0.4667 0.7174 0.5333 0.7104 0.1037
191 0.4667 0.7163 0.5333 0.7099 0.1043
192 0.4667 0.7153 0.5333 0.7094 0.1040
193 0.4667 0.7144 0.5333 0.7089 0.1046
194 0.4750 0.7108 0.5000 0.7188 0.1062
195 0.4750 0.7101 0.5000 0.7181 0.1063
196 0.4750 0.7094 0.5000 0.7174 0.1050
197 0.4750 0.7088 0.5000 0.7168 0.1070
198 0.4750 0.7083 0.5000 0.7162 0.1048
199 0.4750 0.7077 0.5000 0.7156 0.1041
200 0.4667 0.7094 0.5000 0.7150 0.1059
201 0.4583 0.7112 0.5000 0.7145 0.1044
202 0.4583 0.7107 0.5000 0.7139 0.1055
203 0.4583 0.7102 0.5000 0.7134 0.1038
204 0.4583 0.7096 0.5000 0.7130 0.1059
205 0.4250 0.7113 0.5000 0.7125 0.1031
206 0.5167 0.7087 0.5000 0.7120 0.1124
207 0.5083 0.7082 0.5000 0.7116 0.1048
208 0.5083 0.7077 0.5000 0.7112 0.1051
209 0.5083 0.7073 0.5000 0.7108 0.1064
210 0.5083 0.7069 0.5000 0.7104 0.1043
211 0.5083 0.7065 0.5000 0.7100 0.1036
212 0.5083 0.7042 0.5000 0.7096 0.1042
213 0.5083 0.7039 0.5000 0.7093 0.1046
214 0.5083 0.7036 0.5000 0.7089 0.1034
215 0.5083 0.7033 0.5000 0.7085 0.1059
216 0.5083 0.7014 0.5000 0.7082 0.1037
217 0.5083 0.7012 0.5000 0.7078 0.1060
218 0.5083 0.7009 0.5000 0.7075 0.1035
219 0.5083 0.7007 0.5000 0.7072 0.1051
220 0.5083 0.7005 0.5000 0.7069 0.1036
221 0.5083 0.7002 0.5000 0.7065 0.1044
222 0.5083 0.7000 0.5000 0.7062 0.1024
223 0.5083 0.6998 0.5000 0.7059 0.1053
224 0.5083 0.6996 0.5000 0.7057 0.1057
225 0.5083 0.6994 0.5000 0.7054 0.1041
226 0.5083 0.6993 0.5000 0.7051 0.1060
227 0.5083 0.6991 0.5000 0.7049 0.1051
228 0.5083 0.6989 0.5000 0.7046 0.1048
229 0.5083 0.6987 0.5000 0.7044 0.1037
230 0.5083 0.6985 0.5000 0.7041 0.1266
231 0.5083 0.6984 0.5000 0.7039 0.1042
232 0.5083 0.6982 0.5000 0.7036 0.1053
233 0.5083 0.6981 0.5000 0.7034 0.1050
234 0.5083 0.6979 0.5000 0.6973 0.1056
235 0.5083 0.6977 0.5000 0.6971 0.1061
236 0.5083 0.6976 0.5000 0.6970 0.1066
237 0.5083 0.6988 0.5000 0.6968 0.1026
238 0.5083 0.6986 0.5000 0.6967 0.1056
239 0.5083 0.6984 0.5000 0.6966 0.1038
240 0.5083 0.6983 0.5000 0.6965 0.1049
241 0.5083 0.6981 0.5000 0.6963 0.1045
242 0.5083 0.6979 0.5000 0.6963 0.1090
243 0.5083 0.6977 0.5000 0.6962 0.1051
244 0.5083 0.6976 0.5000 0.6961 0.1069
245 0.5083 0.6974 0.5000 0.6960 0.1042
246 0.5083 0.6972 0.5000 0.6959 0.1064
247 0.5083 0.6971 0.5000 0.6958 0.1049
248 0.5083 0.6969 0.5000 0.6957 0.1053
249 0.5083 0.6968 0.5000 0.6956 0.1045
250 0.5083 0.6966 0.5000 0.6955 0.1043
251 0.5083 0.6965 0.5000 0.6954 0.1039
252 0.5083 0.6963 0.5000 0.6953 0.1048
253 0.5083 0.6962 0.5000 0.6953 0.1050
254 0.5083 0.6960 0.5000 0.6952 0.1034
255 0.5083 0.6959 0.5000 0.6951 0.1045
256 0.5083 0.6958 0.5000 0.6950 0.1035
257 0.5083 0.6956 0.5000 0.6949 0.1057
258 0.5083 0.6955 0.5000 0.6948 0.1042
259 0.5083 0.6954 0.5000 0.6947 0.1063
260 0.5083 0.6953 0.5000 0.6947 0.1049
261 0.5083 0.6951 0.5000 0.6946 0.1060
262 0.5083 0.6967 0.5000 0.6945 0.1039
263 0.5083 0.6949 0.5000 0.6944 0.1041
264 0.5083 0.6964 0.5000 0.6943 0.1040
265 0.5083 0.6962 0.5000 0.6978 0.1046
266 0.5083 0.6936 0.5000 0.7008 0.1046
267 0.5083 0.6998 0.5000 0.7136 0.1044
268 0.5083 0.7019 0.5000 0.7173 0.1049
269 0.5083 0.7022 0.5000 0.7123 0.1041
270 0.5083 0.6982 0.5000 0.6999 0.1033
271 0.5083 0.6978 0.5000 0.6998 0.1040
272 0.5083 0.6981 0.5000 0.6997 0.1038
273 0.5083 0.6970 0.5000 0.6995 0.1057
274 0.5083 0.6975 0.5000 0.6992 0.1033
275 0.5083 0.6966 0.5000 0.6989 0.1046
276 0.5083 0.6978 0.5000 0.7052 0.1051
277 0.5083 0.6984 0.5000 0.7075 0.1048
278 0.5083 0.6980 0.5000 0.7069 0.1040
279 0.5083 0.6977 0.5000 0.7063 0.1053
280 0.5083 0.6973 0.5000 0.7034 0.1039
281 0.5083 0.6962 0.5000 0.7029 0.1053
282 0.5083 0.6960 0.5000 0.7025 0.1048
283 0.5083 0.6957 0.5000 0.7021 0.1051
284 0.5083 0.6954 0.5000 0.7017 0.1063
285 0.5083 0.6958 0.5000 0.7014 0.1035
286 0.5083 0.6955 0.5000 0.7010 0.1051
287 0.5083 0.6947 0.5000 0.7006 0.1052
288 0.5083 0.6944 0.5000 0.7003 0.1054
289 0.5083 0.6942 0.5000 0.7000 0.1066
290 0.5083 0.6940 0.5000 0.6996 0.1059
291 0.5083 0.6938 0.5000 0.6993 0.1048
292 0.5083 0.6936 0.5000 0.6990 0.0729
293 0.5083 0.6934 0.5000 0.6987 0.0812
294 0.5083 0.6932 0.5000 0.6985 0.0942
295 0.5083 0.6930 0.5000 0.6982 0.1035
296 0.5083 0.6927 0.5000 0.6979 0.1063
297 0.5083 0.6925 0.5000 0.6976 0.1040
298 0.5083 0.6923 0.5000 0.6974 0.1106
299 0.5083 0.6921 0.5000 0.6972 0.1062
300 0.5083 0.6920 0.5000 0.6969 0.1162
301 0.5083 0.6918 0.5000 0.6943 0.1026
302 0.5083 0.6917 0.5000 0.6941 0.1054
303 0.5083 0.6915 0.5000 0.6940 0.1046
304 0.5083 0.6914 0.5000 0.6938 0.1047
305 0.5083 0.6912 0.5000 0.6937 0.1049
306 0.5083 0.6908 0.5000 0.6935 0.1041
307 0.5083 0.6907 0.5000 0.6933 0.1042
308 0.5083 0.6905 0.5000 0.6932 0.0944
309 0.5083 0.6904 0.5000 0.6931 0.0744
310 0.5083 0.6903 0.5000 0.6929 0.0811
311 0.5083 0.6901 0.5000 0.6928 0.0979
312 0.5083 0.6900 0.5000 0.6927 0.1052
313 0.5083 0.6899 0.5000 0.6925 0.1053
314 0.5083 0.6898 0.5000 0.6924 0.1074
315 0.5083 0.6897 0.5000 0.6923 0.1048
316 0.5083 0.6896 0.5000 0.6921 0.1066
317 0.5083 0.6894 0.5000 0.6920 0.1056
318 0.5083 0.6893 0.5000 0.6919 0.1059
319 0.5083 0.6892 0.5000 0.6918 0.1045
320 0.5083 0.6891 0.5000 0.6917 0.1070
321 0.5083 0.6890 0.5000 0.6916 0.1063
322 0.5083 0.6889 0.5000 0.6915 0.1039
323 0.5083 0.6888 0.5000 0.6914 0.1056
324 0.5083 0.6887 0.5000 0.6912 0.1041
325 0.5083 0.6886 0.5000 0.6911 0.1044
326 0.5083 0.6885 0.5000 0.6910 0.1035
327 0.5083 0.6884 0.5000 0.6909 0.1049
328 0.5083 0.6882 0.5000 0.6908 0.1040
329 0.5083 0.6881 0.5000 0.6907 0.1052
330 0.5083 0.6880 0.5000 0.6906 0.1052
331 0.5083 0.6879 0.5000 0.6905 0.1042
332 0.5083 0.6878 0.5000 0.6904 0.1036
333 0.5083 0.6877 0.5000 0.6903 0.1079
334 0.5083 0.6876 0.5000 0.6902 0.1040
335 0.5083 0.6875 0.5000 0.6902 0.1043
336 0.5083 0.6874 0.5000 0.6901 0.1039
337 0.5083 0.6873 0.5000 0.6900 0.1057
338 0.5083 0.6872 0.5000 0.6899 0.1056
339 0.5083 0.6871 0.5000 0.6898 0.1052
340 0.5083 0.6864 0.5000 0.6897 0.1043
341 0.5083 0.6863 0.5000 0.6896 0.1041
342 0.5083 0.6862 0.5000 0.6895 0.1039
343 0.5083 0.6862 0.5000 0.6894 0.1080
344 0.5083 0.6861 0.5000 0.6893 0.1115
345 0.5083 0.6860 0.5000 0.6892 0.0748
346 0.5083 0.6859 0.5000 0.6892 0.0804
347 0.5083 0.6858 0.5000 0.6891 0.0905
348 0.5083 0.6857 0.5000 0.6890 0.1049
349 0.5083 0.6856 0.5000 0.6889 0.1030
350 0.5083 0.6855 0.5000 0.6888 0.1036
351 0.5083 0.6854 0.5000 0.6887 0.1043
352 0.5083 0.6853 0.5000 0.6886 0.1050
353 0.5083 0.6852 0.5000 0.6885 0.1061
354 0.5083 0.6851 0.5000 0.6884 0.1407
355 0.5083 0.6850 0.5000 0.6883 0.1140
356 0.5083 0.6850 0.5000 0.6882 0.1086
357 0.5083 0.6849 0.5000 0.6881 0.1070
358 0.5083 0.6848 0.5000 0.6880 0.1071
359 0.5083 0.6847 0.5000 0.6879 0.1056
360 0.5083 0.6846 0.5000 0.6879 0.1048
361 0.5083 0.6846 0.5000 0.6878 0.1050
362 0.5083 0.6845 0.5000 0.6877 0.1035
363 0.5083 0.6844 0.5000 0.6876 0.1063
364 0.5083 0.6844 0.5000 0.6875 0.1030
365 0.5083 0.6843 0.5000 0.6874 0.1055
366 0.5083 0.6842 0.5000 0.6873 0.1049
367 0.5083 0.6841 0.5000 0.6872 0.1034
368 0.5083 0.6840 0.5000 0.6871 0.1049
369 0.5083 0.6840 0.5000 0.6870 0.1059
370 0.5083 0.6839 0.5000 0.6883 0.1034
371 0.5083 0.6838 0.5000 0.6882 0.1049
372 0.5083 0.6837 0.5000 0.6881 0.1051
373 0.5083 0.6837 0.5000 0.6880 0.1044
374 0.5083 0.6837 0.5000 0.6880 0.1044
375 0.5083 0.6836 0.5000 0.6879 0.1035
376 0.5083 0.6835 0.5000 0.6878 0.1032
377 0.5083 0.6834 0.5000 0.6877 0.1067
378 0.5083 0.6834 0.5000 0.6876 0.1053
379 0.5083 0.6833 0.5000 0.6875 0.1054
380 0.5083 0.6832 0.5000 0.6874 0.1070
381 0.5083 0.6831 0.5000 0.6873 0.1057
382 0.5083 0.6830 0.5000 0.6872 0.1067
383 0.5083 0.6831 0.5000 0.6872 0.1049
384 0.5083 0.6831 0.5000 0.6871 0.1060
385 0.5083 0.6830 0.5000 0.6870 0.1048
386 0.5167 0.6829 0.5000 0.6868 0.1043
387 0.5167 0.6828 0.5000 0.6867 0.1046
388 0.5167 0.6827 0.5000 0.6866 0.1046
389 0.5167 0.6826 0.5000 0.6865 0.1034
390 0.5167 0.6825 0.5333 0.6864 0.1060
391 0.5167 0.6824 0.5333 0.6863 0.1031
392 0.5167 0.6823 0.5333 0.6862 0.1080
393 0.5167 0.6823 0.5333 0.6861 0.1043
394 0.5167 0.6822 0.5333 0.6861 0.1058
395 0.5167 0.6821 0.5333 0.6860 0.1065
396 0.5167 0.6819 0.5333 0.6859 0.1046
397 0.5167 0.6818 0.5333 0.6858 0.1067
398 0.5250 0.6817 0.5333 0.6857 0.1044
399 0.5250 0.6816 0.5333 0.6856 0.1040
400 0.5250 0.6815 0.5333 0.6855 0.1043
401 0.5250 0.6814 0.5333 0.6854 0.1052
402 0.5250 0.6813 0.5333 0.6853 0.1047
403 0.5250 0.6815 0.5333 0.6852 0.1063
404 0.5250 0.6814 0.5333 0.6851 0.1039
405 0.5250 0.6813 0.5333 0.6850 0.1037
406 0.5250 0.6812 0.5333 0.6849 0.1036
407 0.5250 0.6811 0.5333 0.6849 0.1057
408 0.5250 0.6809 0.5333 0.6848 0.1037
409 0.5250 0.6808 0.5333 0.6846 0.1055
410 0.5250 0.6807 0.5333 0.6845 0.1037
411 0.5250 0.6806 0.5333 0.6844 0.1051
412 0.5250 0.6805 0.5333 0.6843 0.1046
413 0.5250 0.6798 0.5333 0.6841 0.1058
414 0.5333 0.6797 0.5333 0.6841 0.1034
415 0.5333 0.6796 0.5333 0.6840 0.1055
416 0.5333 0.6795 0.5333 0.6839 0.1046
417 0.5333 0.6793 0.5333 0.6839 0.1052
418 0.5333 0.6792 0.5333 0.6838 0.1033
419 0.5333 0.6791 0.5333 0.6837 0.1057
420 0.5333 0.6789 0.5333 0.6837 0.1028
421 0.5333 0.6788 0.5333 0.6836 0.1060
422 0.5333 0.6786 0.5333 0.6836 0.1232
423 0.5333 0.6784 0.5333 0.6835 0.1059
424 0.5333 0.6783 0.5333 0.6834 0.1057
425 0.5333 0.6781 0.5333 0.6834 0.1054
426 0.5333 0.6780 0.5333 0.6833 0.1063
427 0.5333 0.6778 0.5333 0.6832 0.1056
428 0.5333 0.6776 0.5333 0.6832 0.1058
429 0.5333 0.6774 0.5333 0.6831 0.1039
430 0.5333 0.6773 0.5333 0.6830 0.1052
431 0.5333 0.6771 0.5333 0.6829 0.1048
432 0.5417 0.6769 0.5333 0.6829 0.1037
433 0.5500 0.6767 0.5333 0.6828 0.1049
434 0.5583 0.6765 0.5333 0.6827 0.1057
435 0.5583 0.6763 0.5333 0.6826 0.1051
436 0.5583 0.6761 0.5333 0.6825 0.1059
437 0.5583 0.6759 0.5333 0.6824 0.1060
438 0.5583 0.6757 0.5333 0.6824 0.1040
439 0.5583 0.6754 0.5333 0.6823 0.1081
440 0.5583 0.6752 0.5333 0.6822 0.1049
441 0.5750 0.6750 0.5333 0.6821 0.1060
442 0.5750 0.6747 0.5333 0.6820 0.1049
443 0.5750 0.6745 0.5333 0.6819 0.1050
444 0.5750 0.6743 0.5333 0.6818 0.1043
445 0.5750 0.6740 0.5333 0.6817 0.1062
446 0.5750 0.6737 0.5333 0.6817 0.1050
447 0.5750 0.6735 0.5333 0.6816 0.1068
448 0.5750 0.6727 0.5333 0.6815 0.1064
449 0.5750 0.6724 0.5333 0.6815 0.1050
450 0.5750 0.6695 0.5333 0.6819 0.1055
451 0.5833 0.6697 0.5333 0.6816 0.1059
452 0.5833 0.6687 0.5333 0.6813 0.1034
453 0.5917 0.6682 0.5333 0.6815 0.1050
454 0.5917 0.6678 0.5333 0.6814 0.1055
455 0.5917 0.6673 0.5333 0.6809 0.1058
456 0.6000 0.6669 0.5333 0.6809 0.1047
457 0.5917 0.6665 0.5333 0.6809 0.1039
458 0.5917 0.6660 0.5333 0.6807 0.1041
459 0.5917 0.6655 0.5333 0.6804 0.1050
460 0.5917 0.6650 0.5333 0.6804 0.1061
461 0.5917 0.6645 0.5333 0.6803 0.1039
462 0.6000 0.6640 0.5333 0.6802 0.1036
463 0.6083 0.6635 0.5333 0.6800 0.1045
464 0.6167 0.6630 0.5000 0.6800 0.1045
465 0.6250 0.6624 0.5000 0.6799 0.1044
466 0.6333 0.6619 0.5000 0.6798 0.1045
467 0.6333 0.6613 0.5000 0.6797 0.1029
468 0.6417 0.6607 0.5000 0.6796 0.1057
469 0.6417 0.6601 0.5000 0.6796 0.1039
470 0.6417 0.6595 0.5000 0.6795 0.1056
471 0.6417 0.6588 0.5000 0.6793 0.1038
472 0.6500 0.6582 0.5000 0.6792 0.1042
473 0.6500 0.6575 0.5000 0.6791 0.1038
474 0.6500 0.6568 0.5000 0.6790 0.1045
475 0.6500 0.6560 0.5000 0.6789 0.1034
476 0.6500 0.6553 0.5000 0.6787 0.1056
477 0.6583 0.6545 0.5000 0.6786 0.1049
478 0.6583 0.6537 0.5000 0.6784 0.1056
479 0.6583 0.6528 0.5000 0.6782 0.1045
480 0.6667 0.6520 0.5000 0.6779 0.1045
481 0.6667 0.6511 0.5000 0.6776 0.1040
482 0.6667 0.6502 0.4667 0.6774 0.1057
483 0.6667 0.6492 0.4667 0.6770 0.1030
484 0.6667 0.6482 0.4667 0.6766 0.1040
485 0.6667 0.6472 0.4667 0.6762 0.1048
486 0.6750 0.6461 0.5000 0.6758 0.1041
487 0.6750 0.6450 0.5000 0.6752 0.1033
488 0.6750 0.6439 0.5000 0.6747 0.1063
489 0.6750 0.6427 0.5000 0.6740 0.1261
490 0.6750 0.6415 0.5000 0.6733 0.1023
491 0.6750 0.6403 0.5000 0.6726 0.1046
492 0.6750 0.6390 0.5000 0.6718 0.1043
493 0.6750 0.6377 0.5000 0.6708 0.1047
494 0.6833 0.6363 0.5000 0.6699 0.1057
495 0.6833 0.6348 0.5000 0.6688 0.1044
496 0.6833 0.6334 0.5000 0.6676 0.1017
497 0.6833 0.6318 0.5333 0.6664 0.1054
498 0.6833 0.6303 0.5333 0.6651 0.1049
499 0.6833 0.6286 0.5333 0.6636 0.1057
500 0.6833 0.6270 0.5333 0.6621 0.1036
Out[120]:
<class 'skorch.classifier.NeuralNetClassifier'>[initialized](
module_=LSTM(
(rnn): LSTM(1, 64, batch_first=True)
(fc): Linear(in_features=64, out_features=2, bias=True)
),
)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.classifier.NeuralNetClassifier'>[initialized](
module_=LSTM(
(rnn): LSTM(1, 64, batch_first=True)
(fc): Linear(in_features=64, out_features=2, bias=True)
),
)In [121]:
y_pred = lstm.predict(X_gunpoint_test.astype(np.float32))
accuracy_score(y_gunpoint_test, y_pred)
Out[121]:
0.64
In [122]:
history = lstm.history
plt.plot(history[:, 'train_loss'], label='train_loss')
plt.plot(history[:, 'valid_loss'], label='valid_loss')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show()
plt.plot(history[:, 'train_acc'], label='train_acc')
plt.plot(history[:, 'valid_acc'], label='valid_acc')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.show()
ResNet¶
In [70]:
from sktime.classification.deep_learning import ResNetClassifier
In [71]:
resnet = ResNetClassifier(n_epochs=10, random_state=0, verbose=True)
In [72]:
resnet.fit(X_gunpoint_train, y_gunpoint_train)
Model: "functional_2"
┏━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┓ ┃ Layer (type) ┃ Output Shape ┃ Param # ┃ Connected to ┃ ┡━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━┩ │ input_layer_2 │ (None, 150, 1) │ 0 │ - │ │ (InputLayer) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_2 (Conv1D) │ (None, 150, 64) │ 576 │ input_layer_2[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalization │ (None, 150, 64) │ 256 │ conv1d_2[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation │ (None, 150, 64) │ 0 │ batch_normalizat… │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_3 (Conv1D) │ (None, 150, 64) │ 20,544 │ activation[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 64) │ 256 │ conv1d_3[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation_1 │ (None, 150, 64) │ 0 │ batch_normalizat… │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_5 (Conv1D) │ (None, 150, 64) │ 128 │ input_layer_2[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_4 (Conv1D) │ (None, 150, 64) │ 12,352 │ activation_1[0][… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 64) │ 256 │ conv1d_5[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 64) │ 256 │ conv1d_4[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ add (Add) │ (None, 150, 64) │ 0 │ batch_normalizat… │ │ │ │ │ batch_normalizat… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation_2 │ (None, 150, 64) │ 0 │ add[0][0] │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_6 (Conv1D) │ (None, 150, 128) │ 65,664 │ activation_2[0][… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ conv1d_6[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation_3 │ (None, 150, 128) │ 0 │ batch_normalizat… │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_7 (Conv1D) │ (None, 150, 128) │ 82,048 │ activation_3[0][… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ conv1d_7[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation_4 │ (None, 150, 128) │ 0 │ batch_normalizat… │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_9 (Conv1D) │ (None, 150, 128) │ 8,320 │ activation_2[0][… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_8 (Conv1D) │ (None, 150, 128) │ 49,280 │ activation_4[0][… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ conv1d_9[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ conv1d_8[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ add_1 (Add) │ (None, 150, 128) │ 0 │ batch_normalizat… │ │ │ │ │ batch_normalizat… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation_5 │ (None, 150, 128) │ 0 │ add_1[0][0] │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_10 (Conv1D) │ (None, 150, 128) │ 131,200 │ activation_5[0][… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ conv1d_10[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation_6 │ (None, 150, 128) │ 0 │ batch_normalizat… │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_11 (Conv1D) │ (None, 150, 128) │ 82,048 │ activation_6[0][… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ conv1d_11[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation_7 │ (None, 150, 128) │ 0 │ batch_normalizat… │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_12 (Conv1D) │ (None, 150, 128) │ 49,280 │ activation_7[0][… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ activation_5[0][… │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ conv1d_12[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ add_2 (Add) │ (None, 150, 128) │ 0 │ batch_normalizat… │ │ │ │ │ batch_normalizat… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation_8 │ (None, 150, 128) │ 0 │ add_2[0][0] │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ global_average_poo… │ (None, 128) │ 0 │ activation_8[0][… │ │ (GlobalAveragePool… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ dense_2 (Dense) │ (None, 2) │ 258 │ global_average_p… │ └─────────────────────┴───────────────────┴────────────┴───────────────────┘
Total params: 506,818 (1.93 MB)
Trainable params: 504,258 (1.92 MB)
Non-trainable params: 2,560 (10.00 KB)
Epoch 1/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 4s 42ms/step - accuracy: 0.4763 - loss: 1.1951 Epoch 2/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 43ms/step - accuracy: 0.7362 - loss: 0.5622 Epoch 3/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 41ms/step - accuracy: 0.7077 - loss: 0.5196 Epoch 4/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 42ms/step - accuracy: 0.8288 - loss: 0.4016 Epoch 5/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 41ms/step - accuracy: 0.9248 - loss: 0.3191 Epoch 6/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 41ms/step - accuracy: 0.9036 - loss: 0.2759 Epoch 7/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 44ms/step - accuracy: 0.8970 - loss: 0.2546 Epoch 8/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 42ms/step - accuracy: 0.8490 - loss: 0.2944 Epoch 9/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 41ms/step - accuracy: 0.9584 - loss: 0.2019 Epoch 10/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 0s 42ms/step - accuracy: 0.9535 - loss: 0.1608
Out[72]:
ResNetClassifier(n_epochs=10, random_state=0, verbose=True)Please rerun this cell to show the HTML repr or trust the notebook.
ResNetClassifier(n_epochs=10, random_state=0, verbose=True)
In [73]:
resnet.score(X_gunpoint_test, y_gunpoint_test)
4/4 ━━━━━━━━━━━━━━━━━━━━ 0s 72ms/step
Out[73]:
0.5
InceptionTime¶
In [74]:
from sktime.classification.deep_learning import InceptionTimeClassifier
In [75]:
inception = InceptionTimeClassifier(random_state=0, verbose=True, n_epochs=10)
In [76]:
inception.fit(X_gunpoint_train, y_gunpoint_train)
Model: "functional_3"
┏━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┓ ┃ Layer (type) ┃ Output Shape ┃ Param # ┃ Connected to ┃ ┡━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━┩ │ input_layer_3 │ (None, 150, 1) │ 0 │ - │ │ (InputLayer) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ max_pooling1d │ (None, 150, 1) │ 0 │ input_layer_3[0]… │ │ (MaxPooling1D) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_13 (Conv1D) │ (None, 150, 32) │ 1,280 │ input_layer_3[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_14 (Conv1D) │ (None, 150, 32) │ 640 │ input_layer_3[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_15 (Conv1D) │ (None, 150, 32) │ 320 │ input_layer_3[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_16 (Conv1D) │ (None, 150, 32) │ 32 │ max_pooling1d[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ concatenate │ (None, 150, 128) │ 0 │ conv1d_13[0][0], │ │ (Concatenate) │ │ │ conv1d_14[0][0], │ │ │ │ │ conv1d_15[0][0], │ │ │ │ │ conv1d_16[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ concatenate[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation_9 │ (None, 150, 128) │ 0 │ batch_normalizat… │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_17 (Conv1D) │ (None, 150, 32) │ 4,096 │ activation_9[0][… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ max_pooling1d_1 │ (None, 150, 128) │ 0 │ activation_9[0][… │ │ (MaxPooling1D) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_18 (Conv1D) │ (None, 150, 32) │ 40,960 │ conv1d_17[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_19 (Conv1D) │ (None, 150, 32) │ 20,480 │ conv1d_17[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_20 (Conv1D) │ (None, 150, 32) │ 10,240 │ conv1d_17[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_21 (Conv1D) │ (None, 150, 32) │ 4,096 │ max_pooling1d_1[… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ concatenate_1 │ (None, 150, 128) │ 0 │ conv1d_18[0][0], │ │ (Concatenate) │ │ │ conv1d_19[0][0], │ │ │ │ │ conv1d_20[0][0], │ │ │ │ │ conv1d_21[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ concatenate_1[0]… │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation_10 │ (None, 150, 128) │ 0 │ batch_normalizat… │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_22 (Conv1D) │ (None, 150, 32) │ 4,096 │ activation_10[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ max_pooling1d_2 │ (None, 150, 128) │ 0 │ activation_10[0]… │ │ (MaxPooling1D) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_23 (Conv1D) │ (None, 150, 32) │ 40,960 │ conv1d_22[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_24 (Conv1D) │ (None, 150, 32) │ 20,480 │ conv1d_22[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_25 (Conv1D) │ (None, 150, 32) │ 10,240 │ conv1d_22[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_26 (Conv1D) │ (None, 150, 32) │ 4,096 │ max_pooling1d_2[… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ concatenate_2 │ (None, 150, 128) │ 0 │ conv1d_23[0][0], │ │ (Concatenate) │ │ │ conv1d_24[0][0], │ │ │ │ │ conv1d_25[0][0], │ │ │ │ │ conv1d_26[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_27 (Conv1D) │ (None, 150, 128) │ 128 │ input_layer_3[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ concatenate_2[0]… │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ conv1d_27[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation_11 │ (None, 150, 128) │ 0 │ batch_normalizat… │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ add_3 (Add) │ (None, 150, 128) │ 0 │ batch_normalizat… │ │ │ │ │ activation_11[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation_12 │ (None, 150, 128) │ 0 │ add_3[0][0] │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_28 (Conv1D) │ (None, 150, 32) │ 4,096 │ activation_12[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ max_pooling1d_3 │ (None, 150, 128) │ 0 │ activation_12[0]… │ │ (MaxPooling1D) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_29 (Conv1D) │ (None, 150, 32) │ 40,960 │ conv1d_28[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_30 (Conv1D) │ (None, 150, 32) │ 20,480 │ conv1d_28[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_31 (Conv1D) │ (None, 150, 32) │ 10,240 │ conv1d_28[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_32 (Conv1D) │ (None, 150, 32) │ 4,096 │ max_pooling1d_3[… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ concatenate_3 │ (None, 150, 128) │ 0 │ conv1d_29[0][0], │ │ (Concatenate) │ │ │ conv1d_30[0][0], │ │ │ │ │ conv1d_31[0][0], │ │ │ │ │ conv1d_32[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ concatenate_3[0]… │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation_13 │ (None, 150, 128) │ 0 │ batch_normalizat… │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_33 (Conv1D) │ (None, 150, 32) │ 4,096 │ activation_13[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ max_pooling1d_4 │ (None, 150, 128) │ 0 │ activation_13[0]… │ │ (MaxPooling1D) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_34 (Conv1D) │ (None, 150, 32) │ 40,960 │ conv1d_33[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_35 (Conv1D) │ (None, 150, 32) │ 20,480 │ conv1d_33[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_36 (Conv1D) │ (None, 150, 32) │ 10,240 │ conv1d_33[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_37 (Conv1D) │ (None, 150, 32) │ 4,096 │ max_pooling1d_4[… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ concatenate_4 │ (None, 150, 128) │ 0 │ conv1d_34[0][0], │ │ (Concatenate) │ │ │ conv1d_35[0][0], │ │ │ │ │ conv1d_36[0][0], │ │ │ │ │ conv1d_37[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ concatenate_4[0]… │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation_14 │ (None, 150, 128) │ 0 │ batch_normalizat… │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_38 (Conv1D) │ (None, 150, 32) │ 4,096 │ activation_14[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ max_pooling1d_5 │ (None, 150, 128) │ 0 │ activation_14[0]… │ │ (MaxPooling1D) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_39 (Conv1D) │ (None, 150, 32) │ 40,960 │ conv1d_38[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_40 (Conv1D) │ (None, 150, 32) │ 20,480 │ conv1d_38[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_41 (Conv1D) │ (None, 150, 32) │ 10,240 │ conv1d_38[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_42 (Conv1D) │ (None, 150, 32) │ 4,096 │ max_pooling1d_5[… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ concatenate_5 │ (None, 150, 128) │ 0 │ conv1d_39[0][0], │ │ (Concatenate) │ │ │ conv1d_40[0][0], │ │ │ │ │ conv1d_41[0][0], │ │ │ │ │ conv1d_42[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_43 (Conv1D) │ (None, 150, 128) │ 16,384 │ activation_12[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ concatenate_5[0]… │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ conv1d_43[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation_15 │ (None, 150, 128) │ 0 │ batch_normalizat… │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ add_4 (Add) │ (None, 150, 128) │ 0 │ batch_normalizat… │ │ │ │ │ activation_15[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation_16 │ (None, 150, 128) │ 0 │ add_4[0][0] │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ global_average_poo… │ (None, 128) │ 0 │ activation_16[0]… │ │ (GlobalAveragePool… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ dense_3 (Dense) │ (None, 2) │ 258 │ global_average_p… │ └─────────────────────┴───────────────────┴────────────┴───────────────────┘
Total params: 422,498 (1.61 MB)
Trainable params: 420,450 (1.60 MB)
Non-trainable params: 2,048 (8.00 KB)
Epoch 1/10 3/3 ━━━━━━━━━━━━━━━━━━━━ 4s 115ms/step - accuracy: 0.5317 - loss: 0.8970 - learning_rate: 0.0010 Epoch 2/10 3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 110ms/step - accuracy: 0.7971 - loss: 0.4281 - learning_rate: 0.0010 Epoch 3/10 3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 112ms/step - accuracy: 0.9624 - loss: 0.2283 - learning_rate: 0.0010 Epoch 4/10 3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 123ms/step - accuracy: 0.9855 - loss: 0.1042 - learning_rate: 0.0010 Epoch 5/10 3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 111ms/step - accuracy: 0.9855 - loss: 0.0575 - learning_rate: 0.0010 Epoch 6/10 3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 117ms/step - accuracy: 1.0000 - loss: 0.0346 - learning_rate: 0.0010 Epoch 7/10 3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 120ms/step - accuracy: 1.0000 - loss: 0.0233 - learning_rate: 0.0010 Epoch 8/10 3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 125ms/step - accuracy: 1.0000 - loss: 0.0157 - learning_rate: 0.0010 Epoch 9/10 3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 123ms/step - accuracy: 1.0000 - loss: 0.0151 - learning_rate: 0.0010 Epoch 10/10 3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 127ms/step - accuracy: 1.0000 - loss: 0.0126 - learning_rate: 0.0010
Out[76]:
InceptionTimeClassifier(n_epochs=10, random_state=0, verbose=True)Please rerun this cell to show the HTML repr or trust the notebook.
InceptionTimeClassifier(n_epochs=10, random_state=0, verbose=True)
In [77]:
inception.score(X_gunpoint_test, y_gunpoint_test)
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 267ms/step
Out[77]:
0.72
LSTM + FCN¶
In [78]:
from sktime.classification.deep_learning import LSTMFCNClassifier
In [79]:
lstmfcn = LSTMFCNClassifier(n_epochs=10, random_state=0, verbose=True)
In [80]:
lstmfcn.fit(X_gunpoint_train, y_gunpoint_train)
Model: "functional_4"
┏━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┓ ┃ Layer (type) ┃ Output Shape ┃ Param # ┃ Connected to ┃ ┡━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━┩ │ input_layer_4 │ (None, 150, 1) │ 0 │ - │ │ (InputLayer) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_44 (Conv1D) │ (None, 150, 128) │ 1,152 │ input_layer_4[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ conv1d_44[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation_17 │ (None, 150, 128) │ 0 │ batch_normalizat… │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_45 (Conv1D) │ (None, 150, 256) │ 164,096 │ activation_17[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 256) │ 1,024 │ conv1d_45[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation_18 │ (None, 150, 256) │ 0 │ batch_normalizat… │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_46 (Conv1D) │ (None, 150, 128) │ 98,432 │ activation_18[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ permute (Permute) │ (None, 1, 150) │ 0 │ input_layer_4[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ conv1d_46[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ lstm (LSTM) │ (None, 8) │ 5,088 │ permute[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ activation_19 │ (None, 150, 128) │ 0 │ batch_normalizat… │ │ (Activation) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ dropout (Dropout) │ (None, 8) │ 0 │ lstm[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ global_average_poo… │ (None, 128) │ 0 │ activation_19[0]… │ │ (GlobalAveragePool… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ concatenate_6 │ (None, 136) │ 0 │ dropout[0][0], │ │ (Concatenate) │ │ │ global_average_p… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ dense_4 (Dense) │ (None, 2) │ 274 │ concatenate_6[0]… │ └─────────────────────┴───────────────────┴────────────┴───────────────────┘
Total params: 271,090 (1.03 MB)
Trainable params: 270,066 (1.03 MB)
Non-trainable params: 1,024 (4.00 KB)
Epoch 1/10 2/2 ━━━━━━━━━━━━━━━━━━━━ 2s 55ms/step - accuracy: 0.5159 - loss: 0.7421 Epoch 2/10 2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 42ms/step - accuracy: 0.6992 - loss: 0.5798 Epoch 3/10 2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 47ms/step - accuracy: 0.7240 - loss: 0.5544 Epoch 4/10 2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 42ms/step - accuracy: 0.7285 - loss: 0.5423 Epoch 5/10 2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 44ms/step - accuracy: 0.7470 - loss: 0.5082 Epoch 6/10 2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 41ms/step - accuracy: 0.7311 - loss: 0.4882 Epoch 7/10 2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 43ms/step - accuracy: 0.7355 - loss: 0.4820 Epoch 8/10 2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 40ms/step - accuracy: 0.7964 - loss: 0.4511 Epoch 9/10 2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 44ms/step - accuracy: 0.7426 - loss: 0.4463 Epoch 10/10 2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 42ms/step - accuracy: 0.7752 - loss: 0.4243
Out[80]:
LSTMFCNClassifier(n_epochs=10, random_state=0, verbose=True)Please rerun this cell to show the HTML repr or trust the notebook.
LSTMFCNClassifier(n_epochs=10, random_state=0, verbose=True)
In [81]:
lstmfcn.score(X_gunpoint_test, y_gunpoint_test)
WARNING:tensorflow:5 out of the last 56 calls to <function TensorFlowTrainer.make_predict_function.<locals>.one_step_on_data_distributed at 0x33afbafc0> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details. 1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 148ms/step
Out[81]:
0.7
TapNet¶
In [82]:
from sktime.classification.deep_learning import TapNetClassifier
In [52]:
# !pip install keras-self-attention
In [83]:
tapnet = TapNetClassifier(random_state=0, verbose=True, n_epochs=10)
In [84]:
%%time
tapnet.fit(X_gunpoint_train, y_gunpoint_train)
Model: "functional_5"
┏━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┓ ┃ Layer (type) ┃ Output Shape ┃ Param # ┃ Connected to ┃ ┡━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━┩ │ input_layer_5 │ (None, 150, 1) │ 0 │ - │ │ (InputLayer) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ lambda (Lambda) │ (None, 150, 1) │ 0 │ input_layer_5[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ lambda_1 (Lambda) │ (None, 150, 1) │ 0 │ input_layer_5[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_47 (Conv1D) │ (None, 150, 256) │ 2,304 │ lambda[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_50 (Conv1D) │ (None, 150, 256) │ 2,304 │ lambda_1[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ lambda_2 (Lambda) │ (None, 150, 1) │ 0 │ input_layer_5[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 256) │ 1,024 │ conv1d_47[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 256) │ 1,024 │ conv1d_50[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_53 (Conv1D) │ (None, 150, 256) │ 2,304 │ lambda_2[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ leaky_re_lu │ (None, 150, 256) │ 0 │ batch_normalizat… │ │ (LeakyReLU) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ leaky_re_lu_3 │ (None, 150, 256) │ 0 │ batch_normalizat… │ │ (LeakyReLU) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 256) │ 1,024 │ conv1d_53[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_48 (Conv1D) │ (None, 150, 256) │ 524,544 │ leaky_re_lu[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_51 (Conv1D) │ (None, 150, 256) │ 524,544 │ leaky_re_lu_3[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ leaky_re_lu_6 │ (None, 150, 256) │ 0 │ batch_normalizat… │ │ (LeakyReLU) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 256) │ 1,024 │ conv1d_48[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 256) │ 1,024 │ conv1d_51[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_54 (Conv1D) │ (None, 150, 256) │ 524,544 │ leaky_re_lu_6[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ leaky_re_lu_1 │ (None, 150, 256) │ 0 │ batch_normalizat… │ │ (LeakyReLU) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ leaky_re_lu_4 │ (None, 150, 256) │ 0 │ batch_normalizat… │ │ (LeakyReLU) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 256) │ 1,024 │ conv1d_54[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_49 (Conv1D) │ (None, 150, 128) │ 262,272 │ leaky_re_lu_1[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_52 (Conv1D) │ (None, 150, 128) │ 262,272 │ leaky_re_lu_4[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ leaky_re_lu_7 │ (None, 150, 256) │ 0 │ batch_normalizat… │ │ (LeakyReLU) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ conv1d_49[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ conv1d_52[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ conv1d_55 (Conv1D) │ (None, 150, 128) │ 262,272 │ leaky_re_lu_7[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ leaky_re_lu_2 │ (None, 150, 128) │ 0 │ batch_normalizat… │ │ (LeakyReLU) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ leaky_re_lu_5 │ (None, 150, 128) │ 0 │ batch_normalizat… │ │ (LeakyReLU) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalizatio… │ (None, 150, 128) │ 512 │ conv1d_55[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ seq_self_attention… │ (None, 150, 128) │ 16,385 │ leaky_re_lu_2[0]… │ │ (SeqSelfAttention) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ seq_self_attention… │ (None, 150, 128) │ 16,385 │ leaky_re_lu_5[0]… │ │ (SeqSelfAttention) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ leaky_re_lu_8 │ (None, 150, 128) │ 0 │ batch_normalizat… │ │ (LeakyReLU) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ lstm_1 (LSTM) │ (None, 150, 128) │ 66,560 │ input_layer_5[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ global_average_poo… │ (None, 128) │ 0 │ seq_self_attenti… │ │ (GlobalAveragePool… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ global_average_poo… │ (None, 128) │ 0 │ seq_self_attenti… │ │ (GlobalAveragePool… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ seq_self_attention… │ (None, 150, 128) │ 16,385 │ leaky_re_lu_8[0]… │ │ (SeqSelfAttention) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ dropout_1 (Dropout) │ (None, 150, 128) │ 0 │ lstm_1[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ concatenate_7 │ (None, 256) │ 0 │ global_average_p… │ │ (Concatenate) │ │ │ global_average_p… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ global_average_poo… │ (None, 128) │ 0 │ seq_self_attenti… │ │ (GlobalAveragePool… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ seq_self_attention │ (None, 150, 128) │ 16,385 │ dropout_1[0][0] │ │ (SeqSelfAttention) │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ concatenate_8 │ (None, 384) │ 0 │ concatenate_7[0]… │ │ (Concatenate) │ │ │ global_average_p… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ global_average_poo… │ (None, 128) │ 0 │ seq_self_attenti… │ │ (GlobalAveragePool… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ concatenate_9 │ (None, 512) │ 0 │ concatenate_8[0]… │ │ (Concatenate) │ │ │ global_average_p… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ fc_ (Dense) │ (None, 500) │ 256,500 │ concatenate_9[0]… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ relu_ (LeakyReLU) │ (None, 500) │ 0 │ fc_[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ bn_ │ (None, 500) │ 2,000 │ relu_[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ fc_2 (Dense) │ (None, 300) │ 150,300 │ bn_[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ dense_5 (Dense) │ (None, 2) │ 602 │ fc_2[0][0] │ └─────────────────────┴───────────────────┴────────────┴───────────────────┘
Total params: 2,916,542 (11.13 MB)
Trainable params: 2,911,702 (11.11 MB)
Non-trainable params: 4,840 (18.91 KB)
Epoch 1/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 6s 156ms/step - accuracy: 0.5016 - loss: 5.0530 Epoch 2/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 2s 160ms/step - accuracy: 0.7918 - loss: 1.4162 Epoch 3/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 2s 160ms/step - accuracy: 0.8881 - loss: 0.9515 Epoch 4/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 2s 165ms/step - accuracy: 0.8976 - loss: 0.7730 Epoch 5/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 2s 166ms/step - accuracy: 0.9577 - loss: 0.2217 Epoch 6/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 2s 162ms/step - accuracy: 0.9340 - loss: 0.1735 Epoch 7/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 2s 162ms/step - accuracy: 0.9686 - loss: 0.1477 Epoch 8/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 2s 163ms/step - accuracy: 0.9704 - loss: 0.1198 Epoch 9/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 2s 160ms/step - accuracy: 0.9690 - loss: 0.1037 Epoch 10/10 10/10 ━━━━━━━━━━━━━━━━━━━━ 2s 165ms/step - accuracy: 0.9747 - loss: 0.0886 CPU times: user 1min 23s, sys: 14.3 s, total: 1min 37s Wall time: 21.4 s
Out[84]:
TapNetClassifier(n_epochs=10, random_state=0, verbose=True)Please rerun this cell to show the HTML repr or trust the notebook.
TapNetClassifier(n_epochs=10, random_state=0, verbose=True)
In [85]:
tapnet.score(X_gunpoint_test, y_gunpoint_test)
WARNING:tensorflow:6 out of the last 57 calls to <function TensorFlowTrainer.make_predict_function.<locals>.one_step_on_data_distributed at 0x33afb89a0> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details. 4/4 ━━━━━━━━━━━━━━━━━━━━ 1s 141ms/step
Out[85]:
0.56
Kernel-based¶
In [86]:
from sktime.classification.kernel_based import RocketClassifier
In [96]:
%%time
rocket = RocketClassifier(random_state=0, rocket_transform="rocket")
rocket.fit(X_gunpoint_train, y_gunpoint_train)
rocket.score(X_gunpoint_test, y_gunpoint_test)
CPU times: user 12 s, sys: 198 ms, total: 12.2 s Wall time: 10.5 s
Out[96]:
0.52
In [88]:
%%time
rocket = RocketClassifier(random_state=0, rocket_transform="minirocket")
rocket.fit(X_gunpoint_train, y_gunpoint_train)
rocket.score(X_gunpoint_test, y_gunpoint_test)
CPU times: user 4.86 s, sys: 600 ms, total: 5.46 s Wall time: 2.55 s
Out[88]:
0.52
In [59]:
# %%time
## slow
# rocket = RocketClassifier(random_state=0, rocket_transform="multirocket")
# rocket.fit(X_gunpoint_train, y_gunpoint_train)
# rocket.score(X_gunpoint_test, y_gunpoint_test)
Aeon¶
In [ ]:
# !pip install aeon
In [89]:
from aeon.classification.convolution_based import MiniRocketClassifier, MultiRocketHydraClassifier
In [90]:
clf = MiniRocketClassifier(n_jobs=-1)
In [91]:
clf.fit(X_gunpoint_train, y_gunpoint_train)
Out[91]:
MiniRocketClassifier(n_jobs=-1)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
MiniRocketClassifier(n_jobs=-1)
In [92]:
clf.score(X_gunpoint_test, y_gunpoint_test)
Out[92]:
1.0
In [93]:
clf = MultiRocketHydraClassifier(n_jobs=-1)
In [94]:
clf.fit(X_gunpoint_train, y_gunpoint_train)
Out[94]:
MultiRocketHydraClassifier(n_jobs=-1)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
MultiRocketHydraClassifier(n_jobs=-1)
In [95]:
clf.score(X_gunpoint_test, y_gunpoint_test)
Out[95]:
1.0
Ensemble Hybrid¶
In [60]:
from sktime.classification.hybrid import HIVECOTEV2
In [61]:
# %%time
# # slow
# hivecote = HIVECOTEV2(random_state=0)
# hivecote.fit(X_gunpoint_train, y_gunpoint_train)
# hivecote.score(X_gunpoint_test, y_gunpoint_test)
In [ ]:
Some newer models are available only in the AEON library https://www.aeon-toolkit.org/en/stable/index.html. The syntax is the same as sktime.
XAI¶
Integrated Gradients¶
In [164]:
# !pip install captum
import torch
import numpy as np
import matplotlib.pyplot as plt
from captum.attr import IntegratedGradients
from captum.attr import GradientShap
In [167]:
instance_idx = 1
cnn.module_.to("cpu")
ig = IntegratedGradients(cnn.module_)
x = torch.as_tensor(
X_gunpoint_train[instance_idx:instance_idx+1, :, :],
dtype=torch.float32,
device="cpu"
)
target = int(y_gunpoint_train[instance_idx])
baseline = torch.zeros_like(x, device="cpu")
baseline = torch.as_tensor(
X_gunpoint_train.mean(axis=0, keepdims=True),
dtype=torch.float32,
device="cpu"
)
attributions = ig.attribute(
inputs=x,
baselines=baseline,
target=target
)
attributions = attributions.detach().cpu().numpy()
In [168]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.colors import TwoSlopeNorm
ts = X_gunpoint_train[0, 0, :]
attr = attributions[0, 0, :] # first instance, first channel
x_axis = np.arange(len(ts))
points = np.array([x_axis, ts]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
vmax = np.max(np.abs(attr))
norm = TwoSlopeNorm(vmin=-vmax, vcenter=0.0, vmax=vmax)
fig, ax = plt.subplots(dpi=300, figsize=(10, 5))
lc = LineCollection(
segments,
cmap="coolwarm",
norm=norm,
linewidth=2
)
# one attribution value per segment
lc.set_array(attr[:-1])
ax.add_collection(lc)
ax.set_xlim(x_axis.min(), x_axis.max())
ax.set_ylim(ts.min() - 0.1 * np.ptp(ts), ts.max() + 0.1 * np.ptp(ts))
cbar = plt.colorbar(lc, ax=ax)
cbar.set_label("Integrated gradients attribution")
ax.set_xlabel("Time")
ax.set_ylabel("Value")
ax.set_title("Time Series Colored by Attribution")
plt.show()
Subsequences¶
In [99]:
from sktime.transformations.panel.shapelet_transform import RandomShapeletTransform
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import plot_tree
In [100]:
trf = RandomShapeletTransform(n_shapelet_samples=100, random_state=0)
dt = DecisionTreeClassifier(random_state=0)
In [101]:
X_train_trf = trf.fit_transform(X_gunpoint_train, y_gunpoint_train)
X_test_trf = trf.transform(X_gunpoint_test)
In [102]:
dt.fit(X_train_trf, y_gunpoint_train)
Out[102]:
DecisionTreeClassifier(random_state=0)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
DecisionTreeClassifier(random_state=0)
In [103]:
dt.score(X_test_trf, y_gunpoint_test)
Out[103]:
0.94
In [104]:
shapelets = trf.shapelets
In [105]:
plt.figure(figsize=(5, 8))
plot_tree(dt, class_names=le.classes_, feature_names=[f"dist(shp_{i})" for i in range(X_train_trf.shape[1])], filled=True)
plt.show()
In [106]:
plt.plot(X_gunpoint_test[0, 0, :], label="Time Series")
plt.plot(shapelets[dt.tree_.feature[0]][-1], label="Shapelet")
plt.title(f"D(ts, shp)={np.round(X_test_trf.values[0, 0], 4)}")
plt.legend()
plt.show()
Counterfactuals¶
In [107]:
from sktime.classification.distance_based import KNeighborsTimeSeriesClassifier
from scipy.spatial import distance_matrix
In [108]:
clf = KNeighborsTimeSeriesClassifier(n_neighbors=1, distance="euclidean")
In [109]:
clf.fit(X_gunpoint_train, y_gunpoint_train)
Out[109]:
KNeighborsTimeSeriesClassifier(distance='euclidean')Please rerun this cell to show the HTML repr or trust the notebook.
KNeighborsTimeSeriesClassifier(distance='euclidean')
In [110]:
clf.score(X_gunpoint_test, y_gunpoint_test)
Out[110]:
0.92
In [111]:
y_pred = clf.predict(X_gunpoint_test)
In [112]:
x = X_gunpoint_test[0].ravel()
x_label = y_pred[0]
In [113]:
# take instances with a different predicted label w.r.t. x
counterfactuals_idxs = np.argwhere(y_pred != x_label).ravel()
counterfactuals = X_gunpoint_test[counterfactuals_idxs]
In [114]:
%%time
dist_x_counterfactuals = distance_matrix(x.reshape(1,-1), counterfactuals[:, 0, :])
CPU times: user 87 μs, sys: 39 μs, total: 126 μs Wall time: 107 μs
In [115]:
# find the index of the closest one
closest_counterfactual_idx = np.argsort(dist_x_counterfactuals).ravel()[0]
closest_counterfactual_idx
Out[115]:
14
In [116]:
plt.plot(X_gunpoint_test[0, 0, :], label="Instance to Explain")
plt.plot(counterfactuals[closest_counterfactual_idx, 0, :], label="Counterfactual")
plt.legend()
plt.show()