🐻❄️
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 | |
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) |
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 | |
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 |
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
license: gpl-3.0 | |
height: 800 | |
border: yes |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 | |
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]) |
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 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) |
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
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) | |
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 | |
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 | |
""" |
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 | |
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) |
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 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 |
OlderNewer