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 / vae.py
Created June 7, 2019 14:49
Basic variational autoencoder in Keras
import tensorflow as tf
from keras.layers import Input, Dense, Flatten, Reshape, Dropout
from keras.models import Model, Sequential
from keras.optimizers import Adam
from keras.objectives import binary_crossentropy
from keras.layers.merge import concatenate
from keras.losses import mean_squared_error
def variational_autoencoder(n_input_features, latent_space_size=64, hlayer_size=256,
lr=1.0e-3, kl_weight=0.1):
@sergeyprokudin
sergeyprokudin / plot_gauss_pdf.txt
Created April 7, 2019 16:10
plots Guassian density together with some ground truth value gt
import matplotlib.pyplot as plt
import scipy.stats as stats
def plot_gauss_pdf(mu, sigma, gt):
''' plots Guassian density together with some ground truth value gt
'''
x = np.linspace(mu - 5*sigma, mu + 5*sigma, 100)
plt.plot(x, stats.norm.pdf(x, mu, sigma))
plt.axvline(mu, c='blue', label='prediction mean')
@sergeyprokudin
sergeyprokudin / save_h5.py
Created March 7, 2019 13:05
Small function to dave dictionary of numpy arrays to H5 file
import h5py
import os
def save_h5(savepath, arr_dict):
if os.path.exists(savepath):
os.remove(savepath)
h5ds = h5py.File(savepath)
@sergeyprokudin
sergeyprokudin / plot2d.py
Created January 15, 2019 14:42
Plot a set of 2d figures
import matplotlib.pyplot as plt
%matplotlib inline
def plot2d(fig_lst, labels=None, figsize=(10, 10), savepath=None):
"""Plots a set of 2d shapes represented as 2d point arrays
Parameters
----------
fig_lst: list
@sergeyprokudin
sergeyprokudin / robot_in_a_grid.py
Last active January 13, 2019 09:34
Robot in a Grid (dynamic programming, puzzle 8.2 from "Cracking the coding interview")
from collections import deque
def check_path(grid, i=0, j=0, failed_points=None, current_path=None):
""" Find a route between upper-left and lower right corner of the grid,
if you are only allowed to walk right and down.
Input:
1 1 1
0 0 1
0 0 1
@sergeyprokudin
sergeyprokudin / count_steps.py
Created January 12, 2019 18:56
Triple Step (Puzzle 8.1. from "Cracking the Coding Interview")
def count_steps(n):
"""Determine how many ways there are to walk n steps, if you can jump over 1, 2 or 3 steps at the time
f(-1) = 0
f(0) = 1 # just stand?
f(1) = 1
f(2) = 2
f(3) = 4
f(4) = f(n-1)+f(n-2)+f(n-3)
import numpy as np
def pca(x, n_components):
""" Principal component analysis of the data
m - number of samples
n - number of dimensions
k - number of components
TL;DR:
(a) compute the covariance matrix x.T@x
@sergeyprokudin
sergeyprokudin / fibonacci.py
Created January 11, 2019 12:30
Compute Fibonacci Numbers with Python
def fib(n):
""" Compute nth element of a Fibonacci sequence:
1, 2, 3, 5, 8, 13, 21, ...
f(n) = f(n-1) + f(n-2)
f(0) = 0
f(1) = 1
f(2) = 1
f(3) = 2
@sergeyprokudin
sergeyprokudin / kmessed_sort.py
Created January 11, 2019 11:40
Sort k-messed array
import heapq as hq
def sort_k_messed(a, k):
"""Sort an array where each element is at most k steps away from
its sorted position.
Parameters:
-----------
a: list
k-messed input array
@sergeyprokudin
sergeyprokudin / have_route_bfs.py
Created January 10, 2019 22:56
Route Between Nodes (task 4.1 from "Cracking the Coding interview")
from collections import deque
def have_route(g, start_node, end_node):
"""Find out if there is a path in a directed graph g
between start_node and end_node
Algo #1: BFS-based.
0. Check if start_node=end_node. If so, return True;
1. Add start_node to queue and while queue is not empty
1.1. dequeue curr_node;