当前位置: 首页 > news >正文

NLP-transformer学习:(6)dataset 加载与调用

NLP-transformer学习:(6)dataset 加载与调用

在这里插入图片描述

平常其实也经常进行trainning等等,但是觉得还是觉得要补补基础,所以静下心,搞搞基础联系
本章节基于 NLP-transformer学习:(5)讲解了如何做一个简单的训练和模型迁移,这里实践一个长用的dataset


文章目录

  • NLP-transformer学习:(6)dataset 加载与调用
    • @[TOC](文章目录)
  • 1 什么是datasets
  • 2 datasets 实战
    • 2.1 基础操作
  • 2.2 加载某一任务或某一部分
  • 2.3 数据划分
    • 2.4 数据选取和过滤
    • 2.4 数据映射
    • 2.5 数据保存与加载

提示:以下是本篇文章正文内容,下面案例可供参考

1 什么是datasets

地址:https://huggingface.co/datasets
在这里插入图片描述

datasets言而简之就是加载数据集用的
使用之前需要:
pip install datasets
有些特殊的库需要
pip install datasets[vision]
pip install datasets[audio]

2 datasets 实战

2.1 基础操作

加载代码如下:

# if the py name is datasets, the import action will first use the current file 
# not the datasets installed by pip
# for example you may meet the error: will be "NameError: name 'load_dataset' is not defined"from datasets import *if __name__ == "__main__":# add a datasetdata_set = load_dataset("madao33/new-title-chinese")print(data_set)print("------------------------------")print("train[0]:")print(data_set["train"][0])print("------------------------------")print("train[:2]:")print(data_set["train"][:2])print("------------------------------")print("train[\"tile\"][:5]:")print(data_set["train"]["title"][:5])print("------------------------------")

这里注意的是,使用的python 文件名不能是“datasets”即重名,不然会首先找当前文件,然后报错:
NameError: name ‘load_dataset’ is not defined
当改为非datasets 名字后就可以看到数据加载

可以看到这个数据集中只有训练和验证数据集。
在这里插入图片描述
然后我们使用一些切片用法可以看到期望结果:
在这里插入图片描述

2.2 加载某一任务或某一部分

(1)加载某个任务
datasets 部分数据中不是只有数据还包含了很多任务
对于super_gule,这个datasets 是一个 任务的集合,如果我们要添加某一任务
在这里插入图片描述
我们可以这样做,代码如下:

# if the py name is datasets, the import action will first use the current file 
# not the datasets installed by pip
# for example you may meet the error: will be "NameError: name 'load_dataset' is not defined"from datasets import *if __name__ == "__main__":# add specific taskboolq_dataset = load_dataset("super_glue", "boolq",trust_remote_code=True)print(boolq_dataset)

在这里插入图片描述

注意这里有个小细节,如果写成自动化代码时,可以加加上信任主机,这样就不用再敲入一个y
在这里插入图片描述
(2)加载某个部分(也叫某个划分)
load_dataset 支持加载某个部分,并且对某个部分进行切片,且切片还可以用%描述,但不能用小数描述

# if the py name is datasets, the import action will first use the current file 
# not the datasets installed by pip
# for example you may meet the error: will be "NameError: name 'load_dataset' is not defined"from datasets import *if __name__ == "__main__":## add a dataset#data_set = load_dataset("madao33/new-title-chinese")#print(data_set)## add specific task#boolq_dataset = load_dataset("super_glue", "boolq",trust_remote_code=True)#print(boolq_dataset)dataset = load_dataset("madao33/new-title-chinese", split="train")print("train:") print(dataset)dataset = load_dataset("madao33/new-title-chinese", split="train[10:100]")print("train 10:100:") print(dataset)dataset = load_dataset("madao33/new-title-chinese", split="train[10%:50%]")print("train 10%:100%:") print(dataset)dataset = load_dataset("madao33/new-title-chinese", split=["train[:40%]", "train[40%:]"])print("train 40% and 60%:") print(dataset)

运行结果:
在这里插入图片描述

2.3 数据划分

这个dataset 自带了个调整比例的 函数:train_test_split

# if the py name is datasets, the import action will first use the current file 
# not the datasets installed by pip
# for example you may meet the error: will be "NameError: name 'load_dataset' is not defined"from datasets import *if __name__ == "__main__":datasets = load_dataset("madao33/new-title-chinese")print("origin train datasets:")print(datasets["train"])print("-----------------")print("make train set as test 0.1:")dataset = datasets["train"]print(dataset.train_test_split(test_size=0.1))print("-----------------")print("stratify:")boolq_dataset = load_dataset("super_glue", "boolq",trust_remote_code=True)dataset = boolq_dataset["train"]print(dataset.train_test_split(test_size=0.1, stratify_by_column="label"))# 分类数据集可以按照比例划分print("-----------------")

运行结果:
这里 test_size = 0.1 指,将训练数据的 0.1 用作test,即585 = 5850 × 0.1
stratify: 这样可以均衡数据
在这里插入图片描述

2.4 数据选取和过滤


from datasets import *if __name__ == "__main__":datasets = load_dataset("madao33/new-title-chinese")# 选取filter_res = datasets["train"].select([0, 1])print("select:")print(filter_res["title"][:5])# 过滤filter_dataset = datasets["train"].filter(lambda example: "中国" in example["title"])print("filter:")print(filter_dataset["title"][:5])

结果:
在这里插入图片描述

2.4 数据映射

数据映射,就是我们写一个函数,然后对数据集中的每个数据都做这样的处理
(1)将个每个数据处理下,这里举例家了前缀
代码:

from datasets import load_datasetdef add_prefix(example):example["title"] = 'Prefix: ' + example["title"]return exampleif __name__ == "__main__":datasets = load_dataset("madao33/new-title-chinese")prefix_dataset = datasets.map(add_prefix)print(prefix_dataset["train"][:10]["title"])

运行结果:
可以看到和期望一样,将每个title 加了个”prefix“
在这里插入图片描述
(2)将每个数据做tokenizer

from datasets import *
from transformers import AutoTokenizertokenizer = AutoTokenizer.from_pretrained("bert-base-chinese")
def preprocess_function(example, tokenizer = tokenizer):model_inputs = tokenizer(example["content"], max_length = 512, truncation = True)labels = tokenizer(example["title"], max_length=32, truncation=True)# label就是title编码的结果model_inputs["labels"] = labels["input_ids"]return model_inputsif __name__ == "__main__":processed_datasets = datasets.map(preprocess_function)print("train:")print(processed_datasets["train"][:5])print("validation:")print(processed_datasets["validation"][:5])

结果可以看到,数据已经和前几章讲的类似,变成了token。
运行结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

2.5 数据保存与加载

from datasets import *
from transformers import AutoTokenizerif __name__ == "__main__":datasets = load_dataset("madao33/new-title-chinese")processed_datasets = datasets.map(preprocess_function)print("from web:") print(processed_datasets["validation"][:2])processed_datasets = datasets.map(preprocess_function)processed_datasets.save_to_disk("./processed_data")processed_datasets = load_from_disk("./processed_data")print("from local:") print(processed_datasets["validation"][:2])

结果:
在这里插入图片描述
在这里插入图片描述

http://www.lryc.cn/news/433333.html

相关文章:

  • 数据库系统 第43节 数据库复制
  • LabVIEW FIFO详解
  • 如何验证VMWare WorkStation的安装?
  • 论文阅读:AutoDIR Automatic All-in-One Image Restoration with Latent Diffusion
  • C++ | Leetcode C++题解之第392题判断子序列
  • 操作系统概述(三、虚拟化)
  • 基于ARM芯片与OpenCV的工业分拣机器人项目设计与实现流程详解
  • UNITY UI简易反向遮罩
  • 牛客周赛59(A,B,C,D,E二维循环移位,F范德蒙德卷积)
  • C语言中的隐型计算
  • ffmpeg面向对象-待定
  • 大厂嵌入式数字信号处理器(DSP)面试题及参考答案
  • GC-分代收集器
  • C++从入门到起飞之——priority_queue(优先级队列) 全方位剖析!
  • [数据集][目标检测]西红柿缺陷检测数据集VOC+YOLO格式17318张3类别
  • 【小沐学OpenGL】Ubuntu环境下glut的安装和使用
  • ROS 发行版 jazzy 加载urdf 渲染到 RVIZ2
  • SpringBoot中利用EasyExcel+aop实现一个通用Excel导出功能
  • 排序链表(归并排序)
  • Adobe After Effects的插件--------CC Particle World
  • 电脑硬盘数据丢失了怎么恢复?简单实用的硬盘数据找回的方法
  • k8s调度(pod亲和、反亲和、污点、容忍度)
  • 智能制造核心领域:自动化、物联网、大数据分析、人工智能在现代制造业中的应用与融合
  • Android Studio 2024最新版Hello World
  • 请解释Java中的CountDownLatch和CyclicBarrier的区别和使用场景。什么是Java中的Semaphore?它如何控制并发访问?
  • Django+Vue3前后端分离学习(五)(前端登录页面搭建)
  • 虚拟机安装macos系统
  • AI基础 L9 Local Search II 局部搜索
  • 828华为云征文|使用sysbench对Mysql应用加速测评
  • 2024 年高教社杯全国大学生数学建模竞赛题目——D 题 反潜航空深弹命中概率问题的求解