forked from haebeom-lee/l2b
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayers.py
More file actions
64 lines (52 loc) · 2 KB
/
Copy pathlayers.py
File metadata and controls
64 lines (52 loc) · 2 KB
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
54
55
56
57
58
59
60
61
62
63
64
import tensorflow as tf
import numpy as np
# functions
log = lambda x: tf.log(x + 1e-20)
softmax = tf.nn.softmax
relu = tf.nn.relu
softplus = tf.nn.softplus
sigmoid = tf.sigmoid
exp = tf.exp
# distributions
normal = tf.distributions.Normal
def kl_diagnormal_stdnormal(q):
qshape = q.mean().shape
p = normal(tf.zeros(qshape), tf.ones(qshape))
return tf.distributions.kl_divergence(q, p)
# layers
batch_norm = tf.contrib.layers.batch_norm
flatten = tf.layers.flatten
def dense(x, dim, **kwargs):
return tf.layers.dense(x, dim,
kernel_initializer=tf.random_normal_initializer(stddev=0.02),
bias_initializer=tf.zeros_initializer(), **kwargs)
def conv(x, filters, kernel_size=3, strides=1, **kwargs):
return tf.layers.conv2d(x, filters, kernel_size, strides, padding='same',
kernel_initializer=tf.truncated_normal_initializer(stddev=0.02),
bias_initializer=tf.zeros_initializer(), **kwargs)
def pool(x, **kwargs):
return tf.layers.max_pooling2d(x, 2, 2, padding='valid', **kwargs)
# blocks
def conv_block(x, w, b, bn_scope='conv_bn'):
x = tf.nn.conv2d(x, w, [1,1,1,1], 'SAME') + b # NHWC
x = batch_norm(x, activation_fn=relu, scope=bn_scope, reuse=tf.AUTO_REUSE)
x = tf.nn.max_pool(x, [1,2,2,1], [1,2,2,1], 'VALID')
return x
def dense_block(x, w, b):
x = tf.matmul(flatten(x), w) + b
return x
# training modules
def cross_entropy(logits, labels):
losses = tf.nn.softmax_cross_entropy_with_logits_v2(
logits=logits, labels=labels)
return tf.reduce_mean(losses)
def cross_entropy_perclass(logits, labels):
losses = tf.nn.softmax_cross_entropy_with_logits_v2(
logits=logits, labels=labels)
perclass = tf.matmul(tf.transpose(labels), tf.expand_dims(losses,1))
N_t = tf.cast(tf.shape(labels)[0], dtype=tf.float32)
way = tf.cast(tf.shape(labels)[1], dtype=tf.float32)
return tf.squeeze(perclass) * way / N_t
def accuracy(logits, labels, axis=-1):
correct = tf.equal(tf.argmax(logits, -1), tf.argmax(labels, -1))
return tf.reduce_mean(tf.cast(correct, tf.float32), axis)