The indices for validation and test sets are not being assigned correctly by 'divideind'
8 views (last 30 days)
Show older comments
I am new in matlab and I am trying to use 'divideind' to divide my data in order, but unfortunately it gives me this:
trainInd: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 … ] (1×1418 double)
valInd: [1×0 double]
testInd: [1×0 double]
I would appreciate it if you tell me where is the problem?
this is my code:
X = data.inputs;
% Define the dependent variable
y = data.output;
Q = length(y);
% Define the split points
train_end = 1418;
val_end = 1773;
test_end = Q;
% Assign the indices
train_indices = 1:train_end;
val_indices = (train_end + 1):val_end;
test_indices = (val_end + 1):test_end;
% Use divideind to ensure proper division
[train_indices,val_indices,test_indices] = divideind(Q,train_indices,val_indices,test_indices);
% Divide the data into training and testing sets
x_train = log(X(train_indices, :));
x_valid = log(X(val_indices, :));
x_test = log(X(test_indices, :));
y_train = log(y(train_indices));
y_valid = log(y(val_indices));
y_test = log(y(test_indices));
% Transpose the data to fit the input format of the neural network
inputs_train = x_train';
outputs_train = y_train';
inputs_validation = x_valid' ;
outputs_valid = y_valid';
inputs_test = x_test';
outputs_test = y_test';
% Initialize arrays to store RMSE values
train_rmse = zeros(1, 10);
Valid_rmse = zeros(1, 10);
test_rmse = zeros(1, 10);
for i = 1:10
hiddenLayerSize = [i, i];
net = fitnet(hiddenLayerSize, 'trainlm');
net.trainParam.epochs = 200;
net.layers{1}.transferFcn = 'poslin';
net.layers{2}.transferFcn = 'logsig';
net.divideFcn = 'divideind';
net.divideMode = 'sample';
net.divideParam.trainInd = train_indices;
net.divideParam.valInd = val_indices;
net.divideParam.testInd = test_indices;
[net,tr] = train(net, inputs_train, outputs_train);
% Predict on training data
train_predictions = exp(net(inputs_train(:,tr.trainInd)));
yTrainTrue = exp(outputs_train(:,tr.trainInd));
train_rmse(i) = sqrt(mean((train_predictions - yTrainTrue).^2));
% Predict on Validation data
Valid_predictions = exp(net(inputs_validation(:,tr.valInd)));
yValTrue = exp(outputs_valid(:,tr.valInd));
Valid_rmse(i) = sqrt(mean((Valid_predictions - yValTrue).^2));
% Predict on Validation data
test_predictions = exp(net(inputs_test(:,tr.testInd)));
yTestTrue = exp(outputs_test(:,tr.testInd));
test_rmse(i) = sqrt(mean((test_predictions - yTestTrue).^2));
end
0 Comments
Answers (1)
Steven Lord
on 30 May 2024
which -all divideind
When I run this code with a sample Q it gives me the results I'd expect:
callDivideInd(2000)
Or perhaps you're calling it with a smaller Q, say Q equal to train_end?
callDivideInd(1418)
function callDivideInd(Q)
train_end = 1418;
val_end = 1773;
test_end = Q;
% Assign the indices
train_indices = 1:train_end;
val_indices = (train_end + 1):val_end;
test_indices = (val_end + 1):test_end;
% Use divideind to ensure proper division
[train_indices,val_indices,test_indices] = divideind(Q,train_indices,val_indices,test_indices)
end
2 Comments
Steven Lord
on 30 May 2024
Can you run that which command and show us exactly what it returns? And can you show us exactly what the variable named data looks like? We probably don't need to see the values, just the sizes of its fields should be sufficient.
See Also
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!