Skip to content

Instantly share code, notes, and snippets.

@ypeleg
ypeleg / group_kfold.py
Created August 10, 2019 12:37
StratifiedGroupedKFold
import random
import numpy as np
import pandas as pd
from collections import Counter, defaultdict
def stratified_group_k_fold(X, y, groups, k, seed=None):
labels_num = np.max(y) + 1
y_counts_per_group = defaultdict(lambda: np.zeros(labels_num))
y_distr = Counter()
@ypeleg
ypeleg / mish.py
Last active October 6, 2020 06:05
import tensorflow as tf
from tensorflow.keras.layers import Activation
from tensorflow.keras.utils import get_custom_objects
class Mish(Activation):
'''
Mish Activation Function.
.. math::
mish(x) = x * tanh(softplus(x)) = x * tanh(ln(1 + e^{x}))
Shape:
def tf_graph_wrapper(func):
"""Wraps a class method with a tf.Graph context manager"""
@wraps(func)
def wrapper(self, *args, **kwargs):
with self._graph.as_default():
return func(self, *args, **kwargs)
return wrapper
def tf_init(func):
# -*- coding: utf-8 -*-
import os
import sys
import keras
import shutil
import numpy as np
import pandas as pd
import keras.backend as K
from keras.models import Model
import os
import math
import pickle
import shutil
import joblib
import warnings
import datetime
import numpy as np
@ypeleg
ypeleg / gradient_boost.py
Created December 7, 2020 16:42
Keras GrowNet gradient boost
import tensorflow as tf
from copy import deepcopy
import tensorflow.keras.backend as K
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model, clone_model
from tensorflow.keras.layers import Input, Dropout, Dense, ReLU, BatchNormalization, Activation, Concatenate
class DynamicNet(object):
def __init__(self, c0 = None, lr = None, concat_input=False, additive_boosting=False, encoder_layers=None):
@ypeleg
ypeleg / perceiver.py
Created April 30, 2021 06:48
Minimal keras implementation: "Perceiver: General Perception with Iterative Attention. Jaegle et al"
# Cleaned and minimal perceiver transformer, originally from code https://github.com/Rishit-dagli/Perceiver
# Original paper: Perceiver: General Perception with Iterative Attention. Jaegle et al. https://arxiv.org/pdf/2103.03206.pdf.
import math
import tensorflow as tf
from typing import Callable
from einops import rearrange, repeat
import json
import pickle
import struct
import zipfile
import numpy as np
from sentencepiece import SentencePieceProcessor
def rms_norm(x): return (x / np.sqrt(np.square(x).mean(-1, keepdims=True) + 1e-6))
def softmax(x): return (np.exp(x - np.max(x, axis=-1, keepdims=True))) / np.sum((np.exp(x - np.max(x, axis=-1, keepdims=True))), axis=-1, keepdims = True)
import llama
import torch
import pandas as pd
from torch.utils.data import Dataset, random_split
from transformers import TrainingArguments, Trainer
MODEL = 'decapoda-research/llama-7b-hf'
DATA_FILE_PATH = 'elon_musk_tweets.csv'
@ypeleg
ypeleg / rlaif.py
Created July 31, 2023 16:30
RLAIF
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained('NousResearch/Nous-Hermes-Llama2-13b', device_map = 'auto')
tokenizer = AutoTokenizer.from_pretrained('NousResearch/Nous-Hermes-Llama2-13b')
model.eval()
print(tokenizer('yes')) # [1, 4871]
print(tokenizer.decode(4874)) # yes