ModelScope问题之模型encoder配置报错如何解决

本文涉及的产品
交互式建模 PAI-DSW,5000CU*H 3个月
模型训练 PAI-DLC,5000CU*H 3个月
模型在线服务 PAI-EAS,A10/V100等 500元 1个月
简介: ModelScope模型报错是指在使用ModelScope平台进行模型训练或部署时遇到的错误和问题;本合集将收集ModelScope模型报错的常见情况和排查方法,帮助用户快速定位问题并采取有效措施。

问题一:命名实体识别任务——模型encoder配置报错


在进行命名实体识别模型训练时,我想修改模型的结构。我观察到当前modelscope模型以bert-crf为主,我想加入编码层,查阅文档后未能正确配置encoder。       我的问题是: 1. 如何正确配置编码层? 是否像嵌入层一样,设置一个model_name_or_path的路径 2.如何正确引用huggingface的模型? 如上面图3和图4所示,我加入了一个huggingface的模型,但在训练时未能识别出encoder


参考回答:

配置编码层 在使用编码层时,需要确保已经正确安装了 transformers 库,并且已经下载了需要使用的预训练模型。编码层的配置方式与嵌入层稍有不同。您需要在代码中手动创建一个 AutoModel 实例,并将其作为编码层传递给模型。例如,如果您想使用 bert-base-uncased 模型作为编码层,可以按照以下方式进行配置: python Copy from transformers import AutoModel

encoder_layer = AutoModel.from_pretrained('bert-base-uncased') 其中,AutoModel 类是 transformers 库中的一个工厂函数,用于根据模型名称自动加载对应的模型。from_pretrained 方法用于从预训练模型中加载权重。

引用 Hugging Face 的模型 如果您想使用 Hugging Face 的预训练模型,可以使用 transformers 库中的 AutoModel 类或其他相关类。例如,您可以按照以下方式使用 Hugging Face 的 bert-base-uncased 模型: python Copy from transformers import AutoModel

bert_model = AutoModel.from_pretrained('bert-base-uncased') 在训练或推理过程中,您需要将输入数据传递给 bert_model,然后获取模型的输出进行后续处理。需要注意的是,如果您的模型需要进行微调或其他修改,您需要将 bert_model 的参数设置为可训练,并在训练过程中更新其权重。


关于本问题的更多回答可点击原文查看:https://developer.aliyun.com/ask/521689?spm=a2c6h.14164896.0.0.6338508ePQwQ9m


问题二:使用StructBERT FAQ问答 模型进行训练时报错,各位大佬进来看看,帮忙解决解决,十分感谢

问题描述

使用问答模型 damo/nlp_structbert_faq-question-answering_chinese-base 进行训练时,参照官方文档代码出现如下错误

** build_dataset error log: 'structbert is not in the custom_datasets registry group faq-question-answering. Please make sure the correct version of ModelScope library is used.'

官方地址

模型地址

使用的数据集格式 如下

text label answer
数据采集 1 数据采集
采集数据 1 数据采集
数据收集 1 数据采集
开始采集 1 数据采集
采集开始 1 数据采集
收集数据 1 数据采集
数据收集 1 数据采集
问题反馈 2 问题反馈
反馈问题 2 问题反馈
上报问题 2 问题反馈
问题上报 2 问题反馈
开始反馈 2 问题反馈
反馈开始 2 问题反馈
汇报工作 3 工作汇报
工作汇报 3 工作汇报
工作上报 3 工作汇报
上报工作 3 工作汇报
开始汇报 3 工作汇报
汇报开始 3 工作汇报
工作填报 4 工作填报
填报工作 4 工作填报
开始填报 4 工作填报
工作填报 4 工作填报

调试完整代码

import os
from modelscope.metainfo import Trainers
from modelscope.msdatasets import MsDataset
from modelscope.pipelines import pipeline
from modelscope.trainers import build_trainer
from modelscope.utils.config import Config
from modelscope.utils.hub import read_config
  
train_dataset = MsDataset.load("./qa.csv", split='train').remap_columns({'text': 'text'})
print(train_dataset)
eval_dataset = train_dataset
cfg: Config = read_config("damo/nlp_structbert_faq-question-answering_chinese-base")
cfg.train.train_iters_per_epoch = 30
cfg.evaluation.val_iters_per_epoch = 2
cfg.train.seed = 1234
cfg.train.optimizer.lr = 2e-5
cfg.train.hooks = [{
    'type': 'CheckpointHook',
    'by_epoch': False,
    'interval': 50
}, {
    'type': 'EvaluationHook',
    'by_epoch': False,
    'interval': 50
}, {
    'type': 'TextLoggerHook',
    'by_epoch': False,
    'rounding_digits': 5,
    'interval': 10
}]
cfg_file = os.path.join("./model/temp", 'config.json')
cfg.dump(cfg_file)
trainer = build_trainer(
    Trainers.faq_question_answering_trainer,
    default_args=dict(
        model="damo/nlp_structbert_faq-question-answering_chinese-base",
        work_dir="./model/temp",
        train_dataset=train_dataset,
        eval_dataset=eval_dataset,
        cfg_file=cfg_file))
trainer.train()
evaluate_result = trainer.evaluate()
print(evaluate_result)

完整错误信息

Dataset({
    features: ['text', 'label', 'answer'],
    num_rows: 26
})
2023-06-26 20:32:21,659 - modelscope - INFO - initialize model from ./model/damo/nlp_structbert_faq-question-answering_chinese-base
2023-06-26 20:32:22,949 - modelscope - INFO - faq task build protonet network
2023-06-26 20:32:28,135 - modelscope - INFO - All model checkpoint weights were used when initializing SbertForFaqQuestionAnswering.
2023-06-26 20:32:28,136 - modelscope - INFO - All the weights of SbertForFaqQuestionAnswering were initialized from the model checkpoint If your task is similar to the task the model of the checkpoint was trained on, you can already use SbertForFaqQuestionAnswering for predictions without further training.
2023-06-26 20:32:28,137 - modelscope - WARNING - No train key and type key found in preprocessor domain of configuration.json file.
2023-06-26 20:32:28,138 - modelscope - WARNING - Cannot find available config to build preprocessor at mode train, current config: {'max_seq_length': 50, 'model_dir': './model/damo/nlp_structbert_faq-question-answering_chinese-base'}. trying to build by task and model information.
2023-06-26 20:32:28,172 - modelscope - WARNING - No val key and type key found in preprocessor domain of configuration.json file.
2023-06-26 20:32:28,172 - modelscope - WARNING - Cannot find available config to build preprocessor at mode eval, current config: {'max_seq_length': 50, 'model_dir': './model/damo/nlp_structbert_faq-question-answering_chinese-base'}. trying to build by task and model information.
2023-06-26 20:32:28,185 - modelscope - WARNING - ('CUSTOM_DATASETS', 'faq-question-answering', 'structbert') not found in ast index file
2023-06-26 20:32:28,186 - modelscope - WARNING - ('CUSTOM_DATASETS', 'faq-question-answering', 'structbert') not found in ast index file
2023-06-26 20:32:28,187 - modelscope - INFO - cuda is not available, using cpu instead.
2023-06-26 20:32:28,187 - modelscope - INFO - ==========================Training Config Start==========================
2023-06-26 20:32:28,187 - modelscope - INFO - {
    "framework": "pytorch",
    "task": "faq-question-answering",
    "pipeline": {
        "type": "faq-question-answering"
    },
    "model": {
        "type": "structbert",
        "pooling": "avg",
        "metric": "relation"
    },
    "preprocessor": {
        "max_seq_length": 50,
        "model_dir": "./model/damo/nlp_structbert_faq-question-answering_chinese-base"
    },
    "train": {
        "seed": 1234,
        "hooks": [
            {
                "type": "IterTimerHook"
            }
        ],
        "train_iters_per_epoch": 30,
        "max_epochs": 1,
        "sampler": {
            "n_way": 5,
            "k_shot": 5,
            "r_query": 5,
            "min_labels": 2
        },
        "optimizer": {
            "type": "Adam",
            "lr": 2e-05,
            "options": {
                "grad_clip": {
                    "max_norm": 5.0
                }
            }
        },
        "lr_scheduler": {
            "type": "LinearLR",
            "options": {
                "by_epoch": false
            }
        },
        "dataloader": {
            "workers_per_gpu": 1
        },
        "checkpoint": {
            "period": {
                "by_epoch": false,
                "interval": 50
            }
        },
        "logging": {
            "by_epoch": false,
            "rounding_digits": 5,
            "interval": 10
        },
        "work_dir": "./model/temp"
    },
    "evaluation": {
        "metrics": "seq-cls-metric",
        "val_iters_per_epoch": 2,
        "dataloader": {
            "workers_per_gpu": 1
        },
        "period": {
            "by_epoch": false,
            "interval": 50
        }
    }
}
2023-06-26 20:32:28,188 - modelscope - INFO - ===========================Training Config End===========================
2023-06-26 20:32:28,190 - modelscope - INFO - num. of bad sample ids:5/26
2023-06-26 20:32:28,192 - modelscope - INFO - train: label size:3.0, data size:18,                 domain_size:1
2023-06-26 20:32:28,193 - modelscope - WARNING - ('OPTIMIZER', 'default', 'Adam') not found in ast index file
2023-06-26 20:32:28,194 - modelscope - WARNING - ('LR_SCHEDULER', 'default', 'LinearLR') not found in ast index file
2023-06-26 20:32:28,194 - modelscope - INFO - Stage: before_run:
    (ABOVE_NORMAL) OptimizerHook                      
    (LOW         ) LrSchedulerHook                    
    (LOW         ) CheckpointHook                     
    (VERY_LOW    ) TextLoggerHook                     
 -------------------- 
Stage: before_train_epoch:
    (LOW         ) LrSchedulerHook                    
 -------------------- 
Stage: before_train_iter:
    (ABOVE_NORMAL) OptimizerHook                      
 -------------------- 
Stage: after_train_iter:
    (ABOVE_NORMAL) OptimizerHook                      
    (NORMAL      ) EvaluationHook                     
    (LOW         ) LrSchedulerHook                    
    (LOW         ) CheckpointHook                     
    (VERY_LOW    ) TextLoggerHook                     
 -------------------- 
Stage: after_train_epoch:
    (NORMAL      ) EvaluationHook                     
    (LOW         ) LrSchedulerHook                    
    (LOW         ) CheckpointHook                     
    (VERY_LOW    ) TextLoggerHook                     
 -------------------- 
Stage: after_val_epoch:
    (VERY_LOW    ) TextLoggerHook                     
 -------------------- 
Stage: after_run:
    (LOW         ) CheckpointHook                     
 -------------------- 
2023-06-26 20:32:28,197 - modelscope - INFO - Checkpoints will be saved to ./model/temp
2023-06-26 20:32:28,197 - modelscope - INFO - Text logs will be saved to ./model/temp
** build_dataset error log: 'structbert is not in the custom_datasets registry group faq-question-answering. Please make sure the correct version of ModelScope library is used.'
** build_dataset error log: 'structbert is not in the custom_datasets registry group faq-question-answering. Please make sure the correct version of ModelScope library is used.'


参考回答:

可以使用以下命令进行更新:

python

Copy

!pip install -U modelscope

如果更新后仍然存在问题,可以尝试手动注册自定义数据集。在代码中添加以下代码:

python

Copy

from modelscope.datasets import custom_datasets

custom_datasets.register("faq-question-answering", "structbert")


关于本问题的更多回答可点击原文查看:https://developer.aliyun.com/ask/524664?spm=a2c6h.14164896.0.0.6338508ePQwQ9m


问题三:如何将space-t-cn的模型导出为onnx格式


请问如何将space-t-cn的模型导出为onnx格式,我参考了modelscope的文档教程导出时,会报错table_question_answering没有eval()


参考回答:

您可以按照以下步骤将 Space-T-CN 模型导出为 ONNX 格式:

  1. 首先,您需要安装 onnxtorch 库。可以使用以下命令进行安装:
!pip install onnx torch
  1. 然后,您需要下载 Space-T-CN 的预训练模型。可以从阿里云自然语言处理官网上下载。
  2. 接下来,您需要使用 torch.onnx.export() 函数将模型导出为 ONNX 格式。以下是一个示例代码:
import torch
import torchvision.models as models
import onnx
from onnx import helper, TensorProto
# 加载预训练模型
model = models.resnet50(pretrained=True)
model.eval()
# 将模型设置为评估模式
model.eval()
# 定义输入张量的大小和类型
x = torch.randn(1, 3, 224, 224) # 假设输入张量的形状为 (batch_size, channels, height, width)
x = x.to('cuda') # 将输入张量移动到 GPU 上
x = x.detach().cpu() # 将输入张量从 GPU 上移除并转换为 CPU 张量
# 导出模型为 ONNX 格式
onnx_filename = "space_t_cn.onnx" # 定义输出文件名
input_name = "input" # 定义输入节点名称
output_name = "output" # 定义输出节点名称
dynamic_axes = {'input': {0: 'batch_size'}, 'output': {0: 'batch_size'}} # 定义输入和输出张量的维度信息
torch.onnx.export(model, x, onnx_filename, input_names=[input_name], output_names=[output_name], dynamic_axes=dynamic_axes)
print("Model exported to", onnx_filename) # 打印导出结果

在上述代码中,我们首先加载了预训练的 ResNet-50 模型,并将其设置为评估模式。然后,我们定义了一个随机生成的输入张量 x,并将其移动到 GPU 上。最后,我们使用 torch.onnx.export() 函数将模型导出为 ONNX 格式,并指定了输出文件名、输入节点名称、输出节点名称以及输入和输出张量的维度信息。


关于本问题的更多回答可点击原文查看:https://developer.aliyun.com/ask/517055?spm=a2c6h.14164896.0.0.6338508ePQwQ9m


问题四:ModelScope装语音模型报错了,是什么原因呢?


ModelScope装语音模型报错了,是什么原因呢?


参考回答:

可以先尝试在网站的notebook的预安装环境先体验下语音模型


关于本问题的更多回答可点击原文查看:https://developer.aliyun.com/ask/485455?spm=a2c6h.14164896.0.0.6338508ePQwQ9m


问题五:在NoteBook中训练个性化语音合成模型报错


按照模型介绍中的Notebook最佳实践,训练个性化语音合成,在执行到基于PTTS-basemodel微调这一步时,训练代码报错,错误如下:

/opt/conda/lib/python3.7/site-packages/modelscope/models/audio/tts/voice.py in train_sambert(self, work_dir, stage_dir, data_dir, config_path, ignore_pretrain, hparams) 473 config['Model']['KanTtsSAMBERT']['params'].update(ling_unit_size) 474 model, optimizer, scheduler = model_builder(config, self.device, --> 475 self.local_rank, 476 self.distributed) 477

AttributeError: 'Voice' object has no attribute 'local_rank'

以上报错信息的代码定位,是在执行 trainer = build_trainer(Trainers.speech_kantts_trainer, default_args=kwargs)

trainer.train()

前面的所有步骤,均按照模型介绍中执行成功。


参考回答:

检查您的模型文件和代码是否正确上传到了 EAS 服务上,并且路径设置正确。您可以通过在 EAS 服务上执行 ls 命令来查看文件是否存在,并检查路径是否正确。

检查您在 EAS 服务上安装的 Python 环境和依赖项是否与您在本地运行代码时使用的环境和依赖项相同。如果存在不同,可能会导致模型无法加载或运行。您可以在 EAS 服务上执行 pip list 命令来查看已安装的 Python 包和版本,并与您在本地运行代码时的环境进行比较。

检查您在 EAS 服务上设置的模型加载代码是否正确。您可以在 EAS 服务上尝试手动加载模型,并检查是否存在加载模型时的错误。

检查您在 EAS 服务上设置的端口号是否正确,并且在本地代码中进行了正确的配置,以便可以与模型进行通信。


关于本问题的更多回答可点击原文查看:https://developer.aliyun.com/ask/495007?spm=a2c6h.14164896.0.0.6338508ePQwQ9m


相关实践学习
部署Stable Diffusion玩转AI绘画(GPU云服务器)
本实验通过在ECS上从零开始部署Stable Diffusion来进行AI绘画创作,开启AIGC盲盒。
相关文章
|
2月前
|
人工智能 开发框架 物联网
赢万元奖金 | 第七届CCF开源创新大赛ModelScope开源模型应用挑战赛开启报名!
第七届CCF开源创新大赛(后简称“大赛”) 由中国计算机学会(CCF)主办,长沙理工大学、CCF开源发展委员会联合承办。
|
4月前
|
人工智能 开发工具 Swift
ModelScope联手OpenDataLab:直接调用7000+开源数据集,赋能AI模型加速研发
魔搭社区和OpenDatalab浦数合作,共同开启一场模型与数据的深度融合,旨在为中国开发者打造更加高效、开放的AI体验。
|
5月前
|
机器学习/深度学习 人工智能 Swift
PAI x ModelScope: 在PAI使用ModelScope模型
在当前的人工智能领域,特别是大语言模型、文生图等领域,基于预训练模型完成机器学习模型的开发部署已成为重要的应用范式,开发者们依赖于这些先进的开源预训练模型,以简化机器学习应用的开发并加速创新。
|
5月前
|
机器学习/深度学习 测试技术 TensorFlow
ModelScope模型使用与EAS部署调用
本文以魔搭数据的模型为例,演示在DSW实例中如何快速调用模型,然后通过Python SDK将模型部署到阿里云PAI EAS服务,并演示使用EAS SDK实现对服务的快速调用,重点针对官方关于EAS模型上线后示例代码无法正常调通部分进行了补充。
191 2
|
5月前
|
自然语言处理
在ModelScope中,你可以通过设置模型的参数来控制输出的阈值
在ModelScope中,你可以通过设置模型的参数来控制输出的阈值
120 1
|
5月前
|
API 语音技术
ModelScope-FunASR**有支持热词又支持时间戳的模型**。
【2月更文挑战第30天】ModelScope-FunASR**有支持热词又支持时间戳的模型**。
186 2
|
5月前
|
人工智能 API 决策智能
Modelscope结合α-UMi:基于Modelscope的多模型协作Agent
基于单个开源小模型的工具调用Agent,由于模型容量和预训练能力获取的限制,无法在推理和规划、工具调用、回复生成等任务上同时获得比肩大模型等性能。
|
5月前
|
文字识别 并行计算 语音技术
ModelScope问题之下载模型文件报错如何解决
ModelScope模型报错是指在使用ModelScope平台进行模型训练或部署时遇到的错误和问题;本合集将收集ModelScope模型报错的常见情况和排查方法,帮助用户快速定位问题并采取有效措施。
701 3
|
5月前
|
人工智能 达摩院 自然语言处理
超好用的开源模型平台,ModelScope阿里达摩院
超好用的开源模型平台,ModelScope阿里达摩院
352 1
|
5月前
|
API
使用ModelScope平台进行模型验证时
【2月更文挑战第9天】使用ModelScope平台进行模型验证时
138 4
下一篇
无影云桌面