python - 使用 Keras fit_generator 训练后保存的模型无法正确给出预测
问题描述
我fit_generator
用来训练翻译模型。生成器代码添加在下面。
My_Generator 类(序列):
def __init__(self, X_data, Y_data, batch_size):
self.X_data, self.Y_data = X_data, Y_data
#print(X_data)
self.batch_size = batch_size
def __len__(self):
print(int(ceil(len(self.X_data) / float(self.batch_size))))
return int(ceil(len(self.X_data) / float(self.batch_size)))
def __getitem__(self, idx):
#print('hey')
batch_x = self.X_data[idx * self.batch_size:(idx + 1) * self.batch_size]
batch_y = self.Y_data[idx * self.batch_size:(idx + 1) * self.batch_size]
hindi_data= encode_sequences(hin_tokenizer,batch_x,hin_max_len_sent)
eng_data = encode_sequences(eng_tokenizer,batch_y,eng_max_len_sent)
output = encode_output(eng_data,eng_vocab_size)
return array(hindi_data),array(output)
我已经训练了 30 个 epochsval_loss: 0.5665
和val_acc: 0.9268
.
预测代码:
def predict_sequence(model, tokenizer, source):
prediction = model.predict(source, verbose=0)[0]
integers = [argmax(vector) for vector in prediction]
target = list()
for i in integers:
word = word_for_id(i, tokenizer)
print('target ' ,word)
if word is None:
break
target.append(word)
return ' '.join(target)
保存的模型没有为目标语言提供正确的预测。我得到 0 的 bleu。我也尝试过使用predict_generator
。但这似乎也不起作用。对此的任何帮助表示赞赏。
解决方案
推荐阅读
- angular - Fullcalendar - 角度营业时间未显示
- javascript - I don't understand why my for loop return "undefined"
- python - 如何从子集中选择数据
- oop - 如何动态生成用于特征的值?
- python - Selenium 无法在 Instagram 上获取所有元素
- compiler-errors - 为什么临时变量会抑制“类型归属是实验性的”错误?
- ios - 如何动态更改本地化 SwiftUI
- android - 有没有办法让我们按钮 onclick 监听器在 android 上打开导航抽屉
- php - 如何在几天的数组中查询每一天的数据库laravel
- mysql - 这是一个 MySQL 错误吗?