1
我應用了與Predicting the next word using the LSTM ptb model tensorflow example中描述的方法相同的方法來使用張量流LSTM並預測測試文檔中的下一個單詞。但是,每次運行時,LSTM都會爲每個序列預測相同的單詞。張量流中的LSTM ptb模型始終返回相同的字
更具體地說,我添加了這些行:
class PTBModel(object):
"""The PTB model."""
def __init__(self, is_training, config):
# General definition of LSTM (unrolled)
# identical to tensorflow example ...
# omitted for brevity ...
outputs = []
state = self._initial_state
with tf.variable_scope("RNN"):
for time_step in range(num_steps):
if time_step > 0: tf.get_variable_scope().reuse_variables()
(cell_output, state) = cell(inputs[:, time_step, :], state)
outputs.append(cell_output)
output = tf.reshape(tf.concat(1, outputs), [-1, size])
softmax_w = tf.get_variable("softmax_w", [size, vocab_size])
softmax_b = tf.get_variable("softmax_b", [vocab_size])
logits = tf.matmul(output, softmax_w) + softmax_b
#Storing the probabilities and logits
self.probabilities = probabilities = tf.nn.softmax(logits)
self.logits = logits
再變run_epoch通過以下方式:
def run_epoch(session, m, data, eval_op, verbose=True, is_training = True):
"""Runs the model on the given data."""
# first part of function unchanged from example
for step, (x, y) in enumerate(reader.ptb_iterator(data, m.batch_size,
m.num_steps)):
# evaluate proobability and logit tensors too:
cost, state, probs, logits, _ = session.run([m.cost, m.final_state, m.probabilities, m.logits, eval_op],
{m.input_data: x,
m.targets: y,
m.initial_state: state})
costs += cost
iters += m.num_steps
if not is_training:
chosen_word = np.argmax(probs, 1)
print(chosen_word[-1])
return np.exp(costs/iters)
我想預測的測試數據集的下一個單詞。當我運行這個程序時,它總是返回相同的索引(大部分時間索引爲< eos>)。任何幫助表示讚賞。
我該如何預熱? – Sauber
您是否改變了SoftMax中的任何內容? 從我所瞭解的情況來看,LSTM ptb模型應該可以開箱即用。 –