





'''simple docstring'''






import os
from argparse import ArgumentParser, Namespace

from ..data import SingleSentenceClassificationProcessor as Processor
from ..pipelines import TextClassificationPipeline
from ..utils import is_tf_available, is_torch_available, logging
from . import BaseTransformersCLICommand


if not is_tf_available() and not is_torch_available():
	raise RuntimeError("At least one of PyTorch or TensorFlow 2.0+ should be installed to use CLI training")

# TF training parameters
__UpperCamelCase					    =	False
__UpperCamelCase					    =	False







def 					_a						(       _lowerCamelCase ) ->				str:



			"""simple docstring"""


			return TrainCommand(_lowerCamelCase )




class  _A (					__lowercase		):

		@staticmethod
		def 	lowercase__     (   __magic_name__					:   ArgumentParser			)					->    Tuple:


					"""simple docstring"""

					__snake_case					:      Tuple							      =     parser.add_parser("""train"""  ,      help="""CLI tool to train a model on a task."""			)

					train_parser.add_argument(
					    """--train_data"""  ,      type=__magic_name__  ,      required=__magic_name__  ,      help="""path to train (and optionally evaluation) dataset as a csv with tab separated labels and sentences."""  ,      )
					train_parser.add_argument(
					    """--column_label"""  ,      type=__magic_name__  ,      default=0  ,      help="""Column of the dataset csv file with example labels."""			)
					train_parser.add_argument(
					    """--column_text"""  ,      type=__magic_name__  ,      default=1  ,      help="""Column of the dataset csv file with example texts."""			)
					train_parser.add_argument(
					    """--column_id"""  ,      type=__magic_name__  ,      default=2  ,      help="""Column of the dataset csv file with example ids."""			)
					train_parser.add_argument(
					    """--skip_first_row"""  ,      action="""store_true"""  ,      help="""Skip the first row of the csv file (headers)."""			)

					train_parser.add_argument("""--validation_data"""  ,      type=__magic_name__  ,      default=""""""  ,      help="""path to validation dataset."""			)
					train_parser.add_argument(
					    """--validation_split"""  ,      type=__magic_name__  ,      default=0.1  ,      help="""if validation dataset is not provided, fraction of train dataset to use as validation dataset."""  ,      )

					train_parser.add_argument("""--output"""  ,      type=__magic_name__  ,      default="""./"""  ,      help="""path to saved the trained model."""			)

					train_parser.add_argument(
					    """--task"""  ,      type=__magic_name__  ,      default="""text_classification"""  ,      help="""Task to train the model on."""			)
					train_parser.add_argument(
					    """--model"""  ,      type=__magic_name__  ,      default="""bert-base-uncased"""  ,      help="""Model's name or path to stored model."""			)
					train_parser.add_argument("""--train_batch_size"""  ,      type=__magic_name__  ,      default=32  ,      help="""Batch size for training."""			)
					train_parser.add_argument("""--valid_batch_size"""  ,      type=__magic_name__  ,      default=64  ,      help="""Batch size for validation."""			)
					train_parser.add_argument("""--learning_rate"""  ,      type=__magic_name__  ,      default=3E-5  ,      help="""Learning rate."""			)
					train_parser.add_argument("""--adam_epsilon"""  ,      type=__magic_name__  ,      default=1E-08  ,      help="""Epsilon for Adam optimizer."""			)
					train_parser.set_defaults(func=__magic_name__			)

		def __init__(   self					:   Dict  ,      __magic_name__					:   Namespace			)					->    List[Any]:


					"""simple docstring"""

					__snake_case					:      List[str]							      =     logging.get_logger("""transformers-cli/training"""			)

					__snake_case					:      Dict							      =     """tf""" if is_tf_available() else """torch"""

					os.makedirs(args.output  ,      exist_ok=__magic_name__			)
					__snake_case					:      int							      =     args.output

					__snake_case					:      Optional[int]							      =     args.column_label
					__snake_case					:      List[str]							      =     args.column_text
					__snake_case					:      Union[str, Any]							      =     args.column_id

					self.logger.info(f'''Loading {args.task} pipeline for {args.model}'''			)
					if args.task == "text_classification":
								__snake_case					:      List[Any]							      =     TextClassificationPipeline.from_pretrained(args.model			)
					elif args.task == "token_classification":
								raise NotImplementedError
					elif args.task == "question_answering":
								raise NotImplementedError

					self.logger.info(f'''Loading dataset from {args.train_data}'''			)
					__snake_case					:      Optional[int]							      =     Processor.create_from_csv(
					    args.train_data  ,      column_label=args.column_label  ,      column_text=args.column_text  ,      column_id=args.column_id  ,      skip_first_row=args.skip_first_row  ,      )
					__snake_case					:      Optional[int]							      =     None
					if args.validation_data:
								self.logger.info(f'''Loading validation dataset from {args.validation_data}'''			)
								__snake_case					:      List[str]							      =     Processor.create_from_csv(
								    args.validation_data  ,      column_label=args.column_label  ,      column_text=args.column_text  ,      column_id=args.column_id  ,      skip_first_row=args.skip_first_row  ,      )

					__snake_case					:      int							      =     args.validation_split
					__snake_case					:      Tuple							      =     args.train_batch_size
					__snake_case					:      List[str]							      =     args.valid_batch_size
					__snake_case					:      int							      =     args.learning_rate
					__snake_case					:      Any							      =     args.adam_epsilon

		def 	lowercase__     (   self					:   Dict			)					->    Tuple:


					"""simple docstring"""

					if self.framework == "tf":
								return self.run_tf()
					return self.run_torch()

		def 	lowercase__     (   self					:   List[str]			)					->    Union[str, Any]:


					"""simple docstring"""

					raise NotImplementedError

		def 	lowercase__     (   self					:   str			)					->    str:


					"""simple docstring"""

					self.pipeline.fit(
					    self.train_dataset  ,      validation_data=self.valid_dataset  ,      validation_split=self.validation_split  ,      learning_rate=self.learning_rate  ,      adam_epsilon=self.adam_epsilon  ,      train_batch_size=self.train_batch_size  ,      valid_batch_size=self.valid_batch_size  ,      )

					# Save trained pipeline
					self.pipeline.save_pretrained(self.output			)









'''simple docstring'''






from sklearn.metrics import recall_score

import datasets


__UpperCamelCase					    =	"\nRecall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation:\nRecall = TP / (TP + FN)\nWhere TP is the true positives and FN is the false negatives.\n"


__UpperCamelCase					    =	"\nArgs:\n- **predictions** (`list` of `int`): The predicted labels.\n- **references** (`list` of `int`): The ground truth labels.\n- **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Defaults to None.\n- **pos_label** (`int`): The class label to use as the 'positive class' when calculating the recall. Defaults to `1`.\n- **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`.\n    - `'binary'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary.\n    - `'micro'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives.\n    - `'macro'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.\n    - `'weighted'`: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. Note that it can result in an F-score that is not between precision and recall.\n    - `'samples'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).\n- **sample_weight** (`list` of `float`): Sample weights Defaults to `None`.\n- **zero_division** (): Sets the value to return when there is a zero division. Defaults to .\n    - `'warn'`: If there is a zero division, the return value is `0`, but warnings are also raised.\n    - `0`: If there is a zero division, the return value is `0`.\n    - `1`: If there is a zero division, the return value is `1`.\n\nReturns:\n- **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better.\n\nExamples:\n\n    Example 1-A simple example with some errors\n        >>> recall_metric = datasets.load_metric('recall')\n        >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1])\n        >>> print(results)\n        {'recall': 0.6666666666666666}\n\n    Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`.\n        >>> recall_metric = datasets.load_metric('recall')\n        >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0)\n        >>> print(results)\n        {'recall': 0.5}\n\n    Example 3-The same example as Example 1, but with `sample_weight` included.\n        >>> recall_metric = datasets.load_metric('recall')\n        >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8]\n        >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight)\n        >>> print(results)\n        {'recall': 0.55}\n\n    Example 4-A multiclass example, using different averages.\n        >>> recall_metric = datasets.load_metric('recall')\n        >>> predictions = [0, 2, 1, 0, 0, 1]\n        >>> references = [0, 1, 2, 0, 1, 2]\n        >>> results = recall_metric.compute(predictions=predictions, references=references, average='macro')\n        >>> print(results)\n        {'recall': 0.3333333333333333}\n        >>> results = recall_metric.compute(predictions=predictions, references=references, average='micro')\n        >>> print(results)\n        {'recall': 0.3333333333333333}\n        >>> results = recall_metric.compute(predictions=predictions, references=references, average='weighted')\n        >>> print(results)\n        {'recall': 0.3333333333333333}\n        >>> results = recall_metric.compute(predictions=predictions, references=references, average=None)\n        >>> print(results)\n        {'recall': array([1., 0., 0.])}\n"


__UpperCamelCase					    =	"\n@article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011}\n"




@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION						,					_KWARGS_DESCRIPTION		)
class  _A (					datasets.Metric		):

		def 	lowercase__     (   self					:   Optional[int]			)					->    Any:


					"""simple docstring"""

					return datasets.MetricInfo(
					    description=_DESCRIPTION  ,      citation=_CITATION  ,      inputs_description=_KWARGS_DESCRIPTION  ,      features=datasets.Features(
					        {
					            """predictions""": datasets.Sequence(datasets.Value("""int32"""			)			),
					            """references""": datasets.Sequence(datasets.Value("""int32"""			)			),
					        }
					        if self.config_name == """multilabel"""
					        else {
					            """predictions""": datasets.Value("""int32"""			),
					            """references""": datasets.Value("""int32"""			),
					        }			)  ,      reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html"""]  ,      )

		def 	lowercase__     (   self					:   Tuple  ,      __magic_name__					:   int  ,      __magic_name__					:   Union[str, Any]  ,      __magic_name__					:   Any=None  ,      __magic_name__					:   Optional[Any]=1  ,      __magic_name__					:   List[str]="binary"  ,      __magic_name__					:   Tuple=None  ,      __magic_name__					:   Dict="warn"  ,      )					->    Any:


					"""simple docstring"""

					__snake_case					:      Tuple							      =     recall_score(
					    __magic_name__  ,      __magic_name__  ,      labels=__magic_name__  ,      pos_label=__magic_name__  ,      average=__magic_name__  ,      sample_weight=__magic_name__  ,      zero_division=__magic_name__  ,      )
					return {"recall": float(__magic_name__			) if score.size == 1 else score}


