import torch
# 创建一个2 * 3的全0张量
a = torch.zeros(2,3)
print(a)
print("===============================")
# 创建一个3 * 3的全1张量
b = torch.ones(3,3)
print(b)
print("===============================")
c = torch.randn(2,3)
print(c)
print("===============================")
# 从numpy数组中创建张量
import numpy as np
numpy_array = np.array([[1,2,3],[4,5,6]])
tensor_form_numpy = torch.from_numpy(numpy_array)
print(tensor_form_numpy)
print("===============================")
# 在指定设备上创建张量
device = torch.device("cuda" if torch.cuda.is_available() else 'cpu')
d = torch.randn(2,3, device=device)
print(d)
print("===============================")
[pytorch学习笔记]tensor的基本使用
最新推荐文章于 2026-06-22 19:06:12 发布

1383

被折叠的 条评论
为什么被折叠?



