根据这个文章 回答问题最大池化损失 我们建议使用基于最大池的损失函数来训练LSTM进行关键字识别。…
- User
根据这个文章 回答问题最大池化损失 我们建议使用基于最大池的损失函数来训练LSTM进行关键字识别。 鉴于LSTM有能力建模长上下文信息,我们假设不需要教LSTM触发关键字段中的每一帧。一种简单的方法是仅从最后一帧或最后几帧反向传播损耗,以更新权重。 但我们的初步实验表明,LSTM并没有从这个方案中学到太多东西。因此,我们采用了一个基于最大池的损失函数,让LSTM选择信息量最大的关键字帧来自学。最大池损失可以看作是在关键字识别模型训练中从帧级损失到段级损失的过渡。可供选择的段级损失函数包括,例如几何平均值等。 LSTM被添加到CNN层的顶部,其中具有池化的CNN层用于提取特征作为LSTM输入,LSTM的输出用于预测。 对于T输入帧X = (x1, . . . , xT ),输入序列X的最大池损可计算为 如果内部有P个关键字实例,我们使用lp表示一个连续的帧索引范围,其帧与第p个关键字对齐。因此,对于lp中的所有帧,K维目标向量zp 是相同的。 设L = (l1, . . . , lP )表示X中所有P关键字实例的帧索引范围的集合,而表示未与任何关键字对齐的剩余帧的所有索引的集合(即背景帧索引)。我们使用kp†表示lp内帧的目标标签,lp†表示lp内特定的一帧,其kp†的后验值为最大值。 第一项说明,我们计算未与任何关键字对齐的输入帧的交叉熵损失。第二项显示了我们如何为关键字对齐的帧进行最大池化。更详细地说,对于第p段(索引范围lp)的帧,它们与关键字kp†对齐。我们只对单个帧(索引lp†)进行反向传播,该帧的目标kp†的后验值在当前段lp中的所有帧中最大,并丢弃当前段中的所有其他帧。 最大池化损失的概念如图3所示,其中填充帧与关键字对齐,空帧用于背景。给定一个帧的输入序列,在每个关键字段内,只保留对应关键字目标具有最大后验值的帧,而丢弃同一关键字段内的所有其他帧。并保留了所有的背景帧。 这是他的pytorch 版本的实现def max_pooling_loss(logits: torch.Tensor, target: torch.Tensor, lengths: torch.Tensor, min_duration: int = 0): ''' Max-pooling loss For keyword, select the frame with the highest posterior. The keyword is triggered when any of the frames is triggered. For none keyword, select the hardest frame, namely the frame with lowest filler posterior(highest keyword posterior). the keyword is not triggered when all frames are not triggered.
Attributes: logits: (B, T, D), D is the number of keywords target: (B) lengths: (B) min_duration: min duration of the keyword Returns: (float): loss of current batch (float): accuracy of current batch ''' mask = padding_mask(lengths) num_utts = logits.size(0) num_keywords = logits.size(2) target = target.cpu() loss = 0.0 for i in range(num_utts): for j in range(num_keywords): # Add entropy loss CE = -(t * log(p) + (1 - t) * log(1 - p)) if target[i] == j: # For the keyword, do max-polling prob = logits[i, :, j] m = mask[i].clone().detach() m[:min_duration] = True prob = prob.masked_fill(m, 0.0) prob = torch.clamp(prob, 1e-8, 1.0) max_prob = prob.max() loss += -torch.log(max_prob) else: # For other keywords or filler, do min-polling prob = 1 - logits[i, :, j] prob = prob.masked_fill(mask[i], 1.0) prob = torch.clamp(prob, 1e-8, 1.0) min_prob = prob.min() loss += -torch.log(min_prob) loss = loss / num_utts # Compute accuracy of current batch mask = mask.unsqueeze(-1) logits = logits.masked_fill(mask, 0.0) max_logits, index = logits.max(1) num_correct = 0 for i in range(num_utts): max_p, idx = max_logits[i].max(0) # Predict correct as the i'th keyword if max_p > 0.5 and idx == target[i]: num_correct += 1 # Predict correct as the filler, filler id < 0 if max_p < 0.5 and target[i] < 0: num_correct += 1 acc = num_correct / num_utts # acc = 0.0 return loss, acc 写根据你的理解写出tensorflow2.x 版本 - Assistant
以下是根据理解写的TensorFlow 2.x版本的实现:
- User
predict = tf.random.uniform(shape=(32, 120, 2)) label = tf.random.uniform(shape=(32, 2)) print(f"predict:{predict.shape}") print(f"label:{label.shape}") loss = max_pooling_loss(predict, label, label) print(loss) 报错:ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
- Assistant
- User
我的标签是 二分类 0,1 我可以使用softmax 将他变成概率分布然后使用这个loss 吗?
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.