diff --git a/01_building_expressions/01_scalar.py b/01_building_expressions/01_scalar.py index 41c1b98..eed1717 100644 --- a/01_building_expressions/01_scalar.py +++ b/01_building_expressions/01_scalar.py @@ -7,6 +7,7 @@ from theano import function raise NotImplementedError("TODO: add any other imports you need") + def make_scalar(): """ Returns a new Theano scalar. @@ -14,6 +15,7 @@ def make_scalar(): raise NotImplementedError("TODO: implement this function.") + def log(x): """ Returns the logarithm of a Theano scalar x. @@ -21,6 +23,7 @@ def log(x): raise NotImplementedError("TODO: implement this function.") + def add(x, y): """ Adds two theano scalars together and returns the result. @@ -36,7 +39,7 @@ def add(x, y): f = function([a, b], d) a = np.cast[a.dtype](1.) b = np.cast[b.dtype](2.) - actual = f(a,b) + actual = f(a, b) expected = 1. + np.log(2.) assert np.allclose(actual, expected) print "SUCCESS!" diff --git a/01_building_expressions/01_scalar_soln.py b/01_building_expressions/01_scalar_soln.py index 07256c3..95e3ae9 100644 --- a/01_building_expressions/01_scalar_soln.py +++ b/01_building_expressions/01_scalar_soln.py @@ -2,6 +2,7 @@ from theano import function import theano.tensor as T + def make_scalar(): """ Returns a new Theano scalar. @@ -9,6 +10,7 @@ def make_scalar(): return T.scalar() + def log(x): """ Returns the logarithm of a Theano scalar x. @@ -16,6 +18,7 @@ def log(x): return T.log(x) + def add(x, y): """ Adds two theano scalars together and returns the result. @@ -31,7 +34,7 @@ def add(x, y): f = function([a, b], d) a = np.cast[a.dtype](1.) b = np.cast[b.dtype](2.) - actual = f(a,b) + actual = f(a, b) expected = 1. + np.log(2.) assert np.allclose(actual, expected) print "SUCCESS!" diff --git a/01_building_expressions/02_vector_mat.py b/01_building_expressions/02_vector_mat.py index 773b4b2..5117f56 100644 --- a/01_building_expressions/02_vector_mat.py +++ b/01_building_expressions/02_vector_mat.py @@ -7,6 +7,7 @@ from theano import function raise NotImplementedError("TODO: add any other imports you need") + def make_vector(): """ Returns a new Theano vector. @@ -14,6 +15,7 @@ def make_vector(): raise NotImplementedError("TODO: implement this function.") + def make_matrix(): """ Returns a new Theano matrix. @@ -21,6 +23,7 @@ def make_matrix(): raise NotImplementedError("TODO: implement this function.") + def elemwise_mul(a, b): """ a: A theano matrix @@ -30,6 +33,7 @@ def elemwise_mul(a, b): raise NotImplementedError("TODO: implement this function.") + def matrix_vector_mul(a, b): """ a: A theano matrix diff --git a/01_building_expressions/02_vector_mat_soln.py b/01_building_expressions/02_vector_mat_soln.py index 8660970..4d2c695 100644 --- a/01_building_expressions/02_vector_mat_soln.py +++ b/01_building_expressions/02_vector_mat_soln.py @@ -2,6 +2,7 @@ from theano import function import theano.tensor as T + def make_vector(): """ Returns a new Theano vector. @@ -9,6 +10,7 @@ def make_vector(): return T.vector() + def make_matrix(): """ Returns a new Theano matrix. @@ -16,6 +18,7 @@ def make_matrix(): return T.matrix() + def elemwise_mul(a, b): """ a: A theano matrix @@ -25,6 +28,7 @@ def elemwise_mul(a, b): return a * b + def matrix_vector_mul(a, b): """ a: A theano matrix diff --git a/01_building_expressions/03_tensor.py b/01_building_expressions/03_tensor.py index 8a73f79..538fd5a 100644 --- a/01_building_expressions/03_tensor.py +++ b/01_building_expressions/03_tensor.py @@ -7,6 +7,7 @@ from theano import function raise NotImplementedError("TODO: add any other imports you need") + def make_tensor(dim): """ Returns a new Theano tensor with no broadcastable dimensions. @@ -16,6 +17,7 @@ def make_tensor(dim): raise NotImplementedError("TODO: implement this function.") + def broadcasted_add(a, b): """ a: a 3D theano tensor @@ -29,6 +31,7 @@ def broadcasted_add(a, b): raise NotImplementedError("TODO: implement this function.") + def partial_max(a): """ a: a 4D theano tensor @@ -48,7 +51,7 @@ def partial_max(a): c = broadcasted_add(a, b) d = partial_max(c) - f = function([a, b,], d) + f = function([a, b], d) rng = np.random.RandomState([1, 2, 3]) a_value = rng.randn(2, 2, 2).astype(a.dtype) diff --git a/01_building_expressions/03_tensor_soln.py b/01_building_expressions/03_tensor_soln.py index 5f24b2c..9182a97 100644 --- a/01_building_expressions/03_tensor_soln.py +++ b/01_building_expressions/03_tensor_soln.py @@ -2,6 +2,7 @@ from theano import function import theano.tensor as T + def make_tensor(dim): """ Returns a new Theano tensor with no broadcastable dimensions. @@ -10,6 +11,7 @@ def make_tensor(dim): return T.TensorType(broadcastable=tuple([False] * dim), dtype='float32')() + def broadcasted_add(a, b): """ a: a 3D theano tensor @@ -23,6 +25,7 @@ def broadcasted_add(a, b): return a.dimshuffle(2, 'x', 1, 0) + b + def partial_max(a): """ a: a 4D theano tensor @@ -42,7 +45,7 @@ def partial_max(a): c = broadcasted_add(a, b) d = partial_max(c) - f = function([a, b,], d) + f = function([a, b], d) rng = np.random.RandomState([1, 2, 3]) a_value = rng.randn(2, 2, 2).astype(a.dtype) diff --git a/02_compiling_and_running/01_function.py b/02_compiling_and_running/01_function.py index 0e568de..e45eff1 100644 --- a/02_compiling_and_running/01_function.py +++ b/02_compiling_and_running/01_function.py @@ -6,6 +6,7 @@ from theano import tensor as T raise NotImplementedError("TODO: add any other imports you need") + def evaluate(x, y, expr, x_value, y_value): """ x: A theano variable diff --git a/02_compiling_and_running/01_function_soln.py b/02_compiling_and_running/01_function_soln.py index 3d45cef..2b9a4f2 100644 --- a/02_compiling_and_running/01_function_soln.py +++ b/02_compiling_and_running/01_function_soln.py @@ -1,6 +1,7 @@ from theano import tensor as T from theano import function + def evaluate(x, y, expr, x_value, y_value): """ x: A theano variable diff --git a/02_compiling_and_running/02_shared.py b/02_compiling_and_running/02_shared.py index c979ca4..a563194 100644 --- a/02_compiling_and_running/02_shared.py +++ b/02_compiling_and_running/02_shared.py @@ -6,6 +6,7 @@ import numpy as np raise NotImplementedError("TODO: add any other imports you need") + def make_shared(shape): """ Returns a theano shared variable containing a tensor of the specified @@ -14,6 +15,7 @@ def make_shared(shape): """ raise NotImplementedError("TODO: implement the function") + def exchange_shared(a, b): """ a: a theano shared variable @@ -22,6 +24,7 @@ def exchange_shared(a, b): """ raise NotImplementedError("TODO: implement the function") + def make_exchange_func(a, b): """ a: a theano shared variable @@ -34,7 +37,6 @@ def make_exchange_func(a, b): raise NotImplementedError("TODO: implement the function") - if __name__ == "__main__": a = make_shared((5, 4, 3)) assert a.get_value().shape == (5, 4, 3) diff --git a/02_compiling_and_running/02_shared_soln.py b/02_compiling_and_running/02_shared_soln.py index a0acc0e..2884ce5 100644 --- a/02_compiling_and_running/02_shared_soln.py +++ b/02_compiling_and_running/02_shared_soln.py @@ -3,6 +3,7 @@ from theano import function from theano import shared + def make_shared(shape): """ Returns a theano shared variable containing a tensor of the specified @@ -11,6 +12,7 @@ def make_shared(shape): """ return shared(np.zeros(shape)) + def exchange_shared(a, b): """ a: a theano shared variable @@ -21,6 +23,7 @@ def exchange_shared(a, b): a.set_value(b.get_value()) b.set_value(temp) + def make_exchange_func(a, b): """ a: a theano shared variable @@ -38,7 +41,6 @@ def make_exchange_func(a, b): return f - if __name__ == "__main__": a = make_shared((5, 4, 3)) assert a.get_value().shape == (5, 4, 3) diff --git a/03_modifying/01_grad.py b/03_modifying/01_grad.py index eba5658..964f12a 100644 --- a/03_modifying/01_grad.py +++ b/03_modifying/01_grad.py @@ -5,6 +5,7 @@ # compute some derivative. from theano import tensor as T + def grad_sum(x, y, z): """ x: A theano variable @@ -22,5 +23,5 @@ def grad_sum(x, y, z): y = T.scalar() z = x + y s = grad_sum(x, y, z) - assert s.eval({x: 0, y:0}) == 2 + assert s.eval({x: 0, y: 0}) == 2 print "SUCCESS!" diff --git a/03_modifying/01_grad_soln.py b/03_modifying/01_grad_soln.py index 82c0813..cc8c7b3 100644 --- a/03_modifying/01_grad_soln.py +++ b/03_modifying/01_grad_soln.py @@ -3,6 +3,7 @@ # from theano import tensor as T + def grad_sum(x, y, z): """ x: A theano variable @@ -14,12 +15,10 @@ def grad_sum(x, y, z): return sum(T.grad(z, [x, y])) - - if __name__ == "__main__": x = T.scalar() y = T.scalar() z = x + y s = grad_sum(x, y, z) - assert s.eval({x: 0, y:0}) == 2 + assert s.eval({x: 0, y: 0}) == 2 print "SUCCESS!" diff --git a/03_modifying/02_traverse.py b/03_modifying/02_traverse.py index d2568b8..4984843 100644 --- a/03_modifying/02_traverse.py +++ b/03_modifying/02_traverse.py @@ -8,6 +8,7 @@ from theano import tensor as T raise NotImplementedError("Add any imports you need.") + def arg_to_softmax(prob): """ Oh no! Someone has passed you the probability output, diff --git a/03_modifying/02_traverse_soln.py b/03_modifying/02_traverse_soln.py index c2e2843..6751147 100644 --- a/03_modifying/02_traverse_soln.py +++ b/03_modifying/02_traverse_soln.py @@ -2,6 +2,7 @@ from theano.gof import Variable from theano import tensor as T + def arg_to_softmax(prob): """ Oh no! Someone has passed you the probability output,