c++ - 为什么在 Torch C++ API 中就地张量方法是 const 的?
问题描述
我在 PyTorch C++ 前端看到很多就地张量操作,例如mul_
和const:div_
Tensor &mul_(Scalar other) const
这似乎很奇怪,因为就地操作应该修改张量数据,对吧?有谁知道使它们成为常量的原因是什么?
我在 github 上找到了一些讨论,但看起来标题与下面写的内容相矛盾:
'const Tensor' 不提供 const 安全性......因此,这些方法应该是非常量的
解决方案
正如评论和这个线程所强调的那样,这const
是不诚实的,因为它适用于指向底层的指针TensorImpl
,而不是数据本身。这只是为了编译优化,这里没有真正的语义。const int*
这类似于(pointer to const int) 和int* const
(const pointer to int)之间的区别。
torch 中的 const (resp. non-const)函数很容易识别,因为函数名称中没有最后一个下划线(resp。存在)。
推荐阅读
- excel - 为什么 .autoFill 会导致 Excel 崩溃?
- javascript - 如何通过子属性在猫鼬中查找文件?
- sql - 如何使用 SQL 从前 n 行查询?
- image - 如何允许 a-frame 预览来自云端的图像(没有 CORS 策略)?
- c# - 如何在 Visual Studio Code 或 dotnet 命令行中的两个项目之间链接源文件?
- ios - ScrollView 中的 ForEach 不占用整个宽度
- android - EditText 不显示翻译,而 Logcat 显示
- security - 如果未发送所需的 SSL 证书,modsecurity 将不起作用
- r - 如何获得这些移动平均线结果
- c++ - 如何在给定索引之后在链表中插入节点