average_pool functionkeras.ops.average_pool(
inputs, pool_size, strides=None, padding="valid", data_format=None
)
Average pooling operation.
Arguments
inputs has shape
(batch_size,) + inputs_spatial_shape + (num_channels,) if
data_format="channels_last", or
(batch_size, num_channels) + inputs_spatial_shape if
data_format="channels_first". Pooling happens over the spatial
dimensions only.len(inputs_spatial_shape), specifying the size of the pooling
window for each spatial dimension of the input tensor. If
pool_size is int, then every spatial dimension shares the same
pool_size.len(inputs_spatial_shape). The stride of the sliding window for
each spatial dimension of the input tensor. If strides is int,
then every spatial dimension shares the same strides."valid" or "same". "valid" means no
padding is applied, and "same" results in padding evenly to the
left/right or up/down of the input such that output has the
same height/width dimension as the input when strides=1."channels_last" or "channels_first".
data_format determines the ordering of the dimensions in the
inputs. If data_format="channels_last", inputs is of shape
(batch_size, ..., channels) while if
data_format="channels_first", inputs is of shape
(batch_size, channels, ...).Returns
A tensor of rank N+2, the result of the average pooling operation.
batch_normalization functionkeras.ops.batch_normalization(
x, mean, variance, axis, offset=None, scale=None, epsilon=0.001
)
Normalizes x by mean and variance.
This op is typically used by the batch normalization step in a neural network. It normalizes the input tensor along the given axis.
Arguments
axis维度长度相同的均值向量.axis维度长度相同的方差向量.axis维度长度相同的偏移向量.如果不是None,则将offset加到归一化后的张量上.默认为None.axis维度长度相同的缩放向量.如果不是None,则归一化后的张量乘以scale.默认为None.Returns
归一化后的张量.
Example
>>> 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 functionkeras.ops.binary_crossentropy(target, output, from_logits=False)
计算目标张量和输出张量之间的二元交叉熵损失.
二元交叉熵损失通常用于二分类任务中,其中每个输入样本属于两个类别之一.它衡量目标和输出概率或对数之间的差异.
参数:
target: 表示真实二元标签的目标张量.
其形状应与output张量的形状匹配.
output: 表示预测概率或对数的输出张量.
其形状应与target张量的形状匹配.
from_logits: (可选) 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 functionkeras.ops.categorical_crossentropy(target, output, from_logits=False, axis=-1)
计算目标张量和输出张量之间的分类交叉熵损失.
分类交叉熵损失通常用于多类分类任务中,其中每个输入样本可以属于多个类别之一.它衡量目标和输出概率或对数之间的差异.
参数:
target: 表示真实分类标签的目标张量.
其形状应与output张量的形状匹配,除了最后一个维度.
output: 表示预测概率或对数的输出张量.
其形状应与target张量的形状匹配,除了最后一个维度.
from_logits: (可选) output是对数张量还是概率张量.
如果output表示对数,则设置为True;否则,
如果output表示概率,则设置为False.
默认为False.
axis: (可选) 计算分类交叉熵的轴.
默认为-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 functionkeras.ops.conv(
inputs, kernel, strides=1, padding="valid", data_format=None, dilation_rate=1
)
通用 N 维卷积.
此操作支持 1D、2D 和 3D 卷积.
参数:
inputs: 秩为 N+2 的张量.如果 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: 秩为 N+2 的张量.kernel 的形状为
(kernel_spatial_shape, num_input_channels, num_output_channels).
num_input_channels 应与 inputs 中的通道数匹配.
strides: int 或 int 元组/列表,长度为 len(inputs_spatial_shape),
指定沿每个空间维度进行卷积的步幅.如果 strides 是 int,则每个空间维度共享
相同的 strides.
padding: 字符串,可以是 "valid" 或 "same"."valid" 表示不应用填充,而 "same"
结果是在输入的左右或上下均匀填充,使得当 strides=1 时输出具有与输入相同的高度/宽度维度.
data_format: 字符串,可以是 "channels_last" 或 "channels_first".
data_format 确定输入中维度的顺序.如果 data_format="channels_last",inputs 的形状为
(batch_size, ..., channels);如果 data_format="channels_first",inputs 的形状为
(batch_size, channels, ...).
dilation_rate: int 或 int 元组/列表,长度为 len(inputs_spatial_shape),
指定用于扩张卷积的扩张率.如果 dilation_rate 是 int,则每个空间维度共享
相同的 dilation_rate.
返回: 一个秩为 N+2 的张量,卷积操作的结果.
conv_transpose functionkeras.ops.conv_transpose(
inputs,
kernel,
strides,
padding="valid",
output_padding=None,
data_format=None,
dilation_rate=1,
)
通用 N 维卷积转置.
也称为反卷积.此操作支持 1D、2D 和 3D 卷积.
参数:
inputs: 秩为 N+2 的张量.如果 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: 秩为 N+2 的张量.kernel 的形状为
[kernel_spatial_shape, num_output_channels, num_input_channels],
num_input_channels 应与 inputs 中的通道数匹配.
strides: int 或 int 元组/列表,长度为 len(inputs_spatial_shape),
指定卷积在每个空间维度上的步幅.如果 strides 是 int,则每个空间维度共享相同的 strides.
padding: 字符串,"valid" 或 "same"."valid" 表示不应用填充,而 "same" 会导致在输入的左右或上下均匀填充,
使得当 strides=1 时,输出与输入具有相同的高度/宽度维度.
output_padding: int 或 int 元组/列表,长度为 len(inputs_spatial_shape),
指定沿输出张量的高度和宽度的填充量.可以是一个整数,以指定所有空间维度的相同值.
沿给定维度的输出填充量必须小于该维度的步幅.如果设置为 None(默认),则推断输出形状.
data_format: 字符串,"channels_last" 或 "channels_first".
data_format 确定输入中维度的顺序.如果 data_format="channels_last",inputs 的形状为
(batch_size, ..., channels);如果 data_format="channels_first",inputs 的形状为
(batch_size, channels, ...).
dilation_rate: int 或 int 元组/列表,长度为 len(inputs_spatial_shape),
指定用于扩张卷积的扩张率.如果 dilation_rate 是 int,则每个空间维度共享相同的 dilation_rate.
返回: 一个秩为 N+2 的张量,卷积操作的结果.
depthwise_conv functionkeras.ops.depthwise_conv(
inputs, kernel, strides=1, padding="valid", data_format=None, dilation_rate=1
)
通用 N 维深度卷积.
此操作支持 1D 和 2D 深度卷积.
参数:
inputs: 秩为 N+2 的张量.如果 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: 秩为 N+2 的张量.kernel 的形状为
[kernel_spatial_shape, num_input_channels, num_channels_multiplier],
num_input_channels 应与 inputs 中的通道数匹配.
strides: int 或 int 元组/列表,长度为 len(inputs_spatial_shape),
指定沿每个空间维度进行卷积的步幅.如果 strides 是 int,则每个空间维度共享
相同的 strides.
padding: 字符串,可以是 "valid" 或 "same"."valid" 表示不应用填充,而 "same"
结果是在输入的左右或上下均匀填充,使得当 strides=1 时输出具有与输入相同的高度/宽度维度.
data_format: 字符串,可以是 "channels_last" 或 "channels_first".
data_format 确定输入中维度的顺序.如果 data_format="channels_last",inputs 的形状为
(batch_size, ..., channels);如果 data_format="channels_first",inputs 的形状为
(batch_size, channels, ...).
dilation_rate: int 或 int 元组/列表,长度为 len(inputs_spatial_shape),
指定用于扩张卷积的扩张率.如果 dilation_rate 是 int,则每个空间维度共享
相同的 dilation_rate.
返回: 一个秩为 N+2 的张量,深度卷积操作的结果.
elu functionkeras.ops.elu(x, alpha=1.0)
指数线性单元激活函数.
其定义为:
f(x) = alpha * (exp(x) - 1.) for x < 0, f(x) = x for x >= 0.
参数:
x: 输入张量.
alpha: 一个标量,正值部分的斜率.默认为 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 functionkeras.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).
参数:
x: 输入张量.
approximate: GELU激活函数的近似版本.默认为 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 functionkeras.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相同的张量.
示例:
>>> 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 functionkeras.ops.leaky_relu(x, negative_slope=0.2)
修正线性单元激活函数的泄漏版本.
它在单元不活跃时允许一个小的梯度,定义为:
f(x) = alpha * x for x < 0 或 f(x) = x for x >= 0.
参数:
x: 输入张量.
negative_slope: 激活函数在 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 functionkeras.ops.log_sigmoid(x)
对sigmoid激活函数的对数.
它定义为 f(x) = log(1 / (1 + exp(-x))).
参数: 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 functionkeras.ops.log_softmax(x, axis=-1)
对数-softmax激活函数.
其定义为:
f(x) = x - max(x) - log(sum(exp(x - max(x))))
参数:
x: 输入张量.
axis: 整数,沿此轴应用对数-softmax.
默认为 -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 functionkeras.ops.max_pool(
inputs, pool_size, strides=None, padding="valid", data_format=None
)
平均池化操作.
参数:
inputs: 秩为 N+2 的张量.如果 data_format="channels_last",inputs 的形状为
(batch_size,) + inputs_spatial_shape + (num_channels,);如果
data_format="channels_first",inputs 的形状为
(batch_size, num_channels) + inputs_spatial_shape.池化仅在空间维度上进行.
pool_size: int 或大小为 len(inputs_spatial_shape) 的整数元组/列表,指定输入张量每个空间维度的池化窗口大小.如果 pool_size 是 int,则每个空间维度共享相同的 pool_size.
strides: int 或大小为 len(inputs_spatial_shape) 的整数元组/列表.输入张量每个空间维度的滑动窗口步幅.如果 strides 是 int,则每个空间维度共享相同的 strides.
padding: 字符串,"valid" 或 "same"."valid" 表示不应用填充,而 "same" 会在输入的左右或上下均匀填充,使得当 strides=1 时输出与输入的高度/宽度维度相同.
data_format: 字符串,"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 functionkeras.ops.moments(x, axes, keepdims=False, synchronized=False)
计算x的均值和方差.
均值和方差是通过聚合x的内容跨axes计算的.如果x是1维的且axes = [0],这只是向量的均值和方差.
参数:
x: 输入张量.
axes: 计算均值和方差的轴列表.
keepdims: 如果设置为True,则在结果中保留被缩减的轴,尺寸为1.
synchronized: 仅适用于TensorFlow后端.
如果为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 functionkeras.ops.multi_hot(
inputs, num_classes=None, axis=-1, dtype=None, sparse=False, **kwargs
)
将整数标签编码为多热向量.
此函数将整数标签编码为多热向量,其中每个标签在结果向量中映射为一个二进制值.
参数:
inputs: 要转换为多热向量的整数标签张量.
num_classes: 整数,唯一类别的总数.
axis: (可选) 应沿其添加多热编码的轴.默认为 -1,对应于最后一个维度.
dtype: (可选) 结果张量的数据类型.默认为后端的浮点类型.
sparse: 是否返回稀疏张量;适用于支持稀疏张量的后端.
返回: 张量: 多热编码的张量.
示例:
>>> 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)
one_hot functionkeras.ops.one_hot(x, num_classes, axis=-1, dtype=None, sparse=False)
将整数张量 x 转换为 one-hot 张量.
one-hot 编码是一种表示方法,其中每个整数值被转换为一个长度等于 num_classes 的二进制向量,并且与整数值对应的索引被标记为 1,而所有其他索引被标记为 0.
参数:
x: 要编码的整数张量.形状可以是任意的,但 dtype 应为整数.
num_classes: one-hot 编码的类别数.
axis: 执行编码的轴.-1 表示最后一个轴.默认为 -1.
dtype: (可选) 输出张量的数据类型.如果未提供,则默认为后端默认的数据类型.
sparse: 是否返回稀疏张量;适用于支持稀疏张量的后端.
返回:
整数张量: one-hot 编码的张量,形状与 x 相同,除了指定的 axis 维度,该维度将具有 num_classes 的长度.输出张量的 dtype 由 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)
relu functionkeras.ops.relu(x)
修正线性单元激活函数.
它定义为 f(x) = max(0, x).
参数: 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 functionkeras.ops.relu6(x)
修正线性单元激活函数,上限为6.
它定义为 f(x) = np.clip(x, 0, 6).
参数: x: 输入张量.
返回:
与 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 functionkeras.ops.selu(x)
缩放指数线性单元(SELU)激活函数.
它定义为:
f(x) = scale * alpha * (exp(x) - 1.) for x < 0,
f(x) = scale * x for x >= 0.
参数: 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 functionkeras.ops.separable_conv(
inputs,
depthwise_kernel,
pointwise_kernel,
strides=1,
padding="valid",
data_format=None,
dilation_rate=1,
)
通用N维可分离卷积.
此操作支持1D和2D可分离卷积.separable_conv 是一个深度卷积后跟一个点卷积.
参数:
inputs: 秩为N+2的张量.如果 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: 秩为N+2的张量.depthwise_kernel 的形状为
[kernel_spatial_shape, num_input_channels, num_channels_multiplier],
num_input_channels 应与 inputs 中的通道数匹配.
pointwise_kernel: 秩为N+2的张量.pointwise_kernel 的形状为
(*ones_like(kernel_spatial_shape),
num_input_channels * num_channels_multiplier, num_output_channels).
strides: int 或 int 元组/列表,长度为 len(inputs_spatial_shape),
指定沿每个空间维度卷积的步幅.如果 strides 是 int,则每个空间维度共享相同的 strides.
padding: 字符串,可以是 "valid" 或 "same"."valid" 表示不应用填充,
而 "same" 会在输入的左右或上下均匀填充,使得当 strides=1 时输出具有与输入相同的高度/宽度维度.
data_format: 字符串,可以是 "channels_last" 或 "channels_first".
data_format 确定输入中维度的顺序.如果 data_format="channels_last",inputs 的形状为
(batch_size, ..., channels);如果 data_format="channels_first",inputs 的形状为
(batch_size, channels, ...).
dilation_rate: int 或 int 元组/列表,长度为 len(inputs_spatial_shape),
指定用于扩张卷积的扩张率.如果 dilation_rate 是 int,则每个空间维度共享相同的 dilation_rate.
返回: 一个秩为N+2的张量,深度卷积操作的结果.
sigmoid functionkeras.ops.sigmoid(x)
Sigmoid激活函数.
它定义为 f(x) = 1 / (1 + exp(-x)).
参数: 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 functionkeras.ops.silu(x)
Sigmoid 线性单元(SiLU)激活函数,也称为 Swish.
SiLU 激活函数通过将其输入乘以 sigmoid 函数来计算.它定义为 f(x) = x * sigmoid(x).
参数: 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 functionkeras.ops.hard_silu(x)
硬SiLU激活函数,也称为硬Swish.
它定义为:
0 如果 x < -3x 如果 x > 3x * (x + 3) / 6 如果 -3 <= x <= 3它是一种更快的、分段线性近似SiLU激活函数的方法.
参数: x: 输入张量.
返回:
与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 functionkeras.ops.softmax(x, axis=-1)
Softmax激活函数.
输出向量的元素位于范围(0, 1)内,并且它们的总和恰好为1(排除浮点数舍入误差).
每个向量独立处理.axis参数指定在输入中沿哪个轴应用该函数.
其定义为:
f(x) = exp(x) / sum(exp(x))
参数: x: 输入张量. axis: 整数,沿该轴应用softmax.
返回:
与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 functionkeras.ops.softplus(x)
Softplus激活函数.
它定义为 f(x) = log(exp(x) + 1),其中 log 是自然对数,exp 是指数函数.
参数: x: 输入张量.
返回:
与 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 functionkeras.ops.softsign(x)
软符号激活函数.
它定义为 f(x) = x / (abs(x) + 1).
参数: x: 输入张量.
返回:
与 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 functionkeras.ops.sparse_categorical_crossentropy(target, output, from_logits=False, axis=-1)
计算稀疏分类交叉熵损失.
稀疏分类交叉熵损失类似于分类交叉熵,但它用于目标张量包含整数类别标签而不是独热编码向量的情况.它衡量目标和输出概率或对数之间的差异.
参数:
target: 表示真实类别标签的整数的目标张量.其形状应与output张量的形状匹配,除了最后一个维度.
output: 表示预测概率或对数的输出张量.其形状应与target张量的形状匹配,除了最后一个维度.
from_logits: (可选) output是对数张量还是概率张量.
如果output表示对数,则设置为True;否则,如果output表示概率,则设置为False.
默认为False.
axis: (可选) 计算稀疏分类交叉熵的轴.
默认为-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 functionkeras.ops.swish(x)
Sigmoid 线性单元(SiLU)激活函数,也称为 Swish.
SiLU 激活函数通过将其输入乘以 sigmoid 函数来计算.它定义为 f(x) = x * sigmoid(x).
参数: 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 functionkeras.ops.hard_swish(x)
硬SiLU激活函数,也称为硬Swish.
它定义为:
0 如果 x < -3x 如果 x > 3x * (x + 3) / 6 如果 -3 <= x <= 3它是一种更快的、分段线性近似SiLU激活函数的方法.
参数: x: 输入张量.
返回:
与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)