How to Train 1d CNN on Custom dataset in matrix form in MATLAB

6 views (last 30 days)
Hi everyone, i hope you are doing well.
yanqi liu answer the question with 2D CNN, But i wanted to train 1D CNN
i have the following dataset myFile.txt includes 102x5,in which first 4 coloums are the Number of Observation and the last column are the Discrete labels/Classes for the dataset. I want to train 1D-CNN on this dataset
sz = size(dataset);
dataset = dataset(randperm(sz(1)),:);
traindata=dataset(:,1:4);
trainlabel=categorical(dataset(:,5));
classes = unique(trainlabel)
numClasses = numel(unique(trainlabel))
PD = 0.80 ;
Ptrain = []; Ttrain = [];
Ptest = []; Ttest = [];
for i = 1 : length(classes)
indi = find(trainlabel==classes(i));
indi = indi(randperm(length(indi)));
indj = round(length(indi)*PD);
Ptrain = [Ptrain; traindata(indi(1:indj),:)]; Ttrain = [Ttrain; trainlabel(indi(1:indj),:)];
Ptest = [Ptest; traindata(indi(1+indj:end),:)]; Ttest = [Ttest; trainlabel(indi(1+indj:end),:)];
end
Ptrain=(reshape(Ptrain', [4,1,1,size(Ptrain,1)]));
Ptest=(reshape(Ptest', [4,1,1,size(Ptest,1)]));
layers = [imageInputLayer([4 1 1])
convolution2dLayer([3 1],3,'Stride',1)
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2,'Padding',[0 0 0 1])
dropoutLayer
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
options = trainingOptions('adam', ...
'MaxEpochs',3000, ...
'Shuffle','every-epoch', ...
'Plots','training-progress', ...
'Verbose',false, ...
'ValidationData',{Ptest,Ttest},...
'ExecutionEnvironment', 'cpu', ...
'ValidationPatience',Inf);
net = trainNetwork(Ptrain,Ttrain,layers,options);
  3 Comments
Med Future
Med Future on 17 Feb 2022
Edited: Med Future on 17 Feb 2022
@yanqi liu where i can change in convolution2dLayer to set dimension 1

Sign in to comment.

Answers (1)

yanqi liu
yanqi liu on 17 Feb 2022
Edited: yanqi liu on 17 Feb 2022
yes,sir,if 2021b has convolution1dLayer,so we can make the cnn as follows,then we can try train it
layers = [sequenceInputLayer(4)
convolution1dLayer(3,32,Padding="causal")
reluLayer
globalMaxPooling1dLayer
dropoutLayer
fullyConnectedLayer(5)
softmaxLayer
classificationLayer];
layers
layers =
8×1 Layer array with layers: 1 '' Sequence Input Sequence input with 4 dimensions 2 '' Convolution 32 3 convolutions with stride 1 and padding 'causal' 3 '' ReLU ReLU 4 '' 1-D Global Max Pooling 1-D global max pooling 5 '' Dropout 50% dropout 6 '' Fully Connected 5 fully connected layer 7 '' Softmax softmax 8 '' Classification Output crossentropyex
  8 Comments
yanqi liu
yanqi liu on 18 Feb 2022
yes,sir,here on web,we can not see the plot curve,so we get the train status info and plot it
this picture is train acc curve by stats info structure

Sign in to comment.

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!