freeradiantbunny.org

freeradiantbunny.org/blog

torch.nn modules

This page describes torch.nn modules in the context of stock price prediction.

The torch.nn module in PyTorch provides essential classes for building neural networks. When applied to time-series data such as stock prices, many of these components become vital for structuring models, optimizing training, and improving generalization.

Containers

Classes like nn.Sequential, nn.ModuleList, and nn.ModuleDict allow for modular model design. nn.Sequential is commonly used to stack layers linearly for simple models.

Convolution Layers

nn.Conv1d is useful for extracting temporal patterns across time-series windows. For instance, applying a sliding kernel to price or indicator data can highlight trends, reversals, or local extrema.

Pooling Layers

nn.MaxPool1d and nn.AvgPool1d reduce sequence length while preserving key features. This is useful in convolution-based time-series models to decrease computation and control overfitting.

Padding Layers

Used to maintain input size consistency across layers. For example, nn.ConstantPad1d can ensure that sequences fed into Conv1d or LSTM layers remain aligned in size.

Non-linear Activations (weighted sum, nonlinearity)

Activation functions such as nn.ReLU, nn.Tanh, and nn.Sigmoid introduce non-linear transformations, which are crucial for modeling complex financial relationships. For example, nn.Tanh is often used in LSTMs due to its range and smooth curvature.

Non-linear Activations (other)

Functions like nn.Softmax and nn.LogSoftmax are typically used for classification tasks. In stock prediction, these would be applied when predicting movement direction (e.g., up/down) rather than a numeric price.

Normalization Layers

nn.BatchNorm1d and nn.LayerNorm help standardize activations during training. This improves training speed and convergence, especially in deeper models or when using Transformer-based architectures.

Recurrent Layers

Core components for time-series modeling. nn.LSTM and nn.GRU are especially effective in capturing sequential dependencies in stock price data, such as temporal trends and long-term patterns.

Transformer Layers

Modern sequence models that rely on attention mechanisms. nn.TransformerEnkbdr and nn.Transformer offer greater flexibility and performance on long sequences. They are increasingly used in financial time-series modeling due to their ability to learn from unbounded context.

Linear Layers

nn.Linear is used to project data from one space to another. It is found in nearly all network architectures and is typically used in the final layer of a stock price prediction model to produce a single output value.

Dropout Layers

nn.Dropout helps prevent overfitting by randomly deactivating neurons during training. In stock prediction, it is used to improve generalization to unseen data and avoid memorization of training trends.

Sparse Layers

These layers support operations on sparse tensors. While not typically used in price prediction, they might be relevant when embedding large categorical metadata such as stock IDs or sectors.

Distance Functions

nn.CosineSimilarity and nn.PairwiseDistance can be used for model components that compare feature vectors. These are more common in ranking tasks or nearest-neighbor models rather than direct price prediction.

Loss Functions

Key to training. For stock price regression, common choices include nn.MSELoss, nn.L1Loss, and nn.HuberLoss. These measure the difference between predicted and actual prices and guide the optimizer.

Vision Layers

Primarily used for image data. While not directly applicable to price series, they may be used in niche cases where financial charts are analyzed visually.

Shuffle Layers

Useful for shuffling channels in convolutional architectures, mostly used in vision tasks. Not generally applicable to stock price modeling.

DataParallel Layers (multi-GPU, distributed)

These modules, such as nn.DataParallel, enable training on multiple GPUs. They become important when working with large datasets or ensemble models over many financial instruments.

Utilities

Includes functions such as clip_grad_norm_, which is critical when training LSTM models to prevent exploding gradients. Other utilities assist in weight initialization and parameter management.

Quantized Functions

Used to optimize models for deployment by reducing numerical precision. These are useful for mobile or edge inference applications but not typically relevant during model training for stock prediction.

Lazy Modules Initialization

Allows defining models without knowing input dimensions beforehand. Useful when building highly flexible models or when dimensions are determined at runtime based on dynamic stock features.