文章目录 矩阵操作 (1)创建5行3列的矩阵: (2)初始化全零矩阵 (3)传入数据 (4)生成随机矩阵 (5)矩阵大小 (6)矩阵加法 (7)索引 (8)View操作 改变矩阵维度 (9)ndarray和tensor格式互换 矩阵操作 (1)创建5行3列的矩阵: x = torch.empty(5, 3) (2)初始化全零矩阵 x = torch.zeros(5, 3, dtype=torch.long) (3)传入数据 x = torch.tensor([[1, 2], [3, 4], [5, 6]]) (4)生成随机矩阵 x = torch.empty(5, 3) x = torch.randn_like(x, dtype=torch.float) y = torch.rand(5, 3)