Network Architecture Sizing - Research Notes
Experiment Overview
| Item |
Details |
| Date |
2025-12-18 |
| Goal |
Understand relationship between hidden_dims and model file size |
| Environment |
Google Colab A100, PyTorch 2.x, NativePPOTrainer |
| Status |
Documented |
Context
Training runs produced models at ~72 MB instead of expected ~148 MB. Investigation revealed the hidden_dims configuration determines model size, with the first layer dominating total parameter count due to multiplication with observation dimensions.
Architecture Comparison
Model Size vs Architecture
| Architecture |
hidden_dims |
Layers |
Total Params |
File Size |
First Layer Size |
| Large (v2.2) |
(2048, 1024, 512, 256) |
4 |
12.6M |
~148 MB |
2048 × obs_dim |
| Medium (v2.3) |
(1024, 512, 256) |
3 |
6.1M |
~72 MB |
1024 × obs_dim |
| Small |
(512, 256, 128) |
3 |
~1.5M |
~18 MB |
512 × obs_dim |
| Tiny |
(256, 128, 64) |
3 |
~0.4M |
~5 MB |
256 × obs_dim |
Why First Layer Dominates
With 53 features × 100 lookback = 5,300 input dimensions:
- Large:
2048 × 5300 = 10.9M params (86% of network)
- Medium:
1024 × 5300 = 5.4M params (89% of network)
- Small:
512 × 5300 = 2.7M params (90% of network)
Key insight: The first hidden layer dimension has exponentially more impact on model size than deeper layers.
Configuration Locations
Current defaults in ppo_trainer_native.py:
| Function |
GPU Tier |
hidden_dims |
get_auto_config() |
H100 |
(1024, 512, 256) |
get_auto_config() |
A100 |
(1024, 512, 256) |
get_auto_config() |
high (40GB+) |
(512, 256, 128) |
get_auto_config() |
medium (20-40GB) |
(512, 256, 128) |
get_auto_config() |
low (<20GB) |
(256, 128, 64) |
get_a100_config() |
A100-80GB |
(1024, 512, 256) |
get_a100_config() |
A100-40GB |
(512, 256, 128) |
Verified Workflow
To use larger architecture (148 MB models):
from alpaca_trading.gpu.ppo_trainer_native import get_auto_config
config = get_auto_config(total_timesteps=200_000_000, training_mode='production')
config.hidden_dims = (2048, 1024, 512, 256) # Override to 4-layer large
trainer = NativePPOTrainer(env, config)
To verify model architecture before training:
import torch
# Check expected size
obs_dim = 5300 # 53 features × 100 lookback
hidden_dims = (2048, 1024, 512, 256)
params = obs_dim * hidden_dims[0] # First layer
for i in range(len(hidden_dims) - 1):
params += hidden_dims[i] * hidden_dims[i+1]
params += hidden_dims[-1] * 64 * 2 # Actor + critic heads
print(f"Expected params: {params:,}")
print(f"Expected size: ~{params * 4 * 3 / 1024 / 1024:.0f} MB") # float32 × 3 (weights + optimizer state)
To inspect existing model:
import torch
ckpt = torch.load('model.pt', map_location='cpu', weights_only=False)
print(f"hidden_dims: {ckpt['config'].hidden_dims}")
print(f"Total params: {sum(v.numel() for v in ckpt['policy_state_dict'].values()):,}")
Failed Attempts (Critical)
| Attempt |
Why it Failed |
Lesson Learned |
| Assuming all configs use same architecture |
Different GPU tiers have different defaults |
Always check hidden_dims in config before training |
| Only checking layer count |
3-layer (1024,512,256) vs 4-layer (2048,1024,512,256) |
First layer width matters more than depth |
| Not saving config with model |
Couldn't reproduce training |
Always save full config in checkpoint |
| Using large architecture on small GPU |
OOM errors |
Match architecture to available VRAM |
| Assuming bigger = better |
Overfitting on small datasets |
Larger models need more data/regularization |
Performance Considerations
Larger Architecture (2048, 1024, 512, 256)
Pros:
- Higher model capacity for complex patterns
- Better for symbols with rich feature interactions
- May capture longer-term dependencies
Cons:
- 2x file size (~148 MB vs ~72 MB)
- Slower inference (~1.5-2x)
- Higher VRAM usage during training
- More prone to overfitting with limited data
Smaller Architecture (1024, 512, 256)
Pros:
- Faster inference (important for live trading)
- Lower VRAM requirements
- Faster training iterations
- Better generalization on limited data
Cons:
- May underfit complex market dynamics
- Less capacity for feature interactions
Recommended Architecture by Use Case
| Use Case |
Recommended hidden_dims |
Rationale |
| Quick iteration/testing |
(512, 256, 128) |
Fast training, low memory |
| Standard production |
(1024, 512, 256) |
Good balance |
| Complex symbols (crypto) |
(2048, 1024, 512, 256) |
Higher volatility patterns |
| Limited training data (<1 year) |
(512, 256, 128) |
Reduce overfitting |
| Extended training (500M+ steps) |
(2048, 1024, 512, 256) |
Capacity for more learning |
Key Insights
- First layer width dominates model size - doubling first layer ~doubles total params
- File size ≈ params × 12 bytes (float32 weights + Adam optimizer moments)
- Current v2.3 defaults favor smaller models - optimized for speed over capacity
- Architecture mismatch = inference failure - models trained with different hidden_dims are incompatible
- Always log hidden_dims - critical for reproducibility and debugging
Diagnostic Commands
# Compare two model architectures
def compare_models(path1, path2):
m1 = torch.load(path1, map_location='cpu', weights_only=False)
m2 = torch.load(path2, map_location='cpu', weights_only=False)
print(f"Model 1: {m1['config'].hidden_dims}")
print(f"Model 2: {m2['config'].hidden_dims}")
print(f"Params 1: {sum(v.numel() for v in m1['policy_state_dict'].values()):,}")
print(f"Params 2: {sum(v.numel() for v in m2['policy_state_dict'].values()):,}")
References
alpaca_trading/gpu/ppo_trainer_native.py: Lines 1314, 1339, 1726, 1760
alpaca_trading/gpu/ppo_trainer_native.py: NativeActorCritic class (line 305)
- CLAUDE.md: GPU Optimized Settings table
1---
2name: network-architecture-sizing
3description: "PPO network architecture sizing for trading models. Trigger: (1) model files are unexpectedly small/large, (2) choosing hidden_dims for training, (3) balancing model capacity vs inference speed."
4author: Claude Code
5date: 2025-12-18
6metadata:
7 source: https://skillmd.com/skills/smith6jt-cop/network-architecture-sizing
8---
9
10# Network Architecture Sizing - Research Notes
11
12## Experiment Overview
13| Item | Details |
14|------|---------|
15| **Date** | 2025-12-18 |
16| **Goal** | Understand relationship between hidden_dims and model file size |
17| **Environment** | Google Colab A100, PyTorch 2.x, NativePPOTrainer |
18| **Status** | Documented |
19
20## Context
21Training runs produced models at ~72 MB instead of expected ~148 MB. Investigation revealed the `hidden_dims` configuration determines model size, with the first layer dominating total parameter count due to multiplication with observation dimensions.
22
23## Architecture Comparison
24
25### Model Size vs Architecture
26
27| Architecture | hidden_dims | Layers | Total Params | File Size | First Layer Size |
28|--------------|-------------|--------|--------------|-----------|------------------|
29| Large (v2.2) | (2048, 1024, 512, 256) | 4 | 12.6M | ~148 MB | 2048 × obs_dim |
30| Medium (v2.3) | (1024, 512, 256) | 3 | 6.1M | ~72 MB | 1024 × obs_dim |
31| Small | (512, 256, 128) | 3 | ~1.5M | ~18 MB | 512 × obs_dim |
32| Tiny | (256, 128, 64) | 3 | ~0.4M | ~5 MB | 256 × obs_dim |
33
34### Why First Layer Dominates
35
36With 53 features × 100 lookback = 5,300 input dimensions:
37- Large: `2048 × 5300 = 10.9M params` (86% of network)
38- Medium: `1024 × 5300 = 5.4M params` (89% of network)
39- Small: `512 × 5300 = 2.7M params` (90% of network)
40
41**Key insight**: The first hidden layer dimension has exponentially more impact on model size than deeper layers.
42
43## Configuration Locations
44
45Current defaults in `ppo_trainer_native.py`:
46
47| Function | GPU Tier | hidden_dims |
48|----------|----------|-------------|
49| `get_auto_config()` | H100 | (1024, 512, 256) |
50| `get_auto_config()` | A100 | (1024, 512, 256) |
51| `get_auto_config()` | high (40GB+) | (512, 256, 128) |
52| `get_auto_config()` | medium (20-40GB) | (512, 256, 128) |
53| `get_auto_config()` | low (<20GB) | (256, 128, 64) |
54| `get_a100_config()` | A100-80GB | (1024, 512, 256) |
55| `get_a100_config()` | A100-40GB | (512, 256, 128) |
56
57## Verified Workflow
58
59### To use larger architecture (148 MB models):
60```python
61from alpaca_trading.gpu.ppo_trainer_native import get_auto_config
62
63config = get_auto_config(total_timesteps=200_000_000, training_mode='production')
64config.hidden_dims = (2048, 1024, 512, 256) # Override to 4-layer large
65
66trainer = NativePPOTrainer(env, config)
67```
68
69### To verify model architecture before training:
70```python
71import torch
72
73# Check expected size
74obs_dim = 5300 # 53 features × 100 lookback
75hidden_dims = (2048, 1024, 512, 256)
76
77params = obs_dim * hidden_dims[0] # First layer
78for i in range(len(hidden_dims) - 1):
79 params += hidden_dims[i] * hidden_dims[i+1]
80params += hidden_dims[-1] * 64 * 2 # Actor + critic heads
81
82print(f"Expected params: {params:,}")
83print(f"Expected size: ~{params * 4 * 3 / 1024 / 1024:.0f} MB") # float32 × 3 (weights + optimizer state)
84```
85
86### To inspect existing model:
87```python
88import torch
89
90ckpt = torch.load('model.pt', map_location='cpu', weights_only=False)
91print(f"hidden_dims: {ckpt['config'].hidden_dims}")
92print(f"Total params: {sum(v.numel() for v in ckpt['policy_state_dict'].values()):,}")
93```
94
95## Failed Attempts (Critical)
96
97| Attempt | Why it Failed | Lesson Learned |
98|---------|---------------|----------------|
99| Assuming all configs use same architecture | Different GPU tiers have different defaults | Always check `hidden_dims` in config before training |
100| Only checking layer count | 3-layer (1024,512,256) vs 4-layer (2048,1024,512,256) | First layer width matters more than depth |
101| Not saving config with model | Couldn't reproduce training | Always save full config in checkpoint |
102| Using large architecture on small GPU | OOM errors | Match architecture to available VRAM |
103| Assuming bigger = better | Overfitting on small datasets | Larger models need more data/regularization |
104
105## Performance Considerations
106
107### Larger Architecture (2048, 1024, 512, 256)
108**Pros:**
109- Higher model capacity for complex patterns
110- Better for symbols with rich feature interactions
111- May capture longer-term dependencies
112
113**Cons:**
114- 2x file size (~148 MB vs ~72 MB)
115- Slower inference (~1.5-2x)
116- Higher VRAM usage during training
117- More prone to overfitting with limited data
118
119### Smaller Architecture (1024, 512, 256)
120**Pros:**
121- Faster inference (important for live trading)
122- Lower VRAM requirements
123- Faster training iterations
124- Better generalization on limited data
125
126**Cons:**
127- May underfit complex market dynamics
128- Less capacity for feature interactions
129
130## Recommended Architecture by Use Case
131
132| Use Case | Recommended hidden_dims | Rationale |
133|----------|------------------------|-----------|
134| Quick iteration/testing | (512, 256, 128) | Fast training, low memory |
135| Standard production | (1024, 512, 256) | Good balance |
136| Complex symbols (crypto) | (2048, 1024, 512, 256) | Higher volatility patterns |
137| Limited training data (<1 year) | (512, 256, 128) | Reduce overfitting |
138| Extended training (500M+ steps) | (2048, 1024, 512, 256) | Capacity for more learning |
139
140## Key Insights
141
142- **First layer width dominates model size** - doubling first layer ~doubles total params
143- **File size ≈ params × 12 bytes** (float32 weights + Adam optimizer moments)
144- **Current v2.3 defaults favor smaller models** - optimized for speed over capacity
145- **Architecture mismatch = inference failure** - models trained with different hidden_dims are incompatible
146- **Always log hidden_dims** - critical for reproducibility and debugging
147
148## Diagnostic Commands
149
150```python
151# Compare two model architectures
152def compare_models(path1, path2):
153 m1 = torch.load(path1, map_location='cpu', weights_only=False)
154 m2 = torch.load(path2, map_location='cpu', weights_only=False)
155
156 print(f"Model 1: {m1['config'].hidden_dims}")
157 print(f"Model 2: {m2['config'].hidden_dims}")
158 print(f"Params 1: {sum(v.numel() for v in m1['policy_state_dict'].values()):,}")
159 print(f"Params 2: {sum(v.numel() for v in m2['policy_state_dict'].values()):,}")
160```
161
162## References
163- `alpaca_trading/gpu/ppo_trainer_native.py`: Lines 1314, 1339, 1726, 1760
164- `alpaca_trading/gpu/ppo_trainer_native.py`: NativeActorCritic class (line 305)
165- CLAUDE.md: GPU Optimized Settings table