news 2026/7/7 1:25:22

解决ValueError: too many values to unpack (expected 2)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
解决ValueError: too many values to unpack (expected 2)

报错:ValueError: too many values to unpack (expected 2)

复现RGCL模型,遇到的问题,该代码里面报错原因是:collate_fn里的tokens多层嵌套列表(每个样本是分词后的 token 列表,batch 打包后变成[[token1,token2...], [xxx]]),但旧版 transformers(3.1.0)在is_pretokenized=True时内部解包逻辑不兼容,或者数据里混入了非法空样本、维度错乱。

解决方案

原 collate_fn:

def collate_fn(data): u, i, r, tokens = zip(*data) encoding = bert_tokenizer(tokens, return_tensors='pt', padding=True, truncation=True, is_pretokenized=True) return torch.Tensor(u), torch.Tensor(i), torch.Tensor(r), \ encoding['input_ids'], encoding['attention_mask']

改成下面版本,去掉is_pretokenized=True,改用字符串拼接 / 直接传原文本,彻底规避解包 bug:

def collate_fn(data): u, i, r, token_lists = zip(*data) # 把分词列表还原成字符串,不用is_pretokenized text_list = [" ".join(toks) for toks in token_lists] encoding = bert_tokenizer(text_list, return_tensors='pt', padding=True, truncation=True, max_length=args.review_max_length) return torch.Tensor(u), torch.Tensor(i), torch.Tensor(r), \ encoding['input_ids'], encoding['attention_mask']

infact,在修改代码之前,我还多次尝试改变transformers的版本但是还是报一样的错误,可能是没有改到正好可以用的那个版本。(ps.之前跑自己的模型也遇到过版本问题,修改transformers的版本就重新运行了)

完整报错:

(base) root@autodl-container-6d25459816-01b42b10:~/Work/ReviewGraph-main/BERT# python bert_whitening.py libgomp: Invalid value for environment variable OMP_NUM_THREADS 2026-07-06 00:23:43 - Load_Data - Start reading data to pandas. Clean string: 100%|███████████████████████████████████████████████████████████████████████| 963441/963441 [01:36<00:00, 9971.91it/s] Delete unused words: 100%|██████████████████████████████████████████████████████████████| 963441/963441 [00:08<00:00, 107299.97it/s] 2026-07-06 00:25:55 - Load_Data - Truncate review length to 56 words Delete unused words: 100%|██████████████████████████████████████████████████████████████| 963441/963441 [00:03<00:00, 255554.45it/s] check data split: 770753it [00:33, 23069.12it/s] /root/Work/ReviewGraph-main/BERT/../load_data.py:190: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead. train_data = train_data.append([valid_data.loc[valid_drop_user_data_index], pre tokenize: 100%|███████████████████████████████████████████████████████████████████████| 973763/973763 [09:04<00:00, 1789.32it/s] 0%| | 0/7608 [00:00<?, ?it/s] Traceback (most recent call last): File "bert_whitening.py", line 175, in <module> save_sentence_feat(args) File "/root/miniconda3/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context return func(*args, **kwargs) File "bert_whitening.py", line 149, in save_sentence_feat for u, i, r, input_ids, mask in tqdm(data_loader): File "/root/miniconda3/lib/python3.8/site-packages/tqdm/std.py", line 1185, in __iter__ for obj in iterable: File "/root/miniconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 530, in __next__ data = self._next_data() File "/root/miniconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 570, in _next_data data = self._dataset_fetcher.fetch(index) # may raise StopIteration File "/root/miniconda3/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 52, in fetch return self.collate_fn(data) File "bert_whitening.py", line 100, in collate_fn encoding = bert_tokenizer(tokens, return_tensors='pt', padding=True, File "/root/miniconda3/lib/python3.8/site-packages/transformers/tokenization_utils_base.py", line 3021, in __call__ encodings = self._call_one(text=text, text_pair=text_pair, **all_kwargs) File "/root/miniconda3/lib/python3.8/site-packages/transformers/tokenization_utils_base.py", line 3109, in _call_one return self.batch_encode_plus( File "/root/miniconda3/lib/python3.8/site-packages/transformers/tokenization_utils_base.py", line 3311, in batch_encode_plus return self._batch_encode_plus( File "/root/miniconda3/lib/python3.8/site-packages/transformers/tokenization_utils.py", line 886, in _batch_encode_plus ids, pair_ids = ids_or_pair_ids ValueError: too many values to unpack (expected 2)
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/7 1:23:56

毕设项目 深度学习火焰检测识别(源码+论文)

文章目录 0 前言1 项目运行效果1 基于YOLO的火焰检测与识别2 课题背景3 卷积神经网络3.1 卷积层3.2 池化层3.3 激活函数&#xff1a;3.4 全连接层3.5 使用tensorflow中keras模块实现卷积神经网络 4 YOLOV54.1 网络架构图4.2 输入端4.3 基准网络4.4 Neck网络4.5 Head输出层 5 数…

作者头像 李华
网站建设 2026/7/7 1:23:47

企业知识库实战:从 0 到 1 落地全记录

企业知识库实战&#xff1a;从 0 到 1 落地全记录最后一篇实战&#xff0c;把前面 9 篇的知识全部串起来。从需求分析、架构设计、文档处理、检索优化到上线运维&#xff0c;完整讲一遍企业知识库 RAG 系统是怎么从 0 到 1 落地的&#xff0c;踩过哪些坑、每一步优化了多少。大…

作者头像 李华
网站建设 2026/7/7 1:21:33

内外联动精细化运营:解锁安卓APP推广降本增效新路径

当下移动互联网流量红利逐步消退&#xff0c;安卓APP行业告别了粗放式上架流量时代。华为、小米、OPPO、vivo、应用宝等主流安卓应用商店格局固化&#xff0c;叠加各大平台信息流广告竞争白热化&#xff0c;获客成本持续攀升。在此背景下&#xff0c;精细化、低成本、高转化成为…

作者头像 李华
网站建设 2026/7/7 1:18:02

终极GPU显存健康检测:3分钟快速评估显卡稳定性的完整指南

终极GPU显存健康检测&#xff1a;3分钟快速评估显卡稳定性的完整指南 【免费下载链接】memtest_vulkan Vulkan compute tool for testing video memory stability 项目地址: https://gitcode.com/gh_mirrors/me/memtest_vulkan 你是否遇到过游戏突然崩溃、视频渲染失败或…

作者头像 李华
网站建设 2026/7/7 1:17:45

Git提交-完整标准流程

&#x1f539; 日常开发流程&#xff08;每天循环&#xff09;1. 上班第一件事&#xff1a;同步主干&#xff0c;保证基于最新代码开发git fetch origin main git merge origin/main2. 写代码&#xff0c;开发完成后提交git add . git commit -m "【20260402】基金规模优化…

作者头像 李华