I'm looking to adapt the transformer machine learning model arch to my sequence to seqeunce stock modelling problem. My input data is typical stock information "close", "open", "volume", diffrent moving averages etc. In the typical GPT transformer arch a pytorch nn embedding layer is used as the data from the tokeniser will be a sparse one hot encoded vector for the token, whereas in my usecase the timestep or token has a non sparse representation of the various features of the stock at that point.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
You're absolutely right about this key difference. In stock modeling, you already have dense feature vectors at each timestep, so you don't need the sparse-to-dense mapping that embedding layers provide in NLP.
Here are the main approaches for handling your dense input features:
Linear Projection Layer
Replace the embedding layer with a linear transformation that maps your input feature dimension to the transformer's hidden dimension:
python
# Instead of nn.Embedding(vocab_size, d_model)
self.input_projection = nn.Linear(n_features, d_model)
# Where n_features is your number of stock features per timestep
Direct Input (if dimensions match)
If your feature vector already matches the transformer's expected input dimension, you can feed it directly to the transformer blocks after adding positional encoding.
Feature Engineering Considerations
Since you're working with heterogeneous features (prices, volumes, ratios), consider:
Normalizing features to similar scales
Using separate projections for different feature types, then concatenating
Adding learnable feature type embeddings if you want the model to distinguish between price-based vs volume-based features
Positional Encoding
You'll still want positional encoding to help the model understand temporal relationships, but you might experiment with learned positional embeddings rather than sinusoidal ones for time series data.
The transformer's self-attention mechanism should work well for capturing dependencies between different time steps in your stock data. Have you considered whether you want a causal (GPT-style) or bidirectional (BERT-style) attention pattern for your predictions?