如何用卷积层替代全连接层实现25×375到30×375矩阵投影(Matlab)
Hey there! Let's break down how to work around MATLAB's limitation with per-column fully connected layers, using an equivalent convolutional layer that does exactly what your paper describes. This trick is perfect for structured time-series data like EEG recordings.
Core Idea
Your original fully connected layer takes a 25×375 matrix (25 features × 375 time steps) and applies a 30-node FC layer to each column individually—turning every 25-dimensional time step into a 30-dimensional vector, resulting in a 30×375 output.
This is exactly what a 1×1 convolutional layer does! A 1×1 convolution operates on each spatial position (here, each time step) independently, combining all input channels (your 25 features) into new output channels (the 30 you need). It’s a direct, drop-in equivalent for your per-column FC layer.
Parameter Design (MATLAB-Friendly)
MATLAB’s deep learning toolbox expects inputs in the format height × width × channels × batch size, so we’ll adjust our setup to fit that:
Reshape Your Input
- For a single 25×375 EEG sample, reshape it to
375 × 1 × 25 × 1:- Height = number of time steps (375)
- Width = 1 (we’re treating each time step as a single "pixel" in width)
- Channels = original feature count (25)
- Batch size = 1 (for a single sample)
- For batches, use
375 × 1 × 25 × Nwhere N is your batch size.
- For a single 25×375 EEG sample, reshape it to
Configure the Convolutional Layer
- Kernel size:
[1 1](we don’t want to mix time steps—just process each one alone) - Number of output channels: 30 (matches the FC layer’s 30 nodes)
- Padding:
'valid'(no padding needed, since 1×1 kernels fit perfectly) - Stride:
[1 1](process every time step) - Weight/bias mapping:
- Your original FC layer has a 30×25 weight matrix
W. MATLAB’s conv layer expects weights inkernel height × kernel width × input channels × output channelsformat, so reshapeW'to[1 1 25 30]. - The bias vector (30×1) from the FC layer can be used directly for the conv layer.
- Your original FC layer has a 30×25 weight matrix
- Kernel size:
MATLAB Code Example
% Assume you have the original FC layer weights (W: 30×25) and bias (b: 30×1) inputDims = [375 1 25]; % Height × Width × Input Channels numOutputs = 30; % Create the equivalent 1x1 convolution layer convLayer = conv2dLayer([1 1], numOutputs, ... 'Padding', 'valid', ... 'Stride', [1 1]); % Transfer weights and bias from the original FC layer convLayer.Weights = reshape(W', [1 1 25 30]); % Transpose W to match conv dimensions convLayer.Bias = b; % Test with a sample EEG input sampleEEG = randn(25, 375); % 25 features × 375 time steps % Reshape to fit MATLAB's conv layer input format reshapedInput = reshape(sampleEEG', [375 1 25 1]); % Run forward pass convOutput = predict(convLayer, reshapedInput); % Convert back to your original 30×375 format finalOutput = reshape(convOutput, [375 30])'; % Verify equivalence with original FC layer output fcOutput = W * sampleEEG + b; disp(max(abs(finalOutput - fcOutput))); % Should be ~0, confirming they're identical
Why This Works
- A 1×1 convolution is mathematically identical to applying a fully connected layer to each spatial position (time step) independently.
- MATLAB’s conv layers handle this natively, no workarounds needed for per-column processing.
- You can directly reuse the weights and biases from the paper’s FC layer, so your model will behave exactly as described.
内容的提问来源于stack exchange,提问作者S.MC.

