NegativeLogLikelihoodLoss

NegativeLogLikelihoodLoss - 22

版本

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

概述

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

当 target-value 等于 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,

或如果提供了 weight,

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

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

另请参阅 https://pytorch.ac.cn/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'

    应用于损失的归约类型:none, sum, mean(默认)。‘none’:输出是每个样本的损失。‘sum’:输出将被求和。‘mean’:输出的总和将被应用于的权重的总和除以。

输入

输入在 2 到 3 个之间。

  • input(异构)- 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。否则,将被视为所有元素都为一。

输出

  • loss(异构)- T

    负对数似然损失

类型约束

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

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

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

    将 target 约束为整数类型

NegativeLogLikelihoodLoss - 13

版本

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

概述

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

当 target-value 等于 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,

或如果提供了 weight,

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

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

另请参阅 https://pytorch.ac.cn/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'

    应用于损失的归约类型:none, sum, mean(默认)。‘none’:输出是每个样本的损失。‘sum’:输出将被求和。‘mean’:输出的总和将被应用于的权重的总和除以。

输入

输入在 2 到 3 个之间。

  • input(异构)- 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。否则,将被视为所有元素都为一。

输出

  • loss(异构)- T

    负对数似然损失

类型约束

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

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

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

    将 target 约束为整数类型

NegativeLogLikelihoodLoss - 12

版本

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

概述

NegativeLogLikelihoodLoss 运算符计算(加权)负对数似然损失。其“input”张量的形状为 (N, C, d1, d2, …, dk),其中 k >= 0。“input”张量包含输入 input[n, :, d_1, d_2,…, d_k] 属于 [0, C) 类的对数概率。运算符的“target”输入张量的形状为 (N, d1, d2, …, dk)。它编码类别标签(C 个类别中的一个)或可能包含一个特殊值(由属性 ignore_index 指示),用于 N x d1 x d2 x … x dk 个样本。输入 input[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]。当 target-value 等于 ignore_index 时,损失为零。如果“reduction”属性设置为“none”,运算符的输出将是上述形状为 (N, d1, d2, …, dk) 的损失。如果“reduction”属性设置为“mean”(默认属性值),输出损失将按(权重)平均:mean(loss),如果未提供“weight”,或如果提供了 weight,则为所有样本的 sum(loss) / sum(weight[target[n][d_1][d_2]…[d_k]]])。如果“reduction”属性设置为“sum”,输出为一个标量:sum(loss)。另请参阅 https://pytorch.ac.cn/docs/stable/nn.html#torch.nn.NLLLoss。示例 1:// 负对数似然损失,“none”归约 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 归约 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 归约 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

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”(默认属性值),输出损失将被(权重)平均:如果未提供“weight”,则为 mean(loss);如果提供了weight,则对所有样本为 sum(loss) / sum(weight[target[n][d_1][d_2]…[d_k]]])。如果“reduction”属性设置为“sum”,输出将是一个标量:sum(loss)。另请参见 https://pytorch.ac.cn/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'

    应用于损失的归约类型:none, sum, mean(默认)。‘none’:输出是每个样本的损失。‘sum’:输出将被求和。‘mean’:输出的总和将被应用于的权重的总和除以。

输入

输入在 2 到 3 个之间。

  • input(异构)- 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。否则,将被视为所有元素都为一。

输出

  • loss(异构)- T

    负对数似然损失

类型约束

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

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

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

    将 target 约束为整数类型