对批处理中的每个数据点进行操作损失

时间:2021-07-29 04:43:24

标签: pytorch

当使用 64 大小的批次时,我需要对每个数据点的损失值进行精细操作。 我知道我可以在创建损失函数对象时使用 reduction='none' 然后我可以获得细粒度的损失值。但最好是不设置reduction='none'的常规损失对象,以保持与其他代码的一致性。

有没有办法在没有 reduction='none' 的情况下操作更精细的损失值?

1 个答案:

答案 0 :(得分:1)

为什么不使用预定义的选项包装函数?

[ 3, 6, 4, 4, 5, 5 ]

其中 const arr1 = [3, 5, 6, 4, 5, 4]; let str = ``; //Step 1 const step1 = arr1.reduce((a, c) => (c in a ? a[c].push(c) : a[c] = [c], a), {}); str += `Step 1: ${JSON.stringify(step1)}\n\n`; //Step 2 const step2 = Object.values(step1); str += `Step 2: ${JSON.stringify(step2)}\n\n`; //Step 3 const step3 = step2.sort((a, b) => a.length - b.length); str += `Step 3: ${JSON.stringify(step3)}\n\n`; //Step 4 const step4 = step3.reduce((a, c) => a.concat(c)); str += `Step 4: ${JSON.stringify(step4)}\n\n`; //All in one: const allInOne = Object.values(arr1.reduce((a, c) => (c in a ? a[c].push(c) : a[c] = [c], a), {})) .sort((a, b) => a.length - b.length) .reduce((a, c) => a.concat(c)); str += `****************************\n\n`; str += `All In One: ${JSON.stringify(allInOne)}\n\n`; const pre = document.createElement('pre') pre.innerText = str; document.querySelector('body').appendChild(pre); 是内置的 PyTorch 损失,例如 def custom_loss(*args, **kwargs): return some_builtin(*args, **kwargs, reduction='none') some_builtin、...