@dataclass class ModelArguments: """ Arguments pertaining to which…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
@dataclass
class ModelArguments:
"""
Arguments pertaining to which model/config/tokenizer we are going to fine-tune from.
"""
model_name_or_path: str = field(
metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}
)
config_name: Optional[str] = field(
default=None, metadata={"help": "Pretrained config name or path if not the same as model_name"}
)
processor_name: Optional[str] = field(
default=None, metadata={"help": "Pretrained processor name or path if not the same as model_name"}
)
cache_dir: Optional[str] = field(
default=None,
metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"},
)
freeze_backbone: bool = field(default=True, metadata={"help": "Whether to freeze the backbone of the model."})
hidden_dim: int = field(default=256, metadata={"help": "The hidden dimension of the classification head."})
@dataclass
class DataTrainingArguments:
"""
Arguments pertaining to what data we are going to input our model for training and eval.
"""
data_dir: str = field(metadata={"help": "The root directory of the dataset."})
train_csv: str = field(default="train_split.csv", metadata={"help": "The name of the training csv file."})
val_csv: str = field(default="val_split.csv", metadata={"help": "The name of the validation csv file."})
num_labels: int = field(default=3, metadata={"help": "The number of labels for classification."})
parser = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments))
但是却报错
Argument of type "tuple[type[ModelArguments], type[DataTrainingArguments], type[TrainingArguments]]" cannot be assigned to parameter "dataclass_types" of type "DataClassType | Iterable[DataClassType] | None" in function "init"
Type "tuple[type[ModelArguments], type[DataTrainingArguments], type[TrainingArguments]]" is not assignable to type "DataClassType | Iterable[DataClassType] | None"
"tuple[type[ModelArguments], type[DataTrainingArguments], type[TrainingArguments]]" is not assignable to "DataClassType"
"tuple[type[ModelArguments], type[DataTrainingArguments], type[TrainingArguments]]" is not assignable to "Iterable[DataClassType]"
Type parameter "_T_co@Iterable" is covariant, but "type[ModelArguments] | type[DataTrainingArguments] | type[TrainingArguments]" is not a subtype of "DataClassType"
Type "type[ModelArguments] | type[DataTrainingArguments] | type[TrainingArguments]" is not assignable to type "DataClassType"
Type "type[DataTrainingArguments]" is not assignable to type "DataClassType"
"tuple[type[ModelArguments], type[DataTrainingArguments], type[TrainingArguments]]" is not assignable to "None"
非常奇怪,我确认import完全正常。
这个错误看起来是类型检查器(可能是 Pyright 或 mypy)在抱怨类型不匹配。问题在于 HfArgumentParser 期望接收的是数据类(dataclass)类型,但类型检查器认为你的类不是有效的数据类。
这里有几个可能的解决方案:
from dataclasses import dataclass, field
from typing import Optional
from transformers import HfArgumentParser, TrainingArguments
TrainingArguments 是从正确的地方导入的:from transformers import TrainingArguments
from typing import cast
parser = HfArgumentParser((
cast(type, ModelArguments),
cast(type, DataTrainingArguments),
cast(type, TrainingArguments)
))
parser = HfArgumentParser([ModelArguments, DataTrainingArguments, TrainingArguments])
parser = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)) # type: ignore
最可能的原因是你的 IDE 或类型检查器对 TrainingArguments 的类型推断有问题。如果代码实际运行没有问题,只是 IDE 报错,那么使用类型忽略注释是最简单的解决方案。