forked from lazyprogrammer/machine_learning_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf_scan3.py
43 lines (35 loc) · 1.18 KB
/
tf_scan3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# https://deeplearningcourses.com/c/unsupervised-machine-learning-hidden-markov-models-in-python
# https://udemy.com/unsupervised-machine-learning-hidden-markov-models-in-python
# https://lazyprogrammer.me
# tensorflow scan example - low pass filter
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
# original sequence is a noisy sine wave
original = np.sin(np.linspace(0, 3*np.pi, 300))
X = 2*np.random.randn(300) + original
plt.plot(X)
plt.title("original")
plt.show()
# set up placeholders
decay = tf.placeholder(tf.float32, shape=(), name='decay')
sequence = tf.placeholder(tf.float32, shape=(None,), name='sequence')
# the recurrence function and loop
def recurrence(last, x):
return (1.0-decay)*x + decay*last
lpf = tf.scan(
fn=recurrence,
elems=sequence,
initializer=0.0, # sequence[0] to use the first value of the sequence
)
# run it!
with tf.Session() as session:
Y = session.run(lpf, feed_dict={sequence: X, decay: 0.97})
plt.plot(Y)
plt.plot(original)
plt.title("filtered")
plt.show()