error with Input data sizes in using sim

조회 수: 14 (최근 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 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