no. of samples while using 'train' for a neural network

2 views (last 30 days)
I am solving an 'Input-Output Time-Series Problem with a Time Delay Neural Network'.
I am getting an error in the statement-
[net,tr] = train(net,inputs,targets,inputStates,layerStates);
error-- 'Inputs and targets have different numbers of samples'
the dimensions of the various variable are--
inputStates = 1*2 cell
inputs= 1*59 cell
layerStates=1*2 cell
targets=1*59 cell.
Also each column of 'inputs' cell array is a vector of 5 double values. i.e using 5 features of a problem we are predicting a target value. number of timestamps=61, input delays=2.
I dont why is the error??? inputs and targets shows the same dimensions in workspace too..
  2 Comments
Meghna
Meghna on 20 Nov 2014
Edited: Meghna on 20 Nov 2014
%% Import data from spreadsheet
%% Import the data
[~, ~, raw] = xlsread('input_sheet.xlxs');
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
%% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw® = {NaN}; % Replace non-numeric cells
%% Create output variable
data = reshape([raw{:}],size(raw));
%% Clear temporary variables
clearvars R;
%% Import the data
[~, ~, class] = xlsread('outputsheet.xlxs');
class(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),class)) = {''};
%% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),class); % Find non-numeric cells
class® = {NaN}; % Replace non-numeric cells
%% Create output variable
lan=transpose(class);
z=size(data,1);
d=num2cell(data,2);
data_set=transpose(d);
inputSeries = data_set; targetSeries = lan;
% Create a Time Delay Network
inputDelays = 1:2;
hiddenLayerSize = 10;
net = timedelaynet(inputDelays,hiddenLayerSize);
% Choose Input and Output Pre/Post-Processing Functions % For a list of all processing functions type: help nnprocess
net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'};
net.outputs{2}.processFcns = {'removeconstantrows','mapminmax'};
[inputs,inputStates,layerStates,targets] = preparets(net,inputSeries,targetSeries);
% Setup Division of Data for Training, Validation, Testing % For a list of all data division functions type: help nndivide
net.divideFcn = 'dividerand'; % Divide data randomly net.divideMode = 'time'; % Divide up every value net.divideParam.trainRatio = 70/100; net.divideParam.valRatio = 15/100; net.divideParam.testRatio = 15/100;
% For help on training function 'trainlm' type: help trainlm % For a list of all training functions type: help nntrain
net.trainFcn = 'trainlm'; % Levenberg-Marquardt
% Choose a Performance Function
% For a list of all performance functions type: help nnperformance
net.performFcn = 'mse'; % Mean squared error
% Choose Plot Functions
% For a list of all plot functions type: help nnplot
net.plotFcns = {'plotperform','plottrainstate','plotresponse', ... 'ploterrcorr', 'plotinerrcorr'};
% Train the Network
[net,tr] = train(net,inputs,targets,inputStates,layerStates); %HERE IS THE ERROR
% Test the Network
outputs = net(inputs,inputStates,layerStates); errors = gsubtract(targets,outputs); performance = perform(net,targets,outputs);
% Recalculate Training, Validation and Test Performance
trainTargets = gmultiply(targets,tr.trainMask); valTargets = gmultiply(targets,tr.valMask); testTargets = gmultiply(targets,tr.testMask); trainPerformance = perform(net,trainTargets,outputs); valPerformance = perform(net,valTargets,outputs); testPerformance = perform(net,testTargets,outputs);
% View the Network view(net)
PLEASE REPLY AS SOON AS POSSIBLE i am new to this time series prediction.I am working on my project for m.tech thesis.
I have used Pollution Mortality example of the matlab tool box as reference.
|monospaced|

Sign in to comment.

Accepted Answer

Greg Heath
Greg Heath on 23 Nov 2014
I ran your code with the pollution_dataset.
The same error occurred until I placed the code after the plot function continuation symbol "..." on the next line.
I have several suggestions which are exemplified in the following code that takes advantage of defaults:
close all, clear all, clc
[ X, T ] = pollution_dataset; % MIMO
x = cell2mat(X);
t = cell2mat(T);
whos
% Name Size Bytes Class
% T 1x508 69088 cell
% X 1x508 89408 cell
% t 3x508 12192 double
% x 8x508 32512 double
[I N ] = size(x) % [ 8 508 ]
[O N ] = size(t) % [ 3 508 ]
% Avoid overfitting: Choose H <= Hub
Hub = -1+ ceil((0.7*N*O-O) / ( I+O+1)) % 88
ID = 0:2, H = 10 % DEFAULT
net = timedelaynet(ID,H);
[ Xs Xi Ti Ts ] = preparets(net,X,T);
ts = cell2mat(Ts);
MSE00s = mean(var(ts',1)) % 102.62
net.divideFcn = 'divideblock';
Ntrials = 10
rng('default')
for i=1:Ntrials
net = configure(net,Xs,Ts);
[ net tr Ys Es Xf Af ] = train(net,Xs,Ts,Xi,Ti);
% NOTE: outputs = Ys and errors = Es;
R2(i,1) = 1 - mse(Es)/MSE00s;
R2trn(i,1) = 1 - mse(Es(:,tr.trainInd))/MSE00s;
R2val(i,1) = 1 - mse(Es(:,tr.valInd))/MSE00s;
R2tst(i,1) = 1 - mse(Es(:,tr.testInd))/MSE00s;
end
result = [(1:Ntrials)' R2 R2trn R2val R2tst ]
% result =
%
% 1 0.5936 0.63533 0.51213 0.47697
% 2 0.67104 0.7206 0.60579 0.50104
% 3 0.58657 0.73979 0.50454 -0.058704
% 4 0.63051 0.69725 0.60746 0.3368
% 5 0.48704 0.65898 0.34343 -0.1855
% 6 -0.016047 0.66023 0.4646 -3.7067
% 7 0.49035 0.58225 0.54201 0.0025204
% 8 0.54354 0.53697 0.58071 0.53752
% 9 0.60964 0.61695 0.56824 0.61634
% 10 0.61471 0.63631 0.63058 0.49629
% Possible Improvements
% 1. Check for outlier removal
% 2. Choose ID w.r.t input/target crosscorrelations
% 3. Use narxnet and FD w.r.t. target autocorrelations
% 4. Increase H (Hub = 88)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!