Skip to content

Instantly share code, notes, and snippets.

View MLWhiz's full-sized avatar
🤓
Focusing

Rahul Agarwal MLWhiz

🤓
Focusing
View GitHub Profile
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)
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")
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
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):
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):
import math
class Shape3d:
def __init__(self, name):
self.name = name
def surfaceArea(self):
pass
def volume(self):
@MLWhiz
MLWhiz / Shapes.py
Last active November 24, 2020 16:05
import math
class Shape:
def __init__(self, name):
self.name = name
def area(self):
pass
def getName(self):
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()
# 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
class CustomNLLLoss(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x, y):
# x should be output from LogSoftmax Layer
log_prob = -1.0 * x
# Get log_prob based on y class_index as loss=-mean(ylogp)
loss = log_prob.gather(1, y.unsqueeze(1))
loss = loss.mean()
return loss