def rnn_model(self):
# BasicLSTMCell ????LSTM?????????forget_bias(????1)?????????????????????????
# ??????????????????peep-hole????????
# BasicLSTMCell ????? rnn.python.ops?? core_rnn_cell_impl.py
cell = rnn.BasicLSTMCell(num_units=self.n_units)
# MultiRNNCell ????????????????????RNN????????????????????
# ??????????True state_is_tuple = True
# ???????LSTM??????????????????
multi_cell = rnn.MultiRNNCell([cell]*self.n_layers)
# we only need one output so get it wrapped to out one value which is next word index
# ? rnn_cell ??????????? output_size?????size ?????Output_projection?rnn_cell
cell_wrapped = rnn.OutputProjectionWrapper(multi_cell, output_size=1)
# get input embed
# tf.random_uniform(shape, minval, maxval, dtype, seed, name) ? ???? n*n????????minval ? maxval ??
embedding = tf.Variable(initial_value=tf.random_uniform([self.vocab_size, self.n_units], -1.0, 1.0))
# tf.nn.embedding_lokkup(embedding, inputs_id) : ??inputs_id??embedding??????????input_ids=[1,3,5],?
# ??embedding????1,3,5????????????
inputs = tf.nn.embedding_lookup(embedding, self.inputs)
# what is inputs dim??
# add initial state into dynamic rnn, if I am not result would be bad, I tried, don't know why
if self.labels is not None:
# zero_state ? ?????
initial_state = cell_wrapped.zero_state(int(inputs.get_shape()[0]), tf.float32)
else:
initial_state = cell_wrapped.zero_state(1, tf.float32)
# dynamic_rnn ?????????????batch?????????????????batch????????????????
# dynamic_rnn ? rnn ??
outputs, states = tf.nn.dynamic_rnn(cell_wrapped, inputs=inputs, dtype=tf.float32, initial_state=initial_state)
outputs = tf.reshape(outputs, [int(outputs.get_shape()[0]), int(inputs.get_shape()[1])])
# truncated_normal : ???????????
w = tf.Variable(tf.truncated_normal([int(inputs.get_shape()[1]), self.vocab_size]))
b = tf.Variable(tf.zeros([self.vocab_size]))
logits = tf.nn.bias_add(tf.matmul(outputs, w), b)
return logits, states
rnn_model.py 文件源码
python
阅读 22
收藏 0
点赞 0
评论 0
评论列表
文章目录