【发布时间】:2019-04-17 19:10:21
【问题描述】:
亲爱的论坛成员,您好,
我有一个包含 2000 万条随机收集的个人推文的数据集(没有两条推文来自同一个帐户)。让我将此数据集称为“通用”数据集。此外,我还有另一个“特定”数据集,其中包括从药物(阿片类药物)滥用者那里收集的 100,000 条推文。每条推文至少有一个与之关联的标签,例如阿片类药物、成瘾、过量服用、氢可酮等(最多 25 个标签)。
我的目标是使用“特定”数据集使用 Keras 训练模型,然后使用它在“通用”数据集中标记推文,以识别可能由吸毒者撰写的推文。
按照source1 和source2 中的示例,我设法构建了此类模型的简单工作版本:
from tensorflow.python import keras
import pandas as pd
import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.preprocessing import LabelBinarizer, LabelEncoder
from sklearn.metrics import confusion_matrix
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.preprocessing import text, sequence
from keras import utils
# load opioid-specific data set, where post is a tweet and tags is a single tag associated with a tweet
# how would I include multiple tags to be used in training?
data = pd.read_csv("filename.csv")
train_size = int(len(data) * .8)
train_posts = data['post'][:train_size]
train_tags = data['tags'][:train_size]
test_posts = data['post'][train_size:]
test_tags = data['tags'][train_size:]
# tokenize tweets
vocab_size = 100000 # what does vocabulary size really mean?
tokenize = text.Tokenizer(num_words=vocab_size)
tokenize.fit_on_texts(train_posts)
x_train = tokenize.texts_to_matrix(train_posts)
x_test = tokenize.texts_to_matrix(test_posts)
# make sure columns are strings
data['post'] = data['post'].astype(str)
data['tags'] = data['tags'].astype(str)
# labeling
# is this where I add more columns with tags for training?
encoder = LabelBinarizer()
encoder.fit(train_tags)
y_train = encoder.transform(train_tags)
y_test = encoder.transform(test_tags)
# model building
batch_size = 32
model = Sequential()
model.add(Dense(512, input_shape=(vocab_size,)))
model.add(Activation('relu'))
num_labels = np.max(y_train) + 1 #what does this +1 really mean?
model.add(Dense(1865))
model.add(Activation('softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(x_train, y_train, batch_size = batch_size, epochs = 5, verbose = 1, validation_split = 0.1)
# test prediction accuracy
score = model.evaluate(x_test, y_test,
batch_size=batch_size, verbose=1)
print('Test score:', score[0])
print('Test accuracy:', score[1])
# make predictions using a test set
for i in range(1000):
prediction = model.predict(np.array([x_test[i]]))
text_labels = encoder.classes_
predicted_label = text_labels[np.argmax(prediction[0])]
print(test_posts.iloc[i][:50], "...")
print('Actual label:' + test_tags.iloc[i])
print("Predicted label: " + predicted_label)
为了继续前进,我想澄清几点:
- 假设我所有的训练推文都有一个标签——阿片类药物。然后,如果我通过它传递未标记的推文,该模型是否可能只是将所有这些推文都标记为阿片类药物,因为它不知道其他任何东西?为了学习目的,我应该使用各种不同的推文/标签吗?或许,对于出于培训目的选择推文/标签有什么通用指南?
- 如何添加更多带有训练标签的列(代码中没有使用一个类似的列)?
- 一旦我训练模型并达到适当的准确性,我如何通过它传递未标记的推文以进行预测?
- 如何添加混淆矩阵?
非常感谢任何其他相关的反馈。
谢谢!
“一般”推文示例:
everybody messages me when im in class but never communicates on the weekends like this when im free. feels like that anyway lol.
i woke up late, and now i look like shit. im the type of person who will still be early to whatever, ill just look like i just woke up.
“特定”推文示例:
$2 million grant to educate clinicians who prescribe opioids
early and regular marijuana use is associated with use of other illicit drugs, including opioids
【问题讨论】:
-
举个例子,有两条推文,你会说这条推文应该被归类为吸毒者发的推文,而这不是!
-
@RahulAgarwal 我在帖子中添加了两个推文示例。我想这将是区分这两种类型的主要挑战之一,因为一般的推文可能是字面上的任何东西,不太可能包含任何特定的药物相关关键字。我的假设是 Keras 能够从写作风格、拼写、标点符号和其他特定语言的提示中学习。
标签: python machine-learning keras text-classification tweets