gpt4 book ai didi

int - 如何在 PyTorch 中将 boolean 张量转换为整数?

转载 作者:行者123 更新时间:2023-12-04 00:00:48 33 4
gpt4 key购买 nike

假设,我们有一个张量

t = torch.tensor([True, False, True, False])

我们如何将其转换为具有值 [1, 0, 1, 0] 的整数张量?

最佳答案

解决方案只是一行代码。
转换张量 t[True, False, True, False]对于整数张量,只需执行以下操作。

t = torch.tensor([True, False, True, False])
t_integer = t.long()
print(t_integer)
[1, 0, 1, 0]

关于int - 如何在 PyTorch 中将 boolean 张量转换为整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62150659/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com