当前位置:主页 > 软件编程 > Python代码 >

Numpy与Pytorch 矩阵操作方式

时间:2021-07-20 08:55:30 | 栏目:Python代码 | 点击:

Numpy

随机矩阵: np.random.randn(d0, d1, d2, ...)

矩阵大小与形状: np.ndarray.size 与 np.dnarray.shape

Pytorch

随机矩阵: torch.randn(d0, d1, d2, ...)

添加维度: tensor.unsqueeze(0)

压缩维度: tensor.squeeze(0)

按维度拼接tensor: torch.cat(inputs, dim=0, ...)

维度堆叠: torch.stack(inputs, dim=0)

张量排序索引: tensor.sort(descending=True) 返回一个tensor为排序后的tensor, 一个为index_tensor

矩阵元素夹逼: tensor.clamp()

矩阵切割: torch.chunk(tensor, chunks, dim)

矩阵复制: torch.repeat(*size)

生成零矩阵: torch.torch.zeros(5, 3, dtype=torch.long)

生产同形状的随机矩阵:x = torch.randn_like(x, dtype=torch.float)

矩阵中函数名以'_'结尾的,如:y.add_(x),运算结束后会改变y本身

您可能感兴趣的文章:

相关文章