<jupyter_start><jupyter_text>04 - Time SeriesThis module was built by <NAME> among others, see [this](https://forums.fast.ai/t/timeseries/55861/1) megathread for the discussion. The essential goal here is we can use arrays of any dimension instead of just 1 (such as tabular) or 3 (such as images). * Note, this notebook was heavily influenced from his tutorial notebook [here](https://github.com/timeseriesAI/timeseriesAI2/blob/master/tutorial_nbs/01_How_to_use_numpy_arrays_in_fastai.ipynb)<jupyter_code>pip install fastai<jupyter_output><empty_output><jupyter_text>We'll need a few more libraries and the `timeseriesAI2` github:<jupyter_code>pip install pyunpack sktime -q
!git clone 'https://github.com/timeseriesAI/timeseriesAI2.git'
%cd timeseriesAI2<jupyter_output><empty_output><jupyter_text>Now let's grab what we need:<jupyter_code>from fastai.tabular.all import *
from timeseries.imports import *
from timeseries.utils import *
from timeseries.data import *
from timeseries.core import *
from timeseries.models import *<jupyter_output><empty_output><jupyter_text>For our data we'll be utilizing the UCR repository which has 128 univariate and 30 multivariate datasets. In the framework we can quickly grab any dataset we want:<jupyter_code>name = 'StarLightCurves'<jupyter_output><empty_output><jupyter_text>We can now grab our train and validation by calling `get_UCR_data`:<jupyter_code>X_train, y_train, X_valid, y_valid = get_UCR_data(name, verbose=True, on_disk=True)<jupyter_output>Dataset: StarLightCurves
Downloading and decompressing data to data/UCR/StarLightCurves...
...data downloaded and decompressed
X_train: (1000, 1, 1024)
y_train: (1000,)
X_valid: (8236, 1, 1024)
y_valid: (8236,)<jupyter_text>Since data is already split between train and test in the UCR dataset, we are going to merge them and create some indices to split them in the same sets. To save on memory, he figured out a way to utilized the numpy arrays via your disk. We'll do so as follows:<jupyter_code>X = np.concatenate((X_train, X_valid))
y = np.concatenate((y_train, y_valid))
np.save('./data/UCR/StarLightCurves/X.npy', X)
np.save('./data/UCR/StarLightCurves/y.npy', y)
del X, y<jupyter_output><empty_output><jupyter_text>Now we can load them back in and make our splits:<jupyter_code>X = np.load('./data/UCR/StarLightCurves/X.npy', mmap_mode='r')
y = np.load('./data/UCR/StarLightCurves/y.npy', mmap_mode='r')
splits = (L(np.arange(len(X_train)), use_list=True),
          L(np.arange(len(X_train), len(X)), use_list=True))
splits<jupyter_output><empty_output><jupyter_text>Since we used memmap the data is being directly read from memory. Now to make and use your own data they need to be in a three dimentional array with a format of:* Samples* Variables* Length (or timesteps) To use this we have a special `TSTensor` built to handle such data:<jupyter_code>t = TSTensor(X)
t<jupyter_output><empty_output><jupyter_text>So here we can see we had 60 samples with one variable and an overall time length of 570 for each We can directly use this in our `DataBlock` API too! With a `TSTensorBlock`:<jupyter_code>splitter = IndexSplitter(splits[1])
getters = [ItemGetter(0), ItemGetter(1)]
dblock = DataBlock(blocks=(TSTensorBlock, CategoryBlock),
                   getters=getters,
                   splitter=splitter)<jupyter_output><empty_output><jupyter_text>Next we make a call to `itemify` which zips up our x's and our y's<jupyter_code>src = itemify(X, y)<jupyter_output><empty_output><jupyter_text>And now we can make our `DataLoaders`:<jupyter_code>dls = dblock.dataloaders(src, bs=64, val_bs=128)
dls.show_batch(max_n=3)<jupyter_output><empty_output><jupyter_text>And there's our data! Now are we still on disk? *Yes*<jupyter_code>dls.dataset[0]<jupyter_output><empty_output><jupyter_text>Training our ModelTHe particular model we're using is the [Inception Time](https://towardsdatascience.com/deep-learning-for-time-series-classification-inceptiontime-245703f422db) model. To do so we need the number of input classes and our number of variables:<jupyter_code>dls.c
inp_vars = dls.dataset[0][0].shape[-2]
inp_vars
net = InceptionTime(inp_vars, dls.c)
learn = Learner(dls, net, loss_func=CrossEntropyLossFlat(), metrics=accuracy, opt_func=ranger)
learn.lr_find()<jupyter_output><empty_output><jupyter_text>And now we can fit!<jupyter_code>learn.fit_flat_cos(10, 0.025)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10478566568382854, 'fraction_numerical': 0.018144704014515765, 'mean_word_length': 6.4367622259696455, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '210271', 'license': 'MIT', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1514, 'n_words': 420, 'path': 'Tabular Notebooks/04_TimeSeries.ipynb', 'repo_name': 'ANGELLOPARR/Practical-Deep-Learning-for-Coders-2.0', 'size': '4417'}