


import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFAutoModel, is_tensorflow_text_available, is_tf_available
from transformers.models.bert.tokenization_bert import BertTokenizer
from transformers.testing_utils import require_tensorflow_text, require_tf, slow


if is_tf_available():
 import tensorflow as tf

if is_tensorflow_text_available():
 from transformers.models.bert import TFBertTokenizer


lowerCamelCase											=       ['bert-base-uncased', 'bert-base-cased']
lowerCamelCase											=       'hf-internal-testing/tiny-bert-tf-only'

if is_tf_available():




 class A  (   tf.keras.Model     ):






       def __init__(					self						:     Dict			,   lowercase_						:     Any )					->						Tuple:
              """simple docstring"""




              super().__init__()
              _lowerCamelCase       :		Union[str, Any]		=tokenizer
              _lowerCamelCase       :		List[Any]		=AutoConfig.from_pretrained(lowercase_ )
              _lowerCamelCase       :		Tuple		=TFAutoModel.from_config(lowercase_ )






       def        lowerCamelCase       (					self						:     int			,   lowercase_						:     str )					->						str:
              """simple docstring"""




              _lowerCamelCase       :		Any		=self.tokenizer(lowercase_ )
              _lowerCamelCase       :		List[str]		=self.bert(**lowercase_ )
              return out["pooler_output"]




@require_tf
@require_tensorflow_text
class A  (   unittest.TestCase     ):






      def        lowerCamelCase       (					self						:     str )					->						Dict:
             """simple docstring"""




             super().setUp()

             _lowerCamelCase       :		List[str]		=[
                 BertTokenizer.from_pretrained(lowercase_ ) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
             ]  # repeat for when fast_bert_tokenizer=false
             _lowerCamelCase       :		Dict		=[TFBertTokenizer.from_pretrained(lowercase_ ) for checkpoint in TOKENIZER_CHECKPOINTS] + [
                 TFBertTokenizer.from_pretrained(lowercase_			,   use_fast_bert_tokenizer=lowercase_ )
                 for checkpoint in TOKENIZER_CHECKPOINTS
             ]
             assert len(self.tokenizers ) == len(self.tf_tokenizers )

             _lowerCamelCase       :		Optional[int]		=[
                 'This is a straightforward English test sentence.',
                 'This one has some weird characters\rto\nsee\r\nif  those\u00E9break things.',
                 'Now we\'re going to add some Chinese: 一 二 三 一二三',
                 'And some much more rare Chinese: 齉 堃 齉堃',
                 'Je vais aussi écrire en français pour tester les accents',
                 'Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ',
             ]
             _lowerCamelCase       :		List[str]		=list(zip(self.test_sentences			,   self.test_sentences[::-1] ) )






      def        lowerCamelCase       (					self						:     Dict )					->						List[Any]:
             """simple docstring"""




             for tokenizer, tf_tokenizer in zip(self.tokenizers			,   self.tf_tokenizers ):
                    for test_inputs in (self.test_sentences, self.paired_sentences):
                           _lowerCamelCase       :		List[str]		=tokenizer(lowercase_			,   return_tensors='tf'			,   padding='longest' )
                           _lowerCamelCase       :		List[Any]		=tf_tokenizer(lowercase_ )

                           for key in python_outputs.keys():
                                  self.assertTrue(tf.reduce_all(python_outputs[key].shape == tf_outputs[key].shape ) )
                                  self.assertTrue(tf.reduce_all(tf.cast(python_outputs[key]			,   tf.intaa ) == tf_outputs[key] ) )






      @slow
      def        lowerCamelCase       (					self						:     List[Any] )					->						Dict:
             """simple docstring"""




             for tf_tokenizer in self.tf_tokenizers:
                    _lowerCamelCase       :		Dict		=tf_tokenizer(self.paired_sentences )
                    _lowerCamelCase       :		List[str]		=tf_tokenizer(
                        text=[sentence[0] for sentence in self.paired_sentences]			,   text_pair=[sentence[1] for sentence in self.paired_sentences]			,   )
                    for key in merged_outputs.keys():
                           self.assertTrue(tf.reduce_all(tf.cast(merged_outputs[key]			,   tf.intaa ) == separated_outputs[key] ) )






      @slow
      def        lowerCamelCase       (					self						:     Dict )					->						List[Any]:
             """simple docstring"""




             for tf_tokenizer in self.tf_tokenizers:
                    _lowerCamelCase       :		Any		=tf.function(lowercase_ )
                    for test_inputs in (self.test_sentences, self.paired_sentences):
                           _lowerCamelCase       :		Optional[Any]		=tf.constant(lowercase_ )
                           _lowerCamelCase       :		int		=compiled_tokenizer(lowercase_ )
                           _lowerCamelCase       :		List[Any]		=tf_tokenizer(lowercase_ )

                           for key in eager_outputs.keys():
                                  self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key] ) )






      @slow
      def        lowerCamelCase       (					self						:     Dict )					->						Optional[int]:
             """simple docstring"""




             for tf_tokenizer in self.tf_tokenizers:
                    _lowerCamelCase       :		int		=ModelToSave(tokenizer=lowercase_ )
                    _lowerCamelCase       :		Optional[Any]		=tf.convert_to_tensor(self.test_sentences )
                    _lowerCamelCase       :		Any		=model(lowercase_ )  # Build model with some sample inputs
                    with TemporaryDirectory() as tempdir:
                           _lowerCamelCase       :		Optional[int]		=Path(lowercase_ ) / 'saved.model'
                           model.save(lowercase_ )
                           _lowerCamelCase       :		Optional[int]		=tf.keras.models.load_model(lowercase_ )
                    _lowerCamelCase       :		Dict		=loaded_model(lowercase_ )
                    # We may see small differences because the loaded model is compiled, so we need an epsilon for the test
                    self.assertLessEqual(tf.reduce_max(tf.abs(out - loaded_output ) )			,   1E-5 )






from sklearn.metrics import mean_squared_error

import datasets


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

lowerCamelCase											=       '\\nMean Squared Error(MSE) is the average of the square of difference between the predicted\nand actual values.\n'


lowerCamelCase											=       '\nArgs:\n    predictions: array-like of shape (n_samples,) or (n_samples, n_outputs)\n        Estimated target values.\n    references: array-like of shape (n_samples,) or (n_samples, n_outputs)\n        Ground truth (correct) target values.\n    sample_weight: array-like of shape (n_samples,), default=None\n        Sample weights.\n    multioutput: {"raw_values", "uniform_average"} or array-like of shape (n_outputs,), default="uniform_average"\n        Defines aggregating of multiple output values. Array-like value defines weights used to average errors.\n\n        "raw_values" : Returns a full set of errors in case of multioutput input.\n\n        "uniform_average" : Errors of all outputs are averaged with uniform weight.\n\n    squared : bool, default=True\n        If True returns MSE value, if False returns RMSE (Root Mean Squared Error) value.\n\nReturns:\n    mse : mean squared error.\nExamples:\n\n    >>> mse_metric = datasets.load_metric("mse")\n    >>> predictions = [2.5, 0.0, 2, 8]\n    >>> references = [3, -0.5, 2, 7]\n    >>> results = mse_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'mse\': 0.375}\n    >>> rmse_result = mse_metric.compute(predictions=predictions, references=references, squared=False)\n    >>> print(rmse_result)\n    {\'mse\': 0.6123724356957945}\n\n    If you\'re using multi-dimensional lists, then set the config as follows :\n\n    >>> mse_metric = datasets.load_metric("mse", "multilist")\n    >>> predictions = [[0.5, 1], [-1, 1], [7, -6]]\n    >>> references = [[0, 2], [-1, 2], [8, -5]]\n    >>> results = mse_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'mse\': 0.7083333333333334}\n    >>> results = mse_metric.compute(predictions=predictions, references=references, multioutput=\'raw_values\')\n    >>> print(results) # doctest: +NORMALIZE_WHITESPACE\n    {\'mse\': array([0.41666667, 1. ])}\n'




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






      def        lowerCamelCase       (					self						:     List[Any] )					->						List[str]:
             """simple docstring"""




             return datasets.MetricInfo(
                 description=_DESCRIPTION			,   citation=_CITATION			,   inputs_description=_KWARGS_DESCRIPTION			,   features=datasets.Features(self._get_feature_types() )			,   reference_urls=[
                     'https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html'
                 ]			,   )






      def        lowerCamelCase       (					self						:     Tuple )					->						Optional[int]:
             """simple docstring"""




             if self.config_name == "multilist":
                    return {
                        "predictions": datasets.Sequence(datasets.Value('float' ) ),
                        "references": datasets.Sequence(datasets.Value('float' ) ),
                    }
             else:
                    return {
                        "predictions": datasets.Value('float' ),
                        "references": datasets.Value('float' ),
                    }






      def        lowerCamelCase       (					self						:     List[Any]			,   lowercase_						:     List[Any]			,   lowercase_						:     Tuple			,   lowercase_						:     List[str]=None			,   lowercase_						:     Union[str, Any]="uniform_average"			,   lowercase_						:     Tuple=True )					->						Any:
             """simple docstring"""




             _lowerCamelCase       :		List[str]		=mean_squared_error(
                 lowercase_			,   lowercase_			,   sample_weight=lowercase_			,   multioutput=lowercase_			,   squared=lowercase_ )

             return {"mse": mse}


