当前位置:   article > 正文

Matlab:利用1D-CNN(一维卷积神经网络),分析高光谱曲线数据或时序数据_1d cnn卷积神经网络 大气

1d cnn卷积神经网络 大气

1DCNN 简介:

1D-CNN(一维卷积神经网络)是一种特殊类型的卷积神经网络,设计用于处理一维序列数据。这种网络结构通常由多个卷积层和池化层交替组成,最后使用全连接层将提取的特征映射到输出。

以下是1D-CNN的主要组成部分和特点:

  1. 输入层:接收一维序列数据作为模型的输入。
  2. 卷积层:使用一系列可训练的卷积核在输入数据上滑动并提取特征。卷积操作能够有效地提取局部信息,从而捕捉输入序列的局部模式。
  3. 激活函数:对卷积层的输出进行非线性变换,增强模型的表达能力。
  4. 池化层:通过对卷积层输出进行降维,减少计算量,同时提高模型的鲁棒性和泛化能力。
  5. 全连接层:将池化层的输出映射到模型的输出,通常用于分类、回归等任务。

在使用1D-CNN时,通常需要设置一些超参数,如卷积核的大小、卷积层的个数、池化操作的方式、激活函数的选择等。

与传统机器学习对比:

首先,1D-CNN是一种深度学习模型,它使用卷积层来自动提取一维序列数据(如音频、文本等)中的特征。这种方式与传统机器学习中的特征提取方法不同,传统机器学习通常需要手动设计和选择特征。通过自动提取特征,1D-CNN能够减少人工特征提取的工作量,并有可能发现更复杂的特征表示。其次,1D-CNN在处理序列数据时能够更好地捕捉局部关系。卷积操作通过在输入数据上滑动固定大小的窗口来提取局部特征,这使得1D-CNN在语音识别、自然语言处理、时间序列预测等任务中表现出色。而传统机器学习模型,如支持向量机(SVM)或决策树,通常不具备这种处理局部关系的能力。

需要注意的是,在数据尺度较小的时候,如只有100多个参数,相较于传统机器学习模型,1D-CNN并没有优势,表现性能一般和机器学习表现无明显差距。鉴于卷积对于目标特征的提取及压缩的特点,数据长度(参数)越高,1D-CNN就越发有优势。因此在时序回归、高光谱分析、股票预测、音频分析上1D-CNN的表现可圈可点。此外,利用1D-CNN做回归和分类对样本量有较高的要求,因为卷积结构本身对噪声就比较敏感,数据集较少时,特别容易发生严重的过拟合现象,建议样本量800+有比较好的应用效果。

三种不同结构的自定义的1D-CNN

基于VGG结构的1D-CNN(VNet)

基于 VGG 主干网 络设计的 VNet 参考了陈庆等的优化结构,卷积核大 小为 4,包含 6 个卷积深度,并在每个平均池化层后引 入一个比例为 0.3 的随机失活层(dropout layer)防止过拟合,参数量为342K。

matlab构建代码:

  1. function layers=creatCNN2D_VGG(inputsize)
  2. filter=16;
  3. layers = [
  4. imageInputLayer([inputsize],"Name","imageinput")
  5. convolution2dLayer([1 4],filter,"Name","conv","Padding","same")
  6. convolution2dLayer([1 4],filter,"Name","conv_1","Padding","same")
  7. maxPooling2dLayer([1 2],"Name","maxpool","Padding","same","Stride",[1 2])
  8. convolution2dLayer([1 4],filter*2,"Name","conv_2","Padding","same")
  9. convolution2dLayer([1 4],filter*2,"Name","conv_3","Padding","same")
  10. maxPooling2dLayer([1 2],"Name","maxpool_1","Padding","same","Stride",[1 2])
  11. fullyConnectedLayer(filter*8,"Name","fc")
  12. fullyConnectedLayer(1,"Name","fc_1")
  13. regressionLayer("Name","regressionoutput")];

基于EfficienNet结构的1D-CNN (ENet)

ENet 采用 Swish 激活函数,引入了跳跃连接与 SE(squeeze and excitation)注意力机制,其不仅能有效 实现更深的卷积深度,还能对通道方向上的数据特征进 行感知,在数据尺度较大时,有一定优势。参数量170.4K

生成代码:

  1. function lgraph=creatCNN2D_EffiPlus2(inputsize)
  2. filter=8;
  3. lgraph = layerGraph();
  4. tempLayers = [
  5. imageInputLayer([1 1293 1],"Name","imageinput")
  6. convolution2dLayer([1 3],filter,"Name","conv_11","Padding","same","Stride",[1 2])%'DilationFactor',[1,2]
  7. batchNormalizationLayer("Name","batchnorm_8")
  8. swishLayer("Name","swish_1_1_1")];
  9. lgraph = addLayers(lgraph,tempLayers);
  10. tempLayers = [
  11. convolution2dLayer([1 3],filter,"Name","conv_1_1","Padding","same","Stride",[1 1])%%
  12. batchNormalizationLayer("Name","batchnorm_1_1")
  13. swishLayer("Name","swish_1_5")];
  14. lgraph = addLayers(lgraph,tempLayers);
  15. tempLayers = [
  16. globalAveragePooling2dLayer("Name","gapool_1_1")
  17. convolution2dLayer([1 1],2,"Name","conv_2_1_1","Padding","same")
  18. swishLayer("Name","swish_2_1_1")
  19. convolution2dLayer([1 1],filter,"Name","conv_3_1_1","Padding","same")
  20. sigmoidLayer("Name","sigmoid_1_1")];
  21. lgraph = addLayers(lgraph,tempLayers);
  22. tempLayers = [
  23. multiplicationLayer(2,"Name","multiplication_3")
  24. convolution2dLayer([1 3],filter*2,"Name","conv","Padding","same","Stride",[1 2])
  25. batchNormalizationLayer("Name","batchnorm")
  26. swishLayer("Name","swish_1_1")];
  27. lgraph = addLayers(lgraph,tempLayers);
  28. tempLayers = [
  29. convolution2dLayer([1 3],filter*2,"Name","conv_1","Padding","same","Stride",[1 1])%%
  30. batchNormalizationLayer("Name","batchnorm_1")
  31. swishLayer("Name","swish_1")];
  32. lgraph = addLayers(lgraph,tempLayers);
  33. tempLayers = [
  34. globalAveragePooling2dLayer("Name","gapool_1")
  35. convolution2dLayer([1 1],4,"Name","conv_2_1","Padding","same")
  36. swishLayer("Name","swish_2_1")
  37. convolution2dLayer([1 1],filter*2,"Name","conv_3_1","Padding","same")
  38. sigmoidLayer("Name","sigmoid_1")];
  39. lgraph = addLayers(lgraph,tempLayers);
  40. tempLayers = [
  41. multiplicationLayer(2,"Name","multiplication")
  42. convolution2dLayer([1 3],filter*4,"Name","conv_9","Padding","same","Stride",[1 2])
  43. batchNormalizationLayer("Name","batchnorm_6")
  44. swishLayer("Name","swish_1_4")];
  45. lgraph = addLayers(lgraph,tempLayers);
  46. tempLayers = [
  47. convolution2dLayer([1 3],filter*4,"Name","conv_10","Padding","same","Stride",[1 1])%%
  48. batchNormalizationLayer("Name","batchnorm_7")
  49. swishLayer("Name","swish_1_3")];
  50. lgraph = addLayers(lgraph,tempLayers);
  51. tempLayers = [
  52. globalAveragePooling2dLayer("Name","gapool_2")
  53. convolution2dLayer([1 1],8,"Name","conv_2_2","Padding","same")
  54. swishLayer("Name","swish_2_2")
  55. convolution2dLayer([1 1],filter*4,"Name","conv_3_2","Padding","same")
  56. sigmoidLayer("Name","sigmoid_2")];
  57. lgraph = addLayers(lgraph,tempLayers);
  58. tempLayers = [
  59. multiplicationLayer(2,"Name","multiplication_2")
  60. convolution2dLayer([1 3],filter*8,"Name","conv_5","Padding","same","Stride",[1 2])
  61. batchNormalizationLayer("Name","batchnorm_2")];
  62. lgraph = addLayers(lgraph,tempLayers);
  63. tempLayers = [
  64. convolution2dLayer([1 1],filter*8,"Name","conv_6","Padding","same")
  65. batchNormalizationLayer("Name","batchnorm_3")
  66. swishLayer("Name","swish")
  67. convolution2dLayer([1 3],filter*8,"Name","conv_7","Padding","same")
  68. batchNormalizationLayer("Name","batchnorm_4")
  69. swishLayer("Name","swish_1_2")];
  70. lgraph = addLayers(lgraph,tempLayers);
  71. tempLayers = [
  72. globalAveragePooling2dLayer("Name","gapool")
  73. convolution2dLayer([1 1],12,"Name","conv_2","Padding","same")
  74. swishLayer("Name","swish_2")
  75. convolution2dLayer([1 1],filter*8,"Name","conv_3","Padding","same")
  76. sigmoidLayer("Name","sigmoid")];
  77. lgraph = addLayers(lgraph,tempLayers);
  78. tempLayers = [
  79. multiplicationLayer(2,"Name","multiplication_1")
  80. convolution2dLayer([1 3],filter*8,"Name","conv_8","Padding","same")
  81. batchNormalizationLayer("Name","batchnorm_5")];
  82. lgraph = addLayers(lgraph,tempLayers);
  83. tempLayers = [
  84. additionLayer(2,"Name","addition")
  85. convolution2dLayer([1 3],1,"Name","conv_4","Padding","same")
  86. swishLayer("Name","swish_3")
  87. averagePooling2dLayer([1 3],"Name","avgpool2d","Padding","same")
  88. fullyConnectedLayer(1,"Name","fc")
  89. regressionLayer("Name","regressionoutput")];
  90. lgraph = addLayers(lgraph,tempLayers);
  91. lgraph = connectLayers(lgraph,"swish_1_1_1","conv_1_1");
  92. lgraph = connectLayers(lgraph,"swish_1_1_1","gapool_1_1");
  93. lgraph = connectLayers(lgraph,"swish_1_5","multiplication_3/in1");
  94. lgraph = connectLayers(lgraph,"sigmoid_1_1","multiplication_3/in2");
  95. lgraph = connectLayers(lgraph,"swish_1_1","conv_1");
  96. lgraph = connectLayers(lgraph,"swish_1_1","gapool_1");
  97. lgraph = connectLayers(lgraph,"swish_1","multiplication/in1");
  98. lgraph = connectLayers(lgraph,"sigmoid_1","multiplication/in2");
  99. lgraph = connectLayers(lgraph,"swish_1_4","conv_10");
  100. lgraph = connectLayers(lgraph,"swish_1_4","gapool_2");
  101. lgraph = connectLayers(lgraph,"swish_1_3","multiplication_2/in1");
  102. lgraph = connectLayers(lgraph,"sigmoid_2","multiplication_2/in2");
  103. lgraph = connectLayers(lgraph,"batchnorm_2","conv_6");
  104. lgraph = connectLayers(lgraph,"batchnorm_2","addition/in2");
  105. lgraph = connectLayers(lgraph,"swish_1_2","gapool");
  106. lgraph = connectLayers(lgraph,"swish_1_2","multiplication_1/in1");
  107. lgraph = connectLayers(lgraph,"sigmoid","multiplication_1/in2");
  108. lgraph = connectLayers(lgraph,"batchnorm_5","addition/in1");

基于ResNet结构的1D-CNN (RNet)

RNet 由 3 层残差网络模块构成,其结构相较 于 ENet 较为精简,模型容量更少,个人感觉性能比较综合。参数量33.7K

  1. function lgraph=creatCNN2D_ResNet(inputsize)
  2. lgraph = layerGraph();
  3. filter=16;
  4. tempLayers = [
  5. imageInputLayer([inputsize],"Name","imageinput")
  6. convolution2dLayer([1 3],filter,"Name","conv","Padding","same","Stride",[1 2])
  7. batchNormalizationLayer("Name","batchnorm")
  8. reluLayer("Name","relu")
  9. maxPooling2dLayer([1 3],"Name","maxpool","Padding",'same',"Stride",[1 2])];
  10. lgraph = addLayers(lgraph,tempLayers);
  11. tempLayers = [
  12. convolution2dLayer([1 3],filter,"Name","conv_1","Padding","same")
  13. batchNormalizationLayer("Name","batchnorm_1")
  14. reluLayer("Name","relu_1")
  15. convolution2dLayer([1 3],filter,"Name","conv_2","Padding","same")
  16. batchNormalizationLayer("Name","batchnorm_2")];
  17. lgraph = addLayers(lgraph,tempLayers);
  18. tempLayers = [
  19. additionLayer(2,"Name","addition")
  20. reluLayer("Name","relu_3")];
  21. lgraph = addLayers(lgraph,tempLayers);
  22. tempLayers = [
  23. convolution2dLayer([1 3],filter*2,"Name","conv_3","Padding","same","Stride",[1 2])
  24. batchNormalizationLayer("Name","batchnorm_3")
  25. reluLayer("Name","relu_2")
  26. convolution2dLayer([1 3],filter*2,"Name","conv_4","Padding","same")
  27. batchNormalizationLayer("Name","batchnorm_4")];
  28. lgraph = addLayers(lgraph,tempLayers);
  29. tempLayers = [
  30. convolution2dLayer([1 3],filter*2,"Name","conv_8","Padding","same","Stride",[1 2])
  31. batchNormalizationLayer("Name","batchnorm_8")];
  32. lgraph = addLayers(lgraph,tempLayers);
  33. tempLayers = [
  34. additionLayer(2,"Name","addition_1")
  35. reluLayer("Name","relu_5")];
  36. lgraph = addLayers(lgraph,tempLayers);
  37. tempLayers = [
  38. convolution2dLayer([1 3],filter*4,"Name","conv_5","Padding","same","Stride",[1 2])
  39. batchNormalizationLayer("Name","batchnorm_5")
  40. reluLayer("Name","relu_4")
  41. convolution2dLayer([1 3],filter*4,"Name","conv_6","Padding","same")
  42. batchNormalizationLayer("Name","batchnorm_6")];
  43. lgraph = addLayers(lgraph,tempLayers);
  44. tempLayers = [
  45. convolution2dLayer([1 3],filter*4,"Name","conv_7","Padding","same","Stride",[1 2])
  46. batchNormalizationLayer("Name","batchnorm_7")];
  47. lgraph = addLayers(lgraph,tempLayers);
  48. tempLayers = [
  49. additionLayer(2,"Name","addition_2")
  50. reluLayer("Name","res3a_relu")
  51. globalMaxPooling2dLayer("Name","gmpool")
  52. fullyConnectedLayer(1,"Name","fc")
  53. regressionLayer("Name","regressionoutput")];
  54. lgraph = addLayers(lgraph,tempLayers);
  55. lgraph = connectLayers(lgraph,"maxpool","conv_1");
  56. lgraph = connectLayers(lgraph,"maxpool","addition/in2");
  57. lgraph = connectLayers(lgraph,"batchnorm_2","addition/in1");
  58. lgraph = connectLayers(lgraph,"relu_3","conv_3");
  59. lgraph = connectLayers(lgraph,"relu_3","conv_8");
  60. lgraph = connectLayers(lgraph,"batchnorm_4","addition_1/in1");
  61. lgraph = connectLayers(lgraph,"batchnorm_8","addition_1/in2");
  62. lgraph = connectLayers(lgraph,"relu_5","conv_5");
  63. lgraph = connectLayers(lgraph,"relu_5","conv_7");
  64. lgraph = connectLayers(lgraph,"batchnorm_6","addition_2/in1");
  65. lgraph = connectLayers(lgraph,"batchnorm_7","addition_2/in2");

ENet和RNet的结构示意图

训练代码与案例:

训练代码

我们基于RNet采用1293长度的数据对样本进行训练,做回归任务,代码如下:

  1. clear all
  2. load("TestData2.mat");
  3. %数据分割
  4. %[AT,AP]=ks(Alltrain,588);
  5. num_div=1;
  6. %直接载入数据
  7. [numsample,sampleSize]=size(AT);
  8. for i=1:numsample
  9. XTrain(:,:,1,i)=AT(i,1:end-num_div);
  10. YTrain(i,1)=AT(i,end);
  11. end
  12. [numtest,~]=size(AP)
  13. for i=1:numtest
  14. XTest(:,:,1,i)=AP(i,1:end-num_div);
  15. YTest(i,1)=AP(i,end);
  16. end
  17. %Ytrain=inputData(:,end);
  18. figure
  19. histogram(YTrain)
  20. axis tight
  21. ylabel('Counts')
  22. xlabel('TDS')
  23. options = trainingOptions('adam', ...
  24. 'MaxEpochs',150, ...
  25. 'MiniBatchSize',64, ...
  26. 'InitialLearnRate',0.008, ...
  27. 'GradientThreshold',1, ...
  28. 'Verbose',false,...
  29. 'Plots','training-progress',...
  30. 'ValidationData',{XTest,YTest});
  31. layerN=creatCNN2D_ResNet([1,1293,1]);%创建网络,根据自己的需求改函数名称
  32. [Net, traininfo] = trainNetwork(XTrain,YTrain,layerN,options);
  33. YPredicted = predict(Net,XTest);
  34. predictionError = YTest- YPredicted;
  35. squares = predictionError.^2;
  36. rmse = sqrt(mean(squares))
  37. [R P] = corrcoef(YTest,YPredicted)
  38. scatter(YPredicted,YTest,'+')
  39. xlabel("Predicted Value")
  40. ylabel("True Value")
  41. R2=R(1,2)^2;
  42. hold on
  43. plot([0 2000], [-0 2000],'r--')

训练数据输入如下:最后一列为预测值: 

训练过程如下: 

训练数据分享    

    源数据分享:TestData2.mat

链接:https://pan.baidu.com/s/1B1o2xB4aUFOFLzZbwT-7aw?pwd=1xe5 
提取码:1xe5 
 

训练建议    

    以个人经验来说,VNet结构最为简单,但是综合表现最差。对于800-3000长度的数据,容量较小的RNet的表现会比ENet好,对于长度超过的3000的一维数据,ENet的表现更好。

    关于超参数的设计:首先最小批次minibatch设置小于64会好一点,确保最终结果会比较好,反正一维卷积神经网络训练很快。第二,与图片不同,一维数据常常数值精度比较高(图片一般就uint8或16格式),因此学习率不宜太高,要不表现会有所下降。我自己尝试的比较好的学习率是0.008.总体来说0.015-0.0005之间都OK,0.05以上结果就开始下降了。

其他引用函数

KS数据划分

Kennard-Stone(KS)方法是一种常用于数据集划分的方法,尤其适用于化学计量学等领域。其主要原理是保证训练集中的样本按照空间距离分布均匀。

  1. function [XSelected,XRest,vSelectedRowIndex]=ks(X,Num) %Num=三分之二的数值
  2. % ks selects the samples XSelected which uniformly distributed in the exprimental data X's space
  3. % Input
  4. % X:the matrix of the sample spectra
  5. % Num:the number of the sample spectra you want select
  6. % Output
  7. % XSelected:the sample spectras was sel ected from the X
  8. % XRest:the sample spectras remain int the X after select
  9. % vSelectedRowIndex:the row index of the selected sample in the X matrix
  10. % Programmer: zhimin zhang @ central south university on oct 28 ,2007
  11. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  12. % start of the kennard-stone step one
  13. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  14. [nRow,nCol]=size(X); % obtain the size of the X matrix
  15. mDistance=zeros(nRow,nRow); %dim a matrix for the distance storage
  16. vAllofSample=1:nRow;
  17. for i=1:nRow-1
  18. vRowX=X(i,:); % obtain a row of X
  19. for j=i+1:nRow
  20. vRowX1=X(j,:); % obtain another row of X
  21. mDistance(i,j)=norm(vRowX-vRowX1); % calc the Euclidian distance
  22. end
  23. end
  24. [vMax,vIndexOfmDistance]=max(mDistance);
  25. [nMax,nIndexofvMax]=max(vMax);
  26. %vIndexOfmDistance(1,nIndexofvMax)
  27. %nIndexofvMax
  28. vSelectedSample(1)=nIndexofvMax;
  29. vSelectedSample(2)=vIndexOfmDistance(nIndexofvMax);
  30. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  31. % end of the kennard-stone step one
  32. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  33. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  34. % start of the kennard-stone step two
  35. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  36. for i=3:Num
  37. vNotSelectedSample=setdiff(vAllofSample,vSelectedSample);
  38. vMinDistance=zeros(1,nRow-i + 1);
  39. for j=1:(nRow-i+1)
  40. nIndexofNotSelected=vNotSelectedSample(j);
  41. vDistanceNew = zeros(1,i-1);
  42. for k=1:(i-1)
  43. nIndexofSelected=vSelectedSample(k);
  44. if(nIndexofSelected<=nIndexofNotSelected)
  45. vDistanceNew(k)=mDistance(nIndexofSelected,nIndexofNotSelected);
  46. else
  47. vDistanceNew(k)=mDistance(nIndexofNotSelected,nIndexofSelected);
  48. end
  49. end
  50. vMinDistance(j)=min(vDistanceNew);
  51. end
  52. [nUseless,nIndexofvMinDistance]=max(vMinDistance);
  53. vSelectedSample(i)=vNotSelectedSample(nIndexofvMinDistance);
  54. end
  55. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  56. %%%%% end of the kennard-stone step two
  57. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  58. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  59. %%%%% start of export the result
  60. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  61. vSelectedRowIndex=vSelectedSample;
  62. for i=1:length(vSelectedSample)
  63. XSelected(i,:)=X(vSelectedSample(i),:);
  64. end
  65. vNotSelectedSample=setdiff(vAllofSample,vSelectedSample);
  66. for i=1:length(vNotSelectedSample)
  67. XRest(i,:)=X(vNotSelectedSample(i),:);
  68. end
  69. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  70. %%%%% end of export the result
  71. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

参考文献

卷积神经网络的紫外-可见光谱水质分类方法 (wanfangdata.com.cn)

光谱技术结合水分校正与样本增广的棉田土壤盐分精准反演 (tcsae.org)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/364070
推荐阅读
相关标签
  

闽ICP备14008679号