matlab深度学习:DAGNetwork入门之旅

本文详细介绍如何使用DAG网络创建深度学习模型,包括层定义、连接和训练过程。通过实例展示如何添加卷积层、连接层,并使用训练数据进行模型训练及验证集分类准确性评估。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、如何创建自己的网络?

DAG网络是用于深度学习的神经网络,其中的层为有向无环图。 DAG网络可以具有更复杂的体系结构,其中各层具有来自多层的输入和输出到多层的信息。 DAGNetwork对象具有单个输入层和单个输出层。

图层连接,指定为具有两列的表。每个表行代表层图中的一个连接。 第一列“源”指定每个连接的源。 第二列“目标”指定每个连接的目标。 连接源和目标可以是层名称,也可以是“ layerName / IOName”形式,其中“ IOName”是层输入或输出的名称。

下面是层和连接;

Object Functions

activationsCompute convolutional neural network layer activations
classifyClassify data using a trained deep learning neural network
predictPredict responses using a trained deep learning neural network
plotPlot neural network layer graph

layers = [
    imageInputLayer([28 28 1],'Name','input')
    
    convolution2dLayer(5,16,'Padding','same','Name','conv_1')
    batchNormalizationLayer('Name','BN_1')
    reluLayer('Name','relu_1')
    
    convolution2dLayer(3,32,'Padding','same','Stride',2,'Name','conv_2')
    batchNormalizationLayer('Name','BN_2')
    reluLayer('Name','relu_2')
    convolution2dLayer(3,32,'Padding','same','Name','conv_3')
    batchNormalizationLayer('Name','BN_3')
    reluLayer('Name','relu_3')
    
    additionLayer(2,'Name','add')
    
    averagePooling2dLayer(2,'Stride',2,'Name','avpool')
    fullyConnectedLayer(10,'Name','fc')
    softmaxLayer('Name','softmax')
    classificationLayer('Name','classOutput')];

lgraph = layerGraph(layers);
figure
plot(lgraph)

% 添加一层卷积层
skipConv = convolution2dLayer(1,32,'Stride',2,'Name','skipConv');
lgraph = addLayers(lgraph,skipConv);
figure
plot(lgraph)

% 加法层将'relu_3'和'skipConv'层的输出求和。
lgraph = connectLayers(lgraph,'relu_1','skipConv');
lgraph = connectLayers(lgraph,'skipConv','add/in2');
figure
plot(lgraph)

2、加载数据并训练网络

% 加载数据集;28×28的数字图像
[XTrain,YTrain] = digitTrain4DArrayData;
[XValidation,YValidation] = digitTest4DArrayData;

% 指定训练参数
options = trainingOptions('sgdm',...
    'MaxEpochs',10,...
    'Shuffle','every-epoch',...
    'ValidationData',{XValidation,YValidation},...
    'ValidationFrequency',30,...
    'Verbose',false,...
    'Plots','training-progress');

net = trainNetwork(XTrain,YTrain,lgraph,options);

 

% 训练集XTrain的激活
act5 = activations(net,XTrain,'fc','OutputAs','columns');
save('digitTrain_fc.mat','act5');

% 显示训练网络的参数

>>net

net = 
  DAGNetwork with properties:

         Layers: [16×1 nnet.cnn.layer.Layer]
    Connections: [16×2 table]

3、对验证图像进行分类并计算准确性

% 对验证集进行分类并计算准确度
YPredicted = classify(net,XValidation);
accuracy = mean(YPredicted == YValidation);
disp(accuracy);

输出结果准确率accuracy= 0.9976

 

 

 

 

 

 

 

 

 

 

 

 

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林语微光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值