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

6 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

回答(1 个)

yanqi liu
yanqi liu 2022-2-17
编辑:yanqi liu 2022-2-17
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 个评论
yanqi liu
yanqi liu 2022-2-18
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by