我的代码的训练部分可以处理数量级为10^4
的数据,但是考虑到我的整个数据集由大约500000条注释组成,我想用更多的数据来训练它。当我用10万条评论来管理培训师时,我的记忆好像用完了。在
我的get_features
函数似乎是罪魁祸首。在
data = get_data(limit=size)
data = clean_data(data)
all_words = [w.lower() for (comment, category) in data for w in comment]
word_features = []
for i in nltk.FreqDist(all_words).most_common(3000):
word_features.append(i[0])
random.shuffle(data)
def get_features(comment):
features = {}
for word in word_features:
features[word] = (word in set(comment)) # error here
return features
# I can do it myself like this:
feature_set = [(get_features(comment), category) for
(comment, category) in data]
# Or use nltk's Lazy Map implementation which arguable does the same thing:
# feature_set = nltk.classify.apply_features(get_features, data, labeled=True)
为100,000
评论运行这个程序会占用我所有32GB的RAM,并最终在features[word] = (word in set(comment))
行出现一个Memory Error
崩溃。在
我能做些什么来缓解这个问题?
编辑:我显著地减少了特性的数量:我现在只使用前3000个最常见的单词作为特性—这显著地提高了性能(原因很明显)。我还纠正了@Marat指出的一个小错误。在
免责声明:这段代码有很多潜在的缺陷,所以我希望很少有迭代能够找到根本原因。在
参数不匹配:
次优单词查找:
^{pr2}$次优特征计算:
次优功能存储:
相关问题 更多 >
编程相关推荐