我正在做一些深度学习,我想获取所有隐藏层的值。所以我最终编写了这样的函数:
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
如果我不必获取中间值,我会使用更简洁的形式:
out = reduce(lambda u, (w, b): np.maximum(0, u.dot(w)+b), zip(ws, bs), x)
嘭。全部一条线,美观紧凑。但我无法保留任何中间值。
那么,有没有什么办法可以让我的蛋糕(漂亮的紧凑型单行)也能吃掉(返回中间值)?
一般来说,itertools.accumulate() https://docs.python.org/3/library/itertools.html#module-itertools会做什么reduce() https://docs.python.org/3/library/functools.html#functools.reduce但也会给你中间值。也就是说,accumulate 不支持start值,因此它不适用于您的情况。
Example:
>>> import operator, functools, itertools
>>> functools.reduce(operator.mul, range(1, 11))
3628800
>>> list(itertools.accumulate(range(1, 11), operator.mul))
[1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)