335 Star 1.5K Fork 862

MindSpore / docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Cast.md 1.50 KB
一键复制 编辑 原始数据 按行查看 历史
俞涵 提交于 2023-01-29 16:21 . add mindquantum and modify url

Function Differences with torch.Tensor.float

torch.Tensor.float

torch.Tensor.float(memory_format=torch.preserve_format)

For more information, see torch.Tensor.float.

mindspore.ops.Cast

class mindspore.ops.Cast(*args, **kwargs)(
    input_x,
    type
)

For more information, see mindspore.ops.Cast.

Differences

PyTorch: Changes the tensor type to float.

MindSpore:Converts the input type to the specified data type.

Code Example

import mindspore as ms
import mindspore.ops as ops
import torch
import numpy as np

# In MindSpore, you can specify the data type to be transformed into.
input_x = ms.Tensor(np.random.randn(2, 3, 4, 5).astype(np.float32))
cast = ops.Cast()
output = cast(input_x, ms.int32)
print(output.dtype)
print(output.shape)
# Out:
# Int32
# (2, 3, 4, 5)

# In torch, the input will be transformed into float.
input_x = torch.Tensor(np.random.randn(2, 3, 4, 5).astype(np.int32))
output = input_x.float()
print(output.dtype)
print(output.shape)
# Out:
# torch.float32
# torch.Size([2, 3, 4, 5])
1
https://gitee.com/mindspore/docs.git
git@gitee.com:mindspore/docs.git
mindspore
docs
docs
r2.0.0-alpha

搜索帮助

53164aa7 5694891 3bd8fe86 5694891