PyTorch torch.clone 函数

约 252 字 更新于 2026-11-18 5iyo.com

PyTorch torch.clone 函数


torch.clone 是 PyTorch 中用于返回张量副本的函数。

函数定义

torch.clone(input, memory_format)

使用示例

实例

import torch

x = torch.tensor([1, 2, 3])

# 创建副本
y = torch.clone(x)

# 修改副本不影响原张量
y[0] = 100

print("原张量:", x)
print("副本:", y)