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

9 次查看(过去 30 天)
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 个评论
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 中查找有关 Deep Learning Toolbox 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by