Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# 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
# 4 filters: extract 4 features
num_filters = 4
# winx and winy are the dimensions of our window, 5*5 pixels
winx = 5
winy = 5
W1 = tf.Variable(tf.truncated_normal(
[winx, winy, 1 , num_filters],
b1 = tf.Variable(tf.constant(0.1,
# 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],
# 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')
# flatten convolutional output for use in dense layer
# flatten output from a 2D matrix with many channels of output --> a long, one-dimensional vector
p1_size = np.product(
[s.value for s in p1.get_shape()[1:]])
p1f = tf.reshape(p1, [-1, p1_size ])
# Dense layer with 32 neurons
num_hidden = 32
W2 = tf.Variable(tf.truncated_normal(
[p1_size, num_hidden],
b2 = tf.Variable(tf.constant(0.2,
h2 = tf.nn.relu(tf.matmul(p1f,W2) + b2)
# Output Layer
# Logistic regression again
# tf.truncated_normal - Outputs random values from a truncated normal distribution.
W3 = tf.Variable(tf.truncated_normal(
[num_hidden, 2],
b3 = tf.Variable(tf.constant(0.1,shape=[2]))
# Just initialize
# Define model
y = tf.nn.softmax(tf.matmul(h2,W3) + b3)
# 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
You can’t perform that action at this time.