Skip to content

Commit 78efe1b

Browse files
committed
Add Theano scan notebook.
1 parent 3890c74 commit 78efe1b

File tree

4 files changed

+723
-0
lines changed

4 files changed

+723
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ IPython Notebook(s) demonstrating deep learning functionality.
9494
| [tsf-word2vec](http://nbviewer.ipython.org/github/donnemartin/data-science-ipython-notebooks/blob/master/deep-learning/tensor-flow-exercises/5_word2vec.ipynb) | Train a skip-gram model over Text8 data in TensorFlow. |
9595
| [tsf-lstm](http://nbviewer.ipython.org/github/donnemartin/data-science-ipython-notebooks/blob/master/deep-learning/tensor-flow-exercises/6_lstm.ipynb) | Train a LSTM character model over Text8 data in TensorFlow. |
9696
| [theano-intro](http://nbviewer.ipython.org/github/donnemartin/data-science-ipython-notebooks/blob/master/deep-learning/theano-tutorial/intro_theano/intro_theano.ipynb) | Intro to Theano, which allows you to define, optimize, and evaluate mathematical expressions involving multi-dimensional arrays efficiently. It can use GPUs and perform efficient symbolic differentiation. |
97+
| [theano-scan](http://nbviewer.ipython.org/github/donnemartin/data-science-ipython-notebooks/blob/master/deep-learning/theano-tutorial/scan_tutorial/scan_tutorial.ipynb) | Learn scans, a mechanism to perform loops in a Theano graph. |
9798
| [deep-dream](http://nbviewer.ipython.org/github/donnemartin/data-science-ipython-notebooks/blob/master/deep-learning/deep-dream/dream.ipynb) | Caffe-based computer vision program which uses a convolutional neural network to find and enhance patterns in images. |
9899

99100
<br/>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import theano
2+
import theano.tensor as T
3+
import numpy as np
4+
5+
coefficients = T.vector("coefficients")
6+
x = T.scalar("x")
7+
max_coefficients_supported = 10000
8+
9+
10+
def step(coeff, power, prior_value, free_var):
11+
return prior_value + (coeff * (free_var ** power))
12+
13+
# Generate the components of the polynomial
14+
full_range = T.arange(max_coefficients_supported)
15+
outputs_info = np.zeros((), dtype=theano.config.floatX)
16+
17+
components, updates = theano.scan(fn=step,
18+
sequences=[coefficients, full_range],
19+
outputs_info=outputs_info,
20+
non_sequences=x)
21+
22+
polynomial = components[-1]
23+
calculate_polynomial = theano.function(inputs=[coefficients, x],
24+
outputs=polynomial,
25+
updates=updates)
26+
27+
test_coeff = np.asarray([1, 0, 2], dtype=theano.config.floatX)
28+
print(calculate_polynomial(test_coeff, 3))
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import theano
2+
import theano.tensor as T
3+
import numpy as np
4+
5+
probabilities = T.vector()
6+
nb_samples = T.iscalar()
7+
8+
rng = T.shared_randomstreams.RandomStreams(1234)
9+
10+
11+
def sample_from_pvect(pvect):
12+
""" Provided utility function: given a symbolic vector of
13+
probabilities (which MUST sum to 1), sample one element
14+
and return its index.
15+
"""
16+
onehot_sample = rng.multinomial(n=1, pvals=pvect)
17+
sample = onehot_sample.argmax()
18+
return sample
19+
20+
21+
def set_p_to_zero(pvect, i):
22+
""" Provided utility function: given a symbolic vector of
23+
probabilities and an index 'i', set the probability of the
24+
i-th element to 0 and renormalize the probabilities so they
25+
sum to 1.
26+
"""
27+
new_pvect = T.set_subtensor(pvect[i], 0.)
28+
new_pvect = new_pvect / new_pvect.sum()
29+
return new_pvect
30+
31+
32+
def step(p):
33+
sample = sample_from_pvect(p)
34+
new_p = set_p_to_zero(p, sample)
35+
return new_p, sample
36+
37+
output, updates = theano.scan(fn=step,
38+
outputs_info=[probabilities, None],
39+
n_steps=nb_samples)
40+
41+
modified_probabilities, samples = output
42+
43+
f = theano.function(inputs=[probabilities, nb_samples],
44+
outputs=[samples],
45+
updates=updates)
46+
47+
# Testing the function
48+
test_probs = np.asarray([0.6, 0.3, 0.1], dtype=theano.config.floatX)
49+
for i in range(10):
50+
print(f(test_probs, 2))

0 commit comments

Comments
 (0)