Error using trainNetwork. Output size does not match the response size.
조회 수: 4 (최근 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
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Build Deep Neural Networks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!