This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import h5py | |
import os | |
def save_h5(savepath, arr_dict): | |
if os.path.exists(savepath): | |
os.remove(savepath) | |
h5ds = h5py.File(savepath) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |