三木社区

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 462|回复: 0
打印 上一主题 下一主题

简单卷积网络

[复制链接]

1562

主题

1564

帖子

4904

积分

博士

Rank: 8Rank: 8

积分
4904
跳转到指定楼层
楼主
发表于 2017-9-18 08:05:25 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. # 《TensorFlow实战》05 TensorFlow实现卷积神经网络
  2. # win10 Tensorflow1.0.1 python3.5.3
  3. # CUDA v8.0 cudnn-8.0-windows10-x64-v5.1
  4. # filename:sz05.01.py # 简单卷积网络

  5. from tensorflow.examples.tutorials.mnist import input_data
  6. import tensorflow as tf
  7. mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
  8. sess = tf.InteractiveSession()

  9. def weight_variable(shape):
  10.     initial = tf.truncated_normal(shape, stddev=0.1)
  11.     return tf.Variable(initial)

  12. def bias_variable(shape):
  13.     initial = tf.constant(0.1, shape=shape)
  14.     return tf.Variable(initial)

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

  17. def max_pool_2x2(x):
  18.     return tf.nn.max_pool(x, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding = "SAME")

  19. x = tf.placeholder(tf.float32, [None, 784])
  20. y_ = tf.placeholder(tf.float32, [None, 10])
  21. x_image = tf.reshape(x, [-1, 28, 28, 1])

  22. W_conv1 = weight_variable([5, 5, 1, 32])
  23. b_conv1 = bias_variable([32])
  24. h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
  25. h_pool1 = max_pool_2x2(h_conv1)

  26. W_conv2 = weight_variable([5, 5, 32, 64])
  27. b_conv2 = bias_variable([64])
  28. h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
  29. h_pool2 = max_pool_2x2(h_conv2)

  30. W_fc1 = weight_variable([7 * 7 * 64, 1024])
  31. b_fc1 = bias_variable([1024])
  32. h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
  33. h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

  34. keep_prob = tf.placeholder(tf.float32)
  35. h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

  36. W_fc2 = weight_variable([1024, 10])
  37. b_fc2 = bias_variable([10])
  38. y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

  39. cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))
  40. train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

  41. correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
  42. accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

  43. tf.global_variables_initializer().run()
  44. for i in range(20000):
  45.     batch = mnist.train.next_batch(50)
  46.     if i % 1000 == 0:
  47.         train_accuracy = accuracy.eval(feed_dict = {x: batch[0], y_: batch[1], keep_prob: 1.0})
  48.         print("step %d, training accuracy %g" %(i, train_accuracy))
  49.     train_step.run(feed_dict = {x: batch[0], y_: batch[1], keep_prob: 0.5})

  50. print("test accuracy %g " %accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
  51. '''
  52. step 0, training accuracy 0.04
  53. step 1000, training accuracy 0.96
  54. step 2000, training accuracy 0.92
  55. ...
  56. step 16000, training accuracy 0.98
  57. step 17000, training accuracy 1
  58. step 18000, training accuracy 1
  59. step 19000, training accuracy 1
  60. test accuracy 0.9918
  61. '''
复制代码


回复

使用道具 举报

Archiver|手机版|小黑屋|三木电子社区 ( 辽ICP备11000133号-4 )

辽公网安备 21021702000620号

GMT+8, 2024-4-21 00:38 , Processed in 0.028625 second(s), 22 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表