Cleanest way to combine reduce and map in Python
<p>I'm doing a little deep learning, and I want to grab the values of all hidden layers.  So I end up writing functions like this:</p>

<pre><code>def forward_pass(x, ws, bs):
    activations = []
    u = x
    for w, b in zip(ws, bs):
        u = np.maximum(0, u.dot(w)+b)
        activations.append(u)
    return activations
</code></pre>

<p>If I didn't have to get the intermediate values, I'd use the much less verbose form:</p>

<pre><code>out = reduce(lambda u, (w, b): np.maximum(0, u.dot(w)+b), zip(ws, bs), x)
</code></pre>

<p>Bam.  All one line, nice and compact.  But I can't keep any of the intermediate values.</p>

<p>So, what is there any way to have my cake (nice compact one-liner) and eat it too (return intermediate values)?</p>

<p><strong>Edit:  My conclusions so far:</strong><br>
In Python 2.x, there is no clean one-liner for this.<br>
In Python 3, there is <code>itertools.accumulate</code>, but it is still not really clean because it doesn't accept an "initial" input, as <code>reduce</code> does.<br>
In Python 4, I hereby request a "map-reduce comprehension":  </p>

<pre><code>activations = [u=np.maximum(0, u.dot(w)+b) for w, b in zip(ws, bs) from u=x]  
</code></pre>

<p>Which would also give a useful job to that <code>from</code> keyword, which normally just sits around doing nothing after all the imports are done.</p>

<p>In general, <a href="https://docs.python.org/3/library/itertools.html#module-itertools" rel="nofollow"><em>itertools.accumulate()</em></a> will do what <a href="https://docs.python.org/3/library/functools.html#functools.reduce" rel="nofollow"><em>reduce()</em></a> does but will give you the intermediate values as well.  That said, accumulate does not support <em>start</em> value so it make not be applicable in your case.</p>

<p>Example:</p>

<pre><code>&gt;&gt;&gt; import operator, functools, itertools
&gt;&gt;&gt; functools.reduce(operator.mul, range(1, 11))
3628800
&gt;&gt;&gt; list(itertools.accumulate(range(1, 11), operator.mul))
[1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
</code></pre>

python numpy deep-learning reduce