Skip to content

Instantly share code, notes, and snippets.

View xhluca's full-sized avatar
💭
🔨

Xing Han Lu xhluca

💭
🔨
View GitHub Profile
@xhluca
xhluca / jax_topk_vs_np_argpartition.py
Last active June 5, 2024 21:18
Jax.lax's top_k vs np.argpartition for selecting topk results. Thanks: Andreas Madsen for original code & jax suggestions
import numpy as np
import scipy.sparse as sp
import jax.lax
import timeit
def random_sparse_matrix(n_rows, n_cols, num_elements, seed=0):
# Create a random scipy sprase matrix by randomly assigning values and constructing the matrix
np.random.seed(seed)
@xhluca
xhluca / benchmark_pyserini.py
Created April 2, 2024 23:35
Benchmark Pyserini on BEIR, printing speed and scores
import json
from pathlib import Path
import time
from typing import Optional
import warnings
from functools import partial
import subprocess
import json
from pathlib import Path
import sys
@xhluca
xhluca / curry.py
Last active September 7, 2023 20:24
Pythonn currying with functools partial
from functools import partial
def curry(func):
def curried(*args, **kwargs):
if args or kwargs:
return curry(partial(func, *args, **kwargs))
else:
return func()
return curried
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@xhluca
xhluca / dynamically_create_python_functions.md
Last active August 2, 2021 21:28
Here's how to dynamically create functions in Python (with doc string and updated names)

How to dynamically create functions in Python

Context

I was interested in creating functions without having to manually define them. This is useful if we want to automatically create a large number of functions, for instance inside a for loop and using a builder function. Unfortunately I wasn't able to find a canonical example of this.

This should cover everything need for the dynamic creation.

Example - Simple add function

import json
from typing import List
try:
from sklearn.decomposition import TruncatedSVD
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
except:
error_msg = (
"Couldn't import scikit-learn. To use the toy models, you will need to "
See full code

from base64 import b64encode
import dash
import dash_table
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output
import numpy as np
import pandas as pd
/* monochrome theme */
/* Colors */
/* Typography */
/* Borders */
/* Shadows */
/* Layout */
body {
user-select: none;
background: #000;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;