Batch and input shape to DL model

1 回表示 (過去 30 日間)
Ahmed
Ahmed 2025 年 7 月 16 日
編集済み: Umar 2025 年 7 月 17 日
I have a dataset for single channel EEG signal, each record is represented by a vector 1*4000
No I have a DL model where the input layer is a sequence layer and followed by a cwt layer and it's followed by CNN layers ending by a fully connected layer with 64 neurons so that this model extract features for each record, I batched the dataset to learn the model batch by batch, what should be the shape of the batch so that it would be compatible with the model. I've read that batch should be in the CBT format but I expect it to be BCT or even CTB format.
Below is the code for these parts. I'd be thankful for your help.
layers = [sequenceInputLayer(1,'MinLength',segment_length,'Name',"input")
cwtLayer('SignalLength',segment_length,'IncludeLowpass',false,'Wavelet','amor',...
'FrequencyLimits',[0 0.5])
convolution2dLayer([5,10],1,'stride',2)
maxPooling2dLayer([5,10])
convolution2dLayer([5,10],5,'Padding','same')
maxPooling2dLayer([5,10])
batchNormalizationLayer
reluLayer
dropoutLayer(0.2,'Name','drop1')
convolution2dLayer([5,10],10,'Padding','same')
maxPooling2dLayer([2,4])
batchNormalizationLayer
reluLayer
dropoutLayer(0.2,'Name','drop2')
flattenLayer
globalAveragePooling1dLayer
dropoutLayer(0.2,'Name','drop3')
fullyConnectedLayer(64)
];
%% Mini-batch queues
mbqTrain = minibatchqueue(transTrainDS, 2, ...
'minibatchSize', minibatchSize, ...
'OutputAsDlarray', [1,1], ...
'minibatchFcn', @processMB, ...
'OutputCast', {'single','single'}, ...
'minibatchFormat', {'CTB','B'});
function [dlX,dlY] = processMB(Xcell,Ycell)
%PROCESSMB Preprocess mini-batch for 1D time series
%
% Inputs:
% - Xcell: cell array of [1 x T] numeric vectors
% - Ycell: cell array of labels (strings)
%
% Outputs:
% - dlX: dlarray of size [1 x T x N], format 'CTB'
% - dlY: categorical or numeric column vector [N x 1]
Xcell = cellfun(@(x)reshape(x,1,1,[]),Xcell,'uni',false); % Reshape all elements in cell array to be 1 x 1 x T
Ycell = cellfun(@(x)str2double(x),Ycell,'uni',false); % convert the cell array of labels elements from string into double
dlX = cat(1,Xcell{:}); % Convert all records into an array of shape N x 1 x T
dlX = permute(dlX, [2 3 1]); % change oreder to be 1 x T x N
dlY = cat(1,Ycell{:}); % Conver all labels into a single array of shape N x 1
end

回答 (1 件)

Umar
Umar 2025 年 7 月 17 日
編集済み: Umar 2025 年 7 月 17 日

Hi @Ahmed,

Note: I am currently using Matlab Mobile and cwtLayer requires Wavelet Toolbox which I don’t have access to.

However, going through your comments and snippet code provided, bear in mind that each EEG record is a 1D signal of length 4000, when creating mini-batches, it would be beneficial to structure them in a way that respects the input requirements of the sequence layer. For example, if you choose to structure your batches in the 'CTB' format, each batch could have a shape of `[1, 4000, N]`, where `N` is the number of samples in your batch (the batch size). This means: ‘1`: Represents the single channel, ‘4000`: The length of each EEG signal, `N`: The number of samples in each mini-batch.

In your `processMB` function, you should ensure that when reshaping the `Xcell`, it aligns with the expected input shape. The line where you permute `dlX` should ensure that it has dimensions `[1, 4000, N]` if you're following the CTB format. The current implementation reshapes `Xcell` into `[1 x 1 x T]`, which is correct if you're aiming for compatibility with subsequent layers.

Here are some additional tips that might help.

Data Preparation: Ensure that your preprocessing steps adequately prepare your data for CWT and CNN layers. The CWT layer expects an appropriately shaped input to compute wavelet transforms effectively.

Batch Size: Choose a batch size that fits within your computational resources while ensuring efficient training dynamics. A common choice for time-series data might range from 32 to 128 samples per batch.

Model Training Dynamics: Monitor loss and accuracy metrics during training to ensure that your batching strategy effectively captures temporal dependencies in EEG signals.

Hope these tips will help resolve your problem.

カテゴリ

Help Center および File ExchangeAI for Signals and Images についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by