-
Notifications
You must be signed in to change notification settings - Fork 1
/
indrnn_convattention.py
165 lines (126 loc) · 5.92 KB
/
indrnn_convattention.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# -*- coding: utf-8 -*-
import tensorflow as tf
from ind_rnn_cell import IndRNNCell
class DecoderCell(tf.nn.rnn_cell.RNNCell):
def __init__(self, att_cell, c_cell, f_cell):
super(DecoderCell, self).__init__(_scope=None)
self._att_cell = att_cell
self._c_cell = c_cell
self._f_cell = f_cell
@property
def state_size(self):
return (self._att_cell.state_size,
self._c_cell.state_size,
self._f_cell.state_size)
@property
def output_size(self):
return 2
def call(self, inputs, state):
att_out, att_state = self._att_cell(inputs, state[0])
c_out, c_state = self._c_cell(att_out, state[1])
c_sample = tf.multinomial(c_out, 1, output_dtype=tf.int32)
c_sample = tf.scalar_mul(1 / 256, tf.cast(c_sample, tf.float32))
att_out = tf.concat([att_out, c_sample], 1)
f_out, f_state = self._f_cell(att_out, state[2])
f_sample = tf.multinomial(f_out, 1, output_dtype=tf.int32)
f_sample = tf.scalar_mul(1 / 256, tf.cast(f_sample, tf.float32))
out = tf.concat([c_sample, f_sample], 1)
out.set_shape((1, 2))
return out, (att_state, c_state, f_state)
class WaveRNN:
def __init__(self,
input_data, # (B, T, 2) uint8
valid_samp_cnt,
features, # (B, N_f) uint8
DICT_SIZE,
TIME_STEPS,
NUM_UNITS = 256):
BATCH_SIZE = tf.shape(input_data)[0]
# (B, T, coarse + fine)
input_norm = tf.scalar_mul(1 / 256, tf.cast(input_data, tf.float32))
# input data is now in [0, 1]
# c_t-1 . f_t-1
cf_tm1 = input_norm[:, :-1]
# one-hot encode the features
features_1h = tf.one_hot(features, DICT_SIZE) # (B, N_f, DICT_SIZE)
# Regulate each neuron's recurrent weight as recommended in the indRNN paper
# TODO: try the gradient punishing method used in improved gan
# RECURRENT_MAX = pow(2, 1 / TIME_STEPS)
def make_cell(num_units, name):
return tf.contrib.rnn.GRUCell(num_units, name=name)
#return IndRNNCell(NUM_UNITS, recurrent_max_abs=RECURRENT_MAX)
# Sentence Encoder RNN
encoder_cell = tf.contrib.rnn.MultiRNNCell(
[make_cell(NUM_UNITS, "enc0"),
tf.contrib.rnn.ResidualWrapper(make_cell(NUM_UNITS, "enc1"))])
encoder_outputs, encoder_state = tf.nn.dynamic_rnn(
encoder_cell,
features_1h,
initial_state=encoder_cell.zero_state(batch_size=BATCH_SIZE, dtype=tf.float32))
# Attention RNN
att_cell = tf.contrib.seq2seq.AttentionWrapper(
tf.contrib.rnn.MultiRNNCell(
[make_cell(NUM_UNITS, "att0"),
tf.contrib.rnn.ResidualWrapper(make_cell(NUM_UNITS, "att1"))]
),
tf.contrib.seq2seq.BahdanauMonotonicAttention(NUM_UNITS, encoder_outputs),
attention_layer_size=NUM_UNITS,
name="att_cell")
att_init_state = att_cell.zero_state(dtype=tf.float32, batch_size=BATCH_SIZE)
attention, att_state = tf.nn.dynamic_rnn(
att_cell,
cf_tm1,
initial_state=att_init_state,
dtype=tf.float32)
# rnn cell for predicting coarse
c_cell = tf.contrib.rnn.MultiRNNCell(
[tf.contrib.rnn.ResidualWrapper(make_cell(NUM_UNITS, "c_cell0")),
tf.contrib.rnn.ResidualWrapper(make_cell(256, "c_cell1"))])
c_init_state = c_cell.zero_state(dtype=tf.float32, batch_size=BATCH_SIZE)
coarse_logits, c_state = tf.nn.dynamic_rnn(
c_cell,
attention,
initial_state=c_init_state,
dtype=tf.float32)
# rnn cell for predicting fine
f_cell = tf.contrib.rnn.MultiRNNCell(
[make_cell(NUM_UNITS, "f_cell0"),
tf.contrib.rnn.ResidualWrapper(make_cell(256, "f_cell1"))])
# c_t-1 + f_t-1 + c_t
f_input = tf.concat([attention, input_norm[:, 1:, :1]], 2)
f_init_state = f_cell.zero_state(dtype=tf.float32, batch_size=BATCH_SIZE)
fine_logits, f_state = tf.nn.dynamic_rnn(
f_cell,
f_input,
initial_state=f_init_state,
dtype=tf.float32)
self.init_states = (att_init_state, c_init_state, f_init_state)
self.new_states = (att_state, c_state, f_state)
# Decoder
decode_cell = DecoderCell(att_cell, c_cell, f_cell)
helper = tf.contrib.seq2seq.CustomHelper(
initialize_fn=lambda: (False, tf.constant([[0.5, 0.0]])),
sample_fn=lambda time, outputs, state: tf.constant([0]),
next_inputs_fn=lambda time, outputs, state, sample_ids: (False, outputs, state))
decoder = tf.contrib.seq2seq.BasicDecoder(
decode_cell,
helper,
self.init_states)
self.decode = tf.contrib.seq2seq.dynamic_decode(decoder, maximum_iterations=10000)
# Calculate loss and accuracy
self.c_loss = tf.reduce_sum(tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=input_data[:, 1:, 0], logits=coarse_logits)) / valid_samp_cnt
self.c_accuracy = tf.reduce_mean(
tf.cast(tf.equal(
input_data[:, 1:, 0],
tf.argmax(coarse_logits, axis=2, output_type=tf.int32)
), tf.float32))
self.f_loss = tf.reduce_sum(tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=input_data[:, 1:, 1], logits=fine_logits)) / valid_samp_cnt
self.f_accuracy = tf.reduce_mean(
tf.cast(tf.equal(
input_data[:, 1:, 1],
tf.argmax(fine_logits, axis=2, output_type=tf.int32)
), tf.float32))
# probability distribution for introspection
self.prs = tf.concat([tf.nn.softmax(fine_logits), tf.nn.softmax(coarse_logits)], 2)