Skip to content

Instantly share code, notes, and snippets.

View vene's full-sized avatar
🏴
ahoy

Vlad Niculae vene

🏴
ahoy
View GitHub Profile
@vene
vene / multi_mixins.py
Last active August 17, 2021 07:39
python multiple inheritance / mixin MRO
class Base:
def say(self, val):
print("base says", val)
class A(Base):
def say(self, val):
print("say A")
@vene
vene / pardalos_kovoor.py
Last active May 5, 2021 17:53
Pardalos & Kovoor's O(n) solver for singly-constrained bounded QPs
# Author: Vlad Niculae <vlad@vene.ro>
# License: Simplified BSD
import numpy as np
try:
from numba import jit
except ImportError:
print("numba not available")
def jit(nopython):
@vene
vene / magic_memit.py
Created June 30, 2012 06:55
memit: magic memory usage benching for IPython
# Author: Vlad Niculae <vlad@vene.ro>
# Makes use of memory_profiler from Fabian Pedregosa
# available at https://github.com/fabianp/memory_profiler
from IPython.core.magic import Magics, line_magic, magics_class
class MemMagics(Magics):
@line_magic
def memit(self, line='', setup='pass'):
@vene
vene / bench_eucl.py
Created August 9, 2012 16:44
Benchmark euclidean_distances
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse import issparse
from sklearn.utils import atleast2d_or_csr
from sklearn.utils.extmath import safe_sparse_dot
from sklearn.metrics.pairwise import check_pairwise_arrays, euclidean_distances
from sklearn.metrics.euclidean_fast import dense_euclidean_distances, sparse_euclidean_distances
# author: vlad niculae <vlad@vene.ro>
# license: mit
import torch
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
from entmax import sparsemax, entmax15
from entmax.losses import sparsemax_loss, entmax15_loss
# author: vn
import numpy as np
from scipy.optimize import root_scalar
import torch
import matplotlib.pyplot as plt
def entropy(y, a, b):
@vene
vene / gen_csparsemax.py
Last active October 21, 2020 09:33
Generalized constrained projection onto simplex
# Author: vlad niculae <vlad@vene.ro>
# License: 3-clause BSD
import numpy as np
import cvxpy as cx
from copt.constraint import SimplexConstraint
from copt.splitting import minimize_primal_dual
@vene
vene / energy.py
Created October 19, 2020 12:22
energy model with langevin dynamics
# Density estimation with energy-based models
# Langevin sampling, contrastive divergence training.
# Author: Vlad Niculae <vlad@vene.ro>
# License: MIT
import numpy as np
import torch
from sklearn import datasets
import matplotlib.pyplot as plt
# Geometric intepretation of the gradient of the mapping:
# f : (0, inf) x Sphere(k-1) -> R^k
# f(r, u) -> r*u
# The *catch*: R can vary on (0, inf) but u may only vary on the
# k-1--dimensional tangent plane!
import numpy as np
def main():
# linearity of expectation under mixture model
# license: mit
# author: vlad niculae
from scipy.stats import norm
import numpy as np
def main():
rng = np.random.RandomState(42)