关于迁移Paddle脚本的一些心得

转载地址:https://bbs.huaweicloud.com/forum/thread-126066-1-1.html

作者:cqu-yyp

在参加了两期的昇腾众智计划以后,我对于迁移Paddle脚本也有了一些经验和心得。总的来说,我认为模型迁移主要分为四个部分:

1、数据处理

2、模型构造

3、训练优化过程

4、验证评估

对于数据处理来说,发现paddle实现的reader都会实现一个方法叫做data_generator ,这个方法会返回一个wrapper,是一个迭代器。我们可以遍历这个迭代器用filewriter生成mindrecord:

def make_dataset(wrapper=None, output_path=None, task_name=None, mode="train"):
    MINDRECORD_FILE_PATH = output_path + task_name+"/" + task_name + "_" + mode + ".mindrecord"
    if not os.path.exists(output_path + task_name):
        os.makedirs(output_path + task_name)
    if os.path.exists(MINDRECORD_FILE_PATH):
        os.remove(MINDRECORD_FILE_PATH)
        os.remove(MINDRECORD_FILE_PATH + ".db")
    writer = FileWriter(file_name=MINDRECORD_FILE_PATH, shard_num=1)
    datalist = []
    if task_name == 'Sem-L':
        nlp_schema = {
            InstanceName.SRC_IDS: {"type": "int32", "shape": [-1]},
            InstanceName.SENTENCE_IDS: {"type": "int32", "shape": [-1]},
            InstanceName.POS_IDS: {"type": "int32", "shape": [-1]},
            InstanceName.MASK_IDS: {"type": "int32", "shape": [-1]},
            InstanceName.LABEL: {"type": "int32", "shape": [-1]},
            InstanceName.RECORD_ID: {"type": "int32", "shape": [-1]},
        }
        writer.add_schema(nlp_schema, "proprocessed classification dataset")
        for i in wrapper():
            data = {
            InstanceName.SRC_IDS: i[0],
            InstanceName.SENTENCE_IDS: i[1],
            InstanceName.POS_IDS: i[2],
            InstanceName.MASK_IDS: i[4],
            InstanceName.LABEL: i[5],
            InstanceName.RECORD_ID: i[6]
        }
            datalist.append(data)
    if task_name == 'SST-2':
        nlp_schema = {
            InstanceName.RECORD_ID: {"type": "int32", "shape": [-1]},
            InstanceName.LABEL: {"type": "int32", "shape": [-1]},
            InstanceName.SRC_IDS: {"type": "int32", "shape": [-1]},
            InstanceName.SENTENCE_IDS: {"type": "int32", "shape": [-1]},
            InstanceName.POS_IDS: {"type": "int32", "shape": [-1]},
            InstanceName.MASK_IDS: {"type": "int32", "shape": [-1]}
        }
        writer.add_schema(nlp_schema, "proprocessed classification dataset")
        for i in wrapper():
            data = {
            InstanceName.RECORD_ID: i[0],   
            InstanceName.LABEL: i[1],                         
            InstanceName.SRC_IDS: i[2],
            InstanceName.SENTENCE_IDS: i[3],
            InstanceName.POS_IDS: i[4],
            InstanceName.MASK_IDS: i[5]
        }
            datalist.append(data)       
    
    
    writer.write_raw_data(datalist)
    writer.commit()

然后在使用的时候,传入mindrecord的地址到MindDataset,即可使用。 例如

ds = MindDataset(MINDRECORD_FILE_PATH, num_parallel_workers=8, shuffle=True)

对于模型来说,paddle的写法很灵活,他们会定义许多函数,在取模型结果的时候也很不一样。我们可以读懂他们模型是如何连接构造以后,直接全部在一个nn.cell中的construct方法中实现

对于训练过程,paddle一般是使用一个for循环去训练模型,在mindspore中,建议使用model.train()方法。

最后对于评估,我的做法比较简单,直接提取出模型中不含loss的部分,然后直接送入数据,得到结果,进行评估。

总的来说,用mindspore完整的实现一个模型,还是比较容易的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值