Python TensorFlow:预测工作不正常
我对我制作的模型有一些问题 1) 有时,预测返回0的数组(但这可能是正常的) 2) 如果我重复预测多次(使用循环),我总是得到相同的结果 3) 奇怪的是,如果我停止程序并重新运行它,预测就会改变(第2点仍然适用) 4) 结果是错误的。我插入了与训练相同的精确查询(训练时预测的响应是正确的),但得到了非常不同的响应 现在,奇怪的是,在训练的时候,我可以看到它在进步,而且运行得很好 以下是显示正确结果(培训)的代码: 这是我在预测方面遇到的问题: 就这样。我不知道为什么会这样。感谢您的帮助。我还包括了其他方法,以提供更多的上下文Python TensorFlow:预测工作不正常,python,tensorflow,Python,Tensorflow,我对我制作的模型有一些问题 1) 有时,预测返回0的数组(但这可能是正常的) 2) 如果我重复预测多次(使用循环),我总是得到相同的结果 3) 奇怪的是,如果我停止程序并重新运行它,预测就会改变(第2点仍然适用) 4) 结果是错误的。我插入了与训练相同的精确查询(训练时预测的响应是正确的),但得到了非常不同的响应 现在,奇怪的是,在训练的时候,我可以看到它在进步,而且运行得很好 以下是显示正确结果(培训)的代码: 这是我在预测方面遇到的问题: 就这样。我不知道为什么会这样。感谢您的帮助。我还包括
这是下一个\u提要(): 这是批处理法:
def batch_method(inputs, max_sequence_length=None):
"""
Args:
inputs:
list of sentences (integer lists)
max_sequence_length:
integer specifying how large should `max_time` dimension be.
If None, maximum sequence length would be used
Outputs:
inputs_time_major:
input sentences transformed into time-major matrix
(shape [max_time, batch_size]) padded with 0s
sequence_lengths:
batch-sized list of integers specifying amount of active
time steps in each input sequence
"""
sequence_lengths = [len(seq) for seq in inputs]
batch_size_ = len(inputs)
if max_sequence_length is None:
max_sequence_length = max(sequence_lengths)
inputs_batch_major = np.zeros(shape=[batch_size_, max_sequence_length], dtype=np.int32) # == PAD
for index, seq in enumerate(inputs):
for j, element in enumerate(seq):
inputs_batch_major[index, j] = element
# [batch_size, max_time] -> [max_time, batch_size]
inputs_time_major = inputs_batch_major.swapaxes(0, 1)
return inputs_time_major, sequence_lengths
saver.restore(sess, MODEL_LOCATION)
batch__ = []
query = get_formatted_sentence(sentence)
batch__.append(words_to_batch(query))
batch, batch_len = batch_method(batch__)
for x in range(0,10): # This is the loop I was talking about
prediction = sess.run(decoder_prediction, feed_dict={encoder_inputs: batch, encoder_inputs_length: batch_len}) # Notice that here I do not give decoder_targets ( If I understand them correctly they are the 'wanted' result. In this case I have no 'wanted' result, I just want to get a prediction
print(prediction.T)
print(batch_to_words(prediction.T[0]))
def next_feed():
encoder_inputs_, encoder_input_lengths_ = batch_method(batches_query)
decoder_targets_, _ = batch_method(
[sequence + [EOS] + [PAD] for sequence in iter(batches_response)]
)
#print(encoder_inputs_)
#print(encoder_input_lengths_)
return {
encoder_inputs: encoder_inputs_,
encoder_inputs_length: encoder_input_lengths_,
decoder_targets: decoder_targets_,
}
def batch_method(inputs, max_sequence_length=None):
"""
Args:
inputs:
list of sentences (integer lists)
max_sequence_length:
integer specifying how large should `max_time` dimension be.
If None, maximum sequence length would be used
Outputs:
inputs_time_major:
input sentences transformed into time-major matrix
(shape [max_time, batch_size]) padded with 0s
sequence_lengths:
batch-sized list of integers specifying amount of active
time steps in each input sequence
"""
sequence_lengths = [len(seq) for seq in inputs]
batch_size_ = len(inputs)
if max_sequence_length is None:
max_sequence_length = max(sequence_lengths)
inputs_batch_major = np.zeros(shape=[batch_size_, max_sequence_length], dtype=np.int32) # == PAD
for index, seq in enumerate(inputs):
for j, element in enumerate(seq):
inputs_batch_major[index, j] = element
# [batch_size, max_time] -> [max_time, batch_size]
inputs_time_major = inputs_batch_major.swapaxes(0, 1)
return inputs_time_major, sequence_lengths