-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconv_nn.py
38 lines (30 loc) · 2.14 KB
/
conv_nn.py
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
import tensorflow as tf
class conv_nn(object):
def __init__(self, num_classes, img_dim=96):
self.x = tf.placeholder(tf.float32, [None, img_dim * img_dim], name="x_input")
self.y = tf.placeholder(tf.float32, [None, num_classes], name="y_input")
self.x_expanded = tf.expand_dims(tf.reshape(self.x, [-1, img_dim, img_dim]), axis=-1)
with tf.variable_scope("conv1"):
W = tf.get_variable('weights', shape=[2, 2, 1, 32], initializer=tf.contrib.layers.xavier_initializer())
B = tf.get_variable('biases', shape=[32], initializer=tf.constant_initializer(0.0))
conv = tf.nn.conv2d(self.x_expanded, W, strides=[1, 1, 1, 1], padding='VALID')
relu = tf.nn.relu(conv + B)
pool = tf.nn.max_pool(relu, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="VALID")
with tf.variable_scope("conv2"):
W = tf.get_variable('weights', shape=[2, 2, 32, 64], initializer=tf.contrib.layers.xavier_initializer())
B = tf.get_variable('biases', shape=[64], initializer=tf.constant_initializer(0.0))
conv = tf.nn.conv2d(pool, W, strides=[1, 1, 1, 1], padding='VALID')
relu = tf.nn.relu(conv + B)
pool = tf.nn.max_pool(relu, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="VALID")
shape = pool.get_shape().as_list()
x_2 = shape[1] * shape[2] * shape[3]
self.flatten = tf.reshape(pool, [-1, shape[1] * shape[2] * shape[3]])
with tf.variable_scope("fc1"):
W = tf.get_variable('weights', shape=[x_2, 100], initializer=tf.contrib.layers.xavier_initializer())
B = tf.get_variable('biases', shape=[100], initializer=tf.constant_initializer(0.0))
h = tf.matmul(self.flatten, W) + B
with tf.variable_scope("fc2"):
W = tf.get_variable('weights', shape=[100, num_classes], initializer=tf.contrib.layers.xavier_initializer())
B = tf.get_variable('biases', shape=[num_classes], initializer=tf.constant_initializer(0.0))
self.scores = tf.nn.xw_plus_b(h, W, B, name="scores")
self.loss = tf.reduce_mean(tf.square(self.scores - self.y), name="loss")