查看: 4691|回复: 4

[up-board开发板]+tensorflow mnist深度神经网络

[复制链接]
  • TA的每日心情
    开心
    2018-6-10 20:29
  • 签到天数: 711 天

    连续签到: 1 天

    [LV.9]以坛为家II

    发表于 2017-1-8 10:01:46 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 IC爬虫 于 2017-1-8 17:58 编辑

    我现在正在使用UP Board发帖!!!!!!!!!!!!!!!!

    tensorflow的优势是非常简单就能构建对大型数据的的训练,但是这只是它众多亮点中的一个。在训练大规模数据的时候库可以使用多种模型,我分别测试了使用softmax回归模型和深度神经网络模型对数据训练,再对比这两种模型训练出来的精度。
    webwxgetmsgimg.jpg
    使用softmax回归模型:
    1. import tensorflow as tf
    2. import numpy as np
    3. import input_data

    4. mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
    5. sess = tf.InteractiveSession()
    6. x = tf.placeholder("float", shape=[None, 784])
    7. y_ = tf.placeholder("float", shape=[None, 10])
    8. W = tf.Variable(tf.zeros([784,10]))
    9. b = tf.Variable(tf.zeros([10]))
    10. sess.run(tf.initialize_all_variables())
    11. y = tf.nn.softmax(tf.matmul(x,W) + b)
    12. cross_entropy = -tf.reduce_sum(y_*tf.log(y))
    13. train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
    14. for i in range(1000):
    15.         batch = mnist.train.next_batch(50)
    16.         train_step.run(feed_dict={x: batch[0], y_: batch[1]})
    17. correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
    18. accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    19. print accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels})
    复制代码
    计算出在测试数据上的准确率,大概是91%
    tensorflow5.png

    深度卷积网络:
    1. def weight_variable(shape):
    2.         initial = tf.truncated_normal(shape, stddev=0.1)
    3.         return tf.Variable(initial)

    4. def bias_variable(shape):
    5.         initial = tf.constant(0.1, shape=shape)
    6.         return tf.Variable(initial)
    7. def conv2d(x, W):
    8.         return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

    9. def max_pool_2x2(x):
    10.         return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
    11. W_conv1 = weight_variable([5, 5, 1, 32])
    12. b_conv1 = bias_variable([32])        
    13. x_image = tf.reshape(x, [-1,28,28,1])
    14. h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
    15. h_pool1 = max_pool_2x2(h_conv1)

    16. W_conv2 = weight_variable([5, 5, 32, 64])
    17. b_conv2 = bias_variable([64])

    18. h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
    19. h_pool2 = max_pool_2x2(h_conv2)

    20. W_fc1 = weight_variable([7 * 7 * 64, 1024])
    21. b_fc1 = bias_variable([1024])

    22. h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
    23. h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)        
    24. keep_prob = tf.placeholder("float")
    25. h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
    26. W_fc2 = weight_variable([1024, 10])
    27. b_fc2 = bias_variable([10])

    28. y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
    29. cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
    30. train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
    31. correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
    32. accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    33. sess.run(tf.initialize_all_variables())
    34. for i in range(20000):
    35.         batch = mnist.train.next_batch(50)
    36.         if i%100 == 0:
    37.                 train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
    38.             print "step %d, training accuracy %g"%(i, train_accuracy)
    39.         train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

    40. print "test accuracy %g"%accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})
    复制代码
    在最终测试集上的准确率大概是99.2%。
    tensorfow6.png



    回复

    使用道具 举报

  • TA的每日心情
    擦汗
    2019-8-27 21:30
  • 签到天数: 219 天

    连续签到: 1 天

    [LV.7]常住居民III

    发表于 2017-1-8 19:42:14 | 显示全部楼层
    看不懂,想学习一些相关的内容,有什么好的推荐学习方式吗。
    有在看Udacity的Tensorflow的一个视频
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2016-7-19 10:35
  • 签到天数: 8 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    发表于 2017-1-8 20:55:45 | 显示全部楼层
    吃瓜群众路过
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    发表于 2017-1-11 23:13:31 | 显示全部楼层
    支持,优势是非常明显。
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    发表于 2018-7-18 12:25:57 | 显示全部楼层
    大佬,这板子能用来跑神经网络啊?CPU跑起来不会很慢吗?
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-20 15:36 , Processed in 0.175305 second(s), 24 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.