我试图了解 vmap 中的 in_axes 和 out_axes 选项。例如,我想对两个矩阵求和并得到相同形状的输出。
X = np.arange(9).reshape(3,3)
Y = np.arange(0,-9,-1).reshape(3,3)
def sum2(x,y):
return x + y
vmap(sum2,in_axes=((0,1),(0,1)))(X,Y)
我想我分别为 X 和 Y 映射了轴 0 和 1。输出将具有与 X,Y 相同的形状。但我得到了错误,
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-403-103694166574> in <module>
3 def sum2(x,y):
4 return x + y
----> 5 vmap(sum2,in_axes=((0,1),(0,1)))(X,Y)
[... skipping hidden 2 frame]
~/anaconda3/lib/python3.8/site-packages/jax/api_util.py in flatten_axes(name, treedef, axis_tree, kws)
276 assert treedef_is_leaf(leaf)
277 axis_tree, _ = axis_tree
--> 278 raise ValueError(f"{name} specification must be a tree prefix of the "
279 f"corresponding value, got specification {axis_tree} "
280 f"for value tree {treedef}.") from None
ValueError: vmap in_axes specification must be a tree prefix of the corresponding value, got specification ((0, 1), (0, 1)) for value tree PyTreeDef((*, *)).