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

4 views (last 30 days)
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 Comments
Zachary
Zachary on 30 May 2023
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.

Sign in to comment.

Accepted Answer

Ranjeet
Ranjeet on 5 Jun 2023
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:

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!