负对数似然损失

负对数似然损失 - 22

版本

此版本的运算符自版本22起可用。

摘要

NegativeLogLikelihoodLoss 运算符计算(加权)负对数似然损失。 其“输入”张量的形状为 (N, C, d1, d2, …, dk),其中 k >= 0。 “输入”张量包含输入[n, :, d_1, d_2,…, d_k] 属于 [0, C) 类别的对数概率。 运算符的“目标”输入张量的形状为 (N, d1, d2, …, dk)。它编码了类别标签(C 个类别之一) 或者它可能包含一个特殊值(由属性 ignore_index 指示)用于 N x d1 x d2 x … x dk 样本。 输入[n, :, d_1, d_2,…d_k] 被分类为类别 c = target[n][d_1][d_2]…[d_k] 的损失值计算如下:

loss[n][d_1][d_2]...[d_k] = -input[n][c][d_1][d_2]...[d_k].

当提供了一个可选的“weight”时,样本损失的计算方式为:

loss[n][d_1][d_2]...[d_k] = -input[n][c][d_1][d_2]...[d_k] * weight[c].

当目标值等于ignore_index时,损失为零。

loss[n][d_1][d_2]...[d_k] = 0, when target[n][d_1][d_2]...[d_k] = ignore_index

如果“reduction”属性设置为“none”,操作符的输出将是上述形状为(N, d1, d2, …, dk)的损失。 如果“reduction”属性设置为“mean”(默认属性值),输出损失是(权重)平均的:

mean(loss), if "weight" is not provided,

或者如果提供了权重,

sum(loss) / sum(weight[target[n][d_1][d_2]...[d_k]]]), for all samples.

如果“reduction”属性设置为“sum”,则输出是一个标量:sum(loss)

另请参阅 https://pytorch.org/docs/stable/nn.html#torch.nn.NLLLoss.

示例 1:

// negative log likelihood loss, "none" reduction
N, C, d1 = 2, 3, 2
input = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],
          [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]
target = [[2, 1], [0, 2]]

loss = np.zeros((N, d1))
for n in range(N):
    for d_1 in range(d1):
        c = target[n][d_1]
        loss[n][d_1] = -input[n][c][d_1]

// print(loss)
// [[-3. -2.]
//  [-0. -2.]]

示例 2:

// weighted negative log likelihood loss, sum reduction
N, C, d1 = 2, 3, 2
input = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],
        [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]
target = [[2, 1], [0, 2]]
weight = [0.2, 0.3, 0.1]
loss = np.zeros((N, d1))
for n in range(N):
    for d_1 in range(d1):
        c = target[n][d_1]
        loss[n][d_1] = -input[n][c][d_1] * weight[c]

loss = np.sum(loss)
// print(loss)
// -1.1

示例 3:

// weighted negative log likelihood loss, mean reduction
N, C, d1 = 2, 3, 2
input = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],
        [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]
target = [[2, 1], [0, 2]]
weight = [0.2, 0.3, 0.1]
loss = np.zeros((N, d1))
weight_total = 0
for n in range(N):
    for d_1 in range(d1):
        c = target[n][d_1]
        loss[n][d_1] = -input[n][c][d_1] * weight[c]
        weight_total = weight_total + weight[c]

loss = np.sum(loss) / weight_total
// print(loss)
// -1.57

属性

  • ignore_index - INT :

    指定一个被忽略且不贡献于输入梯度的目标值。这是一个可选值。

  • reduction - STRING (默认值为 'mean'):

    应用于损失的类型:无、求和、平均(默认)。‘无’:输出是每个样本的损失。‘求和’:输出将被求和。‘平均’:输出的总和将除以应用权重的总和。

输入

在2到3个输入之间。

  • 输入 (异构) - T:

    输入形状为 (N, C) 或 (N, C, d1, d2, …, dk) 的张量。

  • target (异构) - Tind:

    目标张量的形状为 (N) 或 (N, d1, d2, …, dk)。目标元素值应在 [0, C) 范围内。如果指定了 ignore_index,则其值可能超出 [0, C) 范围,目标值应要么在 [0, C) 范围内,要么具有 ignore_index 的值。

  • weight(可选,异构)- T

    可选的重新缩放权重张量。如果提供,它必须是一个大小为C的张量。否则,它将被视为具有所有元素为1的张量。

输出

  • loss(异质的) - T:

    负对数似然损失

类型约束

  • T 在 ( tensor(bfloat16), tensor(double), tensor(float), tensor(float16) ):

    将输入、权重和输出类型限制为浮点张量。

  • Tind 在 ( tensor(int32), tensor(int64) ) 中:

    将目标限制为整数类型

负对数似然损失 - 13

版本

此版本的运算符自版本13起可用。

摘要

NegativeLogLikelihoodLoss 运算符计算(加权)负对数似然损失。 其“输入”张量的形状为 (N, C, d1, d2, …, dk),其中 k >= 0。 “输入”张量包含输入[n, :, d_1, d_2,…, d_k] 属于 [0, C) 类别的对数概率。 运算符的“目标”输入张量的形状为 (N, d1, d2, …, dk)。它编码了类别标签(C 个类别之一) 或者它可能包含一个特殊值(由属性 ignore_index 指示)用于 N x d1 x d2 x … x dk 样本。 输入[n, :, d_1, d_2,…d_k] 被分类为类别 c = target[n][d_1][d_2]…[d_k] 的损失值计算如下:

loss[n][d_1][d_2]...[d_k] = -input[n][c][d_1][d_2]...[d_k].

当提供了一个可选的“weight”时,样本损失的计算方式为:

loss[n][d_1][d_2]...[d_k] = -input[n][c][d_1][d_2]...[d_k] * weight[c].

当目标值等于ignore_index时,损失为零。

loss[n][d_1][d_2]...[d_k] = 0, when target[n][d_1][d_2]...[d_k] = ignore_index

如果“reduction”属性设置为“none”,操作符的输出将是上述形状为(N, d1, d2, …, dk)的损失。 如果“reduction”属性设置为“mean”(默认属性值),输出损失是(权重)平均的:

mean(loss), if "weight" is not provided,

或者如果提供了权重,

sum(loss) / sum(weight[target[n][d_1][d_2]...[d_k]]]), for all samples.

如果“reduction”属性设置为“sum”,则输出是一个标量:sum(loss)

另请参阅 https://pytorch.org/docs/stable/nn.html#torch.nn.NLLLoss.

示例 1:

// negative log likelihood loss, "none" reduction
N, C, d1 = 2, 3, 2
input = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],
          [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]
target = [[2, 1], [0, 2]]

loss = np.zeros((N, d1))
for n in range(N):
    for d_1 in range(d1):
        c = target[n][d_1]
        loss[n][d_1] = -input[n][c][d_1]

// print(loss)
// [[-3. -2.]
//  [-0. -2.]]

示例 2:

// weighted negative log likelihood loss, sum reduction
N, C, d1 = 2, 3, 2
input = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],
        [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]
target = [[2, 1], [0, 2]]
weight = [0.2, 0.3, 0.1]
loss = np.zeros((N, d1))
for n in range(N):
    for d_1 in range(d1):
        c = target[n][d_1]
        loss[n][d_1] = -input[n][c][d_1] * weight[c]

loss = np.sum(loss)
// print(loss)
// -1.1

示例 3:

// weighted negative log likelihood loss, mean reduction
N, C, d1 = 2, 3, 2
input = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],
        [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]
target = [[2, 1], [0, 2]]
weight = [0.2, 0.3, 0.1]
loss = np.zeros((N, d1))
weight_total = 0
for n in range(N):
    for d_1 in range(d1):
        c = target[n][d_1]
        loss[n][d_1] = -input[n][c][d_1] * weight[c]
        weight_total = weight_total + weight[c]

loss = np.sum(loss) / weight_total
// print(loss)
// -1.57

属性

  • ignore_index - INT :

    指定一个被忽略且不贡献于输入梯度的目标值。这是一个可选值。

  • reduction - STRING (默认值为 'mean'):

    应用于损失的类型:无、求和、平均(默认)。‘无’:输出是每个样本的损失。‘求和’:输出将被求和。‘平均’:输出的总和将除以应用权重的总和。

输入

在2到3个输入之间。

  • 输入 (异构) - T:

    输入形状为 (N, C) 或 (N, C, d1, d2, …, dk) 的张量。

  • target (异构) - Tind:

    目标张量的形状为 (N) 或 (N, d1, d2, …, dk)。目标元素值应在 [0, C) 范围内。如果指定了 ignore_index,则其值可能超出 [0, C) 范围,目标值应位于 [0, C) 范围内或具有 ignore_index 值。

  • weight(可选,异构)- T

    可选的重新缩放权重张量。如果提供,它必须是一个大小为C的张量。否则,它将被视为具有所有元素为1的张量。

输出

  • loss(异质的) - T:

    负对数似然损失

类型约束

  • T 在 ( tensor(double), tensor(float), tensor(float16) ) 中:

    将输入、权重和输出类型限制为浮点张量。

  • Tind 在 ( tensor(int32), tensor(int64) ) 中:

    将目标限制为整数类型

负对数似然损失 - 12

版本

此版本的运算符自版本12起可用。

总结

NegativeLogLikelihoodLoss 运算符计算(加权)负对数似然损失。 其“输入”张量的形状为 (N, C, d1, d2, …, dk),其中 k >= 0。 “输入”张量包含输入[n, :, d_1, d_2,…, d_k] 属于 [0, C) 类别的对数概率。 运算符的“目标”输入张量的形状为 (N, d1, d2, …, dk)。它编码了类别标签(C 个类别之一) 或者它可能包含一个特殊值(由属性 ignore_index 指示)用于 N x d1 x d2 x … x dk 样本。 输入[n, :, d_1, d_2,…d_k] 被分类为类别 c = target[n][d_1][d_2]…[d_k] 的损失值计算为: loss[n][d_1][d_2]…[d_k] = -input[n][c][d_1][d_2]…[d_k]。 当提供了可选的“权重”时,样本损失计算为: loss[n][d_1][d_2]…[d_k] = -input[n][c][d_1][d_2]…[d_k] * weight[c]。 当目标值等于 ignore_index 时,损失为零。

loss[n][d_1][d_2]...[d_k] = 0, when target[n][d_1][d_2]...[d_k] = ignore_index

如果“reduction”属性设置为“none”,操作符的输出将是上述形状为(N, d1, d2, …, dk)的损失。 如果“reduction”属性设置为“mean”(默认属性值),输出损失是(权重)平均的: mean(loss),如果没有提供“weight”, 或者如果提供了权重, sum(loss) / sum(weight[target[n][d_1][d_2]…[d_k]]]),对于所有样本。 如果“reduction”属性设置为“sum”,输出是一个标量: sum(loss)。 另见https://pytorch.org/docs/stable/nn.html#torch.nn.NLLLoss。 示例1: // 负对数似然损失,“none” reduction N, C, d1 = 2, 3, 2 input = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]], [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]] target = [[2, 1], [0, 2]] loss = np.zeros((N, d1)) for n in range(N): for d_1 in range(d1): c = target[n][d_1] loss[n][d_1] = -input[n][c][d_1] // print(loss) // [[-3. -2.] // [-0. -2.]] 示例2: // 加权负对数似然损失,sum reduction N, C, d1 = 2, 3, 2 input = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]], [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]] target = [[2, 1], [0, 2]] weight = [0.2, 0.3, 0.1] loss = np.zeros((N, d1)) for n in range(N): for d_1 in range(d1): c = target[n][d_1] loss[n][d_1] = -input[n][c][d_1] * weight[c] loss = np.sum(loss) // print(loss) // -1.1 示例3: // 加权负对数似然损失,mean reduction N, C, d1 = 2, 3, 2 input = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]], [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]] target = [[2, 1], [0, 2]] weight = [0.2, 0.3, 0.1] loss = np.zeros((N, d1)) weight_total = 0 for n in range(N): for d_1 in range(d1): c = target[n][d_1] loss[n][d_1] = -input[n][c][d_1] * weight[c] weight_total = weight_total + weight[c] loss = np.sum(loss) / weight_total // print(loss) // -1.57

属性

  • ignore_index - INT :

    指定一个被忽略且不贡献于输入梯度的目标值。这是一个可选值。

  • reduction - STRING (默认值为 'mean'):

    应用于损失的类型:无、求和、平均(默认)。‘无’:输出是每个样本的损失。‘求和’:输出将被求和。‘平均’:输出的总和将除以应用权重的总和。

输入

在2到3个输入之间。

  • 输入 (异构) - T:

    输入形状为 (N, C) 或 (N, C, d1, d2, …, dk) 的张量。

  • target (异构) - Tind:

    目标张量的形状为 (N) 或 (N, d1, d2, …, dk)。目标元素值应在 [0, C) 范围内。如果指定了 ignore_index,则其值可能超出 [0, C) 范围,目标值应要么在 [0, C) 范围内,要么具有 ignore_index 的值。

  • weight(可选,异构)- T

    可选的重新缩放权重张量。如果提供,它必须是一个大小为C的张量。否则,它将被视为具有所有元素为1的张量。

输出

  • loss(异质的) - T:

    负对数似然损失

类型约束

  • T 在 ( tensor(double), tensor(float), tensor(float16) ) 中:

    将输入、权重和输出类型限制为浮点张量。

  • Tind 在 ( tensor(int32), tensor(int64) ) 中:

    将目标限制为整数类型