-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathops.py
53 lines (42 loc) · 1.82 KB
/
ops.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import tensorflow as tf
from tensorflow.contrib.layers.python.layers import initializers
def conv2d(x,
output_dim,
kernel_size,
stride,
initializer=tf.contrib.layers.xavier_initializer(),
activation_fn=tf.nn.relu,
data_format='NHWC',
padding='VALID',
name='conv2d'):
with tf.variable_scope(name):
if data_format == 'NCHW':
stride = [1, 1, stride[0], stride[1]]
kernel_shape = [kernel_size[0], kernel_size[1], x.get_shape()[1], output_dim]
elif data_format == 'NHWC':
stride = [1, stride[0], stride[1], 1]
kernel_shape = [kernel_size[0], kernel_size[1], x.get_shape()[-1], output_dim]
w = tf.get_variable('w', kernel_shape, tf.float32, initializer=initializer)
conv = tf.nn.conv2d(x, w, stride, padding, data_format=data_format)
b = tf.get_variable('biases', [output_dim], initializer=tf.constant_initializer(0.0))
out = tf.nn.bias_add(conv, b, data_format)
if activation_fn != None:
out = activation_fn(out)
return out, w, b
def linear(input_, output_size, stddev=0.02, bias_start=0.0, activation_fn=None, name='linear'):
shape = input_.get_shape().as_list()
with tf.variable_scope(name):
w = tf.get_variable('Matrix', [shape[1], output_size], tf.float32,
tf.random_normal_initializer(stddev=stddev))
b = tf.get_variable('bias', [output_size],
initializer=tf.constant_initializer(bias_start))
out = tf.nn.bias_add(tf.matmul(input_, w), b)
if activation_fn != None:
return activation_fn(out), w, b
else:
return out, w, b
def flatten(input_):
in_list = [x for x in input_ if x is not None]
if type(in_list[0]) is list:
in_list = [flatten(elem) for elem in in_list ]
return tf.concat(0, [ tf.reshape(elem, [-1]) for elem in in_list])