In [2]:
import numpy as np
import theano
import theano.tensor as T
In [19]:
# %load ./spoilers/logistic.py
x = T.vector()
s = 1/(1+T.exp(-x))
ds = T.grad(T.sum(s), x) # Need sum to make s scalar
import matplotlib.pyplot as plt
%matplotlib inline
x0 = np.arange(-3, 3, 0.01).astype('float32')
plt.plot(x0, s.eval({x:x0}))
plt.plot(x0, ds.eval({x:x0}))
np.allclose(ds.eval({x:x0}), s.eval({x:x0}) * (1-s.eval({x:x0})))
Out[19]:
In [ ]:
# %load spoilers/fib.py
a = theano.shared(1)
b = theano.shared(1)
f = a + b
updates = {a: b, b: f}
next_term = theano.function([], f, updates=updates)
[next_term() for _ in range(3, 10)]
Implement Conway's Game of Life with periodic boundary conditions (wrapping borders).
In [34]:
board = theano.shared(np.zeros((100, 100), dtype='uint8'))
initial = np.random.binomial(1, 0.1, size=(100, 100)).astype('uint8')
board.set_value(initial)
In [ ]:
# Create a function f that updates board with new values and return the current state
# Uncomment the line below and run for a solution
#%load spoilers/life.py
In [44]:
# After creating your f function, run this cell to animate the output
%matplotlib notebook
import matplotlib.pyplot as plt
from IPython import display
import time
for i in range(50):
plt.gca().cla()
current = f()
plt.imshow(current, interpolation='nearest', cmap='gray')
display.clear_output(wait=True)
display.display(plt.gcf())
time.sleep(0.1)