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"
,则 inputs
的形状为 (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)
计算目标张量和输出张量之间的二元交叉熵损失。
二元交叉熵损失通常用于二元分类任务,其中每个输入样本属于两个类别之一。它衡量目标概率或 logits 与输出概率或 logits 之间的差异。
参数
output
张量的形状匹配。target
张量的形状匹配。output
是否为 logits 张量或概率张量。如果 output
代表 logits,则设置为 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)
计算目标张量和输出张量之间的分类交叉熵损失。
分类交叉熵损失通常用于多类分类任务,其中每个输入样本可以属于多个类别之一。它衡量目标概率或 logits 与输出概率或 logits 之间的差异。
参数
output
张量的形状匹配,但最后一个维度除外。target
张量的形状匹配,但最后一个维度除外。output
是否为 logits 张量或概率张量。如果 output
代表 logits,则设置为 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-D 卷积。
此操作支持 1D、2D 和 3D 卷积。
参数
data_format="channels_last"
,则 inputs
的形状为 (batch_size,) + inputs_spatial_shape + (num_channels,)
;如果 data_format="channels_first"
,则 inputs
的形状为 (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,
padding="valid",
output_padding=None,
data_format=None,
dilation_rate=1,
)
通用 N-D 转置卷积。
也称为反卷积。此操作支持 1D、2D 和 3D 卷积。
参数
data_format="channels_last"
,则 inputs
的形状为 (batch_size,) + inputs_spatial_shape + (num_channels,)
;如果 data_format="channels_first"
,则 inputs
的形状为 (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)
的张量,包含 logits(模型的输出)。它们不应通过 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)
的张量,表示每个序列的概率 logits 总和的负数。如果 strategy="beam_seatch"
,则为形状为 (batch_size, top_paths)
的张量,表示每个序列的对数概率。ctc_loss
函数keras.ops.ctc_loss(target, output, target_length, output_length, mask_index=0)
CTC (Connectionist Temporal Classification,连接时序分类) 损失。
参数
(batch_size, max_length)
的张量,包含整数格式的真实标签。(batch_size, max_length, num_classes)
的张量,包含 logits(模型的输出)。(batch_size,)
的张量,包含真实标签长度。(batch_size,)
的张量,包含输出长度。0
。depthwise_conv
函数keras.ops.depthwise_conv(
inputs, kernel, strides=1, padding="valid", data_format=None, dilation_rate=1
)
通用 N-D 深度可分离卷积。
此操作支持 1D 和 2D 深度可分离卷积。
参数
data_format="channels_last"
,则 inputs
的形状为 (batch_size,) + inputs_spatial_shape + (num_channels,)
;如果 data_format="channels_first"
,则 inputs
的形状为 (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,
)
缩放点积注意力函数。
计算 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 dtype,并且输入布局要求可能因后端而异。返回值
注意力输出的数组,形状与 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)
指数线性单元激活函数。
定义为
当 x < 0
时,f(x) = alpha * (exp(x) - 1.)
;当 x >= 0
时,f(x) = x
。
参数
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 激活函数。
定义为
当 x < -2.5
时,0
;当 x > 2.5
时,1
;当 -2.5 <= x <= 2.5
时,(0.2 * x) + 0.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 版本。
当单元未激活时,它允许小梯度,定义为
当 x < 0
时,f(x) = alpha * x
;当 x >= 0
时,f(x) = x
。
参数
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"
,则 inputs
的形状为 (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
的均值和方差。
通过聚合 x
在 axes
上的内容来计算均值和方差。如果 x
是 1-D 且 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) 激活函数。
定义为
当 x < 0
时,f(x) = scale * alpha * (exp(x) - 1.)
;当 x >= 0
时,f(x) = scale * x
。
参数
返回值
形状与 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-D 可分离卷积。
此操作支持 1D 和 2D 可分离卷积。separable_conv
是深度可分离卷积,后跟逐点卷积。
参数
data_format="channels_last"
,则 inputs
的形状为 (batch_size,) + inputs_spatial_shape + (num_channels,)
;如果 data_format="channels_first"
,则 inputs
的形状为 (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 激活函数,也称为硬 Swish。
定义为
if x < -3
时,0
x > 3
时,x
-3 <= x <= 3
时,x * (x + 3) / 6
它是 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)
计算稀疏分类交叉熵损失。
稀疏分类交叉熵损失类似于分类交叉熵,但当目标张量包含整数类标签而不是独热编码向量时使用。它衡量目标概率或 logits 与输出概率或 logits 之间的差异。
参数
output
张量的形状匹配,但最后一个维度除外。target
张量的形状匹配,但最后一个维度除外。output
是否为 logits 张量或概率张量。如果 output
代表 logits,则设置为 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 激活函数,也称为硬 Swish。
定义为
if x < -3
时,0
x > 3
时,x
-3 <= x <= 3
时,x * (x + 3) / 6
它是 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)
连续可微指数线性单元。
定义为
当 x < 0
时,f(x) = alpha * (exp(x / alpha) - 1)
;当 x >= 0
时,f(x) = x
。
参数
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 激活函数。
定义为
当 x <= -1
时,f(x) = 0
。当 -1 < x < 1
时,f(x) = (1/4) * (x + 1)^2
。当 x >= 1
时,f(x) = x
。
参数
返回值
形状与 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)
软阈值收缩激活函数。
定义为
当 x > threshold
时,f(x) = x - threshold
;当 x < -threshold
时,f(x) = x + threshold
;否则,f(x) = 0
。
参数
返回值
形状与 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
进行阈值处理:当 x > threshold
时,f(x) = x
;否则,f(x) = default_value
。
参数
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 函数。
定义为
当 x < -1
时,f(x) = -1
;当 -1 <= x <= 1
时,f(x) = x
;当 x > 1
时,f(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)
硬阈值收缩激活函数。
硬阈值收缩函数是阈值处理操作,定义为
当 |x| > threshold
时,f(x) = x
;否则,f(x) = 0
。
参数
返回值
形状与 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)