當前位置: 首頁>>代碼示例>>Python>>正文


Python Word2Vec.load方法代碼示例

本文整理匯總了Python中gensim.models.word2vec.Word2Vec.load方法的典型用法代碼示例。如果您正苦於以下問題:Python Word2Vec.load方法的具體用法?Python Word2Vec.load怎麽用?Python Word2Vec.load使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在gensim.models.word2vec.Word2Vec的用法示例。


在下文中一共展示了Word2Vec.load方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: load_last

# 需要導入模塊: from gensim.models.word2vec import Word2Vec [as 別名]
# 或者: from gensim.models.word2vec.Word2Vec import load [as 別名]
def load_last(self, save_dir):
		'''Load last model from dir
		'''
		def extract_number_of_epochs(filename):
			m = re.search('_ne([0-9]+(\.[0-9]+)?)_', filename)
			return float(m.group(1))

		# Get all the models for this RNN
		file = save_dir + self._get_model_filename("*")
		file = np.array(glob.glob(file))

		if len(file) == 0:
			print('No previous model, starting from scratch')
			return 0

		# Find last model and load it
		last_batch = np.amax(np.array(map(extract_number_of_epochs, file)))
		last_model = save_dir + self._get_model_filename(last_batch)
		print('Starting from model ' + last_model)
		self.load(last_model)

		return last_batch 
開發者ID:rdevooght,項目名稱:sequence-based-recommendations,代碼行數:24,代碼來源:ltm.py

示例2: generate_block_seqs

# 需要導入模塊: from gensim.models.word2vec import Word2Vec [as 別名]
# 或者: from gensim.models.word2vec.Word2Vec import load [as 別名]
def generate_block_seqs(self):
        if self.language is 'c':
            from prepare_data import get_blocks as func
        else:
            from utils import get_blocks_v1 as func
        from gensim.models.word2vec import Word2Vec

        word2vec = Word2Vec.load(self.root+self.language+'/train/embedding/node_w2v_' + str(self.size)).wv
        vocab = word2vec.vocab
        max_token = word2vec.syn0.shape[0]

        def tree_to_index(node):
            token = node.token
            result = [vocab[token].index if token in vocab else max_token]
            children = node.children
            for child in children:
                result.append(tree_to_index(child))
            return result

        def trans2seq(r):
            blocks = []
            func(r, blocks)
            tree = []
            for b in blocks:
                btree = tree_to_index(b)
                tree.append(btree)
            return tree
        trees = pd.DataFrame(self.sources, copy=True)
        trees['code'] = trees['code'].apply(trans2seq)
        if 'label' in trees.columns:
            trees.drop('label', axis=1, inplace=True)
        self.blocks = trees

    # merge pairs 
開發者ID:zhangj111,項目名稱:astnn,代碼行數:36,代碼來源:pipeline.py

示例3: generate_block_seqs

# 需要導入模塊: from gensim.models.word2vec import Word2Vec [as 別名]
# 或者: from gensim.models.word2vec.Word2Vec import load [as 別名]
def generate_block_seqs(self,data_path,part):
        from prepare_data import get_blocks as func
        from gensim.models.word2vec import Word2Vec

        word2vec = Word2Vec.load(self.root+'train/embedding/node_w2v_' + str(self.size)).wv
        vocab = word2vec.vocab
        max_token = word2vec.syn0.shape[0]

        def tree_to_index(node):
            token = node.token
            result = [vocab[token].index if token in vocab else max_token]
            children = node.children
            for child in children:
                result.append(tree_to_index(child))
            return result

        def trans2seq(r):
            blocks = []
            func(r, blocks)
            tree = []
            for b in blocks:
                btree = tree_to_index(b)
                tree.append(btree)
            return tree
        trees = pd.read_pickle(data_path)
        trees['code'] = trees['code'].apply(trans2seq)
        trees.to_pickle(self.root+part+'/blocks.pkl')

    # run for processing data to train 
開發者ID:zhangj111,項目名稱:astnn,代碼行數:31,代碼來源:pipeline.py

示例4: get_data

# 需要導入模塊: from gensim.models.word2vec import Word2Vec [as 別名]
# 或者: from gensim.models.word2vec.Word2Vec import load [as 別名]
def get_data():
    train_vecs=np.load(storedpaths+'train_vecs.npy')
    y_train=np.load(storedpaths+'y_train.npy')
    test_vecs=np.load(storedpaths+'test_vecs.npy')
    y_test=np.load(storedpaths+'y_test.npy') 
    return train_vecs,y_train,test_vecs,y_test

# 訓練svm模型 
開發者ID:ruanyangry,項目名稱:Sentiment_Analysis_cnn_lstm_cnnlstm_textcnn_bilstm,代碼行數:10,代碼來源:sentiment_analysis_ml.py

示例5: get_predict_vecs

# 需要導入模塊: from gensim.models.word2vec import Word2Vec [as 別名]
# 或者: from gensim.models.word2vec.Word2Vec import load [as 別名]
def get_predict_vecs(string,n_dim,w2v_model_path):
    '''
    string: 輸入的句子
    n_dim: 詞向量維度
    w2v_model_path: 預訓練詞向量的模型路徑
    '''
    n_dim = n_dim
    text_w2v = Word2Vec.load(w2v_model_path)
    words=[i for i in jieba.cut(string,cut_all=False)]
    train_vecs = buildWordVector(words, n_dim,text_w2v)

    return train_vecs

# 調用訓練模型進行預測 
開發者ID:ruanyangry,項目名稱:Sentiment_Analysis_cnn_lstm_cnnlstm_textcnn_bilstm,代碼行數:16,代碼來源:sentiment_analysis_ml.py

示例6: svm_predict

# 需要導入模塊: from gensim.models.word2vec import Word2Vec [as 別名]
# 或者: from gensim.models.word2vec.Word2Vec import load [as 別名]
def svm_predict(string,trainmodelpath):
    words_vecs=get_predict_vecs(string)
    clf=joblib.load(trainmodelpath)
    result=clf.predict(words_vecs)
    
    return result

# Train model 
開發者ID:ruanyangry,項目名稱:Sentiment_Analysis_cnn_lstm_cnnlstm_textcnn_bilstm,代碼行數:10,代碼來源:sentiment_analysis_ml.py

示例7: load_pretrained_word_embeddings

# 需要導入模塊: from gensim.models.word2vec import Word2Vec [as 別名]
# 或者: from gensim.models.word2vec.Word2Vec import load [as 別名]
def load_pretrained_word_embeddings(self, embedding_path, kernel='kv'):
        trained_embeddings = OrderedDict()
        if kernel == 'gensim':
            from gensim.models.word2vec import Word2Vec

            w2v_model = Word2Vec.load(embedding_path)
            word_dict = w2v_model.wv.vocab
            for token in word_dict:
                if token not in self.word2idx:
                    continue
                trained_embeddings[token] = w2v_model[token].tolist()
                if self.word_embed_dim is None:
                    self.word_embed_dim = len(list(trained_embeddings[token]))
        elif kernel == 'kv':
            import pickle

            with open(embedding_path, 'rb') as fin:
                word_dict = pickle.load(fin)
                for token in word_dict:
                    if token not in self.word2idx:
                        continue
                    trained_embeddings[token] = word_dict[token]
                    if self.word_embed_dim is None:
                        self.word_embed_dim = len(list(trained_embeddings[token]))
        else:
            raise NotImplementedError("Not support embedding kernel {}.".format(kernel))

        filtered_tokens = trained_embeddings.keys()

        self.word2idx = OrderedDict()
        self.id2token = OrderedDict()
        for token in self.init_tokens:
            self.add_word(token, cnt=0)
        for token in filtered_tokens:
            self.add_word(token, cnt=0)

        # load embeddings
        self.word_embeddings = np.random.rand(self.word_size(), self.word_embed_dim)
        for token in self.word2idx.keys():
            if token in trained_embeddings:
                self.word_embeddings[self.get_word_idx(token)] = trained_embeddings[token] 
開發者ID:SeanLee97,項目名稱:clfzoo,代碼行數:43,代碼來源:vocab.py

示例8: load

# 需要導入模塊: from gensim.models.word2vec import Word2Vec [as 別名]
# 或者: from gensim.models.word2vec.Word2Vec import load [as 別名]
def load(self, filename):
		'''Load parameters values form a file
		'''
		self.w2v_model = Word2Vec.load(filename) 
開發者ID:rdevooght,項目名稱:sequence-based-recommendations,代碼行數:6,代碼來源:ltm.py


注:本文中的gensim.models.word2vec.Word2Vec.load方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。