The dot
tells me you are using one or more numpy arrays. So I'll try:
In [28]: b=np.array([1,2,3])
In [29]: x=np.arange(9).reshape(3,3)
In [30]: ws=[x,x,x]
In [31]: forward_pass(x,ws,bs)
Out[31]:
[array([[ 16, 19, 22],
[ 43, 55, 67],
[ 70, 91, 112]]),
array([[ 191, 248, 305],
[ 569, 734, 899],
[ 947, 1220, 1493]]),
array([[ 2577, 3321, 4065],
[ 7599, 9801, 12003],
[12621, 16281, 19941]])]
In py3 I have to write the reduce
solution as:
In [32]: functools.reduce(lambda u, wb: np.maximum(0,
u.dot(wb[0])+wb[1]), zip(ws, bs), x)
Out[32]:
array([[ 2577, 3321, 4065],
[ 7599, 9801, 12003],
[12621, 16281, 19941]])
That intermediate value u
that is passed from one evaluation to the next makes a list comprehension tricky.
accumulate
uses the first item as the start. I can work around that with a function like
def foo(u, wb):
if u[0] is None: u=x # x from global
return np.maximum(0, u.dot(wb[0])+wb[1])
Then I need to add extra start values to ws
and bs
:
In [56]: list(itertools.accumulate(zip([None,x,x,x], np.array([0,1,2,3])), foo))
Out[56]:
[(None, 0),
array([[ 16, 19, 22],
[ 43, 55, 67],
[ 70, 91, 112]]),
array([[ 191, 248, 305],
[ 569, 734, 899],
[ 947, 1220, 1493]]),
array([[ 2577, 3321, 4065],
[ 7599, 9801, 12003],
[12621, 16281, 19941]])]
Here's a list comprehension version, using an external u
:
In [66]: u=x.copy()
In [67]: def foo1(wb):
...: v = np.maximum(0, u.dot(wb[0])+wb[1])
...: u[:]=v
...: return v
...:
In [68]: [foo1(wb) for wb in zip(ws,bs)]
Out[68]:
[array([[ 16, 19, 22],
[ 43, 55, 67],
[ 70, 91, 112]]),
array([[ 191, 248, 305],
[ 569, 734, 899],
[ 947, 1220, 1493]]),
array([[ 2577, 3321, 4065],
[ 7599, 9801, 12003],
[12621, 16281, 19941]])]
No real advantage over the original loop with append
.
numpy.ufunc
have an accumulate
method, but that isn't easy to use with custom Python functions. So there is a np.maximum.accumulate
, but I'm not sure how that could be used in this case. (also np.cumsum
which is np.add.accumulate
).