pypto.tanh#
产品支持情况#
Ascend 950PR/Ascend 950DT:支持
Atlas A3 训练系列产品/Atlas A3 推理系列产品:支持
Atlas A2 训练系列产品/Atlas A2 推理系列产品:支持
功能说明#
对输入Tensor的每个元素应用双曲正切函数(tanh),计算公式为:
该函数将输入映射到 ((-1, 1))区间,常用于神经网络激活函数。
函数原型#
tanh(input: Tensor) -> Tensor
参数说明#
参数名 |
输入/输出 |
说明 |
|---|---|---|
input |
输入 |
源操作数。 |
返回值说明#
返回Tensor类型。其Shape、数据类型与输入Tensor一致,其元素为输入元素经tanh函数映射到 ((-1, 1))区间的结果。
约束说明#
TileShape与input维度保持一致;
由于存在临时内存使用,当输入数据类型为DT_FP32,TileShape大小有额外约束,假设TileShape为 […,H,W](最后两维为H和W),那么:
input_size + output_size + 2 * (W_align8) * H * sizeof(float) + (W_align8 / 8) * H + 32 bytes < UB其中,W_align8 = (W + 7) / 8 * 8(FP32:input + output + 2个float temp tile + 1个compare mask tile + 32 bytes对齐)对于DT_FP16/DT_BF16输入,需要满足:
input_size + output_size + 4 * (W_align8) * H * sizeof(float) + (W_align8 / 8) * H + 32 bytes < UB(FP16/BF16:input + output + 4个float temp tile + 1个compare mask tile + 32 bytes对齐)Tensor类型输入不支持
TileOpFormat.TILEOP_NZ格式。
调用示例#
x = pypto.tensor([4], pypto.DT_FP32)
y = pypto.tanh(x)
结果示例如下:
输入数据x: [-3.0, -1.0, 0.0, 1.0, 3.0]
输出数据y: [-0.9951, -0.7616, 0.0000, 0.7616, 0.9951]
计算过程说明:
tanh(-3.0) ≈ -0.9951,接近 -1
tanh(-1.0) ≈ -0.7616
tanh(0.0) = 0.0
tanh(1.0) ≈ 0.7616
tanh(3.0) ≈ 0.9951,接近1