Skip to content

Instantly share code, notes, and snippets.

@alinazhanguwo
Created March 22, 2019 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alinazhanguwo/ef3de4eb69d9cfce3318ce7a16237636 to your computer and use it in GitHub Desktop.
Save alinazhanguwo/ef3de4eb69d9cfce3318ce7a16237636 to your computer and use it in GitHub Desktop.
# Create an interactive Tensorflow session
sess = tf.InteractiveSession()
# These will be inputs for the model
# 36*36 pixels, image with one channel graysacle
x = tf.placeholder("float", [None, 36, 36])
# -1 is for reshaping which means to fill out the dimensions as needed to maintain the overall size
# 36,36 is image dimension
# the final 1, is now the number of channel
# we have grayscale image that the number of channel is 1
# if we use rgb images, the number of channers would be 3
x_im = tf.reshape(x, [-1,36,36,1])
# Known labels
# None works during variable creation to be unspecified size
y_ = tf.placeholder("float", [None,2])
# Conv layer 1
# 16 filters: extract 16 features
num_filters1 = 16
# winx and winy are the dimensions of our window, 5*5 pixels
winx1 = 5
winy1 = 5
W1 = tf.Variable(tf.truncated_normal(
[winx1, winy1, 1 , num_filters1],
stddev=1./math.sqrt(winx1*winy1)))
b1 = tf.Variable(tf.constant(0.1,
shape=[num_filters1]))
# 5x5 convolution
# tf.nn.conv2d function:
# first pass in reshaped input x_im,
# then the weights that are applied to every window
# then the strides argument
# padding='SAME': the sliding window will run even if part of it is beyond the bounds of input image
# pad with zeros on edges
# If you want to shift by two pixels to the right, and two down → you could input strides=[1,2,2,1]
xw = tf.nn.conv2d(x_im, W1,
strides=[1, 1, 1, 1],
padding='SAME')
# activation function: relu function, which stands for rectified linear: any negative input gets set to zero while positive inputs stay the same
h1 = tf.nn.relu(xw + b1)
# 2x2 Max pooling, no padding on edges
# valid padding - if a window is beyond the bounds of an image, just throw it out
# This does lose some information at the edges but ensures that outputs aren't being biased by padding values, i.e. '0'.
# everything is a tradeoff
p1 = tf.nn.max_pool(h1, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='VALID')
# Conv layer 2
# 4 filters: extract 4 features
num_filters2 = 4
winx2 = 3
winy2 = 3
W2 = tf.Variable(tf.truncated_normal(
[winx2, winy2, num_filters1, num_filters2],
stddev=1./math.sqrt(winx2*winy2)))
b2 = tf.Variable(tf.constant(0.1,
shape=[num_filters2]))
# 3x3 convolution, pad with zeros on edges
p1w2 = tf.nn.conv2d(p1, W2,
strides=[1, 1, 1, 1], padding='SAME')
h1 = tf.nn.relu(p1w2 + b2)
# 2x2 Max pooling, no padding on edges
p2 = tf.nn.max_pool(h1, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='VALID')
# flatten convolutional output for use in dense layer
# flatten output from a 2D matrix with many channels of output --> a long, one-dimensional vector
p2_size = np.product(
[s.value for s in p2.get_shape()[1:]])
p2f = tf.reshape(p2, [-1, p2_size ])
# Dense layer with 32 neurons
num_hidden = 32
W3 = tf.Variable(tf.truncated_normal(
[p2_size, num_hidden],
stddev=2./math.sqrt(p2_size)))
b3 = tf.Variable(tf.constant(0.2,
shape=[num_hidden]))
h3 = tf.nn.relu(tf.matmul(p2f,W3) + b3)
# Output Layer
# Logistic regression again
# tf.truncated_normal - Outputs random values from a truncated normal distribution.
W4 = tf.Variable(tf.truncated_normal(
[num_hidden, 2],
stddev=1./math.sqrt(num_hidden)))
b4 = tf.Variable(tf.constant(0.1,shape=[2]))
# Just initialize
sess.run(tf.global_variables_initializer())
# Define model
y = tf.nn.softmax(tf.matmul(h3,W4) + b4)
# Finish model specification, let us start training the model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment