Skip to content

Instantly share code, notes, and snippets.

@pyk
Created January 7, 2017 17:59
Show Gist options
  • Save pyk/bbd56ec5ccc4b2878f68cc2acb7f572a to your computer and use it in GitHub Desktop.
Save pyk/bbd56ec5ccc4b2878f68cc2acb7f572a to your computer and use it in GitHub Desktop.
# Convolutional layer 1
with tf.name_scope('conv1'):
W = tf.Variable(
tf.truncated_normal(
shape=(
CONV1_FILTER_SIZE,
CONV1_FILTER_SIZE,
NUM_CHANNELS,
CONV1_FILTER_COUNT),
dtype=tf.float32,
stddev=5e-2),
name='weights')
b = tf.Variable(
tf.zeros(
shape=(CONV1_FILTER_COUNT),
dtype=tf.float32),
name='biases')
conv = tf.nn.conv2d(
input=images,
filter=W,
strides=(1, 1, 1, 1),
padding='SAME',
name='convolutional')
conv_bias = tf.nn.bias_add(conv, b)
conv_act = tf.nn.relu(
features=conv_bias,
name='activation')
pool1 = tf.nn.max_pool(
value=conv_act,
ksize=(1, 2, 2, 1),
strides=(1, 2, 2, 1),
padding='SAME',
name='subsampling')
# Convolutional layer 2
with tf.name_scope('conv2'):
W = tf.Variable(
tf.truncated_normal(
shape=(
CONV2_FILTER_SIZE,
CONV2_FILTER_SIZE,
CONV1_FILTER_COUNT,
CONV2_FILTER_COUNT),
dtype=tf.float32,
stddev=5e-2),
name='weights')
b = tf.Variable(
tf.zeros(
shape=(CONV2_FILTER_COUNT),
dtype=tf.float32),
name='biases')
conv = tf.nn.conv2d(
input=pool1,
filter=W,
strides=(1, 1, 1, 1),
padding='SAME',
name='convolutional')
conv_bias = tf.nn.bias_add(conv, b)
conv_act = tf.nn.relu(
features=conv_bias,
name='activation')
pool2 = tf.nn.max_pool(
value=conv_act,
ksize=(1, 2, 2, 1),
strides=(1, 2, 2, 1),
padding='SAME',
name='subsampling')
# Hidden layer
with tf.name_scope('hidden'):
conv_output_size = 28800
W = tf.Variable(
tf.truncated_normal(
shape=(conv_output_size, HIDDEN_LAYER_SIZE),
dtype=tf.float32,
stddev=5e-2),
name='weights')
b = tf.Variable(
tf.zeros(
shape=(HIDDEN_LAYER_SIZE),
dtype=tf.float32),
name='biases')
reshape = tf.reshape(
tensor=pool2,
shape=[BATCH_SIZE, -1])
h1 = tf.nn.relu(
features=tf.add(tf.matmul(reshape, W), b),
name='activation')
# Softmax layer
with tf.name_scope('softmax'):
W = tf.Variable(
tf.truncated_normal(
shape=(HIDDEN_LAYER_SIZE, NUM_CLASS),
dtype=tf.float32,
stddev=5e-2),
name='weights')
b = tf.Variable(
tf.zeros(
shape=(NUM_CLASS),
dtype=tf.float32),
name='biases')
logits = tf.add(tf.matmul(h1, W), b, name='logits')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment