--- title: Learner keywords: fastai sidebar: home_sidebar summary: "This contains fastai Learner extensions." description: "This contains fastai Learner extensions." nb_path: "nbs/052_learner.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}

Learner.show_batch[source]

Learner.show_batch(**kwargs)

{% endraw %} {% raw %}
{% endraw %} {% raw %}

Learner.remove_all_cbs[source]

Learner.remove_all_cbs(max_iters=10)

{% endraw %} {% raw %}
{% endraw %} {% raw %}

Learner.one_batch[source]

Learner.one_batch(i, b)

{% endraw %} {% raw %}
{% endraw %} {% raw %}

Learner.save_all[source]

Learner.save_all(path='export', dls_fname='dls', model_fname='model', learner_fname='learner', verbose=False)

{% endraw %} {% raw %}

load_all[source]

load_all(path='export', dls_fname='dls', model_fname='model', learner_fname='learner', device=None, pickle_module=pickle, verbose=False)

{% endraw %} {% raw %}
{% endraw %} {% raw %}

Recorder.plot_metrics[source]

Recorder.plot_metrics(nrows=None, ncols=None, figsize=None, final_losses=True, perc=0.5, imsize=3, suptitle=None, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None)

{% endraw %} {% raw %}

Learner.plot_metrics[source]

Learner.plot_metrics(nrows=1, ncols=1, figsize=None, imsize=3, suptitle=None, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None)

{% endraw %} {% raw %}
{% endraw %} {% raw %}

Learner.show_probas[source]

Learner.show_probas(figsize=(6, 6), ds_idx=1, dl=None, one_batch=False, max_n=None, nrows=1, ncols=1, imsize=3, suptitle=None, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None)

{% endraw %} {% raw %}
{% endraw %} {% raw %}

get_arch[source]

get_arch(arch_name)

{% endraw %} {% raw %}
{% endraw %} {% raw %}
for arch_name in all_archs_names: get_arch(arch_name)
{% endraw %} {% raw %}
arch_name = 'InceptionTimePlus'
test_eq(get_arch('InceptionTimePlus').__name__, arch_name)
{% endraw %} {% raw %}

ts_learner[source]

ts_learner(dls, arch=None, c_in=None, c_out=None, seq_len=None, d=None, splitter=trainable_params, loss_func=None, opt_func=Adam, lr=0.001, cbs=None, metrics=None, path=None, model_dir='models', wd=None, wd_bn_bias=False, train_bn=True, moms=(0.95, 0.85, 0.95), device=None, verbose=False, pretrained=False, weights_path=None, exclude_head=True, cut=-1, init=None, arch_config={})

{% endraw %} {% raw %}
{% endraw %} {% raw %}

tsimage_learner[source]

tsimage_learner(dls, arch=None, pretrained=False, loss_func=None, opt_func=Adam, lr=0.001, cbs=None, metrics=None, path=None, model_dir='models', wd=None, wd_bn_bias=False, train_bn=True, moms=(0.95, 0.85, 0.95), c_in=None, c_out=None, device=None, verbose=False, init=None, arch_config={}, p=0.0, n_out=1000, stem_szs=(32, 32, 64), widen=1.0, sa=False, act_cls=ReLU, ndim=2, ks=3, stride=2, groups=1, reduction=None, nh1=None, nh2=None, dw=False, g2=1, sym=False, norm_type=<NormType.Batch: 1>, pool=AvgPool, pool_first=True, padding=None, bias=None, bn_1st=True, transpose=False, xtra=None, bias_std=0.01, dilation:Tuple[~T, ~T]][int]=1, padding_mode:str='zeros', dtype=None)

{% endraw %} {% raw %}
{% endraw %} {% raw %}

Learner.decoder[source]

Learner.decoder(o)

{% endraw %} {% raw %}
{% endraw %} {% raw %}

Learner.feature_importance[source]

Learner.feature_importance(X=None, y=None, partial_n=None, feature_names=None, key_metric_idx=0, show_chart=True, save_df_path=False, random_state=23)

Calculates feature importance defined to be the change in a model validation loss or metric when a single feature value is randomly shuffled

This procedure breaks the relationship between the feature and the target, thus the change in the model validation loss or metric is indicative of how much the model depends on the feature.

Args: X: array-like object containing the time series data for which importance will be measured. If None, all data in the validation set will be used. y: array-like object containing the targets. If None, all targets in the validation set will be used. partial_n: number of samples (if int) or percent of the validation set (if float) that will be used to measure feature importance. If None, all data will be used. feature_names (Optional[list(str)]): list of feature names that will be displayed if available. Otherwise they will be var_0, var_1, etc. key_metric_idx (Optional[int]): integer to select the metric used in the calculation. If None or no metric is available, the change is calculated using the validation loss. show_chart (bool): flag to indicate if a chart showing permutation feature importance will be plotted. save_df_path (str): path to saved dataframe containing the permutation feature importance results. random_state (int): controls the shuffling applied to the data. Pass an int for reproducible output across multiple function calls.

{% endraw %} {% raw %}
{% endraw %} {% raw %}
from tsai.data.external import get_UCR_data
from tsai.models.FCNPlus import FCNPlus
dsid = 'OliveOil'
X, y, splits = get_UCR_data(dsid, verbose=True, split_data=False)
tfms  = [None, [TSCategorize()]]
dls = get_ts_dls(X, y, splits=splits, tfms=tfms)
learn = ts_learner(dls, FCNPlus)
for p in learn.model.parameters():
    p.requires_grad=False
test_eq(count_parameters(learn.model), 0)
learn.freeze()
test_eq(count_parameters(learn.model), 1540)
learn.unfreeze()
test_eq(count_parameters(learn.model), 264580)

learn = ts_learner(dls, 'FCNPlus')
for p in learn.model.parameters():
    p.requires_grad=False
test_eq(count_parameters(learn.model), 0)
learn.freeze()
test_eq(count_parameters(learn.model), 1540)
learn.unfreeze()
test_eq(count_parameters(learn.model), 264580)
Dataset: OliveOil
X      : (60, 1, 570)
y      : (60,)
splits : (#30) [0,1,2,3,4,5,6,7,8,9...] (#30) [30,31,32,33,34,35,36,37,38,39...] 

{% endraw %} {% raw %}
learn.show_batch();
{% endraw %} {% raw %}
from tsai.data.preprocessing import TSRobustScale
dsid = 'OliveOil'
X, y, splits = get_UCR_data(dsid, split_data=False)
tfms  = [None, [TSClassification()]]
batch_tfms = TSRobustScale()
dls = get_ts_dls(X, y, tfms=tfms, splits=splits, batch_tfms=batch_tfms)
learn = ts_learner(dls, FCNPlus, metrics=accuracy)
learn.fit_one_cycle(2)
learn.plot_metrics()
learn.show_probas()
epoch train_loss valid_loss accuracy time
0 1.474425 1.406272 0.166667 00:03
1 1.470585 1.407411 0.166667 00:02
{% endraw %} {% raw %}
learn.feature_importance();
Selected metric: accuracy
Computing feature importance...
100.00% [2/2 00:02<00:00]
  0 feature: BASELINE             accuracy: 0.400000
  1 feature: var_0                accuracy: 0.400000

{% endraw %} {% raw %}
learn.feature_importance(X=X[splits[1]], y=y[splits[1]]);
Selected metric: accuracy
Computing feature importance...
100.00% [2/2 00:02<00:00]
  0 feature: BASELINE             accuracy: 0.400000
  1 feature: var_0                accuracy: 0.400000

{% endraw %} {% raw %}
learn.feature_importance(partial_n=.1);
Selected metric: accuracy
Computing feature importance...
100.00% [2/2 00:00<00:00]
  0 feature: BASELINE             accuracy: 0.333333
  1 feature: var_0                accuracy: 0.333333

{% endraw %} {% raw %}
learn.feature_importance(partial_n=10);
Selected metric: accuracy
Computing feature importance...
100.00% [2/2 00:01<00:00]
  0 feature: BASELINE             accuracy: 0.400000
  1 feature: var_0                accuracy: 0.400000

{% endraw %} {% raw %}
if not os.path.exists("./models"): os.mkdir("./models")
if not os.path.exists("./data"): os.mkdir("./data")
np.save("data/X_test.npy", X[splits[1]])
np.save("data/y_test.npy", y[splits[1]])
learn.export("./models/test.pth")
{% endraw %}