-
Notifications
You must be signed in to change notification settings - Fork 95
/
model.py
85 lines (59 loc) · 2.29 KB
/
model.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import sugartensor as tf
__author__ = 'namju.kim@kakaobrain.com'
#
# hyper parameters
#
latent_dim = 400 # hidden layer dimension
num_blocks = 3 # dilated blocks
# residual block
@tf.sg_sugar_func
def sg_res_block(tensor, opt):
# default rate
opt += tf.sg_opt(size=3, rate=1, causal=False, is_first=False)
# input dimension
in_dim = tensor.get_shape().as_list()[-1]
with tf.sg_context(name='block_%d_%d' % (opt.block, opt.rate)):
# reduce dimension
input_ = (tensor
.sg_bypass(act='relu', ln=(not opt.is_first), name='bypass') # do not
.sg_conv1d(size=1, dim=in_dim/2, act='relu', ln=True, name='conv_in'))
# 1xk conv dilated
out = (input_
.sg_aconv1d(size=opt.size, rate=opt.rate, causal=opt.causal, act='relu', ln=True, name='aconv'))
# dimension recover and residual connection
out = out.sg_conv1d(size=1, dim=in_dim, name='conv_out') + tensor
return out
# inject residual multiplicative block
tf.sg_inject_func(sg_res_block)
#
# encode graph ( atrous convolution )
#
def encode(x):
with tf.sg_context(name='encoder'):
res = x
# loop dilated conv block
for i in range(num_blocks):
res = (res
.sg_res_block(size=5, block=i, rate=1, is_first=True)
.sg_res_block(size=5, block=i, rate=2)
.sg_res_block(size=5, block=i, rate=4)
.sg_res_block(size=5, block=i, rate=8)
.sg_res_block(size=5, block=i, rate=16))
return res
#
# decode graph ( causal convolution )
#
def decode(x, voca_size):
with tf.sg_context(name='decoder'):
res = x
# loop dilated causal conv block
for i in range(num_blocks):
res = (res
.sg_res_block(size=3, block=i, rate=1, causal=True, is_first=True)
.sg_res_block(size=3, block=i, rate=2, causal=True)
.sg_res_block(size=3, block=i, rate=4, causal=True)
.sg_res_block(size=3, block=i, rate=8, causal=True)
.sg_res_block(size=3, block=i, rate=16, causal=True))
# final fully convolution layer for softmax
res = res.sg_conv1d(size=1, dim=voca_size, name='conv_final')
return res