根据Label中的内容来计算lable 的高度,使它自适应


> 此文件其实就像swift中扩展。在OC中创建一个文件类型为:Category的文件。

#import <UIKit/UIKit.h>

@interface UILabel (StringFrame)
- (CGSize)boundingRectWithSize:(CGSize)size;
@end

#import "UILabel+StringFrame.h"

@implementation UILabel (StringFrame)
- (CGSize)boundingRectWithSize:(CGSize)size
{
    NSDictionary *attribute = @{NSFontAttributeName: self.font};

    CGSize retSize = [self.text boundingRectWithSize:size
                                             options:\
                      NSStringDrawingTruncatesLastVisibleLine |
                      NSStringDrawingUsesLineFragmentOrigin |
                      NSStringDrawingUsesFontLeading
                                          attributes:attribute
                                             context:nil].size;

    return retSize;
}
@end
将Soft Teacher的单一固定阈值改为自适应阈值(如ACT)需要进行以下修改: 1. 首先,需要定义一个新的Soft Teacher类,以包含自适应阈值的计算方法。 ```python class ACTSoftTeacher(nn.Module): def __init__(self, teacher_model, temperature, act_freq): super(ACTSoftTeacher, self).__init__() self.teacher_model = teacher_model self.temperature = temperature self.act_freq = act_freq self.total_steps = 0 self.threshold = 0.5 # 初始阈值设为0.5 def calc_threshold(self, step): self.total_steps += 1 if self.total_steps % self.act_freq == 0: # 计算最近act_freq个步骤软标签和硬标签的KL散度 kl_div = 0.0 for i in range(self.act_freq): # 计算软标签 teacher_outputs = self.teacher_model(input_ids, attention_mask) teacher_logits = teacher_outputs[0] / self.temperature teacher_probs = F.softmax(teacher_logits, dim=-1) # 计算硬标签 student_outputs = self.student_model(input_ids, attention_mask) student_logits = student_outputs[0] / self.temperature student_probs = F.softmax(student_logits, dim=-1) kl_div += F.kl_div(teacher_probs.log(), student_probs, reduction='batchmean') # 将KL散度作为新的阈值 self.threshold = kl_div.item() def forward(self, input_ids, attention_mask): with torch.no_grad(): teacher_outputs = self.teacher_model(input_ids, attention_mask) teacher_logits = teacher_outputs[0] / self.temperature teacher_probs = F.softmax(teacher_logits, dim=-1) student_outputs = self.student_model(input_ids, attention_mask) student_logits = student_outputs[0] / self.temperature student_probs = F.softmax(student_logits, dim=-1) # 计算软标签和硬标签之间的阈值 self.calc_threshold(step) soft_labels = self.threshold * teacher_probs + (1 - self.threshold) * student_probs hard_labels = student_probs outputs = (student_logits, soft_labels, hard_labels) + student_outputs[1:] return outputs ``` 2. 在使用Soft Teacher进行训练的代码,需要使用新的ACTSoftTeacher类并传入自适应阈值计算的超参数: ```python teacher = ACTSoftTeacher(teacher_model, temperature, act_freq) ``` 其,act_freq是控制阈值计算频率的超参数。 需要注意的是,自适应阈值的计算方法可以根据具体需求进行修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值