Skip to content

Instantly share code, notes, and snippets.

View sergeyprokudin's full-sized avatar

Sergey Prokudin sergeyprokudin

  • ETH Zürich
  • Zürich
View GitHub Profile
@sergeyprokudin
sergeyprokudin / inverse_discrete_cosine_transform.py
Last active August 17, 2018 13:29
Inverse Discrete Cosine Transform
#http://www.svcl.ucsd.edu/courses/ece161c/handouts/DCT.pdf
import numpy as np
import matplotlib.pyplot as plt
pi = np.pi
N = 50 # timesteps
k = 10 # n basis functions
@sergeyprokudin
sergeyprokudin / gauss_neg_loglikelihood_keras.py
Last active January 18, 2024 00:58
Multivariate Gaussian Negative LogLikelihood Loss Keras
import keras.backend as K
import numpy as np
def gaussian_nll(ytrue, ypreds):
"""Keras implmementation of multivariate Gaussian negative loglikelihood loss function.
This implementation implies diagonal covariance matrix.
Parameters
----------
@sergeyprokudin
sergeyprokudin / count_flops.py
Last active October 24, 2019 13:36
Count trainable parameters and FLOPs per sample of a Keras model
import numpy as np
def count_conv_params_flops(conv_layer, verbose=1):
# out shape is n_cells_dim1 * (n_cells_dim2 * n_cells_dim3)
out_shape = conv_layer.output.shape.as_list()
n_cells_total = np.prod(out_shape[1:-1])
n_conv_params_total = conv_layer.count_params()
@sergeyprokudin
sergeyprokudin / get_script_dir.py
Created December 4, 2018 10:42
Commands to get script directory
import os
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
PROJECT_DIR = os.path.dirname(SCRIPT_DIR)
LOG_DIR = os.path.join(PROJECT_DIR, 'logs')
@sergeyprokudin
sergeyprokudin / comment_template.py
Created December 6, 2018 15:56
Numpy style docstring
def random_number_generator(arg1, arg2):
"""
Summary line.
Extended description of function.
Parameters
----------
arg1 : int
Description of arg1
@sergeyprokudin
sergeyprokudin / sample_dball_uniform.py
Created December 6, 2018 21:12
Sample uniformly from d-dimensional ball
def sample_dball_uniform(n_points=1000, n_dims=3):
"""Sample uniformly from d-dimensional ball
The code is inspired by this small note:
https://blogs.sas.com/content/iml/2016/04/06/generate-points-uniformly-in-ball.html
https://www.sciencedirect.com/science/article/pii/S0047259X10001211
Parameters
----------
n_points : int
@sergeyprokudin
sergeyprokudin / hist_pdf.py
Created January 5, 2019 18:41
Histogram Density Estimator
import numpy as np
def hist_pdf(x, data, n_bins=10, minv=None, maxv=None):
"""Histogram density estimator
[minv, minv+delta, , minv+delta*2, ..., 1]
for any x in B_l=[minv+delta*j, minv+delta*(j+1)] density is estimated in the following way
p(x) = p(x | x \in B_l) * p(x \in B_l) = (1/delta) * (\sum_{x_j}{I(x_j \in B_l)} / N)
@sergeyprokudin
sergeyprokudin / kde.py
Last active January 5, 2019 19:31
Kernel and Histogram Density Estimation
#!/usr/bin/env python
# coding: utf-8
# In[32]:
import numpy as np
def hist_pdf(x, data, n_bins=10, minv=None, maxv=None):
"""Histogram density estimator
@sergeyprokudin
sergeyprokudin / is_permuted_palindrome.py
Last active January 6, 2019 13:48
Test if the string is a permuted palindrome (problem 1.4. from "Cracking the Coding Interview")
def is_permuted_palindrome(s):
"""Checks if the string is a permuted version of a palindrome
Upper case do not matter, spaces can be ignored.
Example:
"cat cat" -> Yes (palindrome: "cat tac")
"cat do" -> No
@sergeyprokudin
sergeyprokudin / bits.py
Created January 6, 2019 14:50
Bitwise operations: set clear bits, check set bits
def set_bit(x, i):
bi = 0b1 << i
return x | bi
def xor_bit(x, i):
bi = 0b1 << i
return x ^ bi
def clear_bit(x, i):
bi = ~(0b1 << i)