cleanup code

This commit is contained in:
Maciej Sobkowiak 2021-05-26 04:14:32 +02:00
parent 63d362dc73
commit 04f2c0389d
3 changed files with 1112 additions and 1114 deletions

File diff suppressed because it is too large Load Diff

104
main.py
View File

@ -6,6 +6,8 @@ from gensim import downloader
from nltk.tokenize import word_tokenize from nltk.tokenize import word_tokenize
import csv import csv
BATCH_SIZE = 5
class NeuralNetworkModel(torch.nn.Module): class NeuralNetworkModel(torch.nn.Module):
@ -45,75 +47,71 @@ def read_data():
return x_labels, y_labels, x_train, y_train, x_dev, x_test return x_labels, y_labels, x_train, y_train, x_dev, x_test
x_labels, y_labels, x_train, y_train, x_dev, x_test = read_data() def process_data(x_labels, y_labels, x_train, y_train, x_dev, x_test):
x_train = x_train[x_labels[0]].str.lower()
x_dev = x_dev[x_labels[0]].str.lower()
x_test = x_test[x_labels[0]].str.lower()
y_train = y_train[y_labels[0]]
x_train = x_train[x_labels[0]].str.lower() x_train = [word_tokenize(x) for x in x_train]
x_dev = x_dev[x_labels[0]].str.lower() x_dev = [word_tokenize(x) for x in x_dev]
x_test = x_test[x_labels[0]].str.lower() x_test = [word_tokenize(x) for x in x_test]
y_train = y_train[y_labels[0]]
x_train = [word_tokenize(x) for x in x_train] w2v = downloader.load('glove-wiki-gigaword-200')
x_dev = [word_tokenize(x) for x in x_dev]
x_test = [word_tokenize(x) for x in x_test]
w2v = downloader.load('glove-wiki-gigaword-200') x_train = [np.mean([w2v[w] for w in d if w in w2v] or [
np.zeros(200)], axis=0) for d in x_train]
x_dev = [np.mean([w2v[w] for w in d if w in w2v]
or [np.zeros(200)], axis=0) for d in x_dev]
x_test = [np.mean([w2v[w] for w in d if w in w2v]
or [np.zeros(200)], axis=0) for d in x_test]
x_train = [np.mean([w2v[word] for word in doc if word in w2v] or [ return x_train, y_train, x_dev, x_test
np.zeros(200)], axis=0) for doc in x_train]
x_dev = [np.mean([w2v[word] for word in doc if word in w2v]
or [np.zeros(200)], axis=0) for doc in x_dev]
x_test = [np.mean([w2v[word] for word in doc if word in w2v]
or [np.zeros(200)], axis=0) for doc in x_test]
nn_model = NeuralNetworkModel()
BATCH_SIZE = 5
criterion = torch.nn.BCELoss()
optimizer = torch.optim.SGD(nn_model.parameters(), lr=0.1)
for epoch in range(5): def predict(model, x_data, out_path):
nn_model.train() y_out = []
for i in range(0, y_train.shape[0], BATCH_SIZE): model.eval()
X = x_train[i:i+BATCH_SIZE] with torch.no_grad():
X = torch.tensor(X) for i in range(0, len(x_data), BATCH_SIZE):
Y = y_train[i:i+BATCH_SIZE]
Y = torch.tensor(Y.astype(np.float32).to_numpy()).reshape(-1, 1)
Y_predictions = nn_model(X.float()) x = x_data[i:i+BATCH_SIZE]
x = torch.tensor(x)
pred = nn_model(x.float())
loss = criterion(Y_predictions, Y) y_pred = (pred > 0.5)
optimizer.zero_grad() y_out.extend(y_pred)
loss.backward()
optimizer.step()
y_dev = [] y_data = np.asarray(y_out, dtype=np.int32)
y_test = [] pd.DataFrame(y_data).to_csv(out_path, sep='\t', index=False, header=False)
nn_model.eval()
with torch.no_grad(): if __name__ == "__main__":
for i in range(0, len(x_dev), BATCH_SIZE):
X = x_dev[i:i+BATCH_SIZE]
X = torch.tensor(X)
outputs = nn_model(X.float()) x_labels, y_labels, x_train, y_train, x_dev, x_test = read_data()
y = (outputs > 0.5) x_train, y_train, x_dev, x_test = process_data(
y_dev.extend(y) x_labels, y_labels, x_train, y_train, x_dev, x_test)
for i in range(0, len(x_test), BATCH_SIZE): nn_model = NeuralNetworkModel()
X = x_test[i:i+BATCH_SIZE] criterion = torch.nn.BCELoss()
X = torch.tensor(X) optimizer = torch.optim.SGD(nn_model.parameters(), lr=0.1)
outputs = nn_model(X.float()) for epoch in range(5):
nn_model.train()
y = (outputs > 0.5) for i in range(0, y_train.shape[0], BATCH_SIZE):
y_test.extend(y) X = x_train[i:i+BATCH_SIZE]
X = torch.tensor(X)
Y = y_train[i:i+BATCH_SIZE]
Y = torch.tensor(Y.astype(np.float32).to_numpy()).reshape(-1, 1)
y_dev = np.asarray(y_dev, dtype=np.int32) Y_predictions = nn_model(X.float())
y_test = np.asarray(y_test, dtype=np.int32)
Y_dev = pd.DataFrame({'label': y_dev}) loss = criterion(Y_predictions, Y)
Y_test = pd.DataFrame({'label': y_test}) optimizer.zero_grad()
loss.backward()
optimizer.step()
Y_dev.to_csv(r'dev-0/out.tsv', sep='\t', index=False, header=False) predict(nn_model, x_dev, 'dev-0/out.tsv')
Y_test.to_csv(r'test-A/out.tsv', sep='\t', index=False, header=False) predict(nn_model, x_test, 'test-A/out.tsv')

File diff suppressed because it is too large Load Diff