How to flatten the output of convolution1dLayer
9 views (last 30 days)
I tried Sequence Classification Using 1D Convolution example and replaced its layer structure.
As listed below, I changed the global average pooling layer to a simple flatten layer using the function layer.
layers = [ ...
functionLayer(@(X) dlarray(X(:),"CB"),Formattable=true,Description="My flatten") %globalAveragePooling1dLayer
"analyzeNetwork(layers)" had no error, but training failed.
Error "Incorrect dimensions for matrix multiplication" was poped in trainNetwork process.
I want to evaluate and compare traditional flatten like Keras flatten() to global pooling.
Is there any good way for this work?
Abolfazl Chaman Motlagh on 26 Jun 2022
by X(:) you are reshaping x to Nx1 vector, but you chose "CB" format for the array which is going to assume the array is 2 dimensional.( and it should be) try using size and reshape.
if the previous format is "CTB" then two first dimension should merge into one dimension. so do this:
functionLayer(@(X) dlarray(reshape(X,[size(X,1)*size(X,2),size(X,3)]),"CB"),Formattable=true,Description="My flatten")
if it's not, format your data to be in this order because it is much harder task to merger non consecutive dimensions.
in creating costum layers and networks you will face a lot of errors like this for adaptation of new layer. so you may still have some troubles i guess:)