Source code for TSFEDL.utils
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import torch
from torch import nn
from torch.nn import functional as F
[docs]class TimeDistributed(nn.Module):
"""
TimeDistributed module implementation.
"""
def __init__(self, module):
super(TimeDistributed, self).__init__()
self.module = module
def forward(self, x):
if len(x.size()) <= 2:
return self.module(x)
t, n = x.size(0), x.size(1)
# merge batch and seq dimensions
x_reshape = x.contiguous().view(t * n, *x.size()[2:]).to(torch.float32)
y = self.module(x_reshape)
# We have to reshape Y
y = y.contiguous().view(t, n, *y.size()[1:]).to(torch.float32)
return y
[docs]def flip_indices_for_conv_to_lstm(x: torch.Tensor) -> torch.Tensor:
"""
Changes the (N, C, L) dimension to (N, L, C). This is due to features in PyTorch's LSTMs are expected on the last dim.
Parameters
----------
x : torch.Tensor
Input tensor.
Returns
-------
x : torch.Tensor
Output tensor.
"""
return x.view(x.size(0), x.size(2), x.size(1))
[docs]def flip_indices_for_conv_to_lstm_reshape(x: torch.Tensor) -> torch.Tensor:
"""
Changes the (N, C, L) dimension to (N, L, C). This is due to features in PyTorch's LSTMs are expected on the last dim.
Parameters
----------
x : torch.Tensor
Input tensor.
Returns
-------
x : torch.Tensor
Output tensor.
"""
return x.reshape(x.size(0), x.size(2), x.size(1))
[docs]def full_convolution(x, filters, kernel_size, **kwargs):
"""
It performs a Full convolution operation on the given keras Tensor.
Parameters
----------
x : Keras.Tensor
Input tensor of the full convolution.
filters : int
Number of filters of the full convolution.
kernel_size : int
Kernel size of the convolution.
kwargs : dict
Rest of the arguments, optional.
Returns
-------
x : Keras.Tensor
Output tensor.
"""
# Do a full convolution. Return a keras Tensor
x = layers.ZeroPadding1D(padding=kernel_size - 1)(x)
x = layers.Conv1D(filters=filters, kernel_size=kernel_size, **kwargs)(x)
return x