# helia_edge.models.tcn

## Temporal Convolutional Network (TCN)

### Overview

Temporal convolutional network (TCN) is a type of convolutional neural network (CNN) that is commonly used for sequence modeling tasks such as speech recognition, text generation, and video classification. TCN is a fully convolutional network that consists of a series of dilated causal convolutional layers. The dilated convolutional layers allow TCN to have a large receptive field while maintaining a small number of parameters. TCN is also fully parallelizable, which allows for faster training and inference times.

For more info, refer to the original paper [Temporal Convolutional Networks: A Unified Approach to Action Segmentation](https://doi.org/10.48550/arXiv.1608.08242).

Parameters are in ``helia_edge.models.tcn_params``.

**Functions**

| Name | Description |
| --- | --- |
| `build` | TCN model from ``TcnParams`` |
| `normalization` | Normalization layer |
| `tcn_block_lg` | TCN large block |
| `tcn_block_mb` | TCN mbconv block |
| `tcn_block_sm` | TCN small block |
| `tcn_core` | TCN core |
| `tcn_layer` | TCN functional layer |

### Additions

The TCN architecture has been modified to allow the following:

* Convolutional pairs can be factorized into depthwise separable convolutions.
* Squeeze and excitation (SE) blocks can be added between convolutional pairs.
* Normalization can be set between batch normalization and layer normalization.
* ReLU is replaced with the approximated ReLU6.

### Usage

The following example builds a TCN from `TcnParams` and `TcnBlockParams` through a `ModelSpec`.

```python
import helia_edge as helia

params = helia.models.TcnParams(
    input_kernel=(1, 3),
    input_norm="batch",
    blocks=[
        helia.models.TcnBlockParams(filters=8, kernel=(1, 3), dilation=(1, 1), dropout=0.1, ex_ratio=1, se_ratio=0, norm="batch"),
        helia.models.TcnBlockParams(filters=16, kernel=(1, 3), dilation=(1, 2), dropout=0.1, ex_ratio=1, se_ratio=0, norm="batch"),
        helia.models.TcnBlockParams(filters=24, kernel=(1, 3), dilation=(1, 4), dropout=0.1, ex_ratio=1, se_ratio=4, norm="batch"),
        helia.models.TcnBlockParams(filters=32, kernel=(1, 3), dilation=(1, 8), dropout=0.1, ex_ratio=1, se_ratio=4, norm="batch"),
    ],
    output_kernel=(1, 3),
    include_top=True,
    num_classes=5,
    use_logits=True,
)
model = helia.models.build(helia.models.ModelSpec(params=params, input_shape=(1, 800, 1)))
```

## helia_edge.models.tcn.normalization

`function` · `python`

```python
normalization(norm: str, name: str) -> keras.Layer
```

Normalization layer

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| norm | str | Required | Normalization type |
| name | str | Required | Name |

**Returns**

| Name | Type | Description |
| --- | --- | --- |
|  | keras.Layer | keras.Layer: Layer |

Source: `helia_edge/models/tcn.py:63`

## helia_edge.models.tcn.tcn_block_lg

`function` · `python`

```python
tcn_block_lg(params: TcnBlockParams, name: str) -> keras.Layer
```

TCN large block

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| params | TcnBlockParams | Required | Parameters |
| name | str | Required | Name |

**Returns**

| Name | Type | Description |
| --- | --- | --- |
|  | keras.Layer | keras.Layer: Layer |

Source: `helia_edge/models/tcn.py:92`

## helia_edge.models.tcn.tcn_block_mb

`function` · `python`

```python
tcn_block_mb(params: TcnBlockParams, name: str) -> keras.Layer
```

TCN mbconv block

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| params | TcnBlockParams | Required | Parameters |
| name | str | Required | Name |

**Returns**

| Name | Type | Description |
| --- | --- | --- |
|  | keras.Layer | keras.Layer: Layer |

Source: `helia_edge/models/tcn.py:157`

## helia_edge.models.tcn.tcn_block_sm

`function` · `python`

```python
tcn_block_sm(params: TcnBlockParams, name: str) -> keras.Layer
```

TCN small block

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| params | TcnBlockParams | Required | Parameters |
| name | str | Required | Name |

**Returns**

| Name | Type | Description |
| --- | --- | --- |
|  | keras.Layer | keras.Layer: Layer |

Source: `helia_edge/models/tcn.py:258`

## helia_edge.models.tcn.tcn_core

`function` · `python`

```python
tcn_core(params: TcnParams) -> keras.Layer
```

TCN core

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| params | TcnParams | Required | Parameters |

**Returns**

| Name | Type | Description |
| --- | --- | --- |
|  | keras.Layer | keras.Layer: Layer |

Source: `helia_edge/models/tcn.py:344`

## helia_edge.models.tcn.tcn_layer

`function` · `python`

```python
tcn_layer(x: keras.KerasTensor, params: TcnParams) -> keras.KerasTensor
```

TCN functional layer

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| x | keras.KerasTensor | Required | Input tensor |
| params | TcnParams | Required | Parameters |

**Returns**

| Name | Type | Description |
| --- | --- | --- |
|  | keras.KerasTensor | keras.KerasTensor: Output tensor |

Source: `helia_edge/models/tcn.py:373`

## helia_edge.models.tcn.build

`function` · `python`

```python
build(
    params: TcnParams,
    input_shape: tuple[int | None, ...],
    *,
    batch_size: int | None = None,
    name: str | None = None,
) -> keras.Model
```

Build a TCN model.

**Parameters**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| params | TcnParams | Required | Model parameters. |
| input_shape | tuple[int \| None, ...] | Required | Input shape without the batch axis; None for a variable axis. |
| batch_size | int \| None | None | Static batch size; None for a dynamic batch. |
| name | str \| None | None | Model name; the family when None. |

**Returns**

| Name | Type | Description |
| --- | --- | --- |
|  | keras.Model | keras.Model: The model, named ``tcn`` unless ``name`` is given. |

Source: `helia_edge/models/tcn.py:428`
