Model utilities

Utility functions used to build PyTorch timeseries models.


source

apply_idxs


def apply_idxs(
    o, idxs
):

Function to apply indices to zarr, dask and numpy arrays


source

SeqTokenizer


def SeqTokenizer(
    c_in, embed_dim, token_size:int=60, norm:bool=False
):

Generates non-overlapping tokens from sub-sequences within a sequence by applying a sliding window


source

get_embed_size


def get_embed_size(
    n_cat, rule:str='log2'
):

Call self as a function.

test_eq(get_embed_size(35), 6)

source

has_weight_or_bias


def has_weight_or_bias(
    l
):

Call self as a function.


source

has_weight


def has_weight(
    l
):

Call self as a function.


source

has_bias


def has_bias(
    l
):

Call self as a function.


source

is_conv


def is_conv(
    l
):

Call self as a function.


source

is_affine_layer


def is_affine_layer(
    l
):

Call self as a function.


source

is_conv_linear


def is_conv_linear(
    l
):

Call self as a function.


source

is_bn


def is_bn(
    l
):

Call self as a function.


source

is_linear


def is_linear(
    l
):

Call self as a function.


source

is_layer


def is_layer(
    args:VAR_POSITIONAL
):

Call self as a function.


source

get_layers


def get_layers(
    model, cond:function=noop, full:bool=True
):

Call self as a function.


source

check_weight


def check_weight(
    m, cond:function=noop, verbose:bool=False
):

Call self as a function.


source

check_bias


def check_bias(
    m, cond:function=noop, verbose:bool=False
):

Call self as a function.


source

get_nf


def get_nf(
    m
):

Get nf from model’s first linear layer in head


source

ts_splitter


def ts_splitter(
    m
):

Split of a model between body and head


source

transfer_weights


def transfer_weights(
    model, weights_path:Path, device:device=None, exclude_head:bool=True
):

Utility function that allows to easily transfer weights between models. Taken from the great self-supervised repository created by Kerem Turgutlu. https://github.com/KeremTurgutlu/self_supervised/blob/d87ebd9b4961c7da0efd6073c42782bbc61aaa2e/self_supervised/utils.py


source

build_ts_model


def build_ts_model(
    arch, c_in:NoneType=None, c_out:NoneType=None, seq_len:NoneType=None, d:NoneType=None, dls:NoneType=None,
    device:NoneType=None, verbose:bool=False, s_cat_idxs:NoneType=None, s_cat_embeddings:NoneType=None,
    s_cat_embedding_dims:NoneType=None, s_cont_idxs:NoneType=None, o_cat_idxs:NoneType=None,
    o_cat_embeddings:NoneType=None, o_cat_embedding_dims:NoneType=None, o_cont_idxs:NoneType=None,
    patch_len:NoneType=None, patch_stride:NoneType=None, fusion_layers:int=128, fusion_act:str='relu',
    fusion_dropout:float=0.0, fusion_use_bn:bool=True, pretrained:bool=False, weights_path:NoneType=None,
    exclude_head:bool=True, cut:int=-1, init:NoneType=None, arch_config:dict={}, kwargs:VAR_KEYWORD
):

Call self as a function.

from tsai.data.core import get_ts_dls, TSClassification
from tsai.models.TSiTPlus import TSiTPlus
from fastai.losses import CrossEntropyLossFlat
X = np.random.rand(16, 3, 128)
y = np.random.randint(0, 2, (16, 3))
tfms = [None, [TSClassification()]]
dls = get_ts_dls(X, y, splits=RandomSplitter()(range_of(X)), tfms=tfms)
model = build_ts_model(TSiTPlus, dls=dls, pretrained=False, verbose=True)
xb, yb = dls.one_batch()
output = model(xb)
print(output.shape)
loss = CrossEntropyLossFlat()(output, yb)
print(loss)
assert output.shape == (dls.bs, dls.d, dls.c)
arch: TSiTPlus(c_in=3 c_out=2 seq_len=128 arch_config={} kwargs={'custom_head': functools.partial(<class 'tsai.models.layers.lin_nd_head'>, d=3)})
torch.Size([13, 3, 2])
TensorBase(0.8796, grad_fn=<AliasBackward0>)

source

count_parameters


def count_parameters(
    model, trainable:bool=True
):

Call self as a function.


source

build_tsimage_model


def build_tsimage_model(
    arch, c_in:NoneType=None, c_out:NoneType=None, dls:NoneType=None, pretrained:bool=False, device:NoneType=None,
    verbose:bool=False, init:NoneType=None, arch_config:dict={}, kwargs:VAR_KEYWORD
):

Call self as a function.


source

build_tabular_model


def build_tabular_model(
    arch, dls, layers:NoneType=None, emb_szs:NoneType=None, n_out:NoneType=None, y_range:NoneType=None,
    device:NoneType=None, arch_config:dict={}, kwargs:VAR_KEYWORD
):

Call self as a function.

from tsai.data.external import get_UCR_data
from tsai.data.core import TSCategorize, get_ts_dls
from tsai.data.preprocessing import TSStandardize
from tsai.models.InceptionTime import *
X, y, splits = get_UCR_data('NATOPS', split_data=False)
tfms = [None, TSCategorize()]
batch_tfms = TSStandardize()
dls = get_ts_dls(X, y, splits, tfms=tfms, batch_tfms=batch_tfms)
model = build_ts_model(InceptionTime, dls=dls)
test_eq(count_parameters(model), 460038)

source

get_clones


def get_clones(
    module, N
):

Call self as a function.

m = nn.Conv1d(3,4,3)
get_clones(m, 3)
ModuleList(
  (0-2): 3 x Conv1d(3, 4, kernel_size=(3,), stride=(1,))
)

source

split_model


def split_model(
    m
):

Call self as a function.


source

output_size_calculator


def output_size_calculator(
    mod, c_in, seq_len:NoneType=None
):

Call self as a function.

c_in = 3
seq_len = 30
m = nn.Conv1d(3, 12, kernel_size=3, stride=2)
new_c_in, new_seq_len = output_size_calculator(m, c_in, seq_len)
test_eq((new_c_in, new_seq_len), (12, 14))

source

change_model_head


def change_model_head(
    model, custom_head, kwargs:VAR_KEYWORD
):

Replaces a model’s head by a custom head as long as the model has a head, head_nf, c_out and seq_len attributes


source

true_forecaster


def true_forecaster(
    o, split, horizon:int=1
):

Call self as a function.


source

naive_forecaster


def naive_forecaster(
    o, split, horizon:int=1
):

Call self as a function.

a = np.random.rand(20).cumsum()
split = np.arange(10, 20)
a, naive_forecaster(a, split, 1), true_forecaster(a, split, 1)
(array([0.99029138, 1.68463991, 2.21744589, 2.65448222, 2.85159354,
        3.26171729, 3.67986707, 4.04343956, 4.3077458 , 4.44585435,
        4.76876866, 4.85844441, 4.93256093, 5.52415845, 6.10704489,
        6.74848957, 7.31920741, 8.20198208, 8.78954283, 9.0402    ]),
 array([4.44585435, 4.76876866, 4.85844441, 4.93256093, 5.52415845,
        6.10704489, 6.74848957, 7.31920741, 8.20198208, 8.78954283]),
 array([4.76876866, 4.85844441, 4.93256093, 5.52415845, 6.10704489,
        6.74848957, 7.31920741, 8.20198208, 8.78954283, 9.0402    ]))