MCPcopy Create free account
hub / github.com/Tencent/NeuralNLP-NeuralClassifier / forward

Method forward

model/loss.py:119–142  ·  view source on GitHub ↗
(self, logits, target,
                use_hierar=False,
                is_multi=False,
                *argvs)

Source from the content-addressed store, hash-verified

117 loss_type, LossType.str()))
118
119 def forward(self, logits, target,
120 use_hierar=False,
121 is_multi=False,
122 *argvs):
123 device = logits.device
124 if use_hierar:
125 assert self.loss_type in [LossType.BCE_WITH_LOGITS,
126 LossType.SIGMOID_FOCAL_CROSS_ENTROPY]
127 if not is_multi:
128 target = torch.eye(self.label_size)[target].to(device)
129 hierar_penalty, hierar_paras, hierar_relations = argvs[0:3]
130 return self.criterion(logits, target) + \
131 hierar_penalty * self.cal_recursive_regularize(hierar_paras,
132 hierar_relations,
133 device)
134 else:
135 if is_multi:
136 assert self.loss_type in [LossType.BCE_WITH_LOGITS,
137 LossType.SIGMOID_FOCAL_CROSS_ENTROPY]
138 else:
139 if self.loss_type not in [LossType.SOFTMAX_CROSS_ENTROPY,
140 LossType.SOFTMAX_FOCAL_CROSS_ENTROPY]:
141 target = torch.eye(self.label_size)[target].to(device)
142 return self.criterion(logits, target)
143
144 def cal_recursive_regularize(self, paras, hierar_relations, device="cpu"):
145 """ Only support hierarchical text classification with BCELoss

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected