average_pool 函数keras.ops.average_pool(
inputs, pool_size, strides=None, padding="valid", data_format=None
)
平均池化操作。
参数
data_format="channels_last",则 inputs 的形状为 (batch_size,) + inputs_spatial_shape + (num_channels,);如果 data_format="channels_first",则形状为 (batch_size, num_channels) + inputs_spatial_shape。池化仅发生在空间维度上。len(inputs_spatial_shape) 的整数元组/列表,指定输入张量每个空间维度的池化窗口大小。如果 pool_size 是整数,则每个空间维度共享相同的 pool_size。len(inputs_spatial_shape) 的整数元组/列表。输入张量每个空间维度的滑动窗口的步长。如果 strides 是整数,则每个空间维度共享相同的 strides。"valid" 或 "same"。"valid" 表示不应用填充,"same" 表示在输入的左/右或上/下进行均匀填充,使得当 strides=1 时,输出的高度/宽度维度与输入相同。"channels_last" 或 "channels_first"。data_format 决定了输入中维度的顺序。如果 data_format="channels_last",则 inputs 的形状为 (batch_size, ..., channels);如果 data_format="channels_first",则 inputs 的形状为 (batch_size, channels, ...)。返回
一个秩为 N+2 的张量,即平均池化操作的结果。
batch_normalization 函数keras.ops.batch_normalization(
x, mean, variance, axis, offset=None, scale=None, epsilon=0.001
)
使用 mean 和 variance 对 x 进行归一化。
此操作通常在神经网络的批归一化步骤中使用。它沿指定的轴对输入张量进行归一化。
参数
axis 维度相同。axis 维度相同。axis 维度相同。如果不是 None,则将 offset 加到归一化张量上。默认为 None。axis 维度相同。如果不是 None,则将归一化张量乘以 scale。默认为 None。返回
归一化后的张量。
示例
>>> x = keras.ops.convert_to_tensor(
... [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]]
... )
>>> keras.ops.batch_normalization(
... x,
... mean=[0.4, 0.5, 0.6],
... variance=[0.67, 0.67, 0.67],
... axis=-1
... )
array([[-3.6624e-01, -3.6624e-01, -3.6624e-01],
[-4.6445e-09, 0.0000e+00, -1.8578e-08],
[ 3.6624e-01, 3.6624e-01, 3.6624e-01]])
binary_crossentropy 函数keras.ops.binary_crossentropy(target, output, from_logits=False)
计算目标张量和输出张量之间的二元交叉熵损失。
二元交叉熵损失通常用于二元分类任务,其中每个输入样本属于两个类别之一。它衡量目标概率和输出概率或对数之间的不相似性。
参数
output 张量的形状匹配。target 张量的形状匹配。output 是对数张量还是概率张量。如果 output 表示对数,则设置为 True;否则,如果 output 表示概率,则设置为 False。默认为 False。返回
target 和 output 之间计算的二元交叉熵损失。示例
>>> target = keras.ops.convert_to_tensor([0, 1, 1, 0])
>>> output = keras.ops.convert_to_tensor([0.1, 0.9, 0.8, 0.2])
>>> binary_crossentropy(target, output)
array([0.10536054 0.10536054 0.22314355 0.22314355],
shape=(4,), dtype=float32)
categorical_crossentropy 函数keras.ops.categorical_crossentropy(target, output, from_logits=False, axis=-1)
计算目标张量和输出张量之间的分类交叉熵损失。
分类交叉熵损失通常用于多类别分类任务,其中每个输入样本可以属于多个类别中的一个。它衡量目标概率和输出概率或对数之间的不相似性。
参数
output 张量的形状匹配,但最后一个维度除外。target 张量的形状匹配,但最后一个维度除外。output 是对数张量还是概率张量。如果 output 表示对数,则设置为 True;否则,如果 output 表示概率,则设置为 False。默认为 False。-1,对应张量的最后一个维度。返回
target 和 output 之间计算的分类交叉熵损失。示例
>>> target = keras.ops.convert_to_tensor(
... [[1, 0, 0],
... [0, 1, 0],
... [0, 0, 1]])
>>> output = keras.ops.convert_to_tensor(
... [[0.9, 0.05, 0.05],
... [0.1, 0.8, 0.1],
... [0.2, 0.3, 0.5]])
>>> categorical_crossentropy(target, output)
array([0.10536054 0.22314355 0.6931472 ], shape=(3,), dtype=float32)
conv 函数keras.ops.conv(
inputs, kernel, strides=1, padding="valid", data_format=None, dilation_rate=1
)
通用 N 维卷积。
此操作支持 1D、2D 和 3D 卷积。
参数
data_format="channels_last",则 inputs 的形状为 (batch_size,) + inputs_spatial_shape + (num_channels,);如果 data_format="channels_first",则形状为 (batch_size, num_channels) + inputs_spatial_shape。kernel 的形状为 (kernel_spatial_shape, num_input_channels, num_output_channels)。num_input_channels 应与 inputs 中的通道数匹配。len(inputs_spatial_shape) 的整数元组/列表,指定卷积沿每个空间维度的步长。如果 strides 是整数,则每个空间维度共享相同的 strides。"valid" 或 "same"。"valid" 表示不应用填充,"same" 表示在输入的左/右或上/下进行均匀填充,使得当 strides=1 时,输出的高度/宽度维度与输入相同。"channels_last" 或 "channels_first"。data_format 决定了输入中维度的顺序。如果 data_format="channels_last",则 inputs 的形状为 (batch_size, ..., channels);如果 data_format="channels_first",则 inputs 的形状为 (batch_size, channels, ...)。len(inputs_spatial_shape) 的整数元组/列表,指定用于膨胀卷积的膨胀率。如果 dilation_rate 是整数,则每个空间维度共享相同的 dilation_rate。返回
一个秩为 N+2 的张量,即卷积操作的结果。
conv_transpose 函数keras.ops.conv_transpose(
inputs,
kernel,
strides=1,
padding="valid",
output_padding=None,
data_format=None,
dilation_rate=1,
)
通用 N 维卷积转置。
也称为反卷积。此操作支持 1D、2D 和 3D 卷积。
参数
data_format="channels_last",则 inputs 的形状为 (batch_size,) + inputs_spatial_shape + (num_channels,);如果 data_format="channels_first",则形状为 (batch_size, num_channels) + inputs_spatial_shape。kernel 的形状为 [kernel_spatial_shape, num_output_channels, num_input_channels],num_input_channels 应与 inputs 中的通道数匹配。len(inputs_spatial_shape) 的整数元组/列表,指定卷积沿每个空间维度的步长。如果 strides 是整数,则每个空间维度共享相同的 strides。"valid" 或 "same"。"valid" 表示不应用填充,"same" 表示在输入的左/右或上/下进行均匀填充,使得当 strides=1 时,输出的高度/宽度维度与输入相同。len(inputs_spatial_shape) 的整数元组/列表,指定输出张量的高度和宽度的填充量。可以是一个整数,为所有空间维度指定相同的值。沿给定维度的输出填充量必须低于沿该相同维度的步长。如果设置为 None (默认值),则推断输出形状。"channels_last" 或 "channels_first"。data_format 决定了输入中维度的顺序。如果 data_format="channels_last",则 inputs 的形状为 (batch_size, ..., channels);如果 data_format="channels_first",则 inputs 的形状为 (batch_size, channels, ...)。len(inputs_spatial_shape) 的整数元组/列表,指定用于膨胀卷积的膨胀率。如果 dilation_rate 是整数,则每个空间维度共享相同的 dilation_rate。返回
一个秩为 N+2 的张量,即卷积操作的结果。
ctc_decode 函数keras.ops.ctc_decode(
inputs,
sequence_lengths,
strategy="greedy",
beam_width=100,
top_paths=1,
merge_repeated=True,
mask_index=0,
)
解码 CTC 模型的输出。
参数
(batch_size, max_length, num_classes) 的张量,包含对数(模型输出)。它们不应通过 softmax 进行归一化。(batch_size,) 的张量,包含批次的序列长度。"greedy" 和 "beam_search"。True。0。返回
strategy="greedy",则形状为 (1, batch_size, max_length)。如果 strategy="beam_search",则形状为 (top_paths, batch_size, max_length)。请注意:-1 表示空白标签。strategy="greedy",则为形状为 (batch_size, 1) 的张量,表示每个序列的概率对数的负和。如果 strategy="beam_seatch",则为形状为 (batch_size, top_paths) 的张量,表示每个序列的对数概率。ctc_loss 函数keras.ops.ctc_loss(target, output, target_length, output_length, mask_index=0)
CTC(连接时序分类)损失。
参数
(batch_size, max_length) 的张量,包含整数格式的真实标签。(batch_size, max_length, num_classes) 的张量,包含对数(模型输出)。(batch_size,) 的张量,包含真实标签的长度。(batch_size,) 的张量,包含输出的长度。0。depthwise_conv 函数keras.ops.depthwise_conv(
inputs, kernel, strides=1, padding="valid", data_format=None, dilation_rate=1
)
通用 N 维深度卷积。
此操作支持 1D 和 2D 深度卷积。
参数
data_format="channels_last",则 inputs 的形状为 (batch_size,) + inputs_spatial_shape + (num_channels,);如果 data_format="channels_first",则形状为 (batch_size, num_channels) + inputs_spatial_shape。kernel 的形状为 [kernel_spatial_shape, num_input_channels, num_channels_multiplier],num_input_channels 应与 inputs 中的通道数匹配。len(inputs_spatial_shape) 的整数元组/列表,指定卷积沿每个空间维度的步长。如果 strides 是整数,则每个空间维度共享相同的 strides。"valid" 或 "same"。"valid" 表示不应用填充,"same" 表示在输入的左/右或上/下进行均匀填充,使得当 strides=1 时,输出的高度/宽度维度与输入相同。"channels_last" 或 "channels_first"。data_format 决定了输入中维度的顺序。如果 data_format="channels_last",则 inputs 的形状为 (batch_size, ..., channels);如果 data_format="channels_first",则 inputs 的形状为 (batch_size, channels, ...)。len(inputs_spatial_shape) 的整数元组/列表,指定用于膨胀卷积的膨胀率。如果 dilation_rate 是整数,则每个空间维度共享相同的 dilation_rate。返回
一个秩为 N+2 的张量,即深度卷积操作的结果。
dot_product_attention 函数keras.ops.dot_product_attention(
query,
key,
value,
bias=None,
mask=None,
scale=None,
is_causal=False,
flash_attention=None,
attn_logits_soft_cap=None,
)
缩放点积注意力函数。
计算 Q(query)、K(key)和 V(value)上的注意力函数:attention(Q, K, V) = softmax(Q * K / sqrt(d)) * V。如果我们定义 logits 为 Q * K 的输出,probs 为 softmax 的输出。
在此函数中,我们使用以下表示法表示数组的形状:- B:批次大小 - S:键/值的长度 - T:查询的长度 - N:注意力头的数量 - H:每个注意力头的维度 - K:键/值头的数量 - G:组的数量,等于 N // K
参数
(B, T, N, H) 的查询数组。(B, S, K, H) 的键数组。当 K 等于 N 时,执行多头注意力 (MHA)。否则,如果 N 是 K 的倍数,则执行分组查询注意力 (GQA);如果 K==1(GQA 的特例),则执行多查询注意力 (MQA)。key 相同的价值数组。(B, N, T, S)。True 表示元素应参与注意力。对于加性掩码,用户应将其传递给偏置。形状必须可广播到 (B, N, T, S)。None,则缩放将设置为 1.0 / sqrt(H)。None,则会尝试使用 Flash Attention(如果满足必要条件)。通常,输入必须是 float16 和 bfloat16 数据类型,并且输入布局要求可能因后端而异。返回
形状与 query 相同的注意力输出数组。
示例
>>> query = keras.random.normal((2, 4, 8, 16))
>>> key = keras.random.normal((2, 6, 8, 16))
>>> value = keras.random.normal((2, 6, 8, 16))
>>> keras.ops.nn.dot_product_attention(query, key, value).shape
(2, 4, 8, 16)
elu 函数keras.ops.elu(x, alpha=1.0)
指数线性单元激活函数。
定义为
f(x) = alpha * (exp(x) - 1.) for x < 0,f(x) = x for x >= 0。
参数
1.0。返回
形状与 x 相同的张量。
示例
>>> x = np.array([-1., 0., 1.])
>>> x_elu = keras.ops.elu(x)
>>> print(x_elu)
array([-0.63212055, 0., 1.], shape=(3,), dtype=float64)
gelu 函数keras.ops.gelu(x, approximate=True)
高斯误差线性单元 (GELU) 激活函数。
如果 approximate 为 True,则定义为:f(x) = 0.5 * x * (1 + tanh(sqrt(2 / pi) * (x + 0.044715 * x^3)))
或者如果 approximate 为 False,则定义为:f(x) = x * P(X <= x) = 0.5 * x * (1 + erf(x / sqrt(2))),其中 P(X) ~ N(0, 1)。
参数
True。返回
形状与 x 相同的张量。
示例
>>> x = np.array([-1., 0., 1.])
>>> x_gelu = keras.ops.gelu(x)
>>> print(x_gelu)
array([-0.15865525, 0., 0.84134475], shape=(3,), dtype=float64)
hard_sigmoid 函数keras.ops.hard_sigmoid(x)
硬 sigmoid 激活函数。
定义为
0 if x < -2.5,1 if x > 2.5,(0.2 * x) + 0.5 if -2.5 <= x <= 2.5。
参数
返回
形状与 x 相同的张量。
示例
>>> x = np.array([-1., 0., 1.])
>>> x_hard_sigmoid = keras.ops.hard_sigmoid(x)
>>> print(x_hard_sigmoid)
array([0.3, 0.5, 0.7], shape=(3,), dtype=float64)
leaky_relu 函数keras.ops.leaky_relu(x, negative_slope=0.2)
修正线性单元激活函数的 Leaky 版本。
它在单元不活跃时允许一个小的梯度,定义为
f(x) = alpha * x for x < 0 或 f(x) = x for x >= 0。
参数
0.2。返回
形状与 x 相同的张量。
示例
>>> x = np.array([-1., 0., 1.])
>>> x_leaky_relu = keras.ops.leaky_relu(x)
>>> print(x_leaky_relu)
array([-0.2, 0. , 1. ], shape=(3,), dtype=float64)
log_sigmoid 函数keras.ops.log_sigmoid(x)
sigmoid 激活函数的对数。
定义为 f(x) = log(1 / (1 + exp(-x)))。
参数
返回
形状与 x 相同的张量。
示例
>>> x = keras.ops.convert_to_tensor([-0.541391, 0.0, 0.50, 5.0])
>>> keras.ops.log_sigmoid(x)
array([-1.0000418, -0.6931472, -0.474077, -0.00671535], dtype=float32)
log_softmax 函数keras.ops.log_softmax(x, axis=-1)
Log-softmax 激活函数。
定义为:f(x) = x - max(x) - log(sum(exp(x - max(x))))
参数
-1。返回
形状与 x 相同的张量。
示例
>>> x = np.array([-1., 0., 1.])
>>> x_log_softmax = keras.ops.log_softmax(x)
>>> print(x_log_softmax)
array([-2.40760596, -1.40760596, -0.40760596], shape=(3,), dtype=float64)
max_pool 函数keras.ops.max_pool(
inputs, pool_size, strides=None, padding="valid", data_format=None
)
最大池化操作。
参数
data_format="channels_last",则 inputs 的形状为 (batch_size,) + inputs_spatial_shape + (num_channels,);如果 data_format="channels_first",则形状为 (batch_size, num_channels) + inputs_spatial_shape。池化仅发生在空间维度上。len(inputs_spatial_shape) 的整数元组/列表,指定输入张量每个空间维度的池化窗口大小。如果 pool_size 是整数,则每个空间维度共享相同的 pool_size。len(inputs_spatial_shape) 的整数元组/列表。输入张量每个空间维度的滑动窗口的步长。如果 strides 是整数,则每个空间维度共享相同的 strides。"valid" 或 "same"。"valid" 表示不应用填充,"same" 表示在输入的左/右或上/下进行均匀填充,使得当 strides=1 时,输出的高度/宽度维度与输入相同。"channels_last" 或 "channels_first"。data_format 决定了输入中维度的顺序。如果 data_format="channels_last",则 inputs 的形状为 (batch_size, ..., channels);如果 data_format="channels_first",则 inputs 的形状为 (batch_size, channels, ...)。返回
一个秩为 N+2 的张量,即最大池化操作的结果。
moments 函数keras.ops.moments(x, axes, keepdims=False, synchronized=False)
计算 x 的均值和方差。
通过在 axes 上聚合 x 的内容来计算均值和方差。如果 x 是 1D 且 axes = [0],则这只是向量的均值和方差。
参数
True,则被规约的轴将作为大小为 1 的维度保留在结果中。True,则在分布式训练策略中的每个训练步骤中,跨所有设备同步全局批次统计(均值和方差)。如果为 False,则每个副本使用自己的局部批次统计。返回
一个元组,包含两个张量 - 均值和方差。
示例
>>> x = keras.ops.convert_to_tensor([0, 1, 2, 3, 100], dtype="float32")
>>> keras.ops.moments(x, axes=[0])
(array(21.2, dtype=float32), array(1553.3601, dtype=float32))
multi_hot 函数keras.ops.multi_hot(
inputs, num_classes=None, axis=-1, dtype=None, sparse=False, **kwargs
)
将整数标签编码为多热向量。
此函数将整数标签编码为多热向量,其中每个标签映射到结果向量中的二元值。
参数
-1,对应最后一个维度。返回
示例
>>> data = keras.ops.convert_to_tensor([0, 4])
>>> keras.ops.multi_hot(data, num_classes=5)
array([1.0, 0.0, 0.0, 0.0, 1.0], dtype=float32)
normalize 函数keras.ops.normalize(x, axis=-1, order=2, epsilon=None)
沿指定轴对 x 进行归一化。
定义为:normalize(x) = x / max(norm(x), epsilon)。
参数
backend.epsilon()。返回
归一化后的数组。
示例
>>> x = keras.ops.convert_to_tensor([[1, 2, 3], [4, 5, 6]])
>>> x_norm = keras.ops.math.normalize(x)
>>> print(x_norm)
array([[0.26726124 0.5345225 0.8017837 ]
[0.45584232 0.5698029 0.68376344]], shape=(2, 3), dtype=float32)
one_hot 函数keras.ops.one_hot(x, num_classes, axis=-1, dtype=None, sparse=False)
将整数张量 x 转换为独热张量。
独热编码是一种表示方法,其中每个整数值被转换为一个长度等于 num_classes 的二元向量,并且对应于整数值的索引标记为 1,而所有其他索引标记为 0。
参数
-1 表示最后一个轴。默认为 -1。返回
x 相同的形状的独热编码张量,除了指定的 axis 维度,该维度长度为 num_classes。输出张量的数据类型由 dtype 或后端的默认数据类型确定。示例
>>> x = keras.ops.convert_to_tensor([1, 3, 2, 0])
>>> one_hot(x, num_classes=4)
array([[0. 1. 0. 0.]
[0. 0. 0. 1.]
[0. 0. 1. 0.]
[1. 0. 0. 0.]], shape=(4, 4), dtype=float32)
psnr 函数keras.ops.psnr(x1, x2, max_val)
峰值信噪比 (PSNR) 函数。
此函数计算两个信号 x1 和 x2 之间的峰值信噪比。PSNR 是衡量重建信号质量的指标。PSNR 值越高,表示重建信号与原始信号越接近。请注意,当信号功率小于噪声功率时,PSNR 可能变为负值。
参数
x1 相同。返回
x1 和 x2 之间的 PSNR 值。示例
>>> x1 = keras.random.normal((2, 4, 4, 3))
>>> x2 = keras.random.normal((2, 4, 4, 3))
>>> max_val = 1.0
>>> keras.ops.nn.psnr(x1, x2, max_val)
-3.1697404
relu 函数keras.ops.relu(x)
修正线性单元激活函数。
定义为 f(x) = max(0, x)。
参数
返回
形状与 x 相同的张量。
示例
>>> x1 = keras.ops.convert_to_tensor([-1.0, 0.0, 1.0, 0.2])
>>> keras.ops.relu(x1)
array([0.0, 0.0, 1.0, 0.2], dtype=float32)
relu6 函数keras.ops.relu6(x)
上限为 6 的修正线性单元激活函数。
定义为 f(x) = np.clip(x, 0, 6)。
参数
返回
形状与 x 相同的张量。
示例
>>> x = keras.ops.convert_to_tensor([-3.0, -2.0, 0.1, 0.2, 6.0, 8.0])
>>> keras.ops.relu6(x)
array([0.0, 0.0, 0.1, 0.2, 6.0, 6.0], dtype=float32)
selu 函数keras.ops.selu(x)
缩放指数线性单元 (SELU) 激活函数。
定义为
f(x) = scale * alpha * (exp(x) - 1.) for x < 0,f(x) = scale * x for x >= 0。
参数
返回
形状与 x 相同的张量。
示例
>>> x = np.array([-1., 0., 1.])
>>> x_selu = keras.ops.selu(x)
>>> print(x_selu)
array([-1.11133055, 0., 1.05070098], shape=(3,), dtype=float64)
separable_conv 函数keras.ops.separable_conv(
inputs,
depthwise_kernel,
pointwise_kernel,
strides=1,
padding="valid",
data_format=None,
dilation_rate=1,
)
通用 N 维可分离卷积。
此操作支持 1D 和 2D 可分离卷积。separable_conv 是一个深度卷积后跟一个逐点卷积。
参数
data_format="channels_last",则 inputs 的形状为 (batch_size,) + inputs_spatial_shape + (num_channels,);如果 data_format="channels_first",则形状为 (batch_size, num_channels) + inputs_spatial_shape。depthwise_kernel 的形状为 [kernel_spatial_shape, num_input_channels, num_channels_multiplier],num_input_channels 应与 inputs 中的通道数匹配。pointwise_kernel 的形状为 (*ones_like(kernel_spatial_shape), num_input_channels * num_channels_multiplier, num_output_channels)。len(inputs_spatial_shape) 的整数元组/列表,指定卷积沿每个空间维度的步长。如果 strides 是整数,则每个空间维度共享相同的 strides。"valid" 或 "same"。"valid" 表示不应用填充,"same" 表示在输入的左/右或上/下进行均匀填充,使得当 strides=1 时,输出的高度/宽度维度与输入相同。"channels_last" 或 "channels_first"。data_format 决定了输入中维度的顺序。如果 data_format="channels_last",则 inputs 的形状为 (batch_size, ..., channels);如果 data_format="channels_first",则 inputs 的形状为 (batch_size, channels, ...)。len(inputs_spatial_shape) 的整数元组/列表,指定用于膨胀卷积的膨胀率。如果 dilation_rate 是整数,则每个空间维度共享相同的 dilation_rate。返回
一个秩为 N+2 的张量,即深度卷积操作的结果。
sigmoid 函数keras.ops.sigmoid(x)
Sigmoid 激活函数。
定义为 f(x) = 1 / (1 + exp(-x))。
参数
返回
形状与 x 相同的张量。
示例
>>> x = keras.ops.convert_to_tensor([-6.0, 1.0, 0.0, 1.0, 6.0])
>>> keras.ops.sigmoid(x)
array([0.00247262, 0.7310586, 0.5, 0.7310586, 0.9975274], dtype=float32)
silu 函数keras.ops.silu(x)
Sigmoid 线性单元 (SiLU) 激活函数,也称为 Swish。
SiLU 激活函数通过 sigmoid 函数乘以其输入来计算。定义为 f(x) = x * sigmoid(x)。
参数
返回
形状与 x 相同的张量。
示例
>>> x = keras.ops.convert_to_tensor([-6.0, 1.0, 0.0, 1.0, 6.0])
>>> keras.ops.sigmoid(x)
array([0.00247262, 0.7310586, 0.5, 0.7310586, 0.9975274], dtype=float32)
>>> keras.ops.silu(x)
array([-0.0148357, 0.7310586, 0.0, 0.7310586, 5.9851646], dtype=float32)
hard_silu 函数keras.ops.hard_silu(x)
硬 SiLU 激活函数,也称为 Hard Swish。
定义为
0 if if x < -3x if x > 3x * (x + 3) / 6 if -3 <= x <= 3它是 silu 激活函数的一种更快速、分段线性的近似。
参数
返回
形状与 x 相同的张量。
示例
>>> x = keras.ops.convert_to_tensor([-3.0, -1.0, 0.0, 1.0, 3.0])
>>> keras.ops.hard_silu(x)
array([-0.0, -0.3333333, 0.0, 0.6666667, 3.0], shape=(5,), dtype=float32)
softmax 函数keras.ops.softmax(x, axis=-1)
Softmax 激活函数。
输出向量的元素位于 (0, 1) 范围之间,并且它们的总和恰好为 1(不包括浮点舍入误差)。
每个向量都独立处理。axis 参数指定了在输入张量中应用该函数的轴。
定义为:f(x) = exp(x) / sum(exp(x))
参数
返回
形状与 x 相同的张量。
示例
>>> x = np.array([-1., 0., 1.])
>>> x_softmax = keras.ops.softmax(x)
>>> print(x_softmax)
array([0.09003057, 0.24472847, 0.66524096], shape=(3,), dtype=float64)
softplus 函数keras.ops.softplus(x)
Softplus 激活函数。
定义为 f(x) = log(exp(x) + 1),其中 log 是自然对数,exp 是指数函数。
参数
返回
形状与 x 相同的张量。
示例
>>> x = keras.ops.convert_to_tensor([-0.555, 0.0, 0.555])
>>> keras.ops.softplus(x)
array([0.45366603, 0.6931472, 1.008666], dtype=float32)
softsign 函数keras.ops.softsign(x)
Softsign 激活函数。
定义为 f(x) = x / (abs(x) + 1)。
参数
返回
形状与 x 相同的张量。
示例
>>> x = keras.ops.convert_to_tensor([-0.100, -10.0, 1.0, 0.0, 100.0])
>>> keras.ops.softsign(x)
Array([-0.09090909, -0.90909094, 0.5, 0.0, 0.990099], dtype=float32)
sparse_categorical_crossentropy 函数keras.ops.sparse_categorical_crossentropy(target, output, from_logits=False, axis=-1)
计算稀疏分类交叉熵损失。
稀疏分类交叉熵损失与分类交叉熵类似,但当目标张量包含整数类别标签而不是独热编码向量时使用。它衡量目标概率和输出概率或对数之间的不相似性。
参数
output 张量的形状匹配,但最后一个维度除外。target 张量的形状匹配,但最后一个维度除外。output 是对数张量还是概率张量。如果 output 表示对数,则设置为 True;否则,如果 output 表示概率,则设置为 False。默认为 False。-1,对应张量的最后一个维度。返回
target 和 output 之间计算的稀疏分类交叉熵损失。示例
>>> target = keras.ops.convert_to_tensor([0, 1, 2], dtype=int32)
>>> output = keras.ops.convert_to_tensor(
... [[0.9, 0.05, 0.05],
... [0.1, 0.8, 0.1],
... [0.2, 0.3, 0.5]])
>>> sparse_categorical_crossentropy(target, output)
array([0.10536056 0.22314355 0.6931472 ], shape=(3,), dtype=float32)
silu 函数keras.ops.swish(x)
Sigmoid 线性单元 (SiLU) 激活函数,也称为 Swish。
SiLU 激活函数通过 sigmoid 函数乘以其输入来计算。定义为 f(x) = x * sigmoid(x)。
参数
返回
形状与 x 相同的张量。
示例
>>> x = keras.ops.convert_to_tensor([-6.0, 1.0, 0.0, 1.0, 6.0])
>>> keras.ops.sigmoid(x)
array([0.00247262, 0.7310586, 0.5, 0.7310586, 0.9975274], dtype=float32)
>>> keras.ops.silu(x)
array([-0.0148357, 0.7310586, 0.0, 0.7310586, 5.9851646], dtype=float32)
hard_silu 函数keras.ops.hard_swish(x)
硬 SiLU 激活函数,也称为 Hard Swish。
定义为
0 if if x < -3x if x > 3x * (x + 3) / 6 if -3 <= x <= 3它是 silu 激活函数的一种更快速、分段线性的近似。
参数
返回
形状与 x 相同的张量。
示例
>>> x = keras.ops.convert_to_tensor([-3.0, -1.0, 0.0, 1.0, 3.0])
>>> keras.ops.hard_silu(x)
array([-0.0, -0.3333333, 0.0, 0.6666667, 3.0], shape=(5,), dtype=float32)
celu 函数keras.ops.celu(x, alpha=1.0)
连续可微指数线性单元。
定义为
f(x) = alpha * (exp(x / alpha) - 1) for x < 0,f(x) = x for x >= 0。
参数
1.0。返回
形状与 x 相同的张量。
示例
>>> x = np.array([-1., 0., 1.])
>>> x_celu = keras.ops.celu(x)
>>> print(x_celu)
array([-0.63212056, 0. , 1. ], shape=(3,), dtype=float64)
sparsemax 函数keras.ops.sparsemax(x, axis=-1)
Sparsemax 激活函数。
对于每个批次 i 和类别 j,sparsemax 激活函数定义为
sparsemax(x)[i, j] = max(x[i, j] - τ(x[i, :]), 0)。
参数
int,应用 sparsemax 操作的轴。返回
一个张量,sparsemax 变换的输出。具有与 x 相同的类型和形状。
示例
>>> x = np.array([-1., 0., 1.])
>>> x_sparsemax = keras.ops.sparsemax(x)
>>> print(x_sparsemax)
array([0., 0., 1.], shape=(3,), dtype=float64)
squareplus 函数keras.ops.squareplus(x, b=4)
Squareplus 激活函数。
Squareplus 激活函数定义为
f(x) = (x + sqrt(x^2 + b)) / 2
参数
返回
形状与 x 相同的张量。
示例
>>> x = np.array([-1.0, 0.0, 1.0])
>>> x_squareplus = keras.ops.squareplus(x)
>>> print(x_squareplus)
array([0.6180, 1.0000, 1.6180], dtype=float32)
sparse_plus 函数keras.ops.sparse_plus(x)
SparsePlus 激活函数。
定义为
f(x) = 0 for x <= -1. f(x) = (1/4) * (x + 1)^2 for -1 < x < 1. f(x) = x for x >= 1.
参数
返回
形状与 x 相同的张量。
示例
>>> x = np.array([-1.0, 0.0, 1.0])
>>> x_sparse_plus = keras.ops.sparse_plus(x)
>>> print(x_sparse_plus)
Array([0. 0.25 1. ], shape=(3,), dtype=float32)
soft_shrink 函数keras.ops.soft_shrink(x, threshold=0.5)
Soft Shrink 激活函数。
定义为
f(x) = x - threshold if x > threshold, f(x) = x + threshold if x < -threshold, f(x) = 0 otherwise。
参数
返回
形状与 x 相同的张量。
示例
>>> x = np.array([-1.0, 0.0, 1.0])
>>> x_soft_shrink = keras.ops.soft_shrink(x)
>>> print(x_soft_shrink)
array([-0.5 0. 0.5], shape=(3,), dtype=float64)
threshold 函数keras.ops.threshold(x, threshold, default_value)
阈值激活函数。
该函数按如下方式对输入 x 进行阈值处理:f(x) = x if x > threshold, f(x) = default_value otherwise。
参数
x <= threshold 时分配的值。返回
形状与 x 相同的张量。
示例
>>> x = np.array([-1.0, 0.0, 1.0, 2.0])
>>> x_threshold = keras.ops.threshold(x, 1, 0)
>>> print(x_threshold)
array([0., 0., 0., 2.], shape=(4,), dtype=float64)
glu 函数keras.ops.glu(x, axis=-1)
门控线性单元 (GLU) 激活函数。
定义为
f(x) = a * sigmoid(b),其中 x 沿给定轴分割为 a 和 b。
参数
-1。返回
形状是输入一半的张量。
示例
>>> x = np.array([-1., 0., 1. , 1.])
>>> x_glu = keras.ops.glu(x)
>>> print(x_glu)
array([-0.73105858, 0. ], shape=(2,), dtype=float64)
tanh_shrink 函数keras.ops.tanh_shrink(x)
逐元素应用 tanh 收缩函数。
定义为
f(x) = x - tanh(x).
参数
返回
形状与 x 相同的输出张量,其中每个元素根据 tanh 收缩操作进行转换。
示例
>>> x = np.array([ -1., 0., 1.])
>>> x_tanh_shrink = keras.ops.tanh_shrink(x)
>>> print(x_tanh_shrink)
array([-0.23840584 0. 0.23840584], shape=(3,), dtype=float64)
hard_tanh 函数keras.ops.hard_tanh(x)
逐元素应用 HardTanh 函数。
定义为
f(x) = -1 for x < -1,f(x) = x for -1 <= x <= 1,f(x) = 1 for x > 1。
参数
返回
形状与 x 相同的输出张量,值被限制在 -1 和 1 之间。
示例
>>> x = np.array([-2., -1., 0., 1., 2.])
>>> x_hard_tanh = keras.ops.hard_tanh(x)
>>> print(x_hard_tanh)
array([-1. -1. 0. 1. 1.], shape=(5,), dtype=float64)
hard_shrink 函数keras.ops.hard_shrink(x, threshold=0.5)
Hard Shrink 激活函数。
Hard Shrink 函数是一个阈值操作,定义为
f(x) = x if |x| > threshold, f(x) = 0 otherwise。
参数
返回
形状与 x 相同的张量。
示例
>>> x = np.array([-0.5, 0., 1.])
>>> x_hard_shrink = keras.ops.hard_shrink(x)
>>> print(x_hard_shrink)
array([0. 0. 1.], shape=(3,), dtype=float64)