How to implement PyTorch's Linear layer in Matlab?
20 ビュー (過去 30 日間)
表示 古いコメント
Hello,
The problem is that Linear does not flatten its inputs whereas Matlab's fullyConnectedLayer does, so the two are not equivalent.
Thx,
J
0 件のコメント
回答 (4 件)
Matt J
2023 年 2 月 13 日
編集済み: Matt J
2023 年 2 月 13 日
Another possible way to interpret your question is that you are trying to apply pagemtimes to the input X with a non-learnable matrix A, where the different channels of X are the pages. That can also be done with a functionLayer, as illustrated below both with normal arrays and with dlarrays,
A=rand(4,3); %non-learnable matrix A
xdata=rand(3,3,2); %input layer data with 2 channels
multLayer=functionLayer(@(X) dlarray( pagemtimes(A,stripdims(X)) ,dims(X)) );
X=dlarray(xdata,'SSC');
Y=multLayer.predict(X)
%%Verify agreement with normal pagemtimes
ydata=pagemtimes(A,xdata)
3 件のコメント
Matt J
2023 年 2 月 14 日
編集済み: Matt J
2023 年 2 月 14 日
Another approach is to write your own custom layer for channel-wise matrix multiplication. I have attached a possible version of this,
X=rand(3,3,2);
L=pagemtimesLayer(4); %Custom layer - premultiplies channels by 4-row learnable matrix A
L=initialize(L, X);
Ypred=L.predict(X)
Ycheck=pagemtimes(L.A,X) %Check agreement with a direct call to pagemtimes()
8 件のコメント
Matt J
2023 年 2 月 15 日
That sounds right.
Although, part of me questions whether it was the best design for TMW to make the the user responsible for summing over batched input in the backward() method, since that dimension should always be handled the same way.
参考
カテゴリ
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!