error with Input data sizes in using sim

11 次查看(过去 30 天)
arash rad
arash rad 2022-10-10
回答: Meet 2024-9-19,11:19
Hi i write this code
but i have a problem when i use sim it has an error that "Input data sizes do not match net.inputs{1}.size."
I am new to ANN and i don't know what should i do
clc;clear all ; close all
%% Import Data
filename = 'zafar_queue.csv'; % abc = name of data file
flow_data = csv2struct(filename);
%% Variables
Y = flow_data.nVehContrib;
data = Y';
periods = flow_data.begin;
dates = seconds(periods);
trn = floor(0.95*(length(data)));
data_train = data(1:trn);
data_test = data(trn+1:end);
trnX = data_train(1:end);
trnY = data_train(1:end);
trndates = dates(1:trn);
testX = data_test(1:end);
testY = data_test(1:end);
testdates = dates(trn+1:end);
%% Neural (Model)
net = newfit(trnX',trnY',30);
[net,tr] = train(net, trnX', trnY');
%% Forecast and error
load_forecast = sim(net,testX)';
% err = testY - load_forecast;
% errp = (abs(err)./testY)*100;
%
% mae = mean(testY, load_forecast);
% mape = mean(errp);
%% Figures
h2 = figure;
plot(testY,'DisplayName','testY');hold on;plot(load_forecast,'DisplayName','forecast');hold off;
ylabel('Load');
h3 = figure;
plot(testdates, testY - load_forecast);
xlabel('Date'); ylabel('Error');

回答(1 个)

Meet
Meet 2024-9-19,11:19
Hi Arash,
Before addressing the error message, I would like to highlight that, based on the headers of your data file and the provided code, there appear to be some inconsistencies in how the data is prepared and used for training the neural network.
The code currently utilizes only the ‘nVehContrib’ column for both the input (‘trnX’) and the output (‘trnY’) of the neural network. This may not be the intended behavior if your goal is to predict ‘nVehContrib’ based on other features.
Instead, you could consider using various other features from your dataset to predict ‘nVehContrib’.
For Example:
%% Variables
% Use multiple features for input
X = [flow_data.flow, flow_data.occupancy, flow_data.speed, flow_data.harmonicMeanSpeed]; % 300x4
% Target variable
Y = flow_data.nVehContrib; % 300x1
% Convert to appropriate format
data_size = length(Y);
trn = floor(0.95 * data_size);
% Split data
trnX = X(1:trn, :); % 285x4
trnY = Y(1:trn); % 285x4
testX = X(trn+1:end, :); %15x4
testY = Y(trn+1:end); % 15x1
% Dates for plotting
periods = flow_data.begin;
dates = seconds(periods);
testdates = dates(trn+1:end);
Now, regarding the input data size mismatch, the line
[net, tr] = train(net, trnX', trnY');
creates a neural network with ‘inputs{1}’ size set to 4 in this example. Since you are using ‘testX’ for forecasting with the “sim” function, the input must match the size of the neural network's inputs. Therefore, it is necessary to transpose ‘testX’ when passing it to the “sim” function to ensure it aligns with the size of the network's inputs.
For Example:
%% Neural (Model)
net = newfit(trnX', trnY', 30);
[net, tr] = train(net, trnX', trnY');
%% Forecast and error
load_forecast = sim(net, testX')';
You can refer to the documentation link below for more information:

类别

Help CenterFile Exchange 中查找有关 Statics and Dynamics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by