Clear Filters
Clear Filters

How to train a vector multiple-input multiple-output network

12 views (last 30 days)
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?

Accepted Answer

Matt J
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
Kennosuke Nakabayashi
Kennosuke Nakabayashi on 7 Jun 2024
Thank you for answering my question. You have helped me resolve this error. However, I got a new error statement. The change is that I defined the datastore with the arrayDatastore function.
% 入力とターゲット
x = [[0:0.1:10]' [0:0.1:10]']
t = sin(x)
ds=combine(arrayDatastore(x(:,1),"ReadSize",length(x(:,1))),...
arrayDatastore(x(:,2),"ReadSize",length(x(:,2))),...
arrayDatastore(t(:,1),"ReadSize",length(t(:,1))),...
arrayDatastore(t(:,2),"ReadSize",length(t(:,2))))
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','SB'} ...
)
% ニューラルネットワークのトレーニング
customLossFunction = @(Y, T) customloss(Y, T);
net = trainnet(ds,net,customLossFunction,options) % x,tは縦ベクトル
The statement of that error is this.
Error using trainnet (line 46)
Layer 'input1': Invalid input data. Invalid number of spatial dimensions. Layer expects 0 but received 1.
Error in parallel_learn_test (line 57)
net = trainnet(ds,net,customLossFunction,options) % x,tは縦ベクトル
Sorry for asking so many questions.
Matt J
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
p = 1x4 cell array
{[0]} {[0]} {[0]} {[0]}
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は縦ベクトル
Iteration Epoch TimeElapsed LearnRate TrainingLoss _________ _____ ___________ _________ ____________ 1 1 00:00:01 0.01 9.3311 50 5 00:00:02 0.01 9.9611 100 9 00:00:03 0.01 8.9918 120 10 00:00:03 0.01 8.9776 Training stopped: Max epochs completed
net =
dlnetwork with properties: Layers: [12x1 nnet.cnn.layer.Layer] Connections: [10x2 table] Learnables: [12x3 table] State: [0x3 table] InputNames: {'input1' 'input2'} OutputNames: {'fc_3' 'fc_6'} Initialized: 1 View summary with summary.

Sign in to comment.

More Answers (0)

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!