ver 2 pre-trained word2vec

This commit is contained in:
Łukasz Jędyk 2021-05-21 13:13:30 +02:00
parent 79d0d3491c
commit 6fb853de01
3 changed files with 1188 additions and 1187 deletions

File diff suppressed because it is too large Load Diff

13
main.py
View File

@ -42,12 +42,13 @@ X_dev = [word_tokenize(content) for content in X_dev]
X_test = [word_tokenize(content) for content in X_test]
# word2vec
word2vec = Word2Vec(X_train, vector_size=50, window=5, min_count=1)
X_train = [np.mean([word2vec.wv[word] for word in content if word in word2vec.wv] or [np.zeros(50)], axis=0) for content in X_train]
X_dev = [np.mean([word2vec.wv[word] for word in content if word in word2vec.wv] or [np.zeros(50)], axis=0) for content in X_dev]
X_test = [np.mean([word2vec.wv[word] for word in content if word in word2vec.wv] or [np.zeros(50)], axis=0) for content in X_test]
#word2vec = Word2Vec(X_train, vector_size=50, window=5, min_count=1)
word2vec = gensim.downloader.load('word2vec-google-news-300')
X_train = [np.mean([word2vec[word] for word in content if word in word2vec] or [np.zeros(300)], axis=0) for content in X_train]
X_dev = [np.mean([word2vec[word] for word in content if word in word2vec] or [np.zeros(300)], axis=0) for content in X_dev]
X_test = [np.mean([word2vec[word] for word in content if word in word2vec] or [np.zeros(300)], axis=0) for content in X_test]
model = NeuralNetwork(50, 400, 1)
model = NeuralNetwork(300, 600, 1)
criterion = torch.nn.BCELoss()
optimizer = torch.optim.SGD(model.parameters(), lr = 0.01)
@ -63,7 +64,7 @@ for epoch in range(6):
y = y_train[i:i+batch_size]
y = torch.tensor(y.astype(np.float32).to_numpy()).reshape(-1,1)
outputs = model(X)
outputs = model(X.float())
loss = criterion(outputs, y)
optimizer.zero_grad()

File diff suppressed because it is too large Load Diff