Error using trainNetwork. Output size does not match the response size.

조회 수: 4 (최근 30일)
Zachary
Zachary 2023년 5월 30일
답변: Ranjeet 2023년 6월 5일
I have two sets of data, pressuredata and velocitydata, both are 100x100x10 doubles.
I am attempting to use trainNetwork to predict the velocity based on the pressure. I am met with the following error when I run the code:
Error using trainNetwork
Invalid training data. The output size ([1 1 100000]) of the last layer does not
match the response size ([100 100 10]).
Error in Airfoil_in_cross_flow_data (line 116)
net = trainNetwork(inputData, outputData, layers, options);
What changes to I need to make to the layer settings to get this to function? Current code is below:
%% Define and prepare the input and output data
inputData = pressuredata; % Set input = pressure data
outputData = velocitydata; % Set output = velocity field
%% Determine input size
inputSize = numel(inputData); % 100 x 100 x 10
outputSize = numel(outputData); % 100 x 100 x 10
%% Set layer variables
layers = [
imageInputLayer([100 100 10])
fullyConnectedLayer(100) % Adjust the number of neurons in the fully connected layer as needed
reluLayer % Activation function
fullyConnectedLayer(outputSize)
regressionLayer % Regression layer for continuous output
];
%% Define training options
options = trainingOptions('adam', ...
'MaxEpochs', 50, ...
'MiniBatchSize', 32, ...
'Verbose', true);
%% Train network
net = trainNetwork(inputData, outputData, layers, options);
  댓글 수: 2
Matt J
Matt J 2023년 5월 30일
Why are you flattening the data with ,
inputData(:), outputData(:)
Zachary
Zachary 2023년 5월 30일
Apologies - that was a carry over from another example I was playing around with. Removing that sees a new error, where there is a difference between the output and response size.

댓글을 달려면 로그인하십시오.

채택된 답변

Ranjeet
Ranjeet 2023년 6월 5일
Hi Zachary,
I understand that you want to train a neural network with input and output of size [100, 100, 10]. The input has been given as imageInputLayer but there is a fullyConnectedLayer in the output before regression layer. FullyConnectedLayer does not provide output in the expected dimension but it specifies the output as a one-dimensional vector.
There needs to be reshaping of the data before feeding into regression layer. Following is the modified network and a custom nnet layer to resize the output from fully connected layer has been given:
Modified network:
pressuredata = rand(100, 100, 10);
velocitydata = rand(100, 100, 10);
%% Define and prepare the input and output data
inputData = pressuredata; % Set input = pressure data
outputData = velocitydata; % Set output = velocity field
%% Determine input size
inputSize = size(inputData); % 100 x 100 x 10
outputSize = size(outputData); % 100 x 100 x 10
%% Set layer variables
layers = [
imageInputLayer([100 100 10])
fullyConnectedLayer(100) % Adjust the number of neurons in the fully connected layer as needed
reluLayer % Activation function
fullyConnectedLayer(numel(outputData))
reshapeLayer("reshape layer", outputSize)
regressionLayer % Regression layer for continuous output
];
%% analyze network
analyzeNetwork(layers);
%% Define training options
options = trainingOptions('adam', ...
'MaxEpochs', 50, ...
'MiniBatchSize', 32, ...
'Verbose', true);
%% Train network
net = trainNetwork(inputData, outputData, layers, options);
reshapeLayer class:
classdef reshapeLayer < nnet.layer.Layer
properties
outputSize;
end
properties (Learnable)
end
methods
function layer = reshapeLayer(name, outputSize)
layer.Name = name;
layer.outputSize = outputSize;
end
function [Z] = predict(layer, X)
Z = reshape(X,layer.outputSize(1), layer.outputSize(2), layer.outputSize(3),[]);
end
end
end
You may refer the following example as well:

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Build Deep Neural Networks에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by