Guest User

Untitled

a guest
Jan 17th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. # Build a convolutional neural network
  2. def conv_net(x, n_classes, dropout, reuse, is_training):
  3. # Define a scope for reusing the variables
  4. with tf.variable_scope('ConvNet', reuse=reuse):
  5. # MNIST data input is a 1-D vector of 784 features (28*28 pixels)
  6. # Reshape to match picture format [Height x Width x Channel]
  7. # Tensor input become 4-D: [Batch Size, Height, Width, Channel]
  8. x = tf.reshape(x, shape=[-1, 28, 28, 1])
  9.  
  10. # Convolution Layer with 64 filters and a kernel size of 5
  11. x = tf.layers.conv2d(x, 64, 5, activation=tf.nn.relu)
  12. # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
  13. x = tf.layers.max_pooling2d(x, 2, 2)
  14.  
  15. # Convolution Layer with 256 filters and a kernel size of 5
  16. x = tf.layers.conv2d(x, 256, 3, activation=tf.nn.relu)
  17. # Convolution Layer with 512 filters and a kernel size of 5
  18. x = tf.layers.conv2d(x, 512, 3, activation=tf.nn.relu)
  19. # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
  20. x = tf.layers.max_pooling2d(x, 2, 2)
  21.  
  22. # Flatten the data to a 1-D vector for the fully connected layer
  23. x = tf.contrib.layers.flatten(x)
  24.  
  25. # Fully connected layer (in contrib folder for now)
  26. x = tf.layers.dense(x, 2048)
  27. # Apply Dropout (if is_training is False, dropout is not applied)
  28. x = tf.layers.dropout(x, rate=dropout, training=is_training)
  29.  
  30. # Fully connected layer (in contrib folder for now)
  31. x = tf.layers.dense(x, 1024)
  32. # Apply Dropout (if is_training is False, dropout is not applied)
  33. x = tf.layers.dropout(x, rate=dropout, training=is_training)
  34.  
  35. # Output layer, class prediction
  36. out = tf.layers.dense(x, n_classes)
  37. # Because 'softmax_cross_entropy_with_logits' loss already apply
  38. # softmax, we only apply softmax to testing network
  39. out = tf.nn.softmax(out) if not is_training else out
  40.  
  41. return out
Add Comment
Please, Sign In to add comment