-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNN.py
22 lines (17 loc) · 886 Bytes
/
NN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import math
import tensorflow as tf
def weight(name, shape, init='he'):
assert init == 'he' and len(shape) == 2
var = tf.get_variable(name, shape, initializer=tf.random_normal_initializer(stddev=math.sqrt(2.0 / shape[0])))
tf.add_to_collection('l2', tf.nn.l2_loss(var))
return var
def bias(name, dim, initial_value=1e-2):
return tf.get_variable(name, dim, initializer=tf.constant_initializer(initial_value))
def fully_connected(input, num_neurons, name, activation='elu'):
func = {'linear': tf.identity, 'sigmoid': tf.nn.sigmoid, 'tanh': tf.nn.tanh, 'relu': tf.nn.relu, 'elu': tf.nn.elu}
W = weight(name + '_W', [input.get_shape().as_list()[1], num_neurons], init='he')
b = bias(name + '_b', num_neurons)
l = tf.matmul(input, W) + b
return func[activation](l)
def dropout(x, keep_prob, training):
return tf.cond(training, lambda: tf.nn.dropout(x, keep_prob), lambda: x)