pytorch - 如何制作火炬张量?
问题描述
我想制作一个仅由 1 或 -1 组成的火炬张量。
我只是尝试使用torch.empty()
and torch.randn()
。
tmp = torch.randint(low=0, high=2, size=(4, 4))
tmp[tmp==0] = -1
tmp
>> tensor([[ 1, -1, -1, 1],
[-1, 1, 1, -1],
[-1, -1, 1, 1],
[-1, 1, -1, 1]])
但是,我不知道哪种方法在时间上最有效。
我想把这段代码尽可能地写成一行,因为这段代码位于__init__ ()
解决方案
为什么不:
tmp = -1 + 2 * torch.randint(low=0, high=2, size=(4, 4))
推荐阅读
- asp.net-mvc - 如何在 WEB API 中生成 REST 完整服务
- firebase - 使用 vuejs 和 firestore 进行地理定位
- macos - “$ mdls -recursive”是否存在?
- java - Android Root Check fails on samsung tab while executin /system/bin/which su
- cassandra - 新的 DSE Cassandra 集群 system.hints 缺失
- java - 在 JOOQ 自定义绑定中生成 SQL 时访问字段名称或别名以填充 MySQL JSON 函数
- reactjs - MUIDataTable 'customBodyRender' 错误
- python-3.x - Spark fp 增长不会导致多个项目
- symfony - Symfony 3:RememberMe Cookie 未设置
- c++ - 为什么 OpenCV 中没有 CV_64S 类型?