Skip to content

Instantly share code, notes, and snippets.

@luckmoon
luckmoon / top-k-top-p.py
Created May 9, 2024 08:34 — forked from thomwolf/top-k-top-p.py
Sample the next token from a probability distribution using top-k and/or nucleus (top-p) sampling
def top_k_top_p_filtering(logits, top_k=0, top_p=0.0, filter_value=-float('Inf')):
""" Filter a distribution of logits using top-k and/or nucleus (top-p) filtering
Args:
logits: logits distribution shape (vocabulary size)
top_k >0: keep only top k tokens with highest probability (top-k filtering).
top_p >0.0: keep the top tokens with cumulative probability >= top_p (nucleus filtering).
Nucleus filtering is described in Holtzman et al. (http://arxiv.org/abs/1904.09751)
"""
assert logits.dim() == 1 # batch size 1 for now - could be updated for more but the code would be less clear
top_k = min(top_k, logits.size(-1)) # Safety check
@luckmoon
luckmoon / lorenz_curve_gini_coefficient.py
Created September 20, 2023 02:31 — forked from CMCDragonkai/lorenz_curve_gini_coefficient.py
Lorenz Curve and Gini Coefficient #python
import numpy as np
import matplotlib.pyplot as plt
# ensure your arr is sorted from lowest to highest values first!
arr = np.array([1,4,6,9,100])
def gini(arr):
count = arr.size
coefficient = 2 / count
indexes = np.arange(1, count + 1)
@luckmoon
luckmoon / lorenz_curve_gini_coefficient.py
Created September 20, 2023 02:31 — forked from CMCDragonkai/lorenz_curve_gini_coefficient.py
Lorenz Curve and Gini Coefficient #python
import numpy as np
import matplotlib.pyplot as plt
# ensure your arr is sorted from lowest to highest values first!
arr = np.array([1,4,6,9,100])
def gini(arr):
count = arr.size
coefficient = 2 / count
indexes = np.arange(1, count + 1)
# Working example for my blog post at:
# http://danijar.com/variable-sequence-lengths-in-tensorflow/
import functools
import sets
import tensorflow as tf
from tensorflow.models.rnn import rnn_cell
from tensorflow.models.rnn import rnn
def lazy_property(function):
@luckmoon
luckmoon / gist:3ff508298d6ae10fac52fc4d33918499
Created February 1, 2021 07:59 — forked from glhfgg1024/gist:6d54faf29ccaf5dc7cca8034287e39e0
copy pretrained weights from "saved_model.pb" into a new model for finetuning or transer learning
import numpy as np
import tensorflow as flow
from tensorflow.python.saved_model import loader
# first, read the pretrained weights into a dictionary
variables = {}
g1 = tf.Graph()
with g1.as_default():
restore_from = 'pretrained_model/1513006564'
with tf.Session() as sess:
@luckmoon
luckmoon / tensorflow-graph-error-handling.py
Created December 15, 2020 12:03 — forked from alexwal/tensorflow-graph-error-handling.py
Example of how to handle errors in a tf.data.Dataset input pipeline
import tensorflow as tf
def create_bad_dataset(create_batches=True):
dataset = tf.data.Dataset.from_tensor_slices([1., 2., 0., 4., 8., 16.])
# Computing `tf.check_numerics(1. / 0.)` will raise an InvalidArgumentError.
if create_batches:
# Demonstrates that error handling works with map_and_batch
dataset = dataset.apply(tf.contrib.data.map_and_batch(
map_func=lambda x: tf.check_numerics(1. / x, 'error'), batch_size=2))
@luckmoon
luckmoon / 論文閱讀心得.md
Created April 7, 2020 03:14 — forked from david30907d/論文閱讀心得.md
所有讀過的論文都寫在這邊!!
@luckmoon
luckmoon / pad_packed_demo.py
Created December 5, 2019 06:53 — forked from HarshTrivedi/pad_packed_demo.py
Minimal tutorial on packing (pack_padded_sequence) and unpacking (pad_packed_sequence) sequences in pytorch.
import torch
from torch import LongTensor
from torch.nn import Embedding, LSTM
from torch.autograd import Variable
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
## We want to run LSTM on a batch of 3 character sequences ['long_str', 'tiny', 'medium']
#
# Step 1: Construct Vocabulary
# Step 2: Load indexed data (list of instances, where each instance is list of character indices)
@luckmoon
luckmoon / weight_init.py
Created October 29, 2019 04:24 — forked from jeasinema/weight_init.py
A simple script for parameter initialization for PyTorch
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
import torch
import torch.nn as nn
import torch.nn.init as init
def weight_init(m):
'''
@luckmoon
luckmoon / scroll.py
Created October 8, 2019 07:49 — forked from hmldd/scroll.py
Example of Elasticsearch scrolling using Python client
# coding:utf-8
from elasticsearch import Elasticsearch
import json
# Define config
host = "127.0.0.1"
port = 9200
timeout = 1000
index = "index"