我试着用两种类别的文本分类。通常,我会创建经过训练的模型的Pickle文件,并在培训阶段加载这些泡菜,以消除再培训。
当我每堂课有12000篇评论+ 50000多条推特时,培训模式的大小达到1.4GB。
现在,将这个大型模型数据存储到Pickle中并加载它是不可行的,也是不可取的。
除了这种情况,还有什么更好的选择吗?
下面是示例代码,我尝试了多种选择方法,这里我使用了dill包
def train(self):
global pos, neg, totals
retrain = False
# Load counts if they already exist.
if not retrain and os.path.isfile(CDATA_FILE):
# pos, neg, totals = cPickle.load(open(CDATA_FILE))
pos, neg, totals = dill.load(open(CDATA_FILE, 'r'))
return
for file in os.listdir("./suspected/"):
for word in set(self.negate_sequence(open("./unsuspected/" + file).read())):
neg[word] += 1
pos['not_' + word] += 1
for file in os.listdir("./suspected/"):
for word in set(self.negate_sequence(open("./suspected/" + file).read())):
pos[word] += 1
neg['not_' + word] += 1
self.prune_features()
totals[0] = sum(pos.values())
totals[1] = sum(neg.values())
countdata = (pos, neg, totals)
dill.dump(countdata, open(CDATA_FILE, 'w') )更新:大泡菜背后的原因是,分类数据非常大。我已经考虑了1-4克的特征选择。分类数据集本身约为300 so,因此,考虑多尺度特征选择方法,产生了较大的训练模型。
发布于 2016-03-17 07:12:03
泡菜作为一种格式非常沉重。它存储对象的所有细节。更好的方法是以高效的格式存储数据,比如hdf5。如果您不熟悉hdf5,可以查看如何将数据存储在一个简单的平面文本文件中。您可以根据数据结构使用csv或json。你会发现这两种方法都比泡菜更有效。
您可以查看gzip来创建和加载压缩档案。
发布于 2020-06-21 18:13:55
对这里的问题和解决方案进行了说明。简而言之,问题在于,在进行特性化(例如使用CountVectorizer )时,尽管您可能需要少量的特性(例如,max_features=1000 ),但转换器仍然保留了所有可能的特性的副本,以便进行调试。例如,CountVectorizer具有以下属性:
stop_words_ : set
Terms that were ignored because they either:
- occurred in too many documents (max_df)
- occurred in too few documents (min_df)
- were cut off by feature selection (max_features).
This is only available if no vocabulary was given.这会导致模型尺寸变得太大。要解决这个问题,您可以在选择模型之前将stop_words_设置为None (取自上述链接的示例):(有关详细信息,请查看链接在上面 )。
import pickle
model_name = 'clickbait-model-sm.pkl'
cfr_pipeline.named_steps.vectorizer.stop_words_ = None
pickle.dump(cfr_pipeline, open(model_name, 'wb'), protocol=2)https://stackoverflow.com/questions/36053594
复制相似问题