Skip to content
Snippets Groups Projects
Commit dccb5afb authored by kreuzer's avatar kreuzer
Browse files

Merge branch 'master' into 'kreuzer-master-patch-32167'

# Conflicts:
#   scripts/ActorOnly+CEL/main_ActorOnly.py
#   scripts/ActorOnly+CEL/main_CrossEntropy.py
parents 35c13e27 18b12664
No related branches found
No related tags found
No related merge requests found
import torch
import stanza
from gensim.models import KeyedVectors
from datasets import load_dataset
from structures import *
# loads skipgram gensim
file_name = "data/1-billion-word-language-modeling-benchmark-r13output.word2vec.vec"
model_gensim = KeyedVectors.load_word2vec_format(file_name)
# initialize tokenizer, => sentences splitting and tokenizing, !pip install stanza
nlp = stanza.Pipeline(lang='en', processors='tokenize')
# loads dataset cnn_dailymail, !pip install datasets
dataset = load_dataset('ccdv/cnn_dailymail', '3.0.0', split='test[:5]') # extract subset for testing
# update path
x = PreprocessedDataSet('test5', dataset, model_gensim, nlp)
......@@ -3,6 +3,8 @@ import torch
from torch import nn
import numpy as np
import utils #
class SummarisationModel(nn.Module):
......
import torch
import numpy as np
import matplotlib.pyplot as plt
import pathlib
hist_path = pathlib.Path('hist')
actor_only_hist = torch.load(hist_path/'model_actor_only_val_rouge_hist.pt')
CE_hist = torch.load(hist_path/'model_CE_val_rouge_hist.pt')
hist = torch.rand(10)
scratch_hist = torch.rand(10)
ahist = torch.Tensor(actor_only_hist)
chist = torch.Tensor(CE_hist)
num_epochs = 10
num_epochs = 2
ohist = []
shist = []
ohist = [h.cpu().numpy() for h in hist]
shist = [h.cpu().numpy() for h in scratch_hist]
ohist = [h.cpu().numpy() for h in ahist]
shist = [h.cpu().numpy() for h in chist]
plt.title("Validation Accuracy vs. Number of Training Epochs")
plt.title("Validation Rouge vs. Number of Training Epochs")
plt.xlabel("Training Epochs")
plt.ylabel("Validation Accuracy")
plt.plot(range(1,num_epochs+1),ohist,label="Pretrained")
plt.plot(range(1,num_epochs+1),shist,label="Scratch")
plt.ylim((0,1.))
plt.ylabel("Validation Rouge")
plt.plot(range(1,num_epochs+1),ohist,label="Actor Only")
plt.plot(range(1,num_epochs+1),shist,label="Cross Entropy")
plt.ylim((min(actor_only_hist + CE_hist), max(actor_only_hist + CE_hist)))
plt.xticks(np.arange(1, num_epochs+1, 1.0))
plt.legend()
plt.show()
......@@ -3,27 +3,28 @@ from torch import nn
import numpy as np
from structures import *
from models import *
import utils
import time
# loads dataset cnn_dailymail
train_dataset = PreprocessedDataSet('/workspace/students/kreuzer/train')
validation_dataset = PreprocessedDataSet('/workspace/students/kreuzer/val')
test_dataset = PreprocessedDataSet('/workspace/students/kreuzer/test')
# loads dataset after preprocessing
train_data = PreprocessedDataSet('/workspace/students/kreuzer/train')
val_data = PreprocessedDataSet('/workspace/students/kreuzer/val')
test_data = PreprocessedDataSet('/workspace/students/kreuzer/test')
# hyperparameters
epochs=20
batch_size=20
epochs=20 # 20
batch_size=20 # 20
learning_rate=0.001
training_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, collate_fn=lambda x:x)
train_dataloader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, shuffle=True, collate_fn = lambda x:x) # collate_fn
#test_dataloader = torch.utils.data.DataLoader(test_data, batch_size=batch_size, shuffle=True, collate_fn = lambda x: x)
m = ActorOnlySummarisation()
m = ActorOnlySummarisationModel() # tip error fix!
since = time.time()
val_rouge_history = []
best_rouge = 0.0
best_model_wts = copy.deepcopy(m.state_dict())
best_model_wts = copy.deepcopy(m.state_dict())
for epoch in range(epochs):
......@@ -32,12 +33,13 @@ for epoch in range(epochs):
print('-' * 10)
# train phase
epoch_loss, epoch_rouge = m.training_epoch(training_dataloader)
epoch_loss, epoch_rouge = m.training_epoch(train_dataloader) # train_dataloader
print('Train Loss: {:.4f} Rouge Score: {:.4f}'.format(epoch_loss, epoch_rouge))
# validation phase
epoch_rouge = m.validation(validation_dataset)
val_rouge_history.append(epoch_rouge)
epoch_rouge = m.validation(val_data) #
val_rouge_history.append(epoch_rouge)
print('Validation Rouge Score: {:.4f}'.format(epoch_rouge))
# epoch completed, deep copy the best model sofar
......@@ -51,11 +53,9 @@ print('Training + validation complete in {:.0f}m {:.0f}s'.format(time_elapsed //
print('Best val rouge: {:4f}'.format(best_rouge))
# write val_rouge_history in file
with open("ActorOnly_val_history.txt", "a") as f:
f.write(">>>Start\n")
for event in val_rouge_history:
f.write(str(event)+"\n")
f.write("\n")
pathlib.Path('hist').mkdir(parents=True, exist_ok=True)
hist_path = pathlib.Path('hist')
torch.save(val_rouge_history, hist_path/'model_actor_only_val_rouge_hist.pt') # all hist in one folder hist?
# load best model weights
m.load_state_dict(best_model_wts)
......@@ -63,15 +63,14 @@ m.load_state_dict(best_model_wts)
# for Critic: save model_actor_only
torch.save(m.state_dict(), 'model_actor_only_wts.pth')
# set sent_vecs
train_dataset.compute_sent_vecs(m)
validation_dataset.compute_sent_vecs(m)
test_dataset.compute_sent_vecs(m)
train_data.compute_sent_vecs(m) #
val_data.compute_sent_vecs(m)
test_data.compute_sent_vecs(m)
# testing
since = time.time()
epoch_rouge_1, epoch_rouge_2, epoch_rouge_l = m.test(test_dataset)
epoch_rouge_1, epoch_rouge_2, epoch_rouge_l = m.test(test_data)
print('Test rouge_1: {:.4f} rouge_2: {:.4f} rouge_l: {:.4f} mean: {:.4f}'.format(epoch_rouge_1, epoch_rouge_2, epoch_rouge_l, (epoch_rouge_1+epoch_rouge_2+epoch_rouge_l)/3.0))
# after testing completed
......
......@@ -3,22 +3,23 @@ from torch import nn
import numpy as np
from structures import *
from models import *
import utils
import time
# loads dataset cnn_dailymail
train_dataset = PreprocessedDataSet('/workspace/students/kreuzer/train')
validation_dataset = PreprocessedDataSet('/workspace/students/kreuzer/val')
test_dataset = PreprocessedDataSet('/workspace/students/kreuzer/test')
train_data = PreprocessedDataSet('/workspace/students/kreuzer/train')
validation_data = PreprocessedDataSet('/workspace/students/kreuzer/val')
test_data = PreprocessedDataSet('/workspace/students/kreuzer/test')
# hyperparameters
epochs=20
batch_size=20
batch_size=50 # 5
learning_rate=0.001
training_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, collate_fn=lambda x:x)
train_dataloader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, shuffle=True, collate_fn=lambda x:x)
m = SummarisationModelWithCrossEntropyLoss()
# training
since = time.time()
val_rouge_history = []
......@@ -27,7 +28,6 @@ best_model_wts = copy.deepcopy(m.state_dict())
for epoch in range(epochs):
print()
print('Epoch {}/{}'.format(epoch+1, epochs))
print('-' * 10)
......@@ -36,7 +36,8 @@ for epoch in range(epochs):
print('Train Loss: {:.4f}'.format(epoch_loss))
# validation phase
epoch_rouge = m.validation(validation_dataset)
epoch_rouge = m.validation(val_data)
val_rouge_history.append(epoch_rouge)
print('Validation Rouge Score: {:.4f}'.format(epoch_rouge))
......@@ -51,11 +52,9 @@ print('Training + validation complete in {:.0f}m {:.0f}s'.format(time_elapsed //
print('Best val rouge: {:4f}'.format(best_rouge))
# write val_rouge_history in file
with open("CEL_val_history.txt", "a") as f:
f.write(">>>Start\n")
for event in val_rouge_history:
f.write(str(event)+"\n")
f.write("\n")
pathlib.Path('hist').mkdir(parents=True, exist_ok=True)
hist_path = pathlib.Path('hist')
torch.save(val_rouge_history, hist_path/'model_CE_val_rouge_hist.pt')
# load best model weights
m.load_state_dict(best_model_wts)
......@@ -63,7 +62,8 @@ m.load_state_dict(best_model_wts)
# testing
since = time.time()
epoch_rouge_1, epoch_rouge_2, epoch_rouge_l = m.test(test_dataset)
epoch_rouge_1, epoch_rouge_2, epoch_rouge_l = m.test(test_data)
print('Test rouge_1: {:.4f} rouge_2: {:.4f} rouge_l: {:.4f} mean: {:.4f}'.format(epoch_rouge_1, epoch_rouge_2, epoch_rouge_l, (epoch_rouge_1+epoch_rouge_2+epoch_rouge_l)/3.0))
# after testing completed
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment