<jupyter_start><jupyter_text>DNN model> A basic architecture for time series forecasting.<jupyter_code># hide
from nbdev.showdoc import *
from fastcore.test import *
#export
from fastcore.utils import *
from fastcore.imports import *
from fastai2.basics import *
from fastai2.callback.hook import num_features_model
from fastai2.callback.all import *

def ChannelPool(o, kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False):
    n, c, w = o.size()
    print(o.shape)
    pooled =  F.max_pool1d(o, kernel_size, stride,
                    padding, dilation, ceil_mode,
                    return_indices)

    return o


o = torch.randn(64, 64, 50)
output = ChannelPool(o, 16, 2)
# export
class DNN(torch.nn.Module):
    """Implements a simple `DNN` architecture for time series forecasting. Inherits 
    from pytorch `Module <https://pytorch.org/docs/stable/nn.html#torch.nn.Module>`_.
    
    Arguments:
        * input_channels (int): Number of covariates in input time series.
        * output_channels (int): Number of target time series.
        * horizon (int): Number of time steps to forecast.
        * lookback (int): Number of time steps to lookback.
        * hidden_channels (int): Number of channels in convolutional hidden layers.
        * p_dropout (float): Probality of dropout.
    """
    split = -1
    def __init__(self,
                 input_channels,
                 output_channels,
                 horizon,
                 lookback,
                 hidden_channels=64,
                 p_dropout=.2,
                 ks=3,
                ):
        """Inititalize variables."""
        super(DNN, self).__init__()        
        self.output_channels = output_channels
        self.horizon = horizon
        self.hidden_channels = hidden_channels

        # Set up first layer for input
        conv_input = ConvLayer(input_channels, hidden_channels//4, ks=ks, ndim=1, stride = 2)
        conv_2 = ConvLayer(hidden_channels//4, hidden_channels//2, ks=ks, ndim=1, stride = 2)
        conv_3 = ConvLayer(hidden_channels//2, hidden_channels, ks=ks, ndim=1, stride = 2)
#         adaptive_pool()
        # Set up nonlinear output layers
        self.body = nn.Sequential(conv_input, conv_2, conv_3, Flatten())
        out = int((hidden_channels/8)*lookback)
        self.dnn = LinBnDrop(out,horizon*output_channels)

    def forward(self, inputs):
        """Forward function."""
        hid = self.body(inputs.float())
        out= self.dnn(hid).view(hid.size(0),self.output_channels,-1).float()
        return out
    
    @property
    def n_parameters(self):
        """Returns the number of model parameters."""
        par = list(self.parameters())
        s = sum([np.prod(list(d.size())) for d in par])
        return s

# hide
x = torch.randn(64,1,128)
mdl = DNN(1,1,14,128)
test_eq(mdl(x).shape,(64,1,14))
#export
from fastai2.basics import *
from fastseq.all import *

@delegates(DNN.__init__)
def dnn_learner(dbunch, output_channels=None, metrics=None, **kwargs):
    "Build a dnn style learner"
    output_channels = ifnone(output_channels,dbunch.train[0][0].shape[0])
    
    model = DNN(input_channels=dbunch.train[0][0].shape[0],
        output_channels=output_channels, 
        horizon = dbunch.train_dl.horizon,
        lookback = dbunch.train_dl.lookback, 
        **kwargs
       )
    
    learn = Learner(dbunch, model, loss_func=F.mse_loss, opt_func= Adam, metrics=L(metrics)+L(mae, smape))
    return learn

path = untar_data(URLs.m4_daily)
data = TSDataLoaders.from_folder(path, horizon = 14, lookback = 128, bs = 64, nrows=100)
learn = dnn_learner(data)
print(learn.model)
print(learn.summary())
from fastai2.callback.all import *
learn.lr_find()
learn.fit(2,1e-7)
# hide 
from fastseq.data.external import *
items = dummy_data_generator(50,10, nrows=100)
data = TSDataLoaders.from_items(items, horizon = 5)
learn = dnn_learner(data)
print(learn.model)
print(learn.summary())
# hide
from nbdev.export import *

notebook2script()<jupyter_output>Converted 00_core.ipynb.
Converted 01_data.external.ipynb.
Converted 03_data.load.ipynb.
Converted 04_data.transforms.ipynb.
Converted 05_models.wavenet.ipynb.
Converted 06_models.dnn.ipynb.
Converted 07_interpret.ipynb.
Converted 08_metrics.ipynb.
Converted 09_learner.ipynb.
Converted 10_models.nbeats.ipynb.
Converted 20_models.cnn.learner.ipynb.
Converted 21_models.cnn.transforms.ipynb.
Converted index.ipynb.
{'fraction_non_alphanumeric': 0.11323298135250505, 'fraction_numerical': 0.024713547517411817, 'mean_word_length': 3.1337047353760448, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '72762', 'license': 'Apache-2.0', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1482, 'n_words': 330, 'path': 'nbs/archive/_06_models.dnn.ipynb', 'repo_name': 'jdb78/fastseq', 'size': '4451'}