-
Notifications
You must be signed in to change notification settings - Fork 34.1k
[Examples] Added support for test-file in QA examples with no trainer #11510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
6304af4
11a487d
30097e7
36745f1
599a87f
0f2f74a
1b0af5f
a27bd1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,6 +80,9 @@ def parse_args(): | |
| parser.add_argument( | ||
| "--validation_file", type=str, default=None, help="A csv or a json file containing the validation data." | ||
| ) | ||
| parser.add_argument( | ||
| "--test_file", type=str, default=None, help="A csv or a json file containing the Prediction data." | ||
| ) | ||
| parser.add_argument( | ||
| "--max_seq_length", | ||
| type=int, | ||
|
|
@@ -202,15 +205,23 @@ def parse_args(): | |
| args = parser.parse_args() | ||
|
|
||
| # Sanity checks | ||
| if args.dataset_name is None and args.train_file is None and args.validation_file is None: | ||
| raise ValueError("Need either a dataset name or a training/validation file.") | ||
| if ( | ||
| args.dataset_name is None | ||
| and args.train_file is None | ||
| and args.validation_file is None | ||
| and args.test_file is None | ||
| ): | ||
| raise ValueError("Need either a dataset name or a training/validation/test file.") | ||
| else: | ||
| if args.train_file is not None: | ||
| extension = args.train_file.split(".")[-1] | ||
| assert extension in ["csv", "json"], "`train_file` should be a csv or a json file." | ||
| if args.validation_file is not None: | ||
| extension = args.validation_file.split(".")[-1] | ||
| assert extension in ["csv", "json"], "`validation_file` should be a csv or a json file." | ||
| if args.test_file is not None: | ||
| extension = args.test_file.split(".")[-1] | ||
| assert extension in ["csv", "json"], "`test_file` should be a csv or a json file." | ||
|
|
||
| if args.output_dir is not None: | ||
| os.makedirs(args.output_dir, exist_ok=True) | ||
|
|
@@ -263,8 +274,10 @@ def main(): | |
| data_files["train"] = args.train_file | ||
| if args.validation_file is not None: | ||
| data_files["validation"] = args.validation_file | ||
| if args.test_file is not None: | ||
| data_files["test"] = args.test_file | ||
| extension = args.train_file.split(".")[-1] | ||
| raw_datasets = load_dataset(extension, data_files=data_files) | ||
| raw_datasets = load_dataset(extension, data_files=data_files, field="data") | ||
| # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at | ||
| # https://huggingface.co/docs/datasets/loading_datasets.html. | ||
|
|
||
|
|
@@ -535,11 +548,11 @@ def prepare_validation_features(examples): | |
| train_dataset, shuffle=True, collate_fn=data_collator, batch_size=args.per_device_train_batch_size | ||
| ) | ||
|
|
||
| eval_dataset.set_format(type="torch", columns=["attention_mask", "input_ids", "token_type_ids"]) | ||
| eval_dataset.set_format(type="torch", columns=["attention_mask", "input_ids"]) | ||
| eval_dataloader = DataLoader(eval_dataset, collate_fn=data_collator, batch_size=args.per_device_eval_batch_size) | ||
|
|
||
| if args.do_predict: | ||
| predict_dataset.set_format(type="torch", columns=["attention_mask", "input_ids", "token_type_ids"]) | ||
| predict_dataset.set_format(type="torch", columns=["attention_mask", "input_ids"]) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above here and you can remove the line below |
||
| predict_dataloader = DataLoader( | ||
| predict_dataset, collate_fn=data_collator, batch_size=args.per_device_eval_batch_size | ||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,10 +81,13 @@ def parse_args(): | |
| parser.add_argument( | ||
| "--preprocessing_num_workers", type=int, default=4, help="A csv or a json file containing the training data." | ||
| ) | ||
| parser.add_argument("--do_predict", action="store_true", help="Eval the question answering model") | ||
| parser.add_argument("--do_predict", action="store_true", help="To do prediction on the question answering model") | ||
| parser.add_argument( | ||
| "--validation_file", type=str, default=None, help="A csv or a json file containing the validation data." | ||
| ) | ||
| parser.add_argument( | ||
| "--test_file", type=str, default=None, help="A csv or a json file containing the Prediction data." | ||
| ) | ||
| parser.add_argument( | ||
| "--max_seq_length", | ||
| type=int, | ||
|
|
@@ -231,15 +234,23 @@ def parse_args(): | |
| args = parser.parse_args() | ||
|
|
||
| # Sanity checks | ||
| if args.dataset_name is None and args.train_file is None and args.validation_file is None: | ||
| raise ValueError("Need either a dataset name or a training/validation file.") | ||
| if ( | ||
| args.dataset_name is None | ||
| and args.train_file is None | ||
| and args.validation_file is None | ||
| and args.test_file is None | ||
| ): | ||
| raise ValueError("Need either a dataset name or a training/validation/test file.") | ||
| else: | ||
| if args.train_file is not None: | ||
| extension = args.train_file.split(".")[-1] | ||
| assert extension in ["csv", "json"], "`train_file` should be a csv or a json file." | ||
| if args.validation_file is not None: | ||
| extension = args.validation_file.split(".")[-1] | ||
| assert extension in ["csv", "json"], "`validation_file` should be a csv or a json file." | ||
| if args.test_file is not None: | ||
| extension = args.test_file.split(".")[-1] | ||
| assert extension in ["csv", "json"], "`test_file` should be a csv or a json file." | ||
|
|
||
| if args.output_dir is not None: | ||
| os.makedirs(args.output_dir, exist_ok=True) | ||
|
|
@@ -292,8 +303,10 @@ def main(): | |
| data_files["train"] = args.train_file | ||
| if args.validation_file is not None: | ||
| data_files["validation"] = args.validation_file | ||
| if args.test_file is not None: | ||
| data_files["test"] = args.test_file | ||
| extension = args.train_file.split(".")[-1] | ||
| raw_datasets = load_dataset(extension, data_files=data_files) | ||
| raw_datasets = load_dataset(extension, data_files=data_files, field="data") | ||
|
stas00 marked this conversation as resolved.
|
||
| # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at | ||
| # https://huggingface.co/docs/datasets/loading_datasets.html. | ||
|
|
||
|
|
@@ -540,11 +553,11 @@ def prepare_validation_features(examples): | |
| train_dataset, shuffle=True, collate_fn=data_collator, batch_size=args.per_device_train_batch_size | ||
| ) | ||
|
|
||
| eval_dataset.set_format(type="torch", columns=["attention_mask", "input_ids", "token_type_ids"]) | ||
| eval_dataset.set_format(type="torch", columns=["attention_mask", "input_ids"]) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as in the other script here. |
||
| eval_dataloader = DataLoader(eval_dataset, collate_fn=data_collator, batch_size=args.per_device_eval_batch_size) | ||
|
|
||
| if args.do_predict: | ||
| predict_dataset.set_format(type="torch", columns=["attention_mask", "input_ids", "token_type_ids"]) | ||
| predict_dataset.set_format(type="torch", columns=["attention_mask", "input_ids"]) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here too |
||
| predict_dataloader = DataLoader( | ||
| predict_dataset, collate_fn=data_collator, batch_size=args.per_device_eval_batch_size | ||
| ) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the token type IDs should definitely be there, there are the ones that tell the model which part of the input is inside the question and which is inside the context. There are also more columns to pass in this case (the
XlnetModelForQuestionAnsweringexpects more inputs that are prepared in this script:is_impossible,cls_indexandp_mask. They all should be listed here. Plus the labelsstart_logitsandend_logits.The goal of this line is to remove the columns named
"example_id"which is not accepted by the model, but the contributor forgot to list all relevant inputs. There is an easier way now that datasets has more functionality, I think you should do:and use that when building your DataLoader. Then you can remove the line way below that does
since
eval_datasetdid not change.