From cbe0cf867fde58f35545b27cf9bdec4f2336781a Mon Sep 17 00:00:00 2001 From: Dustin Webb Date: Tue, 6 Jan 2015 10:17:32 -0500 Subject: [PATCH 1/6] Updated documentation for DenseDesignMatrixPyTables. --- pylearn2/datasets/dense_design_matrix.py | 66 +++++++++++++++++------- 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/pylearn2/datasets/dense_design_matrix.py b/pylearn2/datasets/dense_design_matrix.py index ac8e39e080..7302f4e8eb 100644 --- a/pylearn2/datasets/dense_design_matrix.py +++ b/pylearn2/datasets/dense_design_matrix.py @@ -753,7 +753,11 @@ def set_topological_view(self, V, axes=('b', 0, 1, 'c')): V : ndarray An array containing a design matrix representation of training examples. - axes : WRITEME + axes : tuple, optional + The axes ordering of the provided topo_view. Must be some + permutation of ('b', 0, 1, 'c') where 'b' indicates the axis + indexing examples, 0 and 1 indicate the row/cols dimensions and + 'c' indicates the axis indexing color channels. """ if len(V.shape) != len(axes): raise ValueError("The topological view must have exactly 4 " @@ -1169,11 +1173,14 @@ def set_topological_view(self, V, axes=('b', 0, 1, 'c'), start=0): ---------- V : ndarray An array containing a design matrix representation of training \ - examples. If unspecified, the entire dataset (`self.X`) is used \ - instead. - axes : WRITEME - WRITEME - start : WRITEME + examples. + axes : tuple, optional + The axes ordering of the provided topo_view. Must be some + permutation of ('b', 0, 1, 'c') where 'b' indicates the axis + indexing examples, 0 and 1 indicate the row/cols dimensions and + 'c' indicates the axis indexing color channels. + start : int + The start index to write data. """ assert not contains_nan(V) rows = V.shape[axes.index(0)] @@ -1189,11 +1196,15 @@ def set_topological_view(self, V, axes=('b', 0, 1, 'c'), start=0): def init_hdf5(self, path, shapes): """ - .. todo:: + Initializes the hdf5 file into which the data will be stored. This must + be called before calling fill_hdf5. - WRITEME properly - - Initialize hdf5 file to be used ba dataset + Parameters + ---------- + path : string + The name of the hdf5 file. + shapes : tuple + The shapes of X and y. """ x_shape, y_shape = shapes @@ -1217,14 +1228,25 @@ def fill_hdf5(file_handle, start=0, batch_size=5000): """ - .. todo:: + Saves the data to the hdf5 file. - WRITEME properly - - PyTables tends to crash if you write large data on them at once. - This function write data on file_handle in batches + PyTables tends to crash if you write large amounts of data into them + at once. As such this function writes data in batches. - start: the start index to write data + Parameters + ---------- + file_handle : hdf5 file handle + Handle to an hdf5 object. + data_x : nd array + X data. Must be the same shape as specified to init_hdf5. + data_y : nd array, optional + y data. Must be the same shape as specified to init_hdf5. + node : string, optional + The hdf5 node into which the data should be stored. + start : int + The start index to write data. + batch_size : int, optional + The size of the batch to be saved. """ if node is None: @@ -1245,9 +1267,17 @@ def fill_hdf5(file_handle, def resize(self, h5file, start, stop): """ - .. todo:: + Resizes the X and y tables. This must be called before calling + fill_hdf5. - WRITEME + Parameters + --------- + h5file : hdf5 file handle + Handle to an hdf5 object. + start : int + The start index to write data. + stop : int + The index of the record following the last record to be written. """ ensure_tables() # TODO is there any smarter and more efficient way to this? From 5e8ac740ad4d56fcfe75f51389cc749ab161a2c6 Mon Sep 17 00:00:00 2001 From: Dustin Webb Date: Tue, 6 Jan 2015 11:26:52 -0500 Subject: [PATCH 2/6] Fixed pep8 error. --- pylearn2/datasets/dense_design_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pylearn2/datasets/dense_design_matrix.py b/pylearn2/datasets/dense_design_matrix.py index 7302f4e8eb..8feb26d163 100644 --- a/pylearn2/datasets/dense_design_matrix.py +++ b/pylearn2/datasets/dense_design_matrix.py @@ -1271,7 +1271,7 @@ def resize(self, h5file, start, stop): fill_hdf5. Parameters - --------- + ---------- h5file : hdf5 file handle Handle to an hdf5 object. start : int From 89b5ba33bb178851966f93b48e100b7c964b39a7 Mon Sep 17 00:00:00 2001 From: Arnaud Bergeron Date: Tue, 6 Jan 2015 11:58:32 -0500 Subject: [PATCH 3/6] Add a requirements file for readthedocs.org --- .requirements.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .requirements.txt diff --git a/.requirements.txt b/.requirements.txt new file mode 100644 index 0000000000..e316aa7ffc --- /dev/null +++ b/.requirements.txt @@ -0,0 +1,3 @@ +# This is for readthedocs.org to be able to build the documentation, +# ignore otherwise +numpydoc From 5f90d4cf6ea55db0866e8ff3d2d2f09209c3eff7 Mon Sep 17 00:00:00 2001 From: Arnaud Bergeron Date: Tue, 6 Jan 2015 12:44:14 -0500 Subject: [PATCH 4/6] pylearn2 also need theano for the docs ... --- .requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/.requirements.txt b/.requirements.txt index e316aa7ffc..5e5cb3f03a 100644 --- a/.requirements.txt +++ b/.requirements.txt @@ -1,3 +1,4 @@ # This is for readthedocs.org to be able to build the documentation, # ignore otherwise numpydoc +git+git://github.com/Theano/Theano.git From 43c5b67782c6d97d6809c3f289d9a725ea32c53c Mon Sep 17 00:00:00 2001 From: Ia3n Goodfellow Date: Mon, 3 Nov 2014 08:10:18 -0800 Subject: [PATCH 5/6] added arg_of_sigmoid --- pylearn2/expr/nnet.py | 41 +++++++++++++++++++++++++++++++- pylearn2/expr/tests/test_nnet.py | 39 +++++++++++++++++++++++------- 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/pylearn2/expr/nnet.py b/pylearn2/expr/nnet.py index 3d36cc71b6..1cdc676bd9 100644 --- a/pylearn2/expr/nnet.py +++ b/pylearn2/expr/nnet.py @@ -114,6 +114,45 @@ def arg_of_softmax(Y_hat): return z +def arg_of_sigmoid(Y_hat): + """ + Given the output of a call to theano.tensor.nnet.sigmoid, + returns the argument to the sigmoid (by tracing the Theano + graph). + + Parameters + ---------- + Y_hat : Variable + T.nnet.sigmoid(Z) + + Returns + ------- + Z : Variable + The variable that was passed to T.nnet.sigmoid to create `Y_hat`. + Raises an error if `Y_hat` is not actually the output of a theano + sigmoid. + """ + assert hasattr(Y_hat, 'owner') + owner = Y_hat.owner + assert owner is not None + op = owner.op + if isinstance(op, Print): + assert len(owner.inputs) == 1 + Y_hat, = owner.inputs + owner = Y_hat.owner + op = owner.op + success = False + if isinstance(op, T.Elemwise): + if isinstance(op.scalar_op, T.nnet.sigm.ScalarSigmoid): + success = True + if not success: + raise TypeError("Expected Y_hat to be the output of a sigmoid, " + "but it appears to be the output of " + str(op) + + " of type " + str(type(op))) + z ,= owner.inputs + assert z.ndim == 2 + return z + def kl(Y, Y_hat, batch_axis): """ Warning: This function expects a sigmoid nonlinearity in the @@ -323,4 +362,4 @@ def compute_f1(precision, recall): """ f1 = (2. * precision * recall / T.maximum(1, precision + recall)) - return f1 \ No newline at end of file + return f1 diff --git a/pylearn2/expr/tests/test_nnet.py b/pylearn2/expr/tests/test_nnet.py index 6de0f1c3b7..d7e35cda6a 100644 --- a/pylearn2/expr/tests/test_nnet.py +++ b/pylearn2/expr/tests/test_nnet.py @@ -14,12 +14,13 @@ from theano import tensor as T from pylearn2.models.mlp import MLP, Sigmoid +from pylearn2.expr.nnet import arg_of_sigmoid from pylearn2.expr.nnet import pseudoinverse_softmax_numpy from pylearn2.expr.nnet import softmax_numpy from pylearn2.expr.nnet import softmax_ratio from pylearn2.expr.nnet import compute_recall from pylearn2.expr.nnet import kl -from pylearn2.expr.nnet import elemwise_kl +from pylearn2.expr.nnet import elemwise_kl from pylearn2.utils import sharedX @@ -83,7 +84,7 @@ def test_kl(): """ init_mode = theano.config.compute_test_value theano.config.compute_test_value = 'raise' - + try: mlp = MLP(layers=[Sigmoid(dim=10, layer_name='Y', irange=0.1)], nvis=10) @@ -101,7 +102,7 @@ def test_kl(): np.testing.assert_raises(ValueError, kl, Y, Y_hat, 1) Y.tag.test_value[2][3] = -0.1 np.testing.assert_raises(ValueError, kl, Y, Y_hat, 1) - + finally: theano.config.compute_test_value = init_mode @@ -112,10 +113,10 @@ def test_elemwise_kl(): input. """ init_mode = theano.config.compute_test_value - theano.config.compute_test_value = 'raise' - + theano.config.compute_test_value = 'raise' + try: - mlp = MLP(layers=[Sigmoid(dim=10, layer_name='Y', irange=0.1)], + mlp = MLP(layers=[Sigmoid(dim=10, layer_name='Y', irange=0.1)], nvis=10) X = mlp.get_input_space().make_theano_batch() Y = mlp.get_output_space().make_theano_batch() @@ -131,8 +132,30 @@ def test_elemwise_kl(): np.testing.assert_raises(ValueError, elemwise_kl, Y, Y_hat) Y.tag.test_value[2][3] = -0.1 np.testing.assert_raises(ValueError, elemwise_kl, Y, Y_hat) - + finally: theano.config.compute_test_value = init_mode - +def test_arg_of_sigmoid_good(): + """ + Tests that arg_of_sigmoid works when given a good input. + """ + + X = T.matrix() + Y = T.nnet.sigmoid(X) + Z = arg_of_sigmoid(Y) + assert X is Z + +def test_arg_of_sigmoid_bad(): + """ + Tests that arg_of_sigmoid raises an error when given a bad input. + """ + + X = T.matrix() + Y = T.nnet.softmax(X) + try: + Z = arg_of_sigmoid(Y) + except TypeError: + return + assert False # Should have failed + From 43e43caaf26d4dee87c609e9f65d104d4c90dbaa Mon Sep 17 00:00:00 2001 From: Ian Goodfellow Date: Thu, 8 Jan 2015 11:14:47 -0800 Subject: [PATCH 6/6] pep8 --- pylearn2/expr/nnet.py | 3 ++- pylearn2/expr/tests/test_nnet.py | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pylearn2/expr/nnet.py b/pylearn2/expr/nnet.py index 1cdc676bd9..c816e5b810 100644 --- a/pylearn2/expr/nnet.py +++ b/pylearn2/expr/nnet.py @@ -149,10 +149,11 @@ def arg_of_sigmoid(Y_hat): raise TypeError("Expected Y_hat to be the output of a sigmoid, " "but it appears to be the output of " + str(op) + " of type " + str(type(op))) - z ,= owner.inputs + z, = owner.inputs assert z.ndim == 2 return z + def kl(Y, Y_hat, batch_axis): """ Warning: This function expects a sigmoid nonlinearity in the diff --git a/pylearn2/expr/tests/test_nnet.py b/pylearn2/expr/tests/test_nnet.py index d7e35cda6a..2d8277770b 100644 --- a/pylearn2/expr/tests/test_nnet.py +++ b/pylearn2/expr/tests/test_nnet.py @@ -158,4 +158,3 @@ def test_arg_of_sigmoid_bad(): except TypeError: return assert False # Should have failed -