TensorFlow练习1: 编程实现一个简单的CNN, Convolutional Neural Networks

Convolutional Neural Networks翻译为卷积神经网络,常用在图像识别和语音分析等领域。CNN详细介绍参看:

使用TensorFlow创建CNN

执行结果:


下面使用tflearn重写上面代码,tflearn是TensorFlow的高级封装,类似Keras

tflearn提供了更简单、直观的接口。和scikit-learn差不多,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import tflearn
from tflearn . layers . conv import conv_2d , max_pool_2d
from tflearn . layers . core import input_data , dropout , fully_connected
from tflearn . layers . estimator import regression
 
train_x , train_y , test_x , test_y = tflearn . datasets . mnist . load_data ( one_hot = True )
 
train_x = train_x . reshape ( - 1 , 28 , 28 , 1 )
test_x = test_x . reshape ( - 1 , 28 , 28 , 1 )
 
# 定义神经网络模型
conv_net = input_data ( shape = [ None , 28 , 28 , 1 ] , name = 'input' )
conv_net = conv_2d ( conv_net , 32 , 2 , activation = 'relu' )
conv_net = max_pool_2d ( conv _net , 2 )
conv_net = conv_2d ( conv_net , 64 , 2 , activation = 'relu' )
conv_net = max_pool_2d ( conv _net , 2 )
conv_net = fully_connected ( conv_net , 1024 , activation = 'relu' )
conv_net = dropout ( conv_net , 0.8 )
conv_net = fully_connected ( conv_net , 10 , activation = 'softmax' )
conv_net = regression ( conv_net , optimizer = 'adam' , loss = 'categorical_crossentropy' , name = 'output' )
 
model = tflearn . DNN ( conv_net )
 
# 训练
model . fit ( { 'input' : train_x } , { 'output' : train_y } , n_epoch = 13 ,
           validation_set = ( { 'input' : test_x } , { 'output' : test_y } ) ,
           snapshot_step = 300 , show_metric = True , run_id = 'mnist' )
 
model . save ( 'mnist.model' )    # 保存模型
 
"""
model.load('mnist.model')   # 加载模型
model.predict([test_x[1]])  # 预测
"""




TensorFlow练习4: CNN, Convolutional Neural Networks

TensorFlow练习4: CNN, Convolutional Neural Networks

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值