Skip to content

Instantly share code, notes, and snippets.

View IlievskiV's full-sized avatar
🐻‍❄️

Vladimir Ilievski IlievskiV

🐻‍❄️
View GitHub Profile
@IlievskiV
IlievskiV / dataset.py
Created March 27, 2019 09:49
Load data using the TensorFlow Dataset API
import tensorflow as tf
def make_dataset():
sentences = ['The cat sat on the mat', 'The quick brown fox jumped over the lazy dog']
dataset = tf.data.Dataset.from_tensor_slices(sentences) #
dataset = dataset.shuffle(buffer_size=1000)
dataset = dataset.map(lambda sentence: text_to_sequence(sentence), num_parallel_calls=4)
dataset = dataset.batch(batch_size=32)
dataset = dataset.prefetch(1)
@IlievskiV
IlievskiV / feeder.py
Created March 27, 2019 09:51
Loading data in TensorFlow, using low-level queuing mechanism
import tensorflow as tf
class Feeder:
def __init__(self):
self.sess = tf.Session() # session to run operations
self.coordinator = tf.train.Coordinator() # create coordinator for threads
self.placeholder = tf.placeholder(tf.int32, shape=(None, None), name='sentence')
self.queue = tf.FIFOQueue(5, tf.int32, name='input_queue') # hold 5 elements
self.enqueue_op = queue.enqueue(placeholder) # push op
@IlievskiV
IlievskiV / .block
Last active February 19, 2020 22:36
Interactive exploring of the Laptop Prices dataset with Parallel Coordinates
license: gpl-3.0
height: 800
border: yes
@IlievskiV
IlievskiV / random_walk_animated.ipynb
Created March 31, 2020 09:03
Radnom Walk Animated using the MatplotLib's FuncAnimation
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import numpy as np
np.random.seed(1234)
def random_walk(N):
"""
Simulates a discrete random walk
:param int N : the number of steps to take
"""
# event space: set of possible increments
increments = np.array([1, -1])
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure(figsize=(21, 10))
ax = plt.axes(xlim=(0, N), ylim=(np.min(X) - 0.5, np.max(X) + 0.5))
line, = ax.plot([], [], lw=2, color='#0492C2')
ax.set_xticks(np.arange(0, N+1, 50))
ax.set_yticks(np.arange(np.min(X) - 0.5, np.max(X) + 0.5, 0.2))
ax.set_title('2D Random Walk', fontsize=22)
ax.set_xlabel('Steps', fontsize=18)
num_simulations = 10000 # num of simulation
N = 100000 # number od steps in each simulation
dt = 1./N # the time step
X_norm = [0] * num_simulations # the normalized random variable
# run the simulations
for i in range(num_simulations):
X, _ = random_walk(N)
X_norm[i] = X[N - 1] * np.sqrt(dt)
import numpy as np
np.random.seed(1234)
def brownian_motion(N, T, h):
"""
Simulates a Brownian motion
:param int N : the number of discrete steps
:param int T: the number of continuous time steps
:param float h: the variance of the increments
"""
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig = plt.figure(figsize=(21, 10))
ax = plt.axes(xlim=(0, 1))
line, = ax.step([], [], where='mid', color='#0492C2')
# formatting options
ax.set_xticks(np.linspace(0,1,11))
ax.set_xlabel('Time', fontsize=30)
from scipy import stats
num_simulations = 10000 # how many times to repeat
Ns, Ts, hs = 20000, 10.0, 1.0 # discrete steps, continuous steps, veriance
dts = 1.0 * T/N # total number of time steps
u = 2. # the difference in time points
t = int(np.floor((np.random.uniform(low=u+0.01, high=1. * T - u)/T) * N)) # random starting point
# initialize the means