Как встроить ваш Dataframe, используя уже обученную модель с Gensim (GoogleNews-vectors-absolute300.bin) - PullRequest
0 голосов
/ 20 февраля 2020

Я следую этому учебнику , в котором у меня есть следующий набор данных из Quora:

enter image description here

Здесь я уже очистил и токенизировать данные в столбце q1_clean & q1_clean .

Теперь я обучил W2vModel с использованием предварительно обученной модели GoogleNews со следующим кодом.

# We are concating the two columns of Question1 and Question2

nData = pd.Series(pd.concat([data['q1_clean'], data['q2_clean']]))
model_w2v = Word2Vec(nData, size=300) 

# step 2: intersect the initialized word2vec model with the pre-trained fasttext model
model_w2v.intersect_word2vec_format('GoogleNews-vectors-negative300.bin',lockf=1.0,binary=True)

# step 3: improve model with transfer-learning using the training data
model_w2v.train(nData, total_examples=model_w2v.corpus_count, epochs= 10)

Теперь мне нужно выполнить анализ объектов, для этого у меня есть следующая функция, чтобы получить среднее вычисленное расстояние.

def get_pairwise_distance(word1, word2, weight1, weight2, method = 'euclidean'):
    if(word1.size==0 or word2.size==0):
        return np.nan
    dist_matrix = pairwise_distances(word1, word2, metric=method)
    return np.average(dist_matrix, weights=np.matmul(weight1.reshape(-1,1),weight2.reshape(-1,1).T))

Здесь я вычислил tfidf для использования в качестве веса:

X_train_tokens = get_tokenized_questions(data=X_train)

from sklearn.feature_extraction.text import TfidfVectorizer
pass_through = lambda x:x
tfidf = TfidfVectorizer(analyzer=pass_through)
# compute tf-idf weights for the words in the training set questions
X_tfidf = tfidf.fit_transform(X_train_tokens)

# split into two
# X1_tfidf -> tf-idf weights of first question in question pair and 
# X2_tfidf -> tf-idf weights of second question in question pair
X1_tfidf = X_tfidf[:len(X_train)]
X2_tfidf = X_tfidf[len(X_train):]

и я вызываю эту get_pairwise_distance функцию, как в учебнике .

#cosine similarities
# here X1 and X2 are the embedded versions of the first and second questions in the question-pair data
# and X1_tfidf and X2_tfidf are the tf-idf weights of the first and second questions in the question-pair data

cosine = compute_pairwise_dist(X1, X2, X1_tfidf, X2_tfidf)

Для этой функции мне нужно передать встроенную версию q1_clean и q2_clean как X1 и X2, где веса уже вычислены с использованием TFIDF. и я не понимаю, как встроить эти два столбца в векторы, используя предварительно обученную модель, и передать ее данной функции?

1 Ответ

1 голос
/ 21 февраля 2020

Вы можете использовать Keras Embedded Matrix. Перейдите по ссылке ниже. Вложенные слои Keras

...