Hi. Im trying to optimize hyperparameters in my LSTM. Im am using Bayesopt. However bayesopt did not print any result for the optimize hyperparameters (I've even put Verbose = 1 . Here is my code
% SPECIFY TRAIN DATA FILE PATH
display = 'specify file name? ';
file = input(display);
filePath = fullfile('data',file);
display = sprintf('Loading File Path for %s',file);
disp(display)
clearvars display
% READ TRAIN DATA FILE
dataTrain = readtable(filePath);
display = sprintf('%s read',file);
disp(display)
clearvars display
% DIVIDE DATA INTO TRAIN,VAL,TEST
rows = height(dataTrain)
idx = randperm(rows);
trainIDX = rows*0.8*0.8;
valIDX = rows*0.8*0.2;
testIDX = rows*0.2;
idx1 = (idx<=trainIDX);
idx2 = (idx>trainIDX & idx<=(trainIDX+valIDX));
idx3 = (idx>(trainIDX+valIDX));
trainData = dataTrain(idx1,:);
valData = dataTrain(idx2,:);
testData = dataTrain(idx3,:);
clearvars rows idx trainIDX valIDX testIDX idx1 idx2 idx3
% CALCULATE NO OF TRAIN FLEET
totalFleet = max(trainData{:,1});
display = sprintf('Total No. of train fleet is %d',totalFleet);
disp(display)
clearvars display
% TRAIN DATA NORMALIZATION
minTrain = min(trainData{:,3});
maxTrain = max(trainData{:,3});
subTable1 = normalize(trainData(:,4:12),'range');
subTable2 = trainData(:,1:3);
clearvars trainData
trainData = [subTable2,subTable1];
clearvars subTable1 subTable2
% DIVIDE TRAINING DATA
Xtrain = cell(totalFleet,1);
Ytrain = cell(totalFleet,1);
for i = 1:totalFleet;
M = rows2vars(trainData(trainData.Fleet==i,[4:12]));
M = M(:,2:end);
Xtrain{i,:} = M;
Xtrain{i,:} = table2array(Xtrain{i,:});
clearvars M
M = rows2vars(trainData(trainData.Fleet==i,[3]));
M = M(:,2:end);
Ytrain{i,:} = M;
Ytrain{i,:} = table2array(Ytrain{i,:});
clearvars M totalFleet
end
disp('Training Data Divided into Xtrain and Ytrain')
clearvars i display
% CALCULATE NO OF VAL FLEET
totalFleet = max(valData{:,1});
display = sprintf('Total No. of val fleet is %d',totalFleet);
disp(display)
clearvars display
% VAL DATA NORMALIZATION
subTable1 = normalize(valData(:,4:12),'range');
subTable2 = valData(:,1:3);
clearvars valData
valData = [subTable2,subTable1];
clearvars subTable1 subTable2
% DIVIDE VAL DATA
Xval = cell(totalFleet,1);
Yval = cell(totalFleet,1);
for i = 1:totalFleet;
M = rows2vars(valData(valData.Fleet==i,[4:12]));
M = M(:,2:end);
Xval{i,:} = M;
Xval{i,:} = table2array(Xval{i,:});
clearvars M
M = rows2vars(valData(valData.Fleet==i,[3]));
M = M(:,2:end);
Yval{i,:} = M;
Yval{i,:} = table2array(Yval{i,:});
clearvars M totalFleet
end
disp('Training Data Divided into Xval and Yval')
clearvars i display
% CALCULATE NO OF TEST FLEET
totalTestFleet = max(testData{:,1});
display = sprintf('Total No. of test fleet is %d',totalTestFleet);
disp(display)
clearvars display
% DATA TEST NORMALIZATION
minTest = min(testData{:,3});
maxTest = max(testData{:,3});
subTable1 = normalize(testData(:,4:11),'range');
subTable2 = testData(:,1:3);
clearvars testData
testData = [subTable2,subTable1];
clearvars subTable1 subTable2
% DIVIDE TEST DATA
Xtest = cell(totalTestFleet,1);
Ytest = cell(totalTestFleet,1);
for i = 1:totalTestFleet;
M = rows2vars(testData(testData.Fleet==i,[3:11]));
M = M(:,2:end);
Xtest{i,:} = M;
Xtest{i,:} = table2array(Xtest{i,:});
clearvars M
M = rows2vars(testData(testData.Fleet==i,[3]));
M = M(:,2:end);
Ytest{i,:} = M;
Ytest{i,:} = table2array(Ytest{i,:});
clearvars M
end
disp('Training Data Divided into Xtest and Ytest')
clearvars i display
% BAYESIAN OPTIMISATION
optimVars = [
optimizableVariable('MaxEpochs',[60 1000],'Type','integer')
optimizableVariable('InitialLearnRate',[1e-3 1],'Transform','log')
optimizableVariable('MiniBatchSize',[26 260], 'Type', 'integer')
optimizableVariable('Size',[10 500], 'Type', 'integer')
optimizableVariable('numHiddenUnits',[10 1000],'Type','integer')];
ObjFcn = makeObjFcn(Xtrain,Ytrain,Xval,Yval);
BayesObject = BayesianOptimization(ObjFcn,optimVars, ...
'MaxTime',50400, ...
'IsObjectiveDeterministic',false, ...
'Verbose',1, ...
'UseParallel',false);
bestIdx = BayesObject.IndexOfMinimumTrace(end);
fileName = BayesObject.UserDataTrace{bestIdx};
savedStruct = load(fileName);
valError = savedStruct.valError
% SCRIPT ObjFcn
function ObjFcn = makeObjFcn(Xtrain,Ytrain,Xval,Yval) %#ok<*INUSD>
ObjFcn = @valErrorFun;
function [valError,cons,fileName] = valErrorFun(optimVars) %#ok<STOUT>
layers = [ ...
sequenceInputLayer(9)
lstmLayer(optimVars.numHiddenUnits,'OutputMode','sequence')
fullyConnectedLayer(optimVars.Size)
dropoutLayer(0.5)
fullyConnectedLayer(1)
regressionLayer];
options = trainingOptions('adam', ...
'MaxEpochs',optimVars.MaxEpochs, ...
'ValidationData',{Xval,Yval},...
'ValidationFrequency',10,...
'MiniBatchSize',optimVars.MiniBatchSize, ...
'InitialLearnRate',optimVars.InitialLearnRate, ...
'GradientThreshold',1, ...
'Shuffle','never', ...
'Plots','training-progress',...
'SequenceLength','longest',...
'Verbose',false);
net = trainNetwork(Xtrain,Ytrain,layers,options);
YPredicted = predict(net,Xval);
valError = 1 - mean(YPredicted == Yval);
fileName = num2str(valError) + ".mat";
save(fileName,'net','valError','options')
cons = [];
end
end