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
def actor_loss_fn(self, logprobs, old_logprobs, advantages, mask): | |
## policy gradient loss | |
log_ratio = (logprobs - old_logprobs) * mask | |
ratio = torch.exp(log_ratio) | |
pg_loss1 = -advantages * ratio | |
pg_loss2 = -advantages * torch.clamp(ratio, 1.0 - self.cliprange, | |
1.0 + self.cliprange) | |
pg_loss = torch.sum(torch.max(pg_loss1, pg_loss2) * mask) / mask.sum() | |
return pg_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
# Usecase | |
def make_prompt(input_text: str, system_prompt="", max_length=512) -> str: | |
""" | |
Generates text using a large language model, given a prompt and a device. | |
Args: | |
input_text (str): The input text for prompting. | |
system_prompt (str): The system prompt (not used in the function). | |
max_length (int): The maximum length of the generated text. |
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
import random | |
import textwrap | |
import torch # You missed importing the torch module. | |
device = 'cuda' if torch.cuda.is_available() else 'cpu' | |
import textwrap | |
def text_wrapper(text, width=90): | |
# Wraps the input text to the specified width |
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
import torch | |
from datasets import load_dataset | |
from peft import LoraConfig | |
from transformers import ( | |
AutoModelForCausalLM, | |
AutoTokenizer, | |
BitsAndBytesConfig, | |
HfArgumentParser, | |
TrainingArguments, |
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
import argparse | |
import bitsandbytes as bnb | |
from datasets import load_dataset | |
from functools import partial | |
import os | |
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training, AutoPeftModelForCausalLM | |
import torch | |
from transformers import AutoModelForCausalLM, AutoTokenizer, set_seed, Trainer, TrainingArguments, BitsAndBytesConfig, \ | |
DataCollatorForLanguageModeling, Trainer, TrainingArguments | |
from datasets import load_dataset |
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
def train_one_epoch(model, optimizer, scheduler, dataloader, device, epoch): | |
model.train() | |
dataset_size = 0 | |
running_loss = 0.0 | |
bar = tqdm(enumerate(dataloader), total=len(dataloader)) | |
""" The total argument in tqdm specifies the total number of iterations (or updates to the progress bar). In this case, len(dataloader) is used as the total which is the total number of batches in the dataloader. """ | |
for step, data in bar: | |
ids = data['input_ids'].to(device, dtype = torch.long) |
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
from tensorflow import keras | |
import tensorflow as tf | |
class ChildDense(keras.layers.Layer): | |
def __init__(self, units, activation=None): | |
super().__init__() | |
self.units = units | |
self.activation = activation | |
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
from statsmodels.tsa.stattools import adfuller | |
from statsmodels.tsa.seasonal import seasonal_decompose | |
import statsmodels.api as sm | |
from sklearn.metrics import mean_squared_error, mean_absolute_error | |
import math | |
#Test for staionarity | |
def test_stationarity(timeseries): | |
#Determing rolling statistics | |
rolling_mean = timeseries.rolling(12).mean() |
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
# Now generate TPR, FPR and ROC data | |
tpr,fpr,roc_auc = ([[]]*number_of_classes for _ in range(3)) | |
f,ax = plt.subplots() | |
for i in range(number_of_classes): | |
fpr[i], tpr[i], _ = roc_curve(labels==i, predictions[:, i]) | |
roc_auc[i] = auc(fpr[i], tpr[i]) |
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
import numpy as np | |
from sklearn.metrics import roc_auc_score | |
from sklearn.metrics import roc_curve, auc | |
import matplotlib.pyplot as plt | |
#generating synthetic data | |
number_of_classes = 5 | |
samples_per_class= 70 |
NewerOlder