Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

tf1.3-mnist.py 7.7 KB

You have to be logged in to leave a comment. Sign In
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  1. ''' Multi-GPU Training Example.
  2. Train a convolutional neural network on multiple GPU with TensorFlow.
  3. This example is using TensorFlow layers, see 'convolutional_network_raw' example
  4. for a raw TensorFlow implementation with variables.
  5. This example is using the MNIST database of handwritten digits
  6. (http://yann.lecun.com/exdb/mnist/)
  7. Author: Aymeric Damien
  8. Project: https://github.com/aymericdamien/TensorFlow-Examples/
  9. '''
  10. from __future__ import division, print_function, absolute_import
  11. import numpy as np
  12. import tensorflow as tf
  13. import time
  14. # Import MNIST data
  15. from tensorflow.examples.tutorials.mnist import input_data
  16. mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
  17. # Training Parameters
  18. num_gpus = 2
  19. num_steps = 200
  20. learning_rate = 0.001
  21. batch_size = 1024
  22. display_step = 10
  23. # Network Parameters
  24. num_input = 784 # MNIST data input (img shape: 28*28)
  25. num_classes = 10 # MNIST total classes (0-9 digits)
  26. dropout = 0.75 # Dropout, probability to keep units
  27. # Build a convolutional neural network
  28. def conv_net(x, n_classes, dropout, reuse, is_training):
  29. # Define a scope for reusing the variables
  30. with tf.variable_scope('ConvNet', reuse=reuse):
  31. # MNIST data input is a 1-D vector of 784 features (28*28 pixels)
  32. # Reshape to match picture format [Height x Width x Channel]
  33. # Tensor input become 4-D: [Batch Size, Height, Width, Channel]
  34. x = tf.reshape(x, shape=[-1, 28, 28, 1])
  35. # Convolution Layer with 64 filters and a kernel size of 5
  36. x = tf.layers.conv2d(x, 64, 5, activation=tf.nn.relu)
  37. # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
  38. x = tf.layers.max_pooling2d(x, 2, 2)
  39. # Convolution Layer with 256 filters and a kernel size of 5
  40. x = tf.layers.conv2d(x, 256, 3, activation=tf.nn.relu)
  41. # Convolution Layer with 512 filters and a kernel size of 5
  42. x = tf.layers.conv2d(x, 512, 3, activation=tf.nn.relu)
  43. # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
  44. x = tf.layers.max_pooling2d(x, 2, 2)
  45. # Flatten the data to a 1-D vector for the fully connected layer
  46. x = tf.contrib.layers.flatten(x)
  47. # Fully connected layer (in contrib folder for now)
  48. x = tf.layers.dense(x, 2048)
  49. # Apply Dropout (if is_training is False, dropout is not applied)
  50. x = tf.layers.dropout(x, rate=dropout, training=is_training)
  51. # Fully connected layer (in contrib folder for now)
  52. x = tf.layers.dense(x, 1024)
  53. # Apply Dropout (if is_training is False, dropout is not applied)
  54. x = tf.layers.dropout(x, rate=dropout, training=is_training)
  55. # Output layer, class prediction
  56. out = tf.layers.dense(x, n_classes)
  57. # Because 'softmax_cross_entropy_with_logits' loss already apply
  58. # softmax, we only apply softmax to testing network
  59. out = tf.nn.softmax(out) if not is_training else out
  60. return out
  61. def average_gradients(tower_grads):
  62. average_grads = []
  63. for grad_and_vars in zip(*tower_grads):
  64. # Note that each grad_and_vars looks like the following:
  65. # ((grad0_gpu0, var0_gpu0), ... , (grad0_gpuN, var0_gpuN))
  66. grads = []
  67. for g, _ in grad_and_vars:
  68. # Add 0 dimension to the gradients to represent the tower.
  69. expanded_g = tf.expand_dims(g, 0)
  70. # Append on a 'tower' dimension which we will average over below.
  71. grads.append(expanded_g)
  72. # Average over the 'tower' dimension.
  73. grad = tf.concat(grads, 0)
  74. grad = tf.reduce_mean(grad, 0)
  75. # Keep in mind that the Variables are redundant because they are shared
  76. # across towers. So .. we will just return the first tower's pointer to
  77. # the Variable.
  78. v = grad_and_vars[0][1]
  79. grad_and_var = (grad, v)
  80. average_grads.append(grad_and_var)
  81. return average_grads
  82. # By default, all variables will be placed on '/gpu:0'
  83. # So we need a custom device function, to assign all variables to '/cpu:0'
  84. # Note: If GPUs are peered, '/gpu:0' can be a faster option
  85. PS_OPS = ['Variable', 'VariableV2', 'AutoReloadVariable']
  86. def assign_to_device(device, ps_device='/cpu:0'):
  87. def _assign(op):
  88. node_def = op if isinstance(op, tf.NodeDef) else op.node_def
  89. if node_def.op in PS_OPS:
  90. return "/" + ps_device
  91. else:
  92. return device
  93. return _assign
  94. # Place all ops on CPU by default
  95. with tf.device('/cpu:0'):
  96. tower_grads = []
  97. reuse_vars = False
  98. # tf Graph input
  99. X = tf.placeholder(tf.float32, [None, num_input])
  100. Y = tf.placeholder(tf.float32, [None, num_classes])
  101. # Loop over all GPUs and construct their own computation graph
  102. for i in range(num_gpus):
  103. with tf.device(assign_to_device('/gpu:{}'.format(i), ps_device='/cpu:0')):
  104. # Split data between GPUs
  105. _x = X[i * batch_size: (i+1) * batch_size]
  106. _y = Y[i * batch_size: (i+1) * batch_size]
  107. # Because Dropout have different behavior at training and prediction time, we
  108. # need to create 2 distinct computation graphs that share the same weights.
  109. # Create a graph for training
  110. logits_train = conv_net(_x, num_classes, dropout,
  111. reuse=reuse_vars, is_training=True)
  112. # Create another graph for testing that reuse the same weights
  113. logits_test = conv_net(_x, num_classes, dropout,
  114. reuse=True, is_training=False)
  115. # Define loss and optimizer (with train logits, for dropout to take effect)
  116. loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
  117. logits=logits_train, labels=_y))
  118. optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
  119. grads = optimizer.compute_gradients(loss_op)
  120. # Only first GPU compute accuracy
  121. if i == 0:
  122. # Evaluate model (with test logits, for dropout to be disabled)
  123. correct_pred = tf.equal(
  124. tf.argmax(logits_test, 1), tf.argmax(_y, 1))
  125. accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
  126. reuse_vars = True
  127. tower_grads.append(grads)
  128. tower_grads = average_gradients(tower_grads)
  129. train_op = optimizer.apply_gradients(tower_grads)
  130. # Initialize the variables (i.e. assign their default value)
  131. init = tf.global_variables_initializer()
  132. # Start Training
  133. with tf.Session() as sess:
  134. # Run the initializer
  135. sess.run(init)
  136. # Keep training until reach max iterations
  137. for step in range(1, num_steps + 1):
  138. # Get a batch for each GPU
  139. batch_x, batch_y = mnist.train.next_batch(batch_size * num_gpus)
  140. # Run optimization op (backprop)
  141. ts = time.time()
  142. sess.run(train_op, feed_dict={X: batch_x, Y: batch_y})
  143. te = time.time() - ts
  144. if step % display_step == 0 or step == 1:
  145. # Calculate batch loss and accuracy
  146. loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch_x,
  147. Y: batch_y})
  148. print("Step " + str(step) + ": Minibatch Loss= " +
  149. "{:.4f}".format(loss) + ", Training Accuracy= " +
  150. "{:.3f}".format(acc) + ", %i Examples/sec" % int(len(batch_x)/te))
  151. step += 1
  152. print("Optimization Finished!")
  153. # Calculate accuracy for MNIST test images
  154. print("Testing Accuracy:",
  155. np.mean([sess.run(accuracy, feed_dict={X: mnist.test.images[i:i+batch_size],
  156. Y: mnist.test.labels[i:i+batch_size]}) for i in range(0, len(mnist.test.images), batch_size)]))
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...