MNIST的AlexNet实现

来源:互联网 发布:编程怎么写主程序 编辑:程序博客网 时间:2024/05/22 09:47

【参考资料】

1.Tensorflow实战1:利用AlexNet训练MNIST(http://blog.csdn.net/felaim/article/details/65630312)

2.alexnet.py(https://github.com/tensorflow/models/tree/master/slim/nets)

3.《Tensorflow技术解析与实战》李佳璇著(121页——125页)


一开始照着书上的代码敲上去并不能运行,提示:

Caused by op 'SoftmaxCrossEntropyWithLogits', defined at:
  File ".\文档\PycharmProjects\tensorflow\mnist_alexnet.py", line 120, in <module>
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = pred, labels = y))
  File "D:\Python\Python36\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 1594, in softmax_cross_entropy_with_
logits
    precise_logits, labels, name=name)
  File "D:\Python\Python36\lib\site-packages\tensorflow\python\ops\gen_nn_ops.py", line 2380, in _softmax_cross_entropy_
with_logits
    features=features, labels=labels, name=name)
  File "D:\Python\Python36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "D:\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 2506, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "D:\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1269, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): logits and labels must be same size: logits_size=[16,10] labels_size=[64
,10]
         [[Node: SoftmaxCrossEntropyWithLogits = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/repli
ca:0/task:0/gpu:0"](Reshape_2, Reshape_3)]]


后来按照参考资料中第一个博客的可以运行,但是发现给的图是5个卷积层,3个池化层,3个全连接层。上图:


然后按照GitHub上的代码对我写的代码进行了修改。GitHub上的代码片段对于卷积和池化的部分:

# Collect outputs for conv2d, fully_connected and max_pool2d.    with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d],                        outputs_collections=[end_points_collection]):      net = slim.conv2d(inputs, 64, [11, 11], 4, padding='VALID',                        scope='conv1')      net = slim.max_pool2d(net, [3, 3], 2, scope='pool1')      net = slim.conv2d(net, 192, [5, 5], scope='conv2')      net = slim.max_pool2d(net, [3, 3], 2, scope='pool2')      net = slim.conv2d(net, 384, [3, 3], scope='conv3')      net = slim.conv2d(net, 384, [3, 3], scope='conv4')      net = slim.conv2d(net, 256, [3, 3], scope='conv5')      net = slim.max_pool2d(net, [3, 3], 2, scope='pool5')

形成了我后面写好的代码:

#coding=utf-8from __future__ import print_functionfrom tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("/tmp/data", one_hot=True)import tensorflow as tf# 定义网络超参数learning_rate = 0.001training_iters = 200000batch_size = 64display_step = 20# 定义网络参数n_input = 784 # 输入的维度n_classes = 10 # 标签的维度dropout = 0.75 # Dropout 的概率# 占位符输入x = tf.placeholder(tf.float32, [None, n_input])y = tf.placeholder(tf.float32, [None, n_classes])keep_prob = tf.placeholder(tf.float32)# 卷积操作def conv2d(name, l_input, w, b):    return tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(l_input, w, strides=[1, 1, 1, 1], padding='SAME'),b), name=name)# 最大下采样操作def max_pool(name, l_input, k):    return tf.nn.max_pool(l_input, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME', name=name)# 归一化操作def norm(name, l_input, lsize=4):    return tf.nn.lrn(l_input, lsize, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name=name)# 存储所有的网络参数weights = {    'wc1': tf.Variable(tf.random_normal([11, 11, 1, 64])),    'wc2': tf.Variable(tf.random_normal([5, 5, 64, 192])),    'wc3': tf.Variable(tf.random_normal([3, 3, 192, 384])),    'wc4': tf.Variable(tf.random_normal([3, 3, 384, 384])),    'wc5': tf.Variable(tf.random_normal([3, 3, 384, 256])),    'wd1': tf.Variable(tf.random_normal([4*4*256, 4096])),    'wd2': tf.Variable(tf.random_normal([4096, 4096])),    'out': tf.Variable(tf.random_normal([4096, 10]))}biases = {    'bc1': tf.Variable(tf.random_normal([64])),    'bc2': tf.Variable(tf.random_normal([192])),    'bc3': tf.Variable(tf.random_normal([384])),    'bc4': tf.Variable(tf.random_normal([384])),    'bc5': tf.Variable(tf.random_normal([256])),    'bd1': tf.Variable(tf.random_normal([4096])),    'bd2': tf.Variable(tf.random_normal([4096])),    'out': tf.Variable(tf.random_normal([n_classes]))}# 定义整个网络def alex_net(_X, _weights, _biases, _dropout):    # 向量转为矩阵    _X = tf.reshape(_X, shape=[-1, 28, 28, 1])    # 第一层卷积    # 卷积    conv1 = conv2d('conv1', _X, _weights['wc1'], _biases['bc1'])    # 下采样    pool1 = max_pool('pool1', conv1, k=2)    # 归一化    norm1 = norm('norm1', pool1, lsize=4)    # 第二层卷积    # 卷积    conv2 = conv2d('conv2', norm1, _weights['wc2'], _biases['bc2'])    # 下采样    pool2 = max_pool('pool2', conv2, k=2)    # 归一化    norm2 = norm('norm2', pool2, lsize=4)    # 第三层卷积    # 卷积    conv3 = conv2d('conv3', norm2, _weights['wc3'], _biases['bc3'])    # 归一化    norm3 = norm('norm3', conv3, lsize=4)    # 第四层卷积    # 卷积    conv4 = conv2d('conv4', norm3, _weights['wc4'], _biases['bc4'])    # 归一化    norm4 = norm('norm4', conv4, lsize=4)    # 第五层卷积    # 卷积    conv5 = conv2d('conv5', norm4, _weights['wc5'], _biases['bc5'])    # 下采样    pool5 = max_pool('pool5', conv5, k=2)    # 归一化    norm5 = norm('norm5', pool5, lsize=4)    # 全连接层1,先把特征图转为向量    dense1 = tf.reshape(norm5, [-1, _weights['wd1'].get_shape().as_list()[0]])    dense1 = tf.nn.relu(tf.matmul(dense1, _weights['wd1']) + _biases['bd1'], name='fc1')    dense1 = tf.nn.dropout(dense1, _dropout)    # 全连接层2    dense2 = tf.reshape(dense1, [-1, _weights['wd2'].get_shape().as_list()[0]])    dense2 = tf.nn.relu(tf.matmul(dense1, _weights['wd2']) + _biases['bd2'], name='fc2') # Relu activation    dense2 = tf.nn.dropout(dense2, _dropout)    # 网络输出层    out = tf.matmul(dense2, _weights['out']) + _biases['out']    return out# 构建模型pred = alex_net(x, weights, biases, keep_prob)# 定义损失函数和学习步骤cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = pred, labels = y))optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)# 测试网络correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))# 初始化所有的共享变量init = tf.initialize_all_variables()# 开启一个训练with tf.Session() as sess:    sess.run(init)    step = 1    # Keep training until reach max iterations    while step * batch_size < training_iters:        batch_xs, batch_ys = mnist.train.next_batch(batch_size)        # 获取批数据        sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys, keep_prob: dropout})        if step % display_step == 0:            # 计算精度            acc = sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.})            # 计算损失值            loss = sess.run(cost, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.})            print ("Iter " + str(step*batch_size) + ", Minibatch Loss = " + "{:.6f}".format(loss) + ", Training Accuracy = " + "{:.5f}".format(acc))        step += 1    print ("Optimization Finished!")    # 计算测试精度    print ("Testing Accuracy:", sess.run(accuracy, feed_dict={x: mnist.test.images[:256], y: mnist.test.labels[:256], keep_prob: 1.}))

运行结果还算不错:


原创粉丝点击