我是制作 RNN 的新手。我正在制作一个 LSTM 模型,它需要接受 Twitter 帖子作为文本和情绪。我对如何为模型准备预处理数据有疑问。
我正在使用 tesorflow v 2.12.0.
posts 是一组推特帖子。 情绪分为否定、新和肯定。
posts=data['post'].values.tolist()
sent_neg=np.array(data['sent_neg'])
sent_neu=np.array(data['sent_neu'])
sent_pos=np.array(data['sent_pos'])
sequences=tokenizer.texts_to_sequences(posts)
maxlen = max(len(seq) for seq in sequences)
padded_sequence = tf.keras.preprocessing.sequence.pad_sequences(sequences, truncating='post', padding='post', maxlen=maxlen)
我正在使用 VaderSentiment,它返回文本的情绪,它以 [0,1,2] 的形式出现。由于 RNN 接受顺序数据,我想知道如何将情绪添加到填充数据中。
sent_array 是一个数组,其中包含每个帖子的所有发送,其中 sent_array[0] 是帖子 [0] 的发送。
目前我一直在添加发送到这样的填充数据。
input = np.concatenate((padded_sequence, sent_array), axis=1)
尽管 colab 让我运行和训练这个模型。当我尝试通过 Flask 在我的本地主机上进行预测时,它给了我一个错误。我接受单个文本并在其上运行 vader sentiment
这是烧瓶上的代码。
` text = request.get_json()['text'] 填充=预处理(文本) sentiment_dict = sent_analysis(text) # 返回以 [0,1,2,3] 格式发送 打印(sentiment_dict) sent_arr = [sentiment_dict.values() 中的值对值] sent_arr.pop() # 不需要最后一个值
sent_neg = np.array(sent_arr[0])
sent_neu = np.array(sent_arr[1])
sent_pos = np.array(sent_arr[2])
sentiment_array = np.stack((sent_neg,sent_neu,sent_pos), axis=0)
sentiment_array = sentiment_array.reshape((1, -1))
input = np.concatenate((padded, sentiment_array), axis=1) `
这将返回以下错误,
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0
has size 248 and the array at index 1 has size 1
我想知道我是否在两端正确地预处理了我的数据。预先感谢您的任何反馈:)
如果我理解正确的话,在对
(nr. of posts, max_len)
进行标记化之后,您输入了一组具有形状的帖子。现在您想以某种方式在其输入中包含每个帖子的情绪信息。
我认为有两种方法可以做到这一点。
首先,您可以在每个标记化输入前加上其情感值,这样您将获得一个新的输入形状
(nr. of posts, 1 + max_len)
。在这里,您必须注意情绪值 [0,1,2] 不要与分词器的值重叠。所以你需要弄清楚 tokeniser 不使用哪些值。可能 [-1, -2, -3] 应该有效。
另一种选择是在嵌入输入标记后添加情感信息。在这种情况下,您可以再次在值之前加上或实际将情感值加到嵌入向量中(在这种情况下,我会将情感值重新调整为 [-1, 0, 1])。
我认为在代码中它看起来像:
temp = np.zeros((padded_sequence.shape[0], padded_sequence[1]+1)) # assumes padded_sequence has shape (n_samples, max_len)
temp[:, 1:] = padded_sequence
for i in range(temp.shape[0]):
temp[i,0] = all_sents[0] - 3
padded_sequence = temp
padded_sequence 现在在该帖子的每个序列的开头嵌入了情绪值。我这样做
-3
因为值不应该与 tokenisers 输出值重叠。
希望,这会有所帮助,我正确理解了你的问题。