函数向量化,可以对不同的元素使用不同的方法
def func(a,b):
'''针对a,b的操作'''
pass
inputs = np.randn((128,10))
用法
np.vectorize(func)(inputs)
例子
import torch
def func(a,b):
if a > b:
return a + b
else:
return a - b
x = torch.rand((1,3))
y = torch.rand((1,3))
print(x)
print(y)
print(np.vectorize(func)(x,y))
outputs:
tensor([[0.0838, 0.5305, 0.7352]])
tensor([[0.4186, 0.2242, 0.9994]])
[[-0.33478993 0.75468814 -0.26422805]]
971

被折叠的 条评论
为什么被折叠?



