How to flatten the output of convolution1dLayer

8 ビュー (過去 30 日間)
bookmaster
bookmaster 2022 年 6 月 26 日
コメント済み: bookmaster 2022 年 7 月 1 日
Hello!
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 = [ ...
sequenceInputLayer(numFeatures)
convolution1dLayer(filterSize,numFilters,Padding="causal")
reluLayer
layerNormalizationLayer
convolution1dLayer(filterSize,2*numFilters,Padding="causal")
reluLayer
layerNormalizationLayer
functionLayer(@(X) dlarray(X(:),"CB"),Formattable=true,Description="My flatten") %globalAveragePooling1dLayer
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
"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
Abolfazl Chaman Motlagh 2022 年 6 月 26 日
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:)
  1 件のコメント
bookmaster
bookmaster 2022 年 7 月 1 日
Thanks for the quick reply.
I tried the replied method, but the same error still occurred.
As mentioned in your reply, the conv1D output, i.e., each feature map (CBT) for sequence input should be replaced with the feature vector (CB) for the fully connected layer connected by Softmax. Indeed, the global pooling layer performs that kind of thing in this example.
Anyway, it is simple in the Keras environment but these additional troubles occur in the MATLAB environment.
Is that the wrong approach to design 'flatten' using a function layer?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by