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

15
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] X_test = [word_tokenize(content) for content in X_test]
# word2vec # word2vec
word2vec = Word2Vec(X_train, vector_size=50, window=5, min_count=1) #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] word2vec = gensim.downloader.load('word2vec-google-news-300')
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_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_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] 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() criterion = torch.nn.BCELoss()
optimizer = torch.optim.SGD(model.parameters(), lr = 0.01) 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 = y_train[i:i+batch_size]
y = torch.tensor(y.astype(np.float32).to_numpy()).reshape(-1,1) y = torch.tensor(y.astype(np.float32).to_numpy()).reshape(-1,1)
outputs = model(X) outputs = model(X.float())
loss = criterion(outputs, y) loss = criterion(outputs, y)
optimizer.zero_grad() optimizer.zero_grad()
@ -85,7 +86,7 @@ with torch.no_grad():
prediction = (outputs > 0.5) prediction = (outputs > 0.5)
dev_prediction = dev_prediction + prediction.tolist() dev_prediction = dev_prediction + prediction.tolist()
for i in range(0, len(X_test), batch_size): for i in range(0, len(X_test), batch_size):
X = X_test[i:i+batch_size] X = X_test[i:i+batch_size]
X = torch.tensor(X) X = torch.tensor(X)

File diff suppressed because it is too large Load Diff