normalization
Scaling in Stock Price Prediction with PyTorch
- Neural Networks Are Sensitive to Feature Scale: Features with vastly different scales (e.g., stock prices in hundreds vs. RSI values between 0 and 100) cause uneven weight updates during training, which may slow convergence or cause instability.
- Improves Gradient Descent Efficiency: Gradient descent converges faster and more reliably when input features are on a similar scale. Without scaling, some parameters may dominate updates, leading to poor local minima or slow training.
- Prevents Numerical Instability: Large input values can cause exploding gradients or numerical overflow, especially in deep networks, while very small values can lead to vanishing gradients.
- Enables Meaningful Regularization and Weight Initialization: Regularization techniques and common initializations assume input features have zero mean and unit variance, ensuring balanced learning.
- Allows Model Generalization: Proper scaling helps the model learn from relative variations in features rather than absolute magnitudes, improving generalization to unseen data.
Common Scaling Techniques
- Normalization (Min-Max Scaling): Rescales features to a fixed range, usually [0, 1].
X_scaled = (X - X_min) / (X_max - X_min)
- Standardization (Z-score Scaling): Centers features by mean 0 and scales by standard deviation 1.
X_scaled = (X - μ) / σ
For financial time series, standardization is typically preferred due to outliers and varying ranges.
How to Effectively Scale Data in PyTorch
Assuming a dataset with multiple features for stock price prediction:
Step 1: Calculate Scaling Parameters on Training Data Only
import torch
# Example tensor: rows = samples, columns = features
train_features = torch.tensor([
[100.0, 0.7, 30.0], # price, RSI, volume_change_pct
[102.0, 0.8, 25.0],
[98.0, 0.6, 40.0],
# ... more training samples
])
# Compute mean and std only on training set for each feature
mean = train_features.mean(dim=0)
std = train_features.std(dim=0, unbiased=False) # population std for consistency
Step 2: Apply Scaling to Train and Validation/Test Sets
def standardize(tensor, mean, std):
return (tensor - mean) / std
train_scaled = standardize(train_features, mean, std)
# When you get validation or test data:
val_features = torch.tensor([
[101.0, 0.75, 28.0],
# ...
])
val_scaled = standardize(val_features, mean, std)
Important Notes
- Do NOT compute scaling parameters on validation or test sets, only on training data to avoid data leakage.
- If using minibatches, compute mean/std on the entire training set once, not per batch.
- Save mean and std for inference to scale new data consistently.
- After prediction, if your target was scaled, invert the scaling to recover original values:
def invert_standardize(scaled_tensor, mean, std): return scaled_tensor * std + mean
- Optionally, use
torchvision.transforms.Normalize
orsklearn.preprocessing.StandardScaler
to automate scaling.