This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Libraries | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
import torch | |
# Preliminaries | |
from torchtext.data import Field, TabularDataset, BucketIterator, Iterator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') | |
# Model parameter | |
MAX_SEQ_LEN = 128 | |
PAD_INDEX = tokenizer.convert_tokens_to_ids(tokenizer.pad_token) | |
UNK_INDEX = tokenizer.convert_tokens_to_ids(tokenizer.unk_token) | |
# Fields | |
label_field = Field(sequential=False, use_vocab=False, batch_first=True, dtype=torch.float) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BERT(nn.Module): | |
def __init__(self): | |
super(BERT, self).__init__() | |
options_name = "bert-base-uncased" | |
self.encoder = BertForSequenceClassification.from_pretrained(options_name) | |
def forward(self, text, label): | |
loss, text_fea = self.encoder(text, labels=label)[:2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Save and Load Functions | |
def save_checkpoint(save_path, model, valid_loss): | |
if save_path == None: | |
return | |
state_dict = {'model_state_dict': model.state_dict(), | |
'valid_loss': valid_loss} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Training Function | |
def train(model, | |
optimizer, | |
criterion = nn.BCELoss(), | |
train_loader = train_iter, | |
valid_loader = valid_iter, | |
num_epochs = 5, | |
eval_every = len(train_iter) // 2, | |
file_path = destination_folder, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Evaluation Function | |
def evaluate(model, test_loader): | |
y_pred = [] | |
y_true = [] | |
model.eval() | |
with torch.no_grad(): | |
for (labels, title, text, titletext), _ in test_loader: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
train_loss_list, valid_loss_list, global_steps_list = load_metrics(destination_folder + '/metrics.pt') | |
plt.plot(global_steps_list, train_loss_list, label='Train') | |
plt.plot(global_steps_list, valid_loss_list, label='Valid') | |
plt.xlabel('Global Steps') | |
plt.ylabel('Loss') | |
plt.legend() | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Import Library" | |
] | |
}, | |
{ |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
OlderNewer