gpt4 book ai didi

python - 如何将字符串值传递给情感分析 RNN 序列模型并获取预测

转载 作者:行者123 更新时间:2023-11-30 09:45:39 24 4
gpt4 key购买 nike

我使用自己的数据集重新创建了一个情感分析机器学习项目,并进行了一些小的修改以提高其完成时间,我可以创建良好的模型,编译它,拟合它并测试它,没有任何问题,但问题在于如何向模型传递一个新的字符串/文章,然后它返回对字符串评论是正面还是负面的预测,并希望有人可以帮助我。

我在下面发布了我的代码供您审阅。

class tensor_rnn():
def __init__(self, corp_paths, hidden_layers=3, loadfile=True):
self.h_layers = hidden_layers
self.num_words = []
if loadfile == False:
data_set = pd.DataFrame(columns=['Article', 'Polarity'])
craptopass = []
for files in os.listdir(corp_paths[0]):
with open(corp_paths[0] + '\\' + files, 'r', errors='replace') as text_file:
line = text_file.readline().replace('|', '')
text_file.close()
if len(line.split(' ')) > 3:
line = ''.join([i if ord(i) < 128 else ' ' for i in line])
craptopass.append([line, 1])
good = data_set.append(pd.DataFrame(craptopass, columns=['Article', 'Polarity']), ignore_index=True)
data_set = pd.DataFrame(columns=['Article', 'Polarity'])
craptopass = []
for files in os.listdir(corp_paths[1]):
with open(corp_paths[1] + '\\' + files, 'r', errors='replace') as text_file:
line = text_file.readline().replace('|', '')
text_file.close()
if len(line.split(' ')) > 3:
line = ''.join([i if ord(i) < 128 else ' ' for i in line])
craptopass.append([line, -1])
bad = data_set .append(pd.DataFrame(craptopass, columns=['Article', 'Polarity']), ignore_index=True)
for line in good['Article'].tolist():
counter = len(line.split())
self.num_words.append(counter)

for line in bad['Article'].tolist():
counter = len(line.split())
self.num_words.append(counter)
self.features = pd.concat([good, bad]).reset_index(drop=True)
# self.features = self.features.str.replace(',', '')
self.features.to_csv('Headlines.csv', sep='|')
else:
self.features = pd.read_csv('Headlines.csv', sep='|')
self.features['totalwords'] = self.features['Article'].str.count(' ') + 1
self.num_words.extend(self.features['totalwords'].tolist())

self.features = shuffle(self.features)
self.max_len = len(max(self.features['Article'].tolist()))
tokenizer = self.tok = preprocessing.text.Tokenizer(num_words=len(self.num_words), split=' ')
self.tok.fit_on_texts(self.features['Article'].values)
X = tokenizer.texts_to_sequences(self.features['Article'].values)
self.X = preprocessing.sequence.pad_sequences(X)
self.Y = pd.get_dummies(self.features['Polarity']).values
self.X_train, self.X_test, self.Y_train, self.Y_test = train_test_split(self.X, self.Y,
test_size=0.20, random_state=36)

def RNN(self):
embed_dim = 128
lstm_out = 128
model = Sequential()
model.add(Embedding(len(self.num_words), embed_dim, input_length=self.X.shape[1]))
model.add(Bidirectional(CuDNNLSTM(lstm_out)))
model.add(Dropout(0.2))
model.add(Dense(2, activation='softmax'))
opt = Adam(lr=0.0001, decay=1e-4) #1e-3
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
return model

def model_train(self):
self.model = self.RNN()

def model_test(self):
batch_size = 128
self.model.fit(self.X_train, self.Y_train, epochs=4, batch_size=batch_size, verbose=2,
callbacks=[EarlyStopping(monitor='val_loss', min_delta=0.0001,
patience=5, verbose=2, mode='auto')], validation_split=0.2)


if __name__ == "__main__":
paths = 'PATHS TO ARTICLES'
a = tensor_rnn([paths + '\\pos', paths + '\\neg'])
a.model_train()
a.model_test()
a.model.save('RNNModelArticles.h5', include_optimizer=True)

最佳答案

您所需要做的就是预处理要提供给模型的新文本,就像预处理训练文本一样。之后,您应该有一个预测方法,该方法将以与模型在训练中输出预测相同的方式输出其预测。因此,在预测方法中,您应该编写如下内容:

def predict(self, sequence):
presprocessed = preprocess(sequence)
prediction = self.model.predict(preprocessed, batch_size=None, verbose=0, steps=None)

这是否让你明白了一些事情?

关于python - 如何将字符串值传递给情感分析 RNN 序列模型并获取预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52942038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com