python - pytorch 中的 tf.cast 等价物?
问题描述
我是 PyTorch 的新手。TensorFlow 有一个 API tf.cast()和tf.shape()。tf.cast在 TensorFlow中具有特定用途,在火炬中有什么等价物吗?我有张量 x= tensor(shape(128,64,32,32)): tf.shape(x)创建尺寸为 1 x.shape的张量创建真实尺寸。我需要在火炬中使用tf.shape(x) 。
tf.cast的作用与仅仅改变Torch 中的张量dtype不同。
有没有人在 Torch/PyTorch 中有等效的 API。
解决方案
正如他们提到的:
print(x.dtype) # Prints "torch.int64", currently 64-bit integer type
x = x.type(torch.FloatTensor)
print(x.dtype) # Prints "torch.float32", now 32-bit float
print(x.float()) # Still "torch.float32"
print(x.type(torch.DoubleTensor)) # Prints "tensor([0., 1., 2., 3.], dtype=torch.float64)"
print(x.type(torch.LongTensor)) # Cast back to int-64, prints "tensor([0, 1, 2, 3])"
推荐阅读
- javascript - 我不知道这个简单的 javascript 代码有什么问题,我一遍又一遍地检查了这个但不起作用
- vue.js - 如何在子组件中监听父组件的数据变化
- c# - 用于最佳面额的递归硬币更改算法
- java - @JsonFormat的注解格式问题
- java - 按第二个参数排序并按顺序迭代
- google-apps-script - 高级 Google 服务中的 Google Analytics Admin API
- android - 为什么Android BLE的通信可以被所有App访问?
- django - manage.py 在 myapp.urls 文件中找不到任何 url。说一些关于循环导入的事情
- api - 谷歌工作表功能 UrlFetchApp - 调用过多和错误
- web - 功能简单的内网开发