LeNet实例实践

在线体验各类最新模型,更有模型 免费Token 额度领取!
立即体验
简介: 一个寒假一起入门深度学习

紧接之前Yann Lecun的LeNet的论文Gradient-Based Learning Applied to Document Recognition,我们这边来实践一下LeNet网络
这里我们使用Caffe框架进行LeNet的实践,用的数据集是mnist
首先先下载mnist数据集;
在Caffe目录下执行:

./data/mnist/get_mnist.sh
./exampels/mnist/create_mnist.sh

获取mnist数据集并将数据集转换为mnist_train_lmdb和mnist_test_lmdb格式。
在caffe/example/mnist目录下有一个lenet_train_test.prototxt

name: "LeNet"
layer {
  name: "mnist"//名字
  type: "Data"//类型为数据
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    scale: 0.00390625//归一化 1/256
  }
  data_param {
    source: "examples/mnist/mnist_train_lmdb"
    batch_size: 64//分批处理的图像个数
    backend: LMDB
  }
}
layer {
  name: "mnist"
  type: "Data"
  top: "data"//生成two blobs,分别为data blob 和label blob
  top: "label"
  include {
    phase: TEST
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "examples/mnist/mnist_test_lmdb"
    batch_size: 100
    backend: LMDB
  }
}
layer {
  name: "conv1"
  type: "Convolution"//卷积层
  bottom: "data"
  top: "conv1"
  param {
    lr_mult: 1//权重的学习率与solver运行的学习率一致
  }
  param {
    lr_mult: 2//偏置的学习率是solver运行的学习率的2倍
  }
  convolution_param {
    num_output: 20//20个featuremap
    kernel_size: 5//卷积核为5x5
    stride: 1//步长为1
    weight_filler {
      type: "xavier"//用 xavier算法初始化权重
    }
    bias_filler {
      type: "constant"//用常数初始化权重
    }
  }
}
layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 2//核大小2
    stride: 2//步长大小2
  }
}
layer {
  name: "conv2"
  type: "Convolution"
  bottom: "pool1"
  top: "conv2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 50
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool2"
  type: "Pooling"
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "ip1"
  type: "InnerProduct"
  bottom: "pool2"
  top: "ip1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 500
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "ip1"
  top: "ip1"
}
layer {
  name: "ip2"
  type: "InnerProduct"
  bottom: "ip1"
  top: "ip2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 10
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "ip2"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}

用Netscope可视化的结果:
2018_01_30_20_11_30_
(这边用relu代替了原来的sigmoid函数)
在/caffe/examples/mnist/lenet_solver.prototxt中有训练参数的配置:

# The train/test net protocol buffer definition//使bi用网络结构
net: "examples/mnist/lenet_train_test.prototxt"
# test_iter specifies how many forward passes the test should carry out.
# In the case of MNIST, we have test batch size 100 and 100 test iterations,
# covering the full 10,000 testing images.
test_iter: 100//测试时bitch_size100
# Carry out testing every 500 training iterations.//每500轮测试一次
test_interval: 500
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.01
momentum: 0.9
weight_decay: 0.0005
# The learning rate policy
lr_policy: "inv"
gamma: 0.0001
power: 0.75
# Display every 100 iterations每100次迭代次数显示一次训练时lr和loss
display: 100
# The maximum number of iterations最大迭代次数
max_iter: 10000
# snapshot intermediate results每5000次迭代输出模型
snapshot: 5000
snapshot_prefix: "examples/mnist/lenet"//模型保存路径
# solver mode: CPU or GPU
solver_mode: GPU

通过运行

./examples/mnist/train_lenet.sh 

train_lenet.sh:

#!/usr/bin/env sh
set -e

./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt $@

可以进行训练,下面是训练结果:
2018_01_30_20_44_29_
可以看到准确率可以达到99.03%,已经是相当高了,但网络对于像mnist效果好,但对于稍大一点的数据集效果就会下降地很明显。
可以看到caffe能快速地进行网络的搭建,但是相对于tensorflow来说不够灵活,而且caffe无法进行递归循环网络的搭建。
55cf39c9f60555467220312a80352b3a_hd

目录
相关文章
|
存储 算法 Linux
DNF和Yum
DNF和Yum
1221 3
|
存储 SQL 运维
大规模订单系统解读-架构篇
从最早的互联网高速发展、到移动互联网的爆发式增长,再到今天的产业互联网、物联网的快速崛起,各种各样新应用、新系统产生了众多订单类型的需求,比如购物订单、交流流水,外卖订单、支付账单、设备信息等。数据范围不仅越来越广,而且数据量越来越大,原有的经典架构方案已经很难满足当前新的业务场景。在新的需求下,对存储规模、开发效率、查询功能、未来扩展性等众多方面提出了更高的要求,要设计一款可靠稳定且扩展性好
9669 1
大规模订单系统解读-架构篇
|
JavaScript 前端开发 Android开发
在Eclipse中使用JSLint保证JavaScript代码质量
JSLint简介   JavaScript 作为一门语法灵活多变且对格式要求相对松散的语言,代码格式的混乱和某些语言特性的不正确使用,往往使得最终交付的产品中包含许多因编码风格约定造成的未预见的行为或错误,这种习惯性的问题如果不及时指出并修改,往往会在项目的迭代过程中不断的重现,严重影响 Web 产品的稳定性与安全性。
1105 0
|
3天前
|
人工智能 弹性计算 运维
|
1天前
|
缓存 人工智能 安全
GPT-5.6 Terra与GPT-5.5性能实测:成本减半后的跑分对比与快速迁移指南
GPT-5.6 Terra 的定价为每百万 token 输入 2.50/输出 15。GPT-5.5 则是 5/ 30。Terra 的每一项费率,包括 $0.25/M 的缓存读取,都恰好是 GPT-5.5 的一半,因此在任何工作负载组合下,Terra 都固定 便宜 2.0x。以每天 10 万次请求、3K token 提示词计算,大约是 Terra 每天 2,000,GPT−5.5每天 4,000,即每月约 60,000对 120,000。问题在于:OpenAI 没有发布任何针对 Terra 的编码基准。那个著名的 91.9% Terminal-Bench 数字是 Sol 在 Ul
|
25天前
|
Linux 程序员 数据格式
【2026最新】Notepad++下载、安装和使用一篇搞定(附中文版安装包)
Notepad++ 是一款免费开源、轻量高效的 Windows 文本编辑器,支持 C/Python/HTML 等 80+ 语言语法高亮、代码折叠、正则替换、编码转换及插件扩展,专为程序员与文本处理用户打造,完美替代系统记事本。(239字)
|
10天前
|
人工智能 缓存 安全
Claude Code 封号真实原因曝光,这次彻底不装了,直接针对国内开发者的账号下手?
Claude Code 封号潮背后:逆向扒出客户端隐写区域标记,Anthropic 政策收紧叠加 DeepSeek 7 月涨价,国产替代更紧迫。
|
19天前
|
存储 人工智能 监控
QoderWork完全指南:从入门到精通,把“AI实习生”变成你的全能工作搭档
阿里云2026年推出的桌面端AI工作助手QoderWork,不止聊天,更可动手干活:本地运行、安全可控,支持文件整理、数据分析、PPT生成、网页开发等;内置专家套件、多Agent协作与自定义Skills,让AI真正成为你身边的“AI实习生”。
|
15天前
|
人工智能 JSON 自然语言处理
让教学更智慧:用阿里云百炼工作流,自动生成中小学教材内容#小有可为#有温度的AI
通过可视化工作流编排,将大模型推理能力转化为标准化的教学内容生成引擎。教师只需输入教材标题和适用学段,即可自动获得结构完整、符合课程标准的章节内容,大幅降低备课门槛,助力教育资源均衡化。
503 127