error with Input data sizes in using sim

16 ビュー (過去 30 日間)
arash rad
arash rad 2022 年 10 月 10 日
回答済み: Meet 2024 年 9 月 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');
  2 件のコメント
Benjamin Thompson
Benjamin Thompson 2022 年 10 月 10 日
Can you attach your CSV file so the Community can run your example code?
arash rad
arash rad 2022 年 10 月 11 日
Hi Benjamin
I attached my data

サインインしてコメントする。

回答 (1 件)

Meet
Meet 2024 年 9 月 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 Center および File ExchangeStatics and Dynamics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by