循环LSTM模型预测

时间:2020-09-29 19:24:16

标签: python tensorflow nlp

我很好奇如何使用预先训练的模型和权重化的权标在for循环中实现预测。

我目前每次循环给模型一个文本字符串,它可以预测情绪。我不能给它一个数据帧,因为我希望脚本实时运行,将口语转录成文本,然后返回情绪。然后继续下一个人,依此类推。

即遍历人员列表并每次调用load_model.predict(padded_text)都会导致以下警告:

我的代码有效,但是我收到了WARNING:tensorflow:7 out of the last 12 calls to <function Model.make_predict_function.<locals>.predict_function at 0x000001E5E152D670> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args and https://www.tensorflow.org/api_docs/python/tf/function for more details.

我做了一些研究,但仍然感到困惑。有人可以为我分解一下/提供潜在的解决方案。

代码:

 while len(storage) < len(jurors):
    print('Juror' + ' ' + jurors[len(storage)] + ' ' + 'is speaking:')
    init_rec = sr.Recognizer()
    with sr.Microphone() as source:
        audio_string = []
        audio_data = init_rec.adjust_for_ambient_noise(source)
        audio_data = init_rec.listen(source) #each juror speaks for 10 seconds
        audio_text = init_rec.recognize_google(audio_data)
        print('End of juror' + ' ' + jurors[len(storage)] + ' ' + 'speech')
      
        #storage of all spoken text (maybe convert to dict for key, value with juror name)
        storage.append(audio_text)
        audio_string.append(audio_text)
       
        #funtions
        cleaned = clean_text(audio_string)
        tokenized = tokenize_text(audio_string)
        padded_text = padding(audio_string, tokenized) #fix padded text elongating rows
        
        
        loaded_score = loaded_model.predict(padded_text)
        y_loaded_pred = np.argmax(loaded_score, axis = 1).reshape(-1,1)
        y_sentiment = np.vectorize(label_dict.get)(y_loaded_pred)

0 个答案:

没有答案