Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into LiveMonitorGUI
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamStone committed Jan 8, 2015
2 parents b10552c + 8c34a73 commit 48ad0d8
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 27 deletions.
4 changes: 4 additions & 0 deletions .requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This is for readthedocs.org to be able to build the documentation,
# ignore otherwise
numpydoc
git+git://github.com/Theano/Theano.git
66 changes: 48 additions & 18 deletions pylearn2/datasets/dense_design_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down Expand Up @@ -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)]
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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?
Expand Down
42 changes: 41 additions & 1 deletion pylearn2/expr/nnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,46 @@ 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
Expand Down Expand Up @@ -323,4 +363,4 @@ def compute_f1(precision, recall):
"""
f1 = (2. * precision * recall /
T.maximum(1, precision + recall))
return f1
return f1
38 changes: 30 additions & 8 deletions pylearn2/expr/tests/test_nnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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()
Expand All @@ -131,8 +132,29 @@ 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

0 comments on commit 48ad0d8

Please sign in to comment.