import torch # 创建一个 2x3 的全 0 张量 a = torch.zeros(2, 3) print(a) # 创建一个 2x3 的全 1 张量 b = torch.ones(2, 3) print(b) # 创建一个 2x3 的随机数张量 c = torch.randn(2, 3) print(c) # 从 NumPy 数组创建张量 import numpy as np numpy_array = np.array([[1, 2], [3, 4]]) tensor_from_numpy = torch.from_numpy(numpy_array) print(tensor_from_numpy) # 在指定设备(CPU/GPU)上创建张量 device = torch.device("cuda" if torch.cuda.is_available() else "cpu") d = torch.randn(2, 3, device=device) print(torch.cuda.is_available()) print(d) # 逐元素乘法(要求形状相同) X = torch.tensor([[1, 2], [3, 4]]) Y = torch.tensor([[5, 6], [7, 8]]) Z = X * Y # 或 torch.mul(X, Y) print("逐元素乘法结果:\n", Z)