python中tensor与variable_Pytorch 中 Variable 和 Tensor 的 bool 特性

Variable 的 bool

>>> vx

Variable containing:

0.3535 0.5137 0.4131 0.5732

0.8076 0.4160 1.0000 0.6436

0.3682 0.8086 0.4863 0.9268

[torch.cuda.FloatTensor of size 3x4 (GPU 0)]

>>> vx > 0.5

Variable containing:

0 1 0 1

1 0 1 1

0 1 0 1

[torch.cuda.ByteTensor of size 3x4 (GPU 0)]

# 不能使用 np.where !!!!!!!!!

>>> np.where(vx>0.5)

Traceback (most recent call last):

File "", line 1, in

File "/anaconda/envs/th36/lib/python3.6/site-packages/torch/autograd/variable.py", line 125, in __bool__

torch.typename(self.data) + " is ambiguous")

RuntimeError: bool value of Variable objects containing non-empty torch.cuda.ByteTensor is ambiguous

# 不能使用 np.bool !!!!!!!!!

>>> np.bool(vx > 0.5)

Traceback (most recent call last):

File "", line 1, in

File "/anaconda/envs/th36/lib/python3.6/site-packages/torch/autograd/variable.py", line 125, in __bool__

torch.typename(self.data) + " is ambiguous")

RuntimeError: bool value of Variable objects containing non-empty torch.cuda.ByteTensor is ambiguous

Tensor 的 bool

>>> vx.data > 0.5

0 1 0 1

1 0 1 1

0 1 0 1

[torch.cuda.ByteTensor of size 3x4 (GPU 0)]

# 可以使用 np.where !!!!!!!!

>>> np.where(vx.data > 0.5)

(array([0, 0, 1, 1, 1, 2, 2]), array([1, 3, 0, 2, 3, 1, 3]))

# 不能使用 np.bool !!!!!!!!!

>>> np.bool(vx.data > 0.5)

Traceback (most recent call last):

File "", line 1, in

File "/anaconda/envs/th36/lib/python3.6/site-packages/torch/tensor.py", line 163, in __bool__

" objects is ambiguous")

RuntimeError: bool value of non-empty torch.cuda.ByteTensor objects is ambiguous

两者的值的比较

import torch

a = torch.tensor([[1,2,3],[4,5,6],[7,8,9],[10,11,12]],dtype=torch.float)

for i in a[0,:]:

if i == 1:

print('Yes')

else:

print('No')

Yes

No

No

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值