开发者

Using Pickle object like an API call

开发者 https://www.devze.com 2023-02-22 17:29 出处:网络
I trained a NaiveBayes classifier t开发者_如何学编程o do elementary sentiment analysis. The model is 208MB . I want to load it only once and then use Gearman workers to keep calling the model to get t

I trained a NaiveBayes classifier t开发者_如何学编程o do elementary sentiment analysis. The model is 208MB . I want to load it only once and then use Gearman workers to keep calling the model to get the results. It takes rather long time to load it only once. How do i load the model only once and then keep calling it ?

Some code , hope this helps :

import nltk.data
c=nltk.data.load("/path/to/classifier.pickle")

This remains as the loader script.

Now i have a gearman worker script which should call this "c" object and then classify the text.

c.classify('features')

This is what i want to do . Thanks.


If the question is how to use pickle, than that's the answer

    import pickle
    class Model(object):
        #some crazy array of data
        def getClass(sentiment)
            #return class of sentiment

    def loadModel(filename):
        f = open(filename, 'rb')
        res = pickle.load(f)
        f.close()
        return res

    def saveModel(model, filename):
        f = open(filename, 'wb')
        pickle.dump(model, f)
        f.close()
    m = loadModel('bayesian.pickle')

if it's a problem to load large object in such a way, than I don't know whether pickle could help

0

精彩评论

暂无评论...
验证码 换一张
取 消