pad_sequences我正在致力于将语音转录为文本,并在 Keras 中使用时遇到了问题(我认为) 。我预训练了一个在数据帧上使用的模型pad_sequences,它将数据放入一个数组中,每个值的列数和行数都相同。然而,当我用于pad_sequences转录文本时,该语音字符串中的字符数就是作为 numpy 数组返回的行数。
假设我有一个包含 4 个字符的字符串,那么它将返回一个4 X 500Numpy 数组。对于 6 个字符的字符串,它将返回6 X 500Numpy 数组等。
我的澄清代码:
import speech_recognition as sr
import pyaudio
import pandas as pd
from helperFunctions import *
jurors = ['Zack', 'Ben']
storage = []
storage_df = pd.DataFrame()
while len(storage) < len(jurors):
print('Juror' + ' ' + jurors[len(storage)] + ' ' + 'is speaking:')
init_rec = sr.Recognizer()
with sr.Microphone() as source:
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.append(audio_text)
cleaned = clean_text(audio_text)
tokenized = tokenize_text(cleaned)
padded_text = padding(cleaned, tokenized) #fix padded text elongating rows
我使用辅助函数脚本:
def clean_text(text, stem=False):
text_clean = '@\S+|https?:\S|[^A-Za-z0-9]+'
text = re.sub(text_clean, ' ', str(text).lower()).strip()
#text = tf.strings.substr(text, 0, 300) #restrict text size to 300 chars
return text
def tokenize_text(text):
tokenizer = Tokenizer()
tokenizer.fit_on_texts(text)
return tokenizer
def padding(text, tokenizer):
text = pad_sequences(tokenizer.texts_to_sequences(text),
maxlen = 500)
return text
返回的文本将被输入到预先训练的模型中,我非常确定不同长度的行会导致问题。
偶然的你
相关分类