How to train a vector multiple-input multiple-output network
12 views (last 30 days)
Show older comments
I want to train a vector multi-input, multi-output network, but I get the error “Number of input data formats (1) and number of network inputs (2) must match”. This is the code,
clear
input1ds=signalDatastore("input1.csv")
input2ds=signalDatastore("input2.csv")
output1ds=signalDatastore("output1.csv")
output2ds=signalDatastore("output2.csv")
ds=combine(input1ds,input2ds,output1ds,output2ds)
isShuffleable(ds)
% 入力とターゲット
% x = [[0:0.1:10]' [0:0.1:10]']
% t = sin(x)
net=dlnetwork
% ニューラルネットワークの定義
layers1 = [
featureInputLayer(1,"name","input1")
fullyConnectedLayer(10) % 10ユニットの全結合層
tanhLayer
fullyConnectedLayer(10) % 10ユニットの全結合層
tanhLayer
fullyConnectedLayer(1) % 1ユニットの全結合層
];
layers2 = [
featureInputLayer(1,"Name","input2")
fullyConnectedLayer(10) % 10ユニットの全結合層
tanhLayer
fullyConnectedLayer(10) % 10ユニットの全結合層
tanhLayer
fullyConnectedLayer(1) % 1ユニットの全結合層
];
net=addLayers(net,layers1)
net=addLayers(net,layers2)
net.plot
% layers=layerGraph()
% layers=addLayers(layers,layers1)
% layers=addLayers(layers,layers2)
% オプションの設定
options = trainingOptions('sgdm', ... % 最適化アルゴリズム
'MaxEpochs', 500, ... % 最大エポック数
'MiniBatchSize', 2^3, ... % ミニバッチサイズ
'Verbose', true,...% 進行状況の表示
'InputDataFormats', {'SB'} ...
)
% ニューラルネットワークのトレーニング
customLossFunction = @(Y, T) customloss(Y, T);
net = trainnet(ds,net,customLossFunction,options) % x,tは縦ベクトル
The “input1.csv” and “output1.csv” all contain a single column of vertical vectors. This time, it is a 2-input, 2-output network.
The function “customloss” is a function I defined myself. This is the error statement.
Error using trainnet (line 46)
Number of input data formats (1) and number of network inputs (2) must match.
Error in parallel_learn_test (line 53)
net = trainnet(ds,net,customLossFunction,options) % x,tは縦ベクトル
What is wrong? And what solutions are available?
0 Comments
Accepted Answer
Matt J
on 6 Jun 2024
Edited: Matt J
on 6 Jun 2024
You have only entered one InputDataFormat. The error message is telling you that you need two of them, e.g.,
options = trainingOptions(______
'InputDataFormats', {'SB','SB'} ...
)
because the network has two inputs.
2 Comments
Matt J
on 7 Jun 2024
Edited: Matt J
on 7 Jun 2024
Use 'CB' as the input format ReadSize=1,
% 入力とターゲット
x = [[0:0.1:10]' [0:0.1:10]'];
t = sin(x);
N=height(x);
fcn=@(z) arrayDatastore(z,'ReadSize',1);
ds=combine( fcn(x(:,1)), fcn(x(:,2)) , fcn(t(:,1)), fcn(t(:,2)) );
p=ds.preview
net=dlnetwork;
% ニューラルネットワークの定義
layers1 = [
featureInputLayer(1,"name","input1")
fullyConnectedLayer(10) % 10ユニットの全結合層
tanhLayer
fullyConnectedLayer(10) % 10ユニットの全結合層
tanhLayer
fullyConnectedLayer(1) % 1ユニットの全結合層
];
layers2 = [
featureInputLayer(1,"Name","input2")
fullyConnectedLayer(10) % 10ユニットの全結合層
tanhLayer
fullyConnectedLayer(10) % 10ユニットの全結合層
tanhLayer
fullyConnectedLayer(1) % 1ユニットの全結合層
];
net=addLayers(net,layers1);
net=addLayers(net,layers2);
% layers=layerGraph()
% layers=addLayers(layers,layers1)
% layers=addLayers(layers,layers2)
% オプションの設定
options = trainingOptions('sgdm', ... % 最適化アルゴリズム
'MaxEpochs', 10, ... % 最大エポック数
'MiniBatchSize', 2^3, ... % ミニバッチサイズ
'Verbose', true,...% 進行状況の表示
'InputDataFormats', {'CB','CB'} ...
);
% ニューラルネットワークのトレーニング
customLossFunction = @(Y1,Y2, T1,T2) sum(abs([Y1,Y2]-[T1,T2]),'all');
net = trainnet(ds,net,customLossFunction,options) % x,tは縦ベクトル
More Answers (0)
See Also
Categories
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!