We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Remove core files from whitelist
- [x] models/mlp.py
- [x] `costs/mlp/__init__.py`
- [x] costs/mlp/dropout.py
- [x] monitor.py
- [x] optimization/batch_gradient_descent.py
- [x] blocks.py
- [ ] expr/nnet.py
- [x] costs/cost.py
- [x] datasets/dense_design_matrix.py
- [x] datasets/dataset.py


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pylearn2/expr/nnet.py`
Content:
```
1 """
2 Useful expressions common to many neural network applications.
3 """
4 __authors__ = "Ian Goodfellow"
5 __copyright__ = "Copyright 2010-2013, Universite de Montreal"
6 __credits__ = ["Ian Goodfellow"]
7 __license__ = "3-clause BSD"
8 __maintainer__ = "LISA Lab"
9 __email__ = "pylearn-dev@googlegroups"
10 
11 import numpy as np
12 import theano
13 from theano.printing import Print
14 from theano import tensor as T
15 from theano.gof.op import get_debug_values
16 
17 def softmax_numpy(x):
18     """
19     .. todo::
20 
21         WRITEME properly
22 
23     Parameters
24     ----------
25     x : matrix
26 
27     Returns
28     -------
29     rval : vector
30         rval[i] is the softmax of row i of x
31     """
32     stable_x = (x.T - x.max(axis=1)).T
33     numer = np.exp(stable_x)
34     return (numer.T / numer.sum(axis=1)).T
35 
36 def pseudoinverse_softmax_numpy(x):
37     """
38     .. todo::
39 
40         WRITEME properly
41 
42     Parameters
43     ----------
44     x : vector
45 
46     Returns
47     -------
48     y : vector
49         softmax(y) = x
50 
51     Notes
52     -----
53     This problem is underdetermined, so we also impose y.mean() = 0
54     """
55     rval = np.log(x)
56     rval -= rval.mean()
57     return rval
58 
59 def sigmoid_numpy(x):
60     """
61     .. todo::
62 
63         WRITEME
64     """
65     assert not isinstance(x, theano.gof.Variable)
66     return 1. / (1. + np.exp(-x))
67 
68 def inverse_sigmoid_numpy(x):
69     """
70     .. todo::
71 
72         WRITEME
73     """
74     return np.log(x / (1. - x))
75 
76 def arg_of_softmax(Y_hat):
77     """
78     Given the output of a call to theano.tensor.nnet.softmax,
79     returns the argument to the softmax (by tracing the Theano
80     graph).
81 
82     Parameters
83     ----------
84     Y_hat : Variable
85         softmax(Z)
86 
87     Returns
88     -------
89     Z : Variable
90         The variable that was passed to the Softmax op to create `Y_hat`.
91         Raises an error if `Y_hat` is not actually the output of a
92         Softmax.
93     """
94     assert hasattr(Y_hat, 'owner')
95     owner = Y_hat.owner
96     assert owner is not None
97     op = owner.op
98     if isinstance(op, Print):
99         assert len(owner.inputs) == 1
100         Y_hat, = owner.inputs
101         owner = Y_hat.owner
102         op = owner.op
103     if not isinstance(op, T.nnet.Softmax):
104         raise ValueError("Expected Y_hat to be the output of a softmax, "
105                 "but it appears to be the output of " + str(op) + " of type "
106                 + str(type(op)))
107     z ,= owner.inputs
108     assert z.ndim == 2
109     return z
110 
111 def kl(Y, Y_hat, batch_axis):
112     """
113     Warning: This function expects a sigmoid nonlinearity in the
114     output layer. Returns a batch (vector) of mean across units of
115     KL divergence for each example,
116     KL(P || Q) where P is defined by Y and Q is defined by Y_hat:
117 
118     p log p - p log q + (1-p) log (1-p) - (1-p) log (1-q)
119     For binary p, some terms drop out:
120     - p log q - (1-p) log (1-q)
121     - p log sigmoid(z) - (1-p) log sigmoid(-z)
122     p softplus(-z) + (1-p) softplus(z)
123 
124     Parameters
125     ----------
126     Y : Variable
127         targets for the sigmoid outputs. Currently Y must be purely binary.
128         If it's not, you'll still get the right gradient, but the
129         value in the monitoring channel will be wrong.
130     Y_hat : Variable
131         predictions made by the sigmoid layer. Y_hat must be generated by
132         fprop, i.e., it must be a symbolic sigmoid.
133     batch_axis : list
134         list of axes to compute average kl divergence across.
135 
136     Returns
137     -------
138     ave : Variable
139         average kl divergence between Y and Y_hat.
140     """
141 
142     assert hasattr(Y_hat, 'owner')
143     assert batch_axis is not None
144 
145     owner = Y_hat.owner
146     assert owner is not None
147     op = owner.op
148 
149     if not hasattr(op, 'scalar_op'):
150         raise ValueError("Expected Y_hat to be generated by an Elemwise "
151                          "op, got "+str(op)+" of type "+str(type(op)))
152     assert isinstance(op.scalar_op, T.nnet.sigm.ScalarSigmoid)
153 
154     for Yv in get_debug_values(Y):
155         if not (Yv.min() >= 0.0 and Yv.max() <= 1.0):
156             raise ValueError("Expected Y to be between 0 and 1. Either Y"
157                              + "< 0 or Y > 1 was found in the input.")
158 
159     z, = owner.inputs
160 
161     term_1 = Y * T.nnet.softplus(-z)
162     term_2 = (1 - Y) * T.nnet.softplus(z)
163 
164     total = term_1 + term_2
165     naxes = total.ndim
166     axes_to_reduce = list(range(naxes))
167     del axes_to_reduce[batch_axis]
168     ave = total.mean(axis=axes_to_reduce)
169 
170     return ave
171 
172 def elemwise_kl(Y, Y_hat):
173     """
174     Warning: This function expects a sigmoid nonlinearity in the
175     output layer. Returns a batch (vector) of mean across units of
176     KL divergence for each example,
177     KL(P || Q) where P is defined by Y and Q is defined by Y_hat:
178 
179     p log p - p log q + (1-p) log (1-p) - (1-p) log (1-q)
180     For binary p, some terms drop out:
181     - p log q - (1-p) log (1-q)
182     - p log sigmoid(z) - (1-p) log sigmoid(-z)
183     p softplus(-z) + (1-p) softplus(z)
184 
185     Parameters
186     ----------
187     Y : Variable
188         targets for the sigmoid outputs. Currently Y must be purely binary.
189         If it's not, you'll still get the right gradient, but the
190         value in the monitoring channel will be wrong.
191     Y_hat : Variable
192         predictions made by the sigmoid layer. Y_hat must be generated by
193         fprop, i.e., it must be a symbolic sigmoid.
194 
195     Returns
196     -------
197     ave : Variable
198         kl divergence between Y and Y_hat.
199     """
200     assert hasattr(Y_hat, 'owner')
201 
202     owner = Y_hat.owner
203     assert owner is not None
204     op = owner.op
205 
206     if not hasattr(op, 'scalar_op'):
207         raise ValueError("Expected Y_hat to be generated by an Elemwise "
208                          "op, got "+str(op)+" of type "+str(type(op)))
209     assert isinstance(op.scalar_op, T.nnet.sigm.ScalarSigmoid)
210 
211     for Yv in get_debug_values(Y):
212         if not (Yv.min() >= 0.0 and Yv.max() <= 1.0):
213             raise ValueError("Expected Y to be between 0 and 1. Either Y"
214                              + "< 0 or Y > 1 was found in the input.")
215 
216     z, = owner.inputs
217 
218     term_1 = Y * T.nnet.softplus(-z)
219     term_2 = (1 - Y) * T.nnet.softplus(z)
220 
221     total = term_1 + term_2
222 
223     return total
224 
225 
226 def softmax_ratio(numer, denom):
227     """
228     .. todo::
229 
230         WRITEME properly
231 
232     Parameters
233     ----------
234     numer : Variable
235         Output of a softmax.
236     denom : Variable
237         Output of a softmax.
238 
239     Returns
240     -------
241     ratio : Variable
242         numer / denom, computed in a numerically stable way
243     """
244 
245     numer_Z = arg_of_softmax(numer)
246     denom_Z = arg_of_softmax(denom)
247     numer_Z -= numer_Z.max(axis=1).dimshuffle(0, 'x')
248     denom_Z -= denom_Z.min(axis=1).dimshuffle(0, 'x')
249 
250     new_num = T.exp(numer_Z - denom_Z) * (T.exp(denom_Z).sum(
251         axis=1).dimshuffle(0, 'x'))
252     new_den = (T.exp(numer_Z).sum(axis=1).dimshuffle(0, 'x'))
253 
254     return new_num / new_den
255 
256 def compute_precision(tp, fp):
257     """
258     Computes the precision for the binary decisions.
259     Computed as tp/(tp + fp).
260 
261     Parameters
262     ----------
263     tp : Variable
264         True positives.
265     fp : Variable
266         False positives.
267 
268     Returns
269     -------
270     precision : Variable
271         Precision of the binary classifications.
272     """
273     precision = tp / T.maximum(1., tp + fp)
274     return precision
275 
276 def compute_recall(y, tp):
277     """
278     Computes the recall for the binary classification.
279 
280     Parameters
281     ----------
282     y : Variable
283         Targets for the binary classifications.
284     tp : Variable
285         True positives.
286 
287     Returns
288     -------
289     recall : Variable
290         Recall for the binary classification.
291     """
292     recall = tp / T.maximum(1., y.sum())
293     return recall
294 
295 def compute_f1(precision, recall):
296     """
297     Computes the f1 score for the binary classification.
298     Computed as,
299 
300     f1 = 2 * precision * recall / (precision + recall)
301 
302     Parameters
303     ----------
304     precision : Variable
305         Precision score of the binary decisions.
306     recall : Variable
307         Recall score of the binary decisions.
308 
309     Returns
310     -------
311     f1 : Variable
312         f1 score for the binary decisions.
313     """
314     f1 = (2. * precision * recall /
315             T.maximum(1, precision + recall))
316     return f1
317 
318
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/pylearn2/expr/nnet.py b/pylearn2/expr/nnet.py
--- a/pylearn2/expr/nnet.py
+++ b/pylearn2/expr/nnet.py
@@ -14,6 +14,7 @@
 from theano import tensor as T
 from theano.gof.op import get_debug_values
 
+
 def softmax_numpy(x):
     """
     .. todo::
@@ -33,6 +34,7 @@
     numer = np.exp(stable_x)
     return (numer.T / numer.sum(axis=1)).T
 
+
 def pseudoinverse_softmax_numpy(x):
     """
     .. todo::
@@ -56,6 +58,7 @@
     rval -= rval.mean()
     return rval
 
+
 def sigmoid_numpy(x):
     """
     .. todo::
@@ -65,6 +68,7 @@
     assert not isinstance(x, theano.gof.Variable)
     return 1. / (1. + np.exp(-x))
 
+
 def inverse_sigmoid_numpy(x):
     """
     .. todo::
@@ -73,6 +77,7 @@
     """
     return np.log(x / (1. - x))
 
+
 def arg_of_softmax(Y_hat):
     """
     Given the output of a call to theano.tensor.nnet.softmax,
@@ -102,12 +107,13 @@
         op = owner.op
     if not isinstance(op, T.nnet.Softmax):
         raise ValueError("Expected Y_hat to be the output of a softmax, "
-                "but it appears to be the output of " + str(op) + " of type "
-                + str(type(op)))
-    z ,= owner.inputs
+                         "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
@@ -169,6 +175,7 @@
 
     return ave
 
+
 def elemwise_kl(Y, Y_hat):
     """
     Warning: This function expects a sigmoid nonlinearity in the
@@ -253,6 +260,7 @@
 
     return new_num / new_den
 
+
 def compute_precision(tp, fp):
     """
     Computes the precision for the binary decisions.
@@ -273,6 +281,7 @@
     precision = tp / T.maximum(1., tp + fp)
     return precision
 
+
 def compute_recall(y, tp):
     """
     Computes the recall for the binary classification.
@@ -292,6 +301,7 @@
     recall = tp / T.maximum(1., y.sum())
     return recall
 
+
 def compute_f1(precision, recall):
     """
     Computes the f1 score for the binary classification.
@@ -312,6 +322,5 @@
         f1 score for the binary decisions.
     """
     f1 = (2. * precision * recall /
-            T.maximum(1, precision + recall))
-    return f1
-
+          T.maximum(1, precision + recall))
+    return f1
\ No newline at end of file

{"golden_diff": "diff --git a/pylearn2/expr/nnet.py b/pylearn2/expr/nnet.py\n--- a/pylearn2/expr/nnet.py\n+++ b/pylearn2/expr/nnet.py\n@@ -14,6 +14,7 @@\n from theano import tensor as T\n from theano.gof.op import get_debug_values\n \n+\n def softmax_numpy(x):\n     \"\"\"\n     .. todo::\n@@ -33,6 +34,7 @@\n     numer = np.exp(stable_x)\n     return (numer.T / numer.sum(axis=1)).T\n \n+\n def pseudoinverse_softmax_numpy(x):\n     \"\"\"\n     .. todo::\n@@ -56,6 +58,7 @@\n     rval -= rval.mean()\n     return rval\n \n+\n def sigmoid_numpy(x):\n     \"\"\"\n     .. todo::\n@@ -65,6 +68,7 @@\n     assert not isinstance(x, theano.gof.Variable)\n     return 1. / (1. + np.exp(-x))\n \n+\n def inverse_sigmoid_numpy(x):\n     \"\"\"\n     .. todo::\n@@ -73,6 +77,7 @@\n     \"\"\"\n     return np.log(x / (1. - x))\n \n+\n def arg_of_softmax(Y_hat):\n     \"\"\"\n     Given the output of a call to theano.tensor.nnet.softmax,\n@@ -102,12 +107,13 @@\n         op = owner.op\n     if not isinstance(op, T.nnet.Softmax):\n         raise ValueError(\"Expected Y_hat to be the output of a softmax, \"\n-                \"but it appears to be the output of \" + str(op) + \" of type \"\n-                + str(type(op)))\n-    z ,= owner.inputs\n+                         \"but it appears to be the output of \" + str(op) +\n+                         \" of type \" + str(type(op)))\n+    z, = owner.inputs\n     assert z.ndim == 2\n     return z\n \n+\n def kl(Y, Y_hat, batch_axis):\n     \"\"\"\n     Warning: This function expects a sigmoid nonlinearity in the\n@@ -169,6 +175,7 @@\n \n     return ave\n \n+\n def elemwise_kl(Y, Y_hat):\n     \"\"\"\n     Warning: This function expects a sigmoid nonlinearity in the\n@@ -253,6 +260,7 @@\n \n     return new_num / new_den\n \n+\n def compute_precision(tp, fp):\n     \"\"\"\n     Computes the precision for the binary decisions.\n@@ -273,6 +281,7 @@\n     precision = tp / T.maximum(1., tp + fp)\n     return precision\n \n+\n def compute_recall(y, tp):\n     \"\"\"\n     Computes the recall for the binary classification.\n@@ -292,6 +301,7 @@\n     recall = tp / T.maximum(1., y.sum())\n     return recall\n \n+\n def compute_f1(precision, recall):\n     \"\"\"\n     Computes the f1 score for the binary classification.\n@@ -312,6 +322,5 @@\n         f1 score for the binary decisions.\n     \"\"\"\n     f1 = (2. * precision * recall /\n-            T.maximum(1, precision + recall))\n-    return f1\n-\n+          T.maximum(1, precision + recall))\n+    return f1\n\\ No newline at end of file\n", "issue": "Remove core files from whitelist\n- [x] models/mlp.py\n- [x] `costs/mlp/__init__.py`\n- [x] costs/mlp/dropout.py\n- [x] monitor.py\n- [x] optimization/batch_gradient_descent.py\n- [x] blocks.py\n- [ ] expr/nnet.py\n- [x] costs/cost.py\n- [x] datasets/dense_design_matrix.py\n- [x] datasets/dataset.py\n\n", "before_files": [{"content": "\"\"\"\nUseful expressions common to many neural network applications.\n\"\"\"\n__authors__ = \"Ian Goodfellow\"\n__copyright__ = \"Copyright 2010-2013, Universite de Montreal\"\n__credits__ = [\"Ian Goodfellow\"]\n__license__ = \"3-clause BSD\"\n__maintainer__ = \"LISA Lab\"\n__email__ = \"pylearn-dev@googlegroups\"\n\nimport numpy as np\nimport theano\nfrom theano.printing import Print\nfrom theano import tensor as T\nfrom theano.gof.op import get_debug_values\n\ndef softmax_numpy(x):\n    \"\"\"\n    .. todo::\n\n        WRITEME properly\n\n    Parameters\n    ----------\n    x : matrix\n\n    Returns\n    -------\n    rval : vector\n        rval[i] is the softmax of row i of x\n    \"\"\"\n    stable_x = (x.T - x.max(axis=1)).T\n    numer = np.exp(stable_x)\n    return (numer.T / numer.sum(axis=1)).T\n\ndef pseudoinverse_softmax_numpy(x):\n    \"\"\"\n    .. todo::\n\n        WRITEME properly\n\n    Parameters\n    ----------\n    x : vector\n\n    Returns\n    -------\n    y : vector\n        softmax(y) = x\n\n    Notes\n    -----\n    This problem is underdetermined, so we also impose y.mean() = 0\n    \"\"\"\n    rval = np.log(x)\n    rval -= rval.mean()\n    return rval\n\ndef sigmoid_numpy(x):\n    \"\"\"\n    .. todo::\n\n        WRITEME\n    \"\"\"\n    assert not isinstance(x, theano.gof.Variable)\n    return 1. / (1. + np.exp(-x))\n\ndef inverse_sigmoid_numpy(x):\n    \"\"\"\n    .. todo::\n\n        WRITEME\n    \"\"\"\n    return np.log(x / (1. - x))\n\ndef arg_of_softmax(Y_hat):\n    \"\"\"\n    Given the output of a call to theano.tensor.nnet.softmax,\n    returns the argument to the softmax (by tracing the Theano\n    graph).\n\n    Parameters\n    ----------\n    Y_hat : Variable\n        softmax(Z)\n\n    Returns\n    -------\n    Z : Variable\n        The variable that was passed to the Softmax op to create `Y_hat`.\n        Raises an error if `Y_hat` is not actually the output of a\n        Softmax.\n    \"\"\"\n    assert hasattr(Y_hat, 'owner')\n    owner = Y_hat.owner\n    assert owner is not None\n    op = owner.op\n    if isinstance(op, Print):\n        assert len(owner.inputs) == 1\n        Y_hat, = owner.inputs\n        owner = Y_hat.owner\n        op = owner.op\n    if not isinstance(op, T.nnet.Softmax):\n        raise ValueError(\"Expected Y_hat to be the output of a softmax, \"\n                \"but it appears to be the output of \" + str(op) + \" of type \"\n                + str(type(op)))\n    z ,= owner.inputs\n    assert z.ndim == 2\n    return z\n\ndef kl(Y, Y_hat, batch_axis):\n    \"\"\"\n    Warning: This function expects a sigmoid nonlinearity in the\n    output layer. Returns a batch (vector) of mean across units of\n    KL divergence for each example,\n    KL(P || Q) where P is defined by Y and Q is defined by Y_hat:\n\n    p log p - p log q + (1-p) log (1-p) - (1-p) log (1-q)\n    For binary p, some terms drop out:\n    - p log q - (1-p) log (1-q)\n    - p log sigmoid(z) - (1-p) log sigmoid(-z)\n    p softplus(-z) + (1-p) softplus(z)\n\n    Parameters\n    ----------\n    Y : Variable\n        targets for the sigmoid outputs. Currently Y must be purely binary.\n        If it's not, you'll still get the right gradient, but the\n        value in the monitoring channel will be wrong.\n    Y_hat : Variable\n        predictions made by the sigmoid layer. Y_hat must be generated by\n        fprop, i.e., it must be a symbolic sigmoid.\n    batch_axis : list\n        list of axes to compute average kl divergence across.\n\n    Returns\n    -------\n    ave : Variable\n        average kl divergence between Y and Y_hat.\n    \"\"\"\n\n    assert hasattr(Y_hat, 'owner')\n    assert batch_axis is not None\n\n    owner = Y_hat.owner\n    assert owner is not None\n    op = owner.op\n\n    if not hasattr(op, 'scalar_op'):\n        raise ValueError(\"Expected Y_hat to be generated by an Elemwise \"\n                         \"op, got \"+str(op)+\" of type \"+str(type(op)))\n    assert isinstance(op.scalar_op, T.nnet.sigm.ScalarSigmoid)\n\n    for Yv in get_debug_values(Y):\n        if not (Yv.min() >= 0.0 and Yv.max() <= 1.0):\n            raise ValueError(\"Expected Y to be between 0 and 1. Either Y\"\n                             + \"< 0 or Y > 1 was found in the input.\")\n\n    z, = owner.inputs\n\n    term_1 = Y * T.nnet.softplus(-z)\n    term_2 = (1 - Y) * T.nnet.softplus(z)\n\n    total = term_1 + term_2\n    naxes = total.ndim\n    axes_to_reduce = list(range(naxes))\n    del axes_to_reduce[batch_axis]\n    ave = total.mean(axis=axes_to_reduce)\n\n    return ave\n\ndef elemwise_kl(Y, Y_hat):\n    \"\"\"\n    Warning: This function expects a sigmoid nonlinearity in the\n    output layer. Returns a batch (vector) of mean across units of\n    KL divergence for each example,\n    KL(P || Q) where P is defined by Y and Q is defined by Y_hat:\n\n    p log p - p log q + (1-p) log (1-p) - (1-p) log (1-q)\n    For binary p, some terms drop out:\n    - p log q - (1-p) log (1-q)\n    - p log sigmoid(z) - (1-p) log sigmoid(-z)\n    p softplus(-z) + (1-p) softplus(z)\n\n    Parameters\n    ----------\n    Y : Variable\n        targets for the sigmoid outputs. Currently Y must be purely binary.\n        If it's not, you'll still get the right gradient, but the\n        value in the monitoring channel will be wrong.\n    Y_hat : Variable\n        predictions made by the sigmoid layer. Y_hat must be generated by\n        fprop, i.e., it must be a symbolic sigmoid.\n\n    Returns\n    -------\n    ave : Variable\n        kl divergence between Y and Y_hat.\n    \"\"\"\n    assert hasattr(Y_hat, 'owner')\n\n    owner = Y_hat.owner\n    assert owner is not None\n    op = owner.op\n\n    if not hasattr(op, 'scalar_op'):\n        raise ValueError(\"Expected Y_hat to be generated by an Elemwise \"\n                         \"op, got \"+str(op)+\" of type \"+str(type(op)))\n    assert isinstance(op.scalar_op, T.nnet.sigm.ScalarSigmoid)\n\n    for Yv in get_debug_values(Y):\n        if not (Yv.min() >= 0.0 and Yv.max() <= 1.0):\n            raise ValueError(\"Expected Y to be between 0 and 1. Either Y\"\n                             + \"< 0 or Y > 1 was found in the input.\")\n\n    z, = owner.inputs\n\n    term_1 = Y * T.nnet.softplus(-z)\n    term_2 = (1 - Y) * T.nnet.softplus(z)\n\n    total = term_1 + term_2\n\n    return total\n\n\ndef softmax_ratio(numer, denom):\n    \"\"\"\n    .. todo::\n\n        WRITEME properly\n\n    Parameters\n    ----------\n    numer : Variable\n        Output of a softmax.\n    denom : Variable\n        Output of a softmax.\n\n    Returns\n    -------\n    ratio : Variable\n        numer / denom, computed in a numerically stable way\n    \"\"\"\n\n    numer_Z = arg_of_softmax(numer)\n    denom_Z = arg_of_softmax(denom)\n    numer_Z -= numer_Z.max(axis=1).dimshuffle(0, 'x')\n    denom_Z -= denom_Z.min(axis=1).dimshuffle(0, 'x')\n\n    new_num = T.exp(numer_Z - denom_Z) * (T.exp(denom_Z).sum(\n        axis=1).dimshuffle(0, 'x'))\n    new_den = (T.exp(numer_Z).sum(axis=1).dimshuffle(0, 'x'))\n\n    return new_num / new_den\n\ndef compute_precision(tp, fp):\n    \"\"\"\n    Computes the precision for the binary decisions.\n    Computed as tp/(tp + fp).\n\n    Parameters\n    ----------\n    tp : Variable\n        True positives.\n    fp : Variable\n        False positives.\n\n    Returns\n    -------\n    precision : Variable\n        Precision of the binary classifications.\n    \"\"\"\n    precision = tp / T.maximum(1., tp + fp)\n    return precision\n\ndef compute_recall(y, tp):\n    \"\"\"\n    Computes the recall for the binary classification.\n\n    Parameters\n    ----------\n    y : Variable\n        Targets for the binary classifications.\n    tp : Variable\n        True positives.\n\n    Returns\n    -------\n    recall : Variable\n        Recall for the binary classification.\n    \"\"\"\n    recall = tp / T.maximum(1., y.sum())\n    return recall\n\ndef compute_f1(precision, recall):\n    \"\"\"\n    Computes the f1 score for the binary classification.\n    Computed as,\n\n    f1 = 2 * precision * recall / (precision + recall)\n\n    Parameters\n    ----------\n    precision : Variable\n        Precision score of the binary decisions.\n    recall : Variable\n        Recall score of the binary decisions.\n\n    Returns\n    -------\n    f1 : Variable\n        f1 score for the binary decisions.\n    \"\"\"\n    f1 = (2. * precision * recall /\n            T.maximum(1, precision + recall))\n    return f1\n\n", "path": "pylearn2/expr/nnet.py"}], "after_files": [{"content": "\"\"\"\nUseful expressions common to many neural network applications.\n\"\"\"\n__authors__ = \"Ian Goodfellow\"\n__copyright__ = \"Copyright 2010-2013, Universite de Montreal\"\n__credits__ = [\"Ian Goodfellow\"]\n__license__ = \"3-clause BSD\"\n__maintainer__ = \"LISA Lab\"\n__email__ = \"pylearn-dev@googlegroups\"\n\nimport numpy as np\nimport theano\nfrom theano.printing import Print\nfrom theano import tensor as T\nfrom theano.gof.op import get_debug_values\n\n\ndef softmax_numpy(x):\n    \"\"\"\n    .. todo::\n\n        WRITEME properly\n\n    Parameters\n    ----------\n    x : matrix\n\n    Returns\n    -------\n    rval : vector\n        rval[i] is the softmax of row i of x\n    \"\"\"\n    stable_x = (x.T - x.max(axis=1)).T\n    numer = np.exp(stable_x)\n    return (numer.T / numer.sum(axis=1)).T\n\n\ndef pseudoinverse_softmax_numpy(x):\n    \"\"\"\n    .. todo::\n\n        WRITEME properly\n\n    Parameters\n    ----------\n    x : vector\n\n    Returns\n    -------\n    y : vector\n        softmax(y) = x\n\n    Notes\n    -----\n    This problem is underdetermined, so we also impose y.mean() = 0\n    \"\"\"\n    rval = np.log(x)\n    rval -= rval.mean()\n    return rval\n\n\ndef sigmoid_numpy(x):\n    \"\"\"\n    .. todo::\n\n        WRITEME\n    \"\"\"\n    assert not isinstance(x, theano.gof.Variable)\n    return 1. / (1. + np.exp(-x))\n\n\ndef inverse_sigmoid_numpy(x):\n    \"\"\"\n    .. todo::\n\n        WRITEME\n    \"\"\"\n    return np.log(x / (1. - x))\n\n\ndef arg_of_softmax(Y_hat):\n    \"\"\"\n    Given the output of a call to theano.tensor.nnet.softmax,\n    returns the argument to the softmax (by tracing the Theano\n    graph).\n\n    Parameters\n    ----------\n    Y_hat : Variable\n        softmax(Z)\n\n    Returns\n    -------\n    Z : Variable\n        The variable that was passed to the Softmax op to create `Y_hat`.\n        Raises an error if `Y_hat` is not actually the output of a\n        Softmax.\n    \"\"\"\n    assert hasattr(Y_hat, 'owner')\n    owner = Y_hat.owner\n    assert owner is not None\n    op = owner.op\n    if isinstance(op, Print):\n        assert len(owner.inputs) == 1\n        Y_hat, = owner.inputs\n        owner = Y_hat.owner\n        op = owner.op\n    if not isinstance(op, T.nnet.Softmax):\n        raise ValueError(\"Expected Y_hat to be the output of a softmax, \"\n                         \"but it appears to be the output of \" + str(op) +\n                         \" of type \" + str(type(op)))\n    z, = owner.inputs\n    assert z.ndim == 2\n    return z\n\n\ndef kl(Y, Y_hat, batch_axis):\n    \"\"\"\n    Warning: This function expects a sigmoid nonlinearity in the\n    output layer. Returns a batch (vector) of mean across units of\n    KL divergence for each example,\n    KL(P || Q) where P is defined by Y and Q is defined by Y_hat:\n\n    p log p - p log q + (1-p) log (1-p) - (1-p) log (1-q)\n    For binary p, some terms drop out:\n    - p log q - (1-p) log (1-q)\n    - p log sigmoid(z) - (1-p) log sigmoid(-z)\n    p softplus(-z) + (1-p) softplus(z)\n\n    Parameters\n    ----------\n    Y : Variable\n        targets for the sigmoid outputs. Currently Y must be purely binary.\n        If it's not, you'll still get the right gradient, but the\n        value in the monitoring channel will be wrong.\n    Y_hat : Variable\n        predictions made by the sigmoid layer. Y_hat must be generated by\n        fprop, i.e., it must be a symbolic sigmoid.\n    batch_axis : list\n        list of axes to compute average kl divergence across.\n\n    Returns\n    -------\n    ave : Variable\n        average kl divergence between Y and Y_hat.\n    \"\"\"\n\n    assert hasattr(Y_hat, 'owner')\n    assert batch_axis is not None\n\n    owner = Y_hat.owner\n    assert owner is not None\n    op = owner.op\n\n    if not hasattr(op, 'scalar_op'):\n        raise ValueError(\"Expected Y_hat to be generated by an Elemwise \"\n                         \"op, got \"+str(op)+\" of type \"+str(type(op)))\n    assert isinstance(op.scalar_op, T.nnet.sigm.ScalarSigmoid)\n\n    for Yv in get_debug_values(Y):\n        if not (Yv.min() >= 0.0 and Yv.max() <= 1.0):\n            raise ValueError(\"Expected Y to be between 0 and 1. Either Y\"\n                             + \"< 0 or Y > 1 was found in the input.\")\n\n    z, = owner.inputs\n\n    term_1 = Y * T.nnet.softplus(-z)\n    term_2 = (1 - Y) * T.nnet.softplus(z)\n\n    total = term_1 + term_2\n    naxes = total.ndim\n    axes_to_reduce = list(range(naxes))\n    del axes_to_reduce[batch_axis]\n    ave = total.mean(axis=axes_to_reduce)\n\n    return ave\n\n\ndef elemwise_kl(Y, Y_hat):\n    \"\"\"\n    Warning: This function expects a sigmoid nonlinearity in the\n    output layer. Returns a batch (vector) of mean across units of\n    KL divergence for each example,\n    KL(P || Q) where P is defined by Y and Q is defined by Y_hat:\n\n    p log p - p log q + (1-p) log (1-p) - (1-p) log (1-q)\n    For binary p, some terms drop out:\n    - p log q - (1-p) log (1-q)\n    - p log sigmoid(z) - (1-p) log sigmoid(-z)\n    p softplus(-z) + (1-p) softplus(z)\n\n    Parameters\n    ----------\n    Y : Variable\n        targets for the sigmoid outputs. Currently Y must be purely binary.\n        If it's not, you'll still get the right gradient, but the\n        value in the monitoring channel will be wrong.\n    Y_hat : Variable\n        predictions made by the sigmoid layer. Y_hat must be generated by\n        fprop, i.e., it must be a symbolic sigmoid.\n\n    Returns\n    -------\n    ave : Variable\n        kl divergence between Y and Y_hat.\n    \"\"\"\n    assert hasattr(Y_hat, 'owner')\n\n    owner = Y_hat.owner\n    assert owner is not None\n    op = owner.op\n\n    if not hasattr(op, 'scalar_op'):\n        raise ValueError(\"Expected Y_hat to be generated by an Elemwise \"\n                         \"op, got \"+str(op)+\" of type \"+str(type(op)))\n    assert isinstance(op.scalar_op, T.nnet.sigm.ScalarSigmoid)\n\n    for Yv in get_debug_values(Y):\n        if not (Yv.min() >= 0.0 and Yv.max() <= 1.0):\n            raise ValueError(\"Expected Y to be between 0 and 1. Either Y\"\n                             + \"< 0 or Y > 1 was found in the input.\")\n\n    z, = owner.inputs\n\n    term_1 = Y * T.nnet.softplus(-z)\n    term_2 = (1 - Y) * T.nnet.softplus(z)\n\n    total = term_1 + term_2\n\n    return total\n\n\ndef softmax_ratio(numer, denom):\n    \"\"\"\n    .. todo::\n\n        WRITEME properly\n\n    Parameters\n    ----------\n    numer : Variable\n        Output of a softmax.\n    denom : Variable\n        Output of a softmax.\n\n    Returns\n    -------\n    ratio : Variable\n        numer / denom, computed in a numerically stable way\n    \"\"\"\n\n    numer_Z = arg_of_softmax(numer)\n    denom_Z = arg_of_softmax(denom)\n    numer_Z -= numer_Z.max(axis=1).dimshuffle(0, 'x')\n    denom_Z -= denom_Z.min(axis=1).dimshuffle(0, 'x')\n\n    new_num = T.exp(numer_Z - denom_Z) * (T.exp(denom_Z).sum(\n        axis=1).dimshuffle(0, 'x'))\n    new_den = (T.exp(numer_Z).sum(axis=1).dimshuffle(0, 'x'))\n\n    return new_num / new_den\n\n\ndef compute_precision(tp, fp):\n    \"\"\"\n    Computes the precision for the binary decisions.\n    Computed as tp/(tp + fp).\n\n    Parameters\n    ----------\n    tp : Variable\n        True positives.\n    fp : Variable\n        False positives.\n\n    Returns\n    -------\n    precision : Variable\n        Precision of the binary classifications.\n    \"\"\"\n    precision = tp / T.maximum(1., tp + fp)\n    return precision\n\n\ndef compute_recall(y, tp):\n    \"\"\"\n    Computes the recall for the binary classification.\n\n    Parameters\n    ----------\n    y : Variable\n        Targets for the binary classifications.\n    tp : Variable\n        True positives.\n\n    Returns\n    -------\n    recall : Variable\n        Recall for the binary classification.\n    \"\"\"\n    recall = tp / T.maximum(1., y.sum())\n    return recall\n\n\ndef compute_f1(precision, recall):\n    \"\"\"\n    Computes the f1 score for the binary classification.\n    Computed as,\n\n    f1 = 2 * precision * recall / (precision + recall)\n\n    Parameters\n    ----------\n    precision : Variable\n        Precision score of the binary decisions.\n    recall : Variable\n        Recall score of the binary decisions.\n\n    Returns\n    -------\n    f1 : Variable\n        f1 score for the binary decisions.\n    \"\"\"\n    f1 = (2. * precision * recall /\n          T.maximum(1, precision + recall))\n    return f1", "path": "pylearn2/expr/nnet.py"}]}