PyTorch与MindSpore

由社区提供的PyTorch APIs和MindSpore APIs之间的映射,可能在参数、输入、输出、逻辑功能和特定场景等方面存在差异,可详见各API描述或已提供的差异对比。

也欢迎更多的MindSpore开发者参与完善映射内容。

torch

PyTorch 1.5.0 APIs MindSpore APIs 说明
torch.abs mindspore.ops.Abs
torch.acos mindspore.ops.ACos
torch.add mindspore.ops.Add
torch.arange mindspore.numpy.arange
torch.argmax mindspore.ops.Argmax
torch.argmin mindspore.ops.Argmin
torch.argsort mindspore.ops.Sort 差异对比
torch.asin mindspore.ops.Asin
torch.atan mindspore.ops.Atan
torch.atan2 mindspore.ops.Atan2
torch.bartlett_window mindspore.numpy.bartlett
torch.bincount mindspore.numpy.bincount
torch.bitwise_and mindspore.ops.BitwiseAnd
torch.bitwise_or mindspore.ops.BitwiseOr
torch.bitwise_xor mindspore.ops.BitwiseXor
torch.blackman_window mindspore.numpy.blackman
torch.bmm mindspore.ops.BatchMatMul
torch.broadcast_tensors mindspore.ops.BroadcastTo 差异对比
torch.cat mindspore.ops.Concat 差异对比
torch.ceil mindspore.ops.Ceil
torch.chunk mindspore.ops.Split
torch.clamp mindspore.ops.clip_by_value 差异对比
torch.conj mindspore.ops.Conj
torch.cos mindspore.ops.Cos
torch.cosh mindspore.ops.Cosh
torch.cross mindspore.numpy.cross 差异对比
torch.cumprod mindspore.ops.CumProd
torch.cumsum mindspore.ops.CumSum
torch.diag mindspore.nn.MatrixDiag 差异对比
torch.diagflat mindspore.numpy.diagflat
torch.diagonal mindspore.numpy.diagonal
torch.div mindspore.ops.Div
torch.dot mindspore.ops.tensor_dot 差异对比
torch.empty mindspore.numpy.empty
torch.empty_like mindspore.numpy.empty_like
torch.eq mindspore.ops.Equal
torch.erfc mindspore.ops.Erfc
torch.erfinv mindspore.ops.Erfinv
torch.exp mindspore.ops.Exp
torch.expm1 mindspore.ops.Expm1
torch.eye mindspore.ops.Eye
torch.flatten mindspore.ops.Flatten 差异对比
torch.flip mindspore.ops.ReverseV2
torch.floor mindspore.ops.Floor
torch.floor_divide mindspore.ops.FloorDiv 差异对比
torch.fmod mindspore.ops.Mod
torch.from_numpy mindspore.tensor.from_numpy
torch.full mindspore.numpy.full
torch.full_like mindspore.numpy.full_like
torch.gather mindspore.ops.GatherD
torch.ge mindspore.ops.GreaterEqual
torch.gt mindspore.ops.Greater
torch.hamming_window mindspore.numpy.hamming
torch.hann_window mindspore.numpy.hanning
torch.histc mindspore.ops.HistogramFixedWidth
torch.imag mindspore.ops.Imag
torch.isfinite mindspore.ops.IsFinite
torch.isinf mindspore.ops.IsInf
torch.isnan mindspore.ops.IsNan
torch.le mindspore.ops.LessEqual
torch.lerp mindspore.ops.Lerp
torch.linspace mindspore.ops.LinSpace
torch.load mindspore.load_checkpoint
torch.log mindspore.ops.Log
torch.log1p mindspore.ops.Log1p
torch.log2 mindspore.numpy.log2
torch.logical_and mindspore.ops.LogicalAnd 差异对比
torch.logical_not mindspore.numpy.logical_not 差异对比
torch.logical_or mindspore.ops.LogicalOr 差异对比
torch.logical_xor mindspore.numpy.logical_xor 差异对比
torch.logspace mindspore.numpy.logspace
torch.lt mindspore.ops.Less
torch.matmul mindspore.nn.MatMul
torch.matrix_power mindspore.numpy.matrix_power
torch.matrix_rank mindspore.ops.Rank
torch.max mindspore.ops.ArgMaxWithValue 差异对比
torch.maximum mindspore.ops.Maximum
torch.mean mindspore.ops.ReduceMean
torch.meshgrid mindspore.ops.Meshgrid
torch.min mindspore.ops.ArgMinWithValue 差异对比
torch.mm mindspore.ops.MatMul
torch.mul mindspore.ops.Mul
torch.multinomial mindspore.ops.multinomial
torch.ne mindspore.ops.NotEqual
torch.neg mindspore.ops.Neg
torch.norm mindspore.ops.LpNorm 差异对比
torch.numel mindspore.ops.Size
torch.ones mindspore.ops.Ones
torch.ones_like mindspore.ops.OnesLike
torch.poisson mindspore.ops.poisson
torch.pow mindspore.ops.Pow
torch.prod mindspore.ops.ReduceProd
torch.promote_types mindspore.numpy.promote_types
torch.rand mindspore.ops.UniformReal
torch.rand_like mindspore.ops.UniformReal
torch.randint mindspore.ops.UniformInt
torch.randint_like mindspore.ops.UniformInt
torch.randn mindspore.ops.StandardNormal
torch.randperm mindspore.ops.Randperm 差异对比
torch.range mindspore.nn.Range
torch.real mindspore.ops.Real
torch.reciprocal mindspore.ops.Reciprocal
torch.remainder mindspore.numpy.remainder
torch.reshape mindspore.ops.Reshape
torch.result_type mindspore.numpy.result_type
torch.roll mindspore.nn.Roll
torch.rot90 mindspore.numpy.rot90
torch.round mindspore.ops.Rint
torch.rsqrt mindspore.ops.Rsqrt
torch.save mindspore.save_checkpoint
torch.sigmoid mindspore.ops.Sigmoid
torch.sin mindspore.ops.Sin
torch.sinh mindspore.ops.Sinh
torch.sort mindspore.ops.Sort
torch.sparse_coo_tensor mindspore.SparseTensor 差异对比
torch.split mindspore.ops.Split
torch.sqrt mindspore.ops.Sqrt
torch.square mindspore.ops.Square
torch.squeeze mindspore.ops.Squeeze
torch.stack mindspore.ops.Stack
torch.std mindspore.Tensor.std
torch.std_mean mindspore.ops.ReduceMean 差异对比
torch.sum mindspore.ops.ReduceSum
torch.take mindspore.Tensor.take
torch.tan mindspore.ops.Tan
torch.tanh mindspore.ops.Tanh
torch.tensor mindspore.Tensor
torch.Tensor mindspore.Tensor
torch.tensordot mindspore.numpy.tensordot
torch.topk mindspore.ops.TopK 差异对比
torch.trace mindspore.Tensor.trace
torch.transpose mindspore.ops.Transpose
torch.trapz mindspore.numpy.trapz
torch.tril mindspore.nn.Tril
torch.tril_indices mindspore.numpy.tril_indices
torch.triu mindspore.nn.Triu
torch.triu_indices mindspore.numpy.triu_indices
torch.true_divide mindspore.numpy.true_divide
torch.trunc mindspore.numpy.trunc
torch.unbind mindspore.ops.Unstack
torch.unique mindspore.ops.Unique 差异对比
torch.unsqueeze mindspore.ops.ExpandDims
torch.var mindspore.Tensor.var
torch.where mindspore.numpy.where
torch.zeros mindspore.ops.Zeros
torch.zeros_like mindspore.ops.ZerosLike

torch.distributions

PyTorch 1.5.0 APIs MindSpore APIs 说明
torch.distributions.gamma.Gamma mindspore.ops.Gamma

torch.nn

PyTorch 1.5.0 APIs MindSpore APIs 说明
torch.nn.AdaptiveAvgPool2d mindspore.ops.AdaptiveAvgPool2D
torch.nn.AvgPool1d mindspore.nn.AvgPool1d
torch.nn.AvgPool2d mindspore.nn.AvgPool2d
torch.nn.BatchNorm1d mindspore.nn.BatchNorm1d 差异对比
torch.nn.BatchNorm2d mindspore.nn.BatchNorm2d 差异对比
torch.nn.BCEWithLogitsLoss mindspore.nn.BCEWithLogitsLoss
torch.nn.Conv1d mindspore.nn.Conv1d
torch.nn.Conv2d mindspore.nn.Conv2d 差异对比
torch.nn.Conv3d mindspore.nn.Conv3d
torch.nn.ConvTranspose1d mindspore.nn.Conv1dTranspose
torch.nn.ConvTranspose2d mindspore.nn.Conv2dTranspose
torch.nn.ConvTranspose3d mindspore.nn.Conv3dTranspose
torch.nn.CrossEntropyLoss mindspore.nn.SoftmaxCrossEntropyWithLogits
torch.nn.CTCLoss mindspore.ops.CTCLoss
torch.nn.Dropout mindspore.nn.Dropout 差异对比
torch.nn.Embedding mindspore.nn.Embedding 差异对比
torch.nn.Flatten mindspore.nn.Flatten 差异对比
torch.nn.GELU mindspore.nn.GELU
torch.nn.GELU mindspore.nn.FastGelu 差异对比
torch.nn.GroupNorm mindspore.nn.GroupNorm
torch.nn.GRU mindspore.nn.GRU
torch.nn.Hardshrink mindspore.nn.HShrink
torch.nn.KLDivLoss mindspore.ops.KLDivLoss
torch.nn.L1Loss mindspore.nn.L1Loss
torch.nn.LayerNorm mindspore.nn.LayerNorm
torch.nn.LeakyReLU mindspore.nn.LeakyReLU
torch.nn.Linear mindspore.nn.Dense 差异对比
torch.nn.LSTM mindspore.nn.LSTM
torch.nn.LSTMCell mindspore.nn.LSTMCell
torch.nn.MaxPool1d mindspore.nn.MaxPool1d 差异对比
torch.nn.MaxPool2d mindspore.nn.MaxPool2d 差异对比
torch.nn.MultiheadAttention mindspore.nn.transformer.MultiHeadAttention 差异对比
torch.nn.Module mindspore.nn.Cell
torch.nn.Module.add_module mindspore.nn.Cell.insert_child_to_cell
torch.nn.Module.buffers mindspore.nn.Cell.untrainable_params 差异对比
torch.nn.Module.children mindspore.nn.Cell.cells 差异对比
torch.nn.Module.load_state_dict mindspore.load_param_into_net
torch.nn.Module.named_children mindspore.nn.Cell.name_cells 差异对比
torch.nn.Module.named_modules mindspore.nn.Cell.cells_and_names
torch.nn.Module.parameters mindspore.nn.Cell.get_parameters 差异对比
torch.nn.Module.state_dict mindspore.nn.Cell.parameters_dict
torch.nn.Module.train mindspore.nn.Cell.set_train
torch.nn.ModuleList mindspore.nn.CellList
torch.nn.MSELoss mindspore.nn.MSELoss
torch.nn.Parameter mindspore.Parameter
torch.nn.ParameterList mindspore.ParameterTuple 差异对比
torch.nn.PixelShuffle mindspore.ops.DepthToSpace
torch.nn.PReLU mindspore.nn.PReLU
torch.nn.ReLU mindspore.nn.ReLU
torch.nn.ReplicationPad2d mindspore.nn.Pad
torch.nn.SELU mindspore.ops.SeLU
torch.nn.Sequential mindspore.nn.SequentialCell
torch.nn.Sigmoid mindspore.nn.Sigmoid
torch.nn.SmoothL1Loss mindspore.nn.SmoothL1Loss
torch.nn.Softmax mindspore.nn.Softmax
torch.nn.Softshrink mindspore.nn.SoftShrink
torch.nn.SyncBatchNorm.convert_sync_batchnorm mindspore.nn.GlobalBatchNorm
torch.nn.Tanh mindspore.nn.Tanh
torch.nn.Transformer mindspore.nn.transformer.Transformer 差异对比
torch.nn.TransformerEncoder mindspore.nn.transformer.TransformerEncoder 差异对比
torch.nn.TransformerDecoder mindspore.nn.transformer.TransformerDecoder 差异对比
torch.nn.TransformerEncoderLayer mindspore.nn.transformer.TransformerEncoderLayer 差异对比
torch.nn.TransformerDecoderLayer mindspore.nn.transformer.TransformerDecoderLayer 差异对比
torch.nn.Unfold mindspore.nn.Unfold 差异对比
torch.nn.Upsample mindspore.nn.ResizeBilinear 差异对比

torch.nn.functional

PyTorch 1.5.0 APIs MindSpore APIs 说明
torch.nn.functional.adaptive_avg_pool2d mindspore.ops.AdaptiveAvgPool2D
torch.nn.functional.avg_pool1d mindspore.nn.AvgPool1d
torch.nn.functional.avg_pool2d mindspore.ops.AvgPool
torch.nn.functional.avg_pool3d mindspore.ops.AvgPool3D
torch.nn.functional.batch_norm mindspore.ops.BatchNorm
torch.nn.functional.binary_cross_entropy mindspore.ops.BinaryCrossEntropy
torch.nn.functional.conv2d mindspore.ops.Conv2D
torch.nn.functional.cosine_embedding_loss mindspore.nn.CosineEmbeddingLoss
torch.nn.functional.ctc_loss mindspore.ops.CTCLoss
torch.nn.functional.elu mindspore.ops.Elu
torch.nn.functional.kl_div mindspore.ops.KLDivLoss
torch.nn.functional.l1_loss mindspore.nn.L1Loss
torch.nn.functional.layer_norm mindspore.ops.LayerNorm
torch.nn.functional.leaky_relu mindspore.nn.LeakyReLU
torch.nn.functional.log_softmax mindspore.nn.LogSoftmax
torch.nn.functional.max_pool1d mindspore.nn.MaxPool1d 差异对比
torch.nn.functional.max_pool2d mindspore.nn.MaxPool2d 差异对比
torch.nn.functional.max_pool3d mindspore.ops.MaxPool3D 差异对比
torch.nn.functional.mse_loss mindspore.nn.MSELoss
torch.nn.functional.nll_loss mindspore.ops.NLLLoss 差异对比
torch.nn.functional.normalize mindspore.ops.L2Normalize 差异对比
torch.nn.functional.one_hot mindspore.ops.OneHot
torch.nn.functional.pad mindspore.nn.Pad 差异对比
torch.nn.functional.pixel_shuffle mindspore.ops.DepthToSpace
torch.nn.functional.relu mindspore.ops.ReLU
torch.nn.functional.smooth_l1_loss mindspore.nn.SmoothL1Loss
torch.nn.functional.soft_margin_loss mindspore.nn.SoftMarginLoss
torch.nn.functional.softmax mindspore.ops.Softmax 差异对比
torch.nn.functional.softplus mindspore.ops.Softplus
torch.nn.functional.softsign mindspore.ops.Softsign

torch.Tensor

PyTorch 1.5.0 APIs MindSpore APIs 说明
torch.repeat_interleave mindspore.ops.repeat_elements
torch.BoolTensor.all mindspore.Tensor.all
torch.BoolTensor.any mindspore.Tensor.any
torch.minimum mindspore.ops.Minimum
torch.Tensor.abs mindspore.Tensor.abs
torch.Tensor.argmax mindspore.Tensor.argmax
torch.Tensor.argmin mindspore.Tensor.argmin
torch.Tensor.chunk mindspore.ops.Split
torch.Tensor.copy_ mindspore.Tensor.copy
torch.Tensor.cuda mindspore.set_context 差异对比
torch.Tensor.cumsum mindspore.Tensor.cumsum
torch.Tensor.diagonal mindspore.Tensor.diagonal
torch.Tensor.dtype mindspore.Tensor.dtype
torch.Tensor.expand mindspore.ops.BroadcastTo
torch.Tensor.expand_as mindspore.Tensor.expand_as
torch.Tensor.fill_ mindspore.ops.Fill
torch.Tensor.flatten mindspore.Tensor.flatten
torch.Tensor.float mindspore.ops.Cast 差异对比
torch.Tensor.index_add mindspore.ops.InplaceAdd
torch.Tensor.item mindspore.Tensor.item 差异对比
torch.Tensor.max mindspore.Tensor.max
torch.Tensor.mean mindspore.Tensor.mean
torch.Tensor.min mindspore.Tensor.min
torch.Tensor.mm mindspore.ops.MatMul
torch.Tensor.mul mindspore.ops.Mul
torch.Tensor.ndim mindspore.Tensor.ndim
torch.Tensor.new_full mindspore.numpy.full
torch.Tensor.new_zeros mindspore.ops.Zeros
torch.Tensor.norm mindspore.ops.LpNorm 差异对比
torch.Tensor.numpy mindspore.Tensor.asnumpy
torch.Tensor.pow mindspore.ops.Pow
torch.Tensor.permute mindspore.ops.Transpose
torch.Tensor.repeat mindspore.numpy.tile 差异对比
torch.Tensor.requires_grad_ mindspore.Parameter.requires_grad
torch.Tensor.reshape_as mindspore.tensor.reshape
torch.Tensor.resize_ mindspore.Tensor.resize
torch.Tensor.round mindspore.ops.Round
torch.Tensor.scatter mindspore.ops.ScatterNd
torch.Tensor.scatter_add_ mindspore.ops.ScatterNdAdd 差异对比
torch.Tensor.sigmoid mindspore.nn.Sigmoid
torch.Tensor.sign mindspore.ops.Sign
torch.Tensor.size mindspore.Tensor.shape
torch.Tensor.sqrt mindspore.ops.Sqrt
torch.Tensor.stride mindspore.Tensor.strides 差异对比
torch.Tensor.squeeze mindspore.Tensor.squeeze
torch.Tensor.sub mindspore.ops.Sub
torch.Tensor.sum mindspore.Tensor.sum 差异对比
torch.Tensor.t mindspore.ops.Transpose 差异对比
torch.Tensor.t mindspore.Tensor.T
torch.Tensor.take mindspore.Tensor.take 差异对比
torch.Tensor.transpose mindspore.Tensor.transpose
torch.Tensor.unsqueeze mindspore.ops.ExpandDims
torch.Tensor.view mindspore.Tensor.view
torch.Tensor.view_as mindspore.Tensor.view
torch.Tensor.zero_ mindspore.ops.ZerosLike

torchvision

TorchVision 0.10.0 APIs MindSpore APIs 说明
torchvision.datasets.CelebA mindspore.dataset.CelebADataset
torchvision.datasets.CIFAR10 mindspore.dataset.Cifar10Dataset
torchvision.datasets.CIFAR100 mindspore.dataset.Cifar100Dataset
torchvision.datasets.CocoDetection mindspore.dataset.CocoDataset 差异对比
torchvision.datasets.ImageFolder mindspore.dataset.ImageFolderDataset
torchvision.datasets.MNIST mindspore.dataset.MnistDataset
torchvision.datasets.VOCDetection mindspore.dataset.VOCDataset 差异对比
torchvision.datasets.VOCSegmentation mindspore.dataset.VOCDataset 差异对比
torchvision.ops.nms mindspore.ops.NMSWithMask
torchvision.ops.roi_align mindspore.ops.ROIAlign
torchvision.transforms.CenterCrop mindspore.dataset.vision.CenterCrop
torchvision.transforms.ColorJitter mindspore.dataset.vision.RandomColorAdjust
torchvision.transforms.Compose mindspore.dataset.transforms.Compose
torchvision.transforms.ConvertImageDtype mindspore.dataset.transforms.TypeCast 差异对比
torchvision.transforms.FiveCrop mindspore.dataset.vision.FiveCrop
torchvision.transforms.GaussianBlur mindspore.dataset.vision.GaussianBlur
torchvision.transforms.Grayscale mindspore.dataset.vision.Grayscale
torchvision.transforms.LinearTransformation mindspore.dataset.vision.LinearTransformation
torchvision.transforms.Normalize mindspore.dataset.vision.Normalize
torchvision.transforms.Pad mindspore.dataset.vision.Pad
torchvision.transforms.RandomAffine mindspore.dataset.vision.RandomAffine
torchvision.transforms.RandomApply mindspore.dataset.transforms.RandomApply
torchvision.transforms.RandomChoice mindspore.dataset.transforms.RandomChoice
torchvision.transforms.RandomCrop mindspore.dataset.vision.RandomCrop
torchvision.transforms.RandomErasing mindspore.dataset.vision.RandomErasing
torchvision.transforms.RandomGrayscale mindspore.dataset.vision.RandomGrayscale
torchvision.transforms.RandomHorizontalFlip mindspore.dataset.vision.RandomHorizontalFlip
torchvision.transforms.RandomOrder mindspore.dataset.transforms.RandomOrder
torchvision.transforms.RandomPerspective mindspore.dataset.vision.RandomPerspective
torchvision.transforms.RandomPosterize mindspore.dataset.vision.RandomPosterize
torchvision.transforms.RandomResizedCrop mindspore.dataset.vision.RandomResizedCrop
torchvision.transforms.RandomRotation mindspore.dataset.vision.RandomRotation
torchvision.transforms.RandomSolarize mindspore.dataset.vision.RandomSolarize 差异对比
torchvision.transforms.RandomVerticalFlip mindspore.dataset.vision.RandomVerticalFlip
torchvision.transforms.Resize mindspore.dataset.vision.Resize
torchvision.transforms.TenCrop mindspore.dataset.vision.TenCrop
torchvision.transforms.ToPILImage mindspore.dataset.vision.ToPIL 差异对比
torchvision.transforms.ToTensor mindspore.dataset.vision.ToTensor 差异对比