This file contains hidden or 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
# Dealing with long docs: | |
max_length = 384 # The maximum length of a feature (question and context) | |
doc_stride = 128 # The authorized overlap between two part of the context when splitting it | |
def prepare_train_features(examples): | |
# Tokenize our examples with truncation and padding, but keep the overflows using a stride. This results | |
# in one example possible giving several features when a context is long, each of those features having a | |
# context that overlaps a bit the context of the previous feature. | |
tokenized_examples = tokenizer( | |
examples["question" ], |
This file contains hidden or 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
datasets = load_dataset("squad") | |
def visualize(datasets, datatype = 'train', n_questions=10): | |
n = len(datasets[datatype]) | |
random_questions=random.choices(list(range(n)),k=n_questions) | |
for i in random_questions: | |
print(f"Context:{datasets[datatype][i]['context']}") | |
print(f"Question:{datasets[datatype][i]['question']}") | |
print(f"Answer:{datasets[datatype][i]['answers']['text']}") | |
print(f"Answer Start in Text:{datasets[datatype][i]['answers']['answer_start']}") | |
print("-"*100) |
This file contains hidden or 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
from datasets import load_dataset, load_metric | |
import random | |
from transformers import AutoTokenizer | |
import transformers | |
from transformers import AutoModelForQuestionAnswering, TrainingArguments, Trainer | |
import torch | |
from transformers import default_data_collator | |
from transformers import AutoTokenizer, AutoModelForQuestionAnswering | |
import torch | |
tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad") |
This file contains hidden or 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
import math | |
class Complex: | |
def __init__(self, re=0, im=0): | |
self.re = re | |
self.im = im | |
def __add__(self, other): | |
# If Int or Float Added, return a Complex number where float/int is added to the real part | |
if isinstance(other, int) or isinstance(other, float): | |
return Complex(self.re + other,self.im) | |
# If Complex Number added return a new complex number having a real and complex part |
This file contains hidden or 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
import math | |
class Complex: | |
def __init__(self, re=0, im=0): | |
self.re = re | |
self.im = im | |
def __add__(self, other): | |
if isinstance(other, int) or isinstance(other, float): | |
return Complex(self.re + other,self.im) | |
elif isinstance(other, Complex): |
This file contains hidden or 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
import math | |
class Complex: | |
def __init__(self, re=0, im=0): | |
self.re = re | |
self.im = im | |
def __add__(self, other): | |
if isinstance(other, int) or isinstance(other, float): | |
return Complex(self.re + other,self.im) | |
elif isinstance(other, Complex): |
This file contains hidden or 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
import math | |
class Shape3d: | |
def __init__(self, name): | |
self.name = name | |
def surfaceArea(self): | |
pass | |
def volume(self): |
This file contains hidden or 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
import math | |
class Shape: | |
def __init__(self, name): | |
self.name = name | |
def area(self): | |
pass | |
def getName(self): |
This file contains hidden or 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
num_epochs = 5 | |
for epoch in range(num_epochs): | |
model.train() | |
for x_batch,y_batch in train_dataloader: | |
if train_on_gpu: | |
x_batch,y_batch = x_batch.cuda(), y_batch.cuda() | |
optimizer.zero_grad() | |
pred = model(x_batch) | |
loss = loss_criterion(pred, y_batch) | |
loss.backward() |
This file contains hidden or 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
# Whether to train on a gpu | |
train_on_gpu = torch.cuda.is_available() | |
print(f'Train on gpu: {train_on_gpu}')# Number of gpus | |
if train_on_gpu: | |
gpu_count = torch.cuda.device_count() | |
print(f'{gpu_count} gpus detected.') | |
if gpu_count > 1: | |
multi_gpu = True | |
else: | |
multi_gpu = False |