影响6个时序Baselines模型的代码Bug

前言

我是从去年年底开始入门时间序列研究,但直到最近我读FITS这篇文章的代码时,才发现从去年12月25号就有人发现了数个时间序列Baseline的代码Bug。如果你已经知道这个Bug了,那可以忽略本文~

这个错误最初在Informer(AAAI 2021 最佳论文)中被发现,是爱丁堡大学的Luke Nicholas Darlow发现。这个问题对时间序列预测领域的一系列广泛研究都有影响,这个Bug影响了包括Patch TST、DLinear、Informer、Autoformer、Fedformer、FiLM在内的经典baseline。

  • PatchTST (ICLR 2023) - Link to affected code

  • DLinear (AAAI 2022 reported version) - Link to affected code

  • Informer (AAAI 2021 Best Paper) - Link to affected code

  • Autoformer (NIPS 2021 reported version) - Link to affected code

  • Fedformer (ICML 2022) - Link to affected code

  • FiLM (ICLR 2023) - Link to affected code

FITS这篇文章发布一个修复方法,以帮助社区在他们的工作中解决这个问题。参考链接:https://anonymous.4open.science/r/FITS/README.md

错误描述

这个错误源于数据加载器中的错误实现。测试数据加载器(test dataloader)使用了drop_last=True那么模型的评估可能会基于不完整的测试数据集,从而导致对模型性能的不准确评估,甚至可能导致不同模型之间比较的不公平。这个问题在使用较大批量大小时尤为明显,因为更大的批量大小更容易导致数据集大小不能被整除的情况。

注:在PyTorch等数据加载框架中,drop_last参数通常用于控制当数据集大小不能被批量大小整除时,是否丢弃最后一个不完整的批量。在训练过程中,为了保持每个epoch迭代次数的稳定性,通常会设置drop_last=True。然而,在测试或验证过程中,为了获得对模型性能的准确评估,应该确保所有测试数据都被使用,因此应该设置drop_last=False

解决方法

在data_factory.py 中,修改代码:

if flag == 'test':    shuffle_flag = False    drop_last = True    batch_size = args.batch_size    freq = args.freq

如下:

if flag == 'test':    shuffle_flag = False    drop_last = False #True    batch_size = args.batch_size    freq = args.freq

在代码 script 文件夹(e.g., ./exp/exp_main.py), 做出如下修改 (约在 290行),from​​​​​​​

preds = np.array(preds)trues = np.array(trues)inputx = np.array(inputx) # some times there is not this line, it does not matter

to:​​​​​​​

preds = np.concatenate(preds, axis=0)trues = np.concatenate(trues, axis=0)inputx = np.concatenate(inputx, axis=0) # if there is not that line, ignore this

作者说可以通过在维度0(即batch大小)上拼接(concatenate)剩余的数据解决问题,而不必丢弃最后一个不完整的batch。

结果更新

图片

已发现的错误主要影响像ETTh1和ETTh2这样的小型数据集的结果。有趣的是,对于其他数据集,如ETTm1上的PatchTST等某些模型,却表现出了增强的性能。FITS(假设是指某个时间序列预测模型)仍然保持了足够好且与其他最先进模型相媲美的性能。

从更新后的结果我们发现,最能打还是Patch TST以及FITS。关于这两篇论文,我之前做过详细的解读,感兴趣可以关注阅读。


大家一定要关注我的公众号【科学最top】,第一时间follow时序高水平论文解读!!!

好的,我可以提供一些帮助。情感分析是使用自然语言处理技术来识别文本中的情绪或情感。以下是构建情感分析Transformer模型的Python代码: 首先,您需要安装必要的库,比如PyTorch,transformers和tokenizers。可以通过pip命令进行安装: ``` pip install torch pip install transformers pip install tokenizers ``` 接下来,您可以使用Hugging Face提供的 Transformers 库来加载一个预训练的Transformer模型,例如BERT模型代码如下: ```python from transformers import BertTokenizer, BertForSequenceClassification # 加载BERT模型和tokenizer tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2) ``` 接下来,您可以使用训练数据来Fine-tune模型。在这里,我们使用GLUE数据集的情感分类任务。数据集可以在以下链接中获得:https://github.com/nyu-mll/GLUE-baselines ```python import torch from transformers import AdamW from transformers import glue_convert_examples_to_features from transformers.data.processors.glue import * from transformers import get_linear_schedule_with_warmup # 加载训练数据 processor = Sst2Processor() train_examples = processor.get_train_examples('/path/to/glue') # 转换训练示例为特征 train_features = glue_convert_examples_to_features(train_examples, tokenizer, max_length=128, task='sst-2') # 训练数据转换为pytorch张量 all_input_ids = torch.tensor([f.input_ids for f in train_features], dtype=torch.long) all_attention_mask = torch.tensor([f.attention_mask for f in train_features], dtype=torch.long) all_token_type_ids = torch.tensor([f.token_type_ids for f in train_features], dtype=torch.long) all_labels = torch.tensor([f.label for f in train_features], dtype=torch.long) # 定义优化器和学习率调度器 optimizer = AdamW(model.parameters(), lr=5e-5, eps=1e-8) total_steps = len(train_examples) * 10 scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=0, num_training_steps=total_steps) # 开始Fine-tune model.train() for epoch in range(10): for step in range(0, len(train_examples), batch_size): batch_input_ids = all_input_ids[step:step+batch_size] batch_input_mask = all_attention_mask[step:step+batch_size] batch_token_type_ids = all_token_type_ids[step:step+batch_size] batch_labels = all_labels[step:step+batch_size] optimizer.zero_grad() outputs = model(input_ids=batch_input_ids, attention_mask=batch_input_mask, token_type_ids=batch_token_type_ids, labels=batch_labels) loss = outputs[0] loss.backward() torch.nn.utils.clip_grad_norm_(model.parameters(), max_grad_norm) optimizer.step() scheduler.step() ``` 最后,您可以使用Fine-tuned模型进行情感分类预测。例如: ```python # 加载测试数据 test_examples = processor.get_test_examples('/path/to/glue') # 转换测试数据为特征 test_features = glue_convert_examples_to_features(test_examples, tokenizer, max_length=128, task='sst-2') # 测试数据转换为pytorch张量 test_input_ids = torch.tensor([f.input_ids for f in test_features], dtype=torch.long) test_attention_mask = torch.tensor([f.attention_mask for f in test_features], dtype=torch.long) test_token_type_ids = torch.tensor([f.token_type_ids for f in test_features], dtype=torch.long) # 预测测试数据 model.eval() with torch.no_grad(): test_outputs = model(input_ids=test_input_ids, attention_mask=test_attention_mask, token_type_ids=test_token_type_ids) test_logits = test_outputs[0].detach().cpu().numpy() test_preds = np.argmax(test_logits, axis=1) for i, example in enumerate(test_examples): print('Input Text: ', example.text_a) print('Predicted Label: ', test_preds[i], ('Positive' if test_preds[i] == 1 else 'Negative')) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值