2 views (last 30 days)
Show older comments
Zachary on 30 May 2023
-
-
Link
Direct link to this question
https://matlabcentral.mathworks.com/matlabcentral/answers/1975619-error-using-trainnetwork-output-size-does-not-match-the-response-size
Answered: Ranjeet on 5 Jun 2023
Accepted Answer: Ranjeet
Open in MATLAB Online
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 Show NoneHide None
Show NoneHide None
Matt J on 30 May 2023
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/1975619-error-using-trainnetwork-output-size-does-not-match-the-response-size#comment_2764579
Open in MATLAB Online
Why are you flattening the data with ,
inputData(:), outputData(:)
Zachary on 30 May 2023
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/1975619-error-using-trainnetwork-output-size-does-not-match-the-response-size#comment_2764659
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.
Sign in to answer this question.
Accepted Answer
Ranjeet on 5 Jun 2023
Open in MATLAB Online
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:
Reshaping fully connected output
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
More Answers (0)
Sign in to answer this question.
See Also
Categories
AI, Data Science, and StatisticsDeep Learning Toolbox
Find more on Deep Learning Toolbox 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!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office