标注偏置问题(label bias)

条件随机场(CRF)作为一种概率模型,在处理序列数据的分割和标注任务中展现出优势,尤其是在克服标注偏置问题方面。标注偏置是指在训练过程中某些状态转移概率较高而导致模型在预测时偏好这些转移路径的问题。CRF通过采用无向图结构和丰富的特征集来避免这一问题。
部署运行你感兴趣的模型镜像
CRF 相比于 maximum entropy Markov models一个很重要的特点就是避免了标注偏置问题。
什么是标注偏置问题?
在Conditional Random fields: Probabilistic Models for Segmenting and Labeling Sequence Data (http://www.cis.upenn.edu/~pereira/papers/crf.pdf)这篇论文中,有解释。

[img]http://dl.iteye.com/upload/attachment/433002/24f91c85-4590-3ae2-95a8-990deb776b40.jpg[/img]


我对这个问题的理解是:
有三个状态a,b,c,
在训练语料中,a转移b的概率,大于a转移到c的概率,造成在进行测试时,始终只能出现a到b状态。


[img]http://dl.iteye.com/upload/attachment/433026/ab3e291f-64bc-3612-8b9a-7160ba714f03.jpg[/img]

CRF 能够采用丰富的特征,其无向图的结构,能够避免这个问题。

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

报错信息 `RuntimeError: size mismatch for f3.weight: copying a param with shape torch.Size([10, 4096]) from checkpoint, the shape in current model is torch.Size([2, 4096])` 和 `size mismatch for f3.bias: copying a param with shape torch.Size([10]) from checkpoint, the shape in current model is torch.Size([2])` 表明预训练模型的 `f3` 层的权重和偏置形状与当前模型不匹配。可以通过部分加载权重的方式来解决这个问题,即只加载形状匹配的参数。 以下是修改后的代码,标注了更改的地方: ```python import torch import torch.utils.data as Data from torchvision import transforms from torchvision.datasets import FashionMNIST from model import AlexNet from torchvision.datasets import ImageFolder from PIL import Image def test_data_process(): # 定义数据集的路径 ROOT_TRAIN = r'../datas/catdog/test' normalize = transforms.Normalize([0.162, 0.151, 0.138], [0.058, 0.052, 0.048]) # 定义数据集处理方法变量 test_transform = transforms.Compose([transforms.Resize((224, 224)), transforms.ToTensor(), normalize]) # 加载数据集 test_data = ImageFolder(ROOT_TRAIN, transform=test_transform) test_dataloader = Data.DataLoader(dataset=test_data, batch_size=1, shuffle=True, num_workers=4) return test_dataloader def test_model_process(model, test_dataloader): # 设定测试所用到的设备,有GPU用GPU没有GPU用CPU device = "mps" if torch.cuda.is_available() else 'cpu' # 讲模型放入到训练设备中 model = model.to(device) # 初始化参数 test_corrects = 0.0 test_num = 0 # 只进行前向传播计算,不计算梯度,从而节省内存,加快运行速度 with torch.no_grad(): for test_data_x, test_data_y in test_dataloader: # 将特征放入到测试设备中 test_data_x = test_data_x.to(device) # 将标签放入到测试设备中 test_data_y = test_data_y.to(device) # 设置模型为评估模式 model.eval() # 前向传播过程,输入为测试数据集,输出为对每个样本的预测值 output= model(test_data_x) # 查找每一行中最大值对应的行标 pre_lab = torch.argmax(output, dim=1) # 如果预测正确,则准确度test_corrects加1 test_corrects += torch.sum(pre_lab == test_data_y.data) # 将所有的测试样本进行累加 test_num += test_data_x.size(0) # 计算测试准确率 test_acc = test_corrects.float().item() / test_num print("测试的准确率为:", test_acc) if __name__ == "__main__": # 加载模型 model = AlexNet() # 修改部分:部分加载权重 state_dict = torch.load('best_model.pth', map_location=torch.device('mps')) model_dict = model.state_dict() # 筛选出形状匹配的权重参数 pretrained_dict = {k: v for k, v in state_dict.items() if k in model_dict and v.shape == model_dict[k].shape} # 更新当前模型的权重 model_dict.update(pretrained_dict) model.load_state_dict(model_dict) # 利用现有的模型进行模型的测试 test_dataloader = test_data_process() test_model_process(model, test_dataloader) # 设定测试所用到的设备,有GPU用GPU没有GPU用CPU device = "mps" if torch.cuda.is_available() else 'cpu' model = model.to(device) classes = ['猫', '狗'] with torch.no_grad(): for b_x, b_y in test_dataloader: b_x = b_x.to(device) b_y = b_y.to(device) # 设置模型为验证模型 model.eval() output = model(b_x) pre_lab = torch.argmax(output, dim=1) result = pre_lab.item() label = b_y.item() print("预测值:", classes[result], "------", "真实值:", classes[label]) image = Image.open('1.jfif') normalize = transforms.Normalize([0.162, 0.151, 0.138], [0.058, 0.052, 0.048]) # 定义数据集处理方法变量 test_transform = transforms.Compose([transforms.Resize((224, 224)), transforms.ToTensor(), normalize]) image = test_transform(image) # 添加批次维度 image = image.unsqueeze(0) with torch.no_grad(): model.eval() image = image.to(device) output = model(image) pre_lab = torch.argmax(output, dim=1) result = pre_lab.item() print("预测值:", classes[result]) ``` ### 更改说明 在 `if __name__ == "__main__":` 部分,将原来直接加载权重的代码: ```python # model.load_state_dict(torch.load('best_model.pth')) model.load_state_dict( torch.load('best_model.pth', map_location=torch.device('mps'), # ← 关键:映射到 MPS weights_only=True) # ← 顺手去警告 ) ``` 修改为部分加载权重的代码: ```python state_dict = torch.load('best_model.pth', map_location=torch.device('mps')) model_dict = model.state_dict() # 筛选出形状匹配的权重参数 pretrained_dict = {k: v for k, v in state_dict.items() if k in model_dict and v.shape == model_dict[k].shape} # 更新当前模型的权重 model_dict.update(pretrained_dict) model.load_state_dict(model_dict) ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值