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.
Classes:
-
TcnParams
–TCN parameters
-
TcnBlockParams
–TCN block parameters
-
TcnModel
–Helper class to generate model from parameters
Functions:
-
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 demonstrates how to create a TCN model using the Tcn
class. The model is defined using a set of parameters defined in the TcnParams
and TcnBlockParams
classes.
import keras
import neuralspot_edge as nse
inputs = keras.Input(shape=(800, 1), name="inputs")
num_classes = 5
params = nse.models.TcnParams(
input_kernel=(1, 3),
input_norm="batch",
blocks=[
nse.models.TcnBlockParams(filters=8, kernel=(1, 3), dilation=(1, 1), dropout=0.1, ex_ratio=1, se_ratio=0, norm="batch"),
nse.models.TcnBlockParams(filters=16, kernel=(1, 3), dilation=(1, 2), dropout=0.1, ex_ratio=1, se_ratio=0, norm="batch"),
nse.models.TcnBlockParams(filters=24, kernel=(1, 3), dilation=(1, 4), dropout=0.1, ex_ratio=1, se_ratio=4, norm="batch"),
nse.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,
use_logits=True,
model_name="tcn",
)
model = nse.models.TcnModel.model_from_params(
inputs=inputs,
params=params,
num_classes=num_classes,
)
Classes
TcnBlockParams
TCN block parameters
Attributes:
-
depth
(int
) –Layer depth
-
branch
(int
) –Number of branches
-
filters
(int
) –Number of filters
-
kernel
(int | tuple[int, int]
) –Kernel size
-
dilation
(int | tuple[int, int]
) –Dilation rate
-
ex_ratio
(float
) –Expansion ratio
-
se_ratio
(float
) –Squeeze and excite ratio
-
dropout
(float | None
) –Dropout rate
-
norm
(Literal['batch', 'layer'] | None
) –Normalization type
-
activation
(str
) –Activation function
TcnParams
TCN parameters
Attributes:
-
input_kernel
(int | tuple[int, int] | None
) –Input kernel size
-
input_norm
(Literal['batch', 'layer'] | None
) –Input normalization type
-
block_type
(Literal['lg', 'mb', 'sm']
) –Block type
-
blocks
(list[TcnBlockParams]
) –TCN blocks
-
output_kernel
(int | tuple[int, int]
) –Output kernel size
-
include_top
(bool
) –Include top
-
use_logits
(bool
) –Use logits
-
output_activation
(str | None
) –Output activation
-
name
(str
) –Model name
TcnModel
Helper class to generate model from parameters
Functions
layer_from_params
staticmethod
Create layer from parameters
Source code in neuralspot_edge/models/tcn.py
model_from_params
staticmethod
Create model from parameters
Source code in neuralspot_edge/models/tcn.py
Functions
normalization
Normalization layer
Parameters:
Returns:
-
Layer
–keras.Layer: Layer
Source code in neuralspot_edge/models/tcn.py
tcn_block_lg
TCN large block
Parameters:
-
params
(TcnBlockParams
) –Parameters
-
name
(str
) –Name
Returns:
-
Layer
–keras.Layer: Layer
Source code in neuralspot_edge/models/tcn.py
tcn_block_mb
TCN mbconv block
Parameters:
-
params
(TcnBlockParams
) –Parameters
-
name
(str
) –Name
Returns:
-
Layer
–keras.Layer: Layer
Source code in neuralspot_edge/models/tcn.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
|
tcn_block_sm
TCN small block
Parameters:
-
params
(TcnBlockParams
) –Parameters
-
name
(str
) –Name
Returns:
-
Layer
–keras.Layer: Layer
Source code in neuralspot_edge/models/tcn.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
|
tcn_core
TCN core
Parameters:
-
params
(TcnParams
) –Parameters
Returns:
-
Layer
–keras.Layer: Layer
Source code in neuralspot_edge/models/tcn.py
tcn_layer
tcn_layer(x: keras.KerasTensor, params: TcnParams, num_classes: int | None = None) -> keras.KerasTensor
TCN functional layer
Parameters:
-
x
(KerasTensor
) –Input tensor
-
params
(TcnParams
) –Parameters
-
num_classes
(int
, default:None
) –Number of classes
Returns:
-
KerasTensor
–keras.KerasTensor: Output tensor