Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pytorch 为什么torch.where方法不像numpy.where那样工作?_Pytorch - Fatal编程技术网

Pytorch 为什么torch.where方法不像numpy.where那样工作?

Pytorch 为什么torch.where方法不像numpy.where那样工作?,pytorch,Pytorch,为了使用Numpy在随机向量中用某个数字替换正值,用另一个数字替换负值,可以执行以下操作: npy_p = np.random.randn(4,6) quant = np.where(npy_p>0, c_plus , np.where(npy_p<0, c_minus , npy_p)) npy\u p=np.random.randn(4,6) quant=np.where(npy_p>0,c_plus,np.where)(npy_p我无法重现此错误,如果您可以分享一个失败的具体

为了使用
Numpy
在随机向量中用某个数字替换正值,用另一个数字替换负值,可以执行以下操作:

npy_p = np.random.randn(4,6)
quant = np.where(npy_p>0, c_plus , np.where(npy_p<0, c_minus , npy_p))
npy\u p=np.random.randn(4,6)

quant=np.where(npy_p>0,c_plus,np.where)(npy_p我无法重现此错误,如果您可以分享一个失败的具体示例(可能是您尝试填充张量的值),可能会更好:

res
是:

tensor([[ 0.1391,  0.0000,  0.2363,  0.0000,  0.0000,  0.0000],
        [ 0.0000, -1.0000,  0.2869,  0.2575,  0.0000,  0.0000],
        [ 0.0000,  0.1138, -1.0000,  0.0000,  0.1553,  0.0000],
        [ 0.0000,  0.0000,  0.0000, -1.0000,  0.0000,  0.0000]])
这个问题是由于您在
火炬中混合了数据类型而引起的。其中
,如果您在常量中明确使用与张量相同的数据类型,则可以正常工作。

使用
火炬。其中(x<0.1,-1,1)
您将替换两次。目标是在最终结果中只有两个数字,而不是像您的
0,1,-1那样有3个值。请查看numpy案例的示例。
tensor([[0.1391, 0.4491, 0.2363, 0.3215, 0.7740, 0.4879],
        [0.3051, 0.0870, 0.2869, 0.2575, 0.8825, 0.8201],
        [0.4419, 0.1138, 0.0825, 0.9489, 0.1553, 0.6505],
        [0.8376, 0.7639, 0.9291, 0.0865, 0.5984, 0.3953]])
tensor([[ 0.1391,  0.0000,  0.2363,  0.0000,  0.0000,  0.0000],
        [ 0.0000, -1.0000,  0.2869,  0.2575,  0.0000,  0.0000],
        [ 0.0000,  0.1138, -1.0000,  0.0000,  0.1553,  0.0000],
        [ 0.0000,  0.0000,  0.0000, -1.0000,  0.0000,  0.0000]])