Differences with torch.nn.ReLU

View Source On Gitee

torch.nn.ReLU

class torch.nn.ReLU(inplace=False)(input) -> Tensor

For more information, see torch.nn.ReLU.

mindspore.nn.ReLU

class mindspore.nn.ReLU()(x) -> Tensor

For more information, see mindspore.nn.ReLU.

Differences

PyTorch: ReLU activation function.

MindSpore: MindSpore implements the same function as PyTorch, but with different parameter settings.

Categories

Subcategories

PyTorch

MindSpore

Difference

Parameter

Parameter 1

inplace

-

Whether to execute in-place, default: False. MindSpore does not have this parameter.

Input

Single input

input

x

Same function, different parameter names

Code Example

The two APIs achieve the same function and have the same usage.

# PyTorch
import torch
from torch import tensor
from torch import nn
import numpy as np

x = tensor(np.array([[0.1, -0.6], [-0.9, 0.8]]), dtype=torch.float32)
m = nn.ReLU()
out = m(x)
output = out.detach().numpy()
print(output)
# [[0.1 0. ]
#  [0.  0.8]]

# MindSpore
import mindspore
import mindspore.nn as nn
from mindspore import Tensor
import numpy as np

x = Tensor(np.array([[0.1, -0.6], [-0.9, 0.8]]), dtype=mindspore.float32)
relu = nn.ReLU()
output = relu(x)
print(output)
# [[0.1 0. ]
#  [0.  0.8]]