基于tensorflow和mnist的LeNet-5模型实现

代码如下


# LeNet-5

import tensorflow as tf
import input_data

def Weight(shape):
    init = tf.truncated_normal(shape, stddev = 0.1, dtype = tf.float32)
    return tf.Variable(init)

def Bias(shape):
    init = tf.constant(0.1, shape = shape, dtype = tf.float32)
    return tf.Variable(init)

def conv2d(x, W, padding):
    return tf.nn.conv2d(x, W, strides = [1, 1, 1, 1], padding = padding)

def pooling(x):
    return tf.nn.max_pool(x, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1],
                          padding = 'SAME')

# read data
mnist = input_data.read_data_sets("MNIST_data/", one_hot = True)
sess = tf.InteractiveSession()

# the network
with tf.name_scope('input'):
    x = tf.placeholder(tf.float32, [None, 784])
    x_mat = tf.reshape(x, [-1, 28, 28, 1])

with tf.name_scope('conv1'):
    W = Weight([5, 5, 1, 6])
    b = Bias([6])
    conv1 = tf.nn.relu(conv2d(x_mat, W, 'SAME') + b)

with tf.name_scope('pool1'):
    pool1 = pooling(conv1)

with tf.name_scope('conv2'):
    W = Weight([5, 5, 6, 16])
    b = Bias([16])
    conv2 = tf.nn.relu(conv2d(pool1, W, 'VALID') + b)

with tf.name_scope('pool2'):
    pool2 = pooling(conv2)

with tf.name_scope('fc1'):
    pool2_flat = tf.reshape(pool2, [-1, 5 * 5 * 16])
    W = Weight([5 * 5 * 16, 120])
    b = Bias([120])
    fc1 = tf.nn.relu(tf.matmul(pool2_flat, W) + b)

with tf.name_scope('fc2'):
    W = Weight([120, 84])
    b = Bias([84])
    fc2 = tf.nn.relu(tf.matmul(fc1, W) + b)

with tf.name_scope('softmax'):
    W = Weight([84, 10])
    b = Bias([10])
    y = tf.nn.softmax(tf.matmul(fc2, W) + b)

ans = tf.placeholder(tf.float32, [None, 10])
loss = -tf.reduce_sum(ans * tf.log(y))
equal = tf.equal(tf.argmax(y, 1), tf.argmax(ans, 1))
accuracy = tf.reduce_mean(tf.cast(equal, tf.float32))

train = tf.train.GradientDescentOptimizer(1e-4).minimize(loss)

sess.run(tf.global_variables_initializer())

for i in range(80000):
    batch = mnist.train.next_batch(50)
    if i % 100 == 0:
        print(('At step %d, accuracy is ' % i) ,)
        print(accuracy.eval(feed_dict = {x: batch[0], ans: batch[1]}))
    train.run(feed_dict = {x: batch[0], ans: batch[1]})


print('Accuracy is ',)
print(accuracy.eval(feed_dict = {x: mnist.test.images, ans: mnist.test.labels}))

运行效果大概如下:

step = 2000, accuracy = 0.9285
step = 10000, accuracy = 0.9781
step = 40000, accuracy = 0.9865
step = 80000, accuracy = 0.9867

效果并不算很好,但是跑起来还是很快的,也是一大乐事。所以说计算消耗和准确率之间有一个trade-off,我想好的网络结构就是尽可能做到两全其美。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值