- Downsample or shorten your sequences. If the full time series isn’t needed for classification, consider reducing the number of time steps (for example, using downsampling or feature extraction) so the network processes shorter sequences.
- Apply dimensionality reduction on your 62 features. Techniques like PCA may help reduce the input size and ease memory demands.
- Use minibatch training with proper sequence truncation. You can specify a maximum sequence length in your training options, ensuring that very long sequences don’t overload the network.
- If possible, adjust the architecture to use layers that are more memory efficient than the current attention layer or use built‐in options to handle variable sequence lengths (for example, using padded sequences and masking).
Sequence to label problem
5 views (last 30 days)
Show older comments
Hello! I'm working on a multi-class problem with labels (-1, 0, 1), using sequence data of size 62×timestamps62 \times \text{timestamps}62×timestamps. I've organized the data into a cell array with dimensions trialNumbers×1 \text{trialNumbers} \times 1trialNumbers×1 (where each cell contains a 62×timestamps62 \times \text{timestamps}62×timestamps matrix). However, I'm encountering RAM size issues when using an attention layer. Could you provide guidance on how to address this problem or an example of how to prepare my data effectively? Thank you!
0 Comments
Accepted Answer
Jack
on 8 Mar 2025
When dealing with sequence‐to‐label problems that use long time series (each trial being a 62×timestamps matrix) and an attention layer, the memory usage can get very high. Here are a few strategies you might try:
Below is a simple example that downsamples your sequences to a target length before training:
% Assume dataCell is your original cell array with dimensions [trialNumbers x 1],
% where each cell is a 62-by-T matrix.
targetLength = 100; % target sequence length (adjust as needed)
dataCellDownsampled = cellfun(@(x) ...
resample(x, targetLength, size(x,2)), ...
dataCell, 'UniformOutput', false);
This approach reduces the number of time steps per trial, which should help mitigate the RAM issues you’re facing with the attention layer.
Follow me so you can message me anytime with future MATLAB questions. If this helps, please accept the answer as well.
2 Comments
Jack
on 9 Mar 2025
Hey Assem,
For your sequence-to-label problem, where each trial is a 62×timestamps matrix and you have labels (-1, 0, 1), your XTrain and YTrain need to be formatted correctly for deep learning in MATLAB.
How Should XTrain and YTrain Be Structured?
- XTrain (Input Data):
- Should be a cell array, where each cell contains a 62×T matrix (T = number of timesteps for that trial).
- This allows for variable-length sequences across trials.
- Example:
XTrain = {randn(62, 120), randn(62, 90), randn(62, 150)}; % Different sequence lengths
- YTrain (Labels):
- Should be a categorical array (if using classification) or a numeric vector (if using regression).
- Since this is a sequence-to-label task, each sequence gets one label (not a sequence of labels).
- Example:
YTrain = categorical([1; 0; -1]); % One label per sequence
- If using regression:
YTrain = [1; 0; -1]; % Numeric labels
Final Format Before Training
If using a LSTM, BiLSTM, or Attention Network, your training data should look like:
XTrain = {randn(62, 120), randn(62, 90), randn(62, 150)}; % Each trial has different timesteps
YTrain = categorical([1; 0; -1]); % One label per trial
Now you can train using trainNetwork(XTrain, YTrain, layers, options).
Follow me so you can message me anytime with future MATLAB questions. If this helps, please upvote the answer as well.
More Answers (0)
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!