




'''simple docstring'''







import math
import time

from transformers import Trainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
   import torch_xla.core.xla_model as xm
   import torch_xla.debug.metrics as met


class 			_a  (	__a ):
       def __init__(	self						:	Optional[int]   ,				*lowercase						:	Optional[Any]   ,				lowercase						:	Optional[Any]=None   ,				lowercase						:	Tuple=None   ,				**lowercase						:	Dict				):



           '''simple docstring'''

           super().__init__(*lowercase   ,				**lowercase				)
           UpperCAmelCase			       =		eval_examples
           UpperCAmelCase			       =		post_process_function
       def     A      (	self						:	str   ,				lowercase						:	List[Any]=None   ,				lowercase						:	Tuple=None   ,				lowercase						:	Optional[Any]=None   ,				lowercase						:	str = "eval"				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.eval_dataset if eval_dataset is None else eval_dataset
           UpperCAmelCase			       =		self.get_eval_dataloader(lowercase				)
           UpperCAmelCase			       =		self.eval_examples if eval_examples is None else eval_examples

           # Temporarily disable metric computation, we will do it in the loop here.
           UpperCAmelCase			       =		self.compute_metrics
           UpperCAmelCase			       =		None
           UpperCAmelCase			       =		self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
           UpperCAmelCase			       =		time.time()
           try:
               UpperCAmelCase			       =		eval_loop(
                   lowercase   ,				description='''Evaluation'''   ,				prediction_loss_only=True if compute_metrics is None else None   ,				ignore_keys=lowercase   ,				metric_key_prefix=lowercase   ,				)
           finally:
               UpperCAmelCase			       =		compute_metrics
           UpperCAmelCase			       =		self.args.eval_batch_size * self.args.world_size
           if f"{metric_key_prefix}_jit_compilation_time" in output.metrics:
               start_time += output.metrics[f"{metric_key_prefix}_jit_compilation_time"]
           output.metrics.update(
               speed_metrics(
                   lowercase   ,				lowercase   ,				num_samples=output.num_samples   ,				num_steps=math.ceil(output.num_samples / total_batch_size				)   ,				)				)
           if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
               # Only the main node write the results by default
               UpperCAmelCase			       =		self.post_process_function(lowercase   ,				lowercase   ,				output.predictions				)
               UpperCAmelCase			       =		self.compute_metrics(lowercase				)

               # Prefix all keys with metric_key_prefix + '_'
               for key in list(metrics.keys()				):
                   if not key.startswith(f"{metric_key_prefix}_"				):
                       UpperCAmelCase			       =		metrics.pop(lowercase				)
               metrics.update(output.metrics				)
           else:
               UpperCAmelCase			       =		output.metrics

           if self.args.should_log:
               # Only the main node log the results by default
               self.log(lowercase				)

           if self.args.tpu_metrics_debug or self.args.debug:
               # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
               xm.master_print(met.metrics_report()				)

           UpperCAmelCase			       =		self.callback_handler.on_evaluate(self.args   ,				self.state   ,				self.control   ,				lowercase				)
           return metrics







       def     A      (	self						:	Optional[Any]   ,				lowercase						:	Optional[Any]   ,				lowercase						:	Tuple   ,				lowercase						:	Optional[Any]=None   ,				lowercase						:	str = "test"				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.get_test_dataloader(lowercase				)

           # Temporarily disable metric computation, we will do it in the loop here.
           UpperCAmelCase			       =		self.compute_metrics
           UpperCAmelCase			       =		None
           UpperCAmelCase			       =		self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
           UpperCAmelCase			       =		time.time()
           try:
               UpperCAmelCase			       =		eval_loop(
                   lowercase   ,				description='''Prediction'''   ,				prediction_loss_only=True if compute_metrics is None else None   ,				ignore_keys=lowercase   ,				metric_key_prefix=lowercase   ,				)
           finally:
               UpperCAmelCase			       =		compute_metrics
           UpperCAmelCase			       =		self.args.eval_batch_size * self.args.world_size
           if f"{metric_key_prefix}_jit_compilation_time" in output.metrics:
               start_time += output.metrics[f"{metric_key_prefix}_jit_compilation_time"]
           output.metrics.update(
               speed_metrics(
                   lowercase   ,				lowercase   ,				num_samples=output.num_samples   ,				num_steps=math.ceil(output.num_samples / total_batch_size				)   ,				)				)

           if self.post_process_function is None or self.compute_metrics is None:
               return output

           UpperCAmelCase			       =		self.post_process_function(lowercase   ,				lowercase   ,				output.predictions   ,				'''predict'''				)
           UpperCAmelCase			       =		self.compute_metrics(lowercase				)

           # Prefix all keys with metric_key_prefix + '_'
           for key in list(metrics.keys()				):
               if not key.startswith(f"{metric_key_prefix}_"				):
                   UpperCAmelCase			       =		metrics.pop(lowercase				)
           metrics.update(output.metrics				)
           return PredictionOutput(predictions=predictions.predictions   ,				label_ids=predictions.label_ids   ,				metrics=lowercase				)










import numpy

# List of input, output pairs
_UpperCAmelCase		:  List[str]	=   (
    ((5, 2, 3), 15),
    ((6, 5, 9), 25),
    ((11, 12, 13), 41),
    ((1, 1, 1), 8),
    ((11, 12, 13), 41),
)
_UpperCAmelCase		:  Optional[Any]	=   (((515, 22, 13), 555), ((61, 35, 49), 150))
_UpperCAmelCase		:  Tuple	=   [2, 4, 1, 5]
_UpperCAmelCase		:  Union[str, Any]	=   len(train_data)
_UpperCAmelCase		:  Dict	=   0.0_0_9



def 					UpperCAmelCase__     (   lowerCamelCase,	lowerCamelCase="train"      ):
  return calculate_hypothesis_value(lowerCamelCase,	lowerCamelCase      ) - output(
      lowerCamelCase,	lowerCamelCase      )



def 					UpperCAmelCase__     (   lowerCamelCase      ):
  lowercase			:str							=  0
  for i in range(len(lowerCamelCase      ) - 1      ):
    hyp_val += data_input_tuple[i] * parameter_vector[i + 1]
  hyp_val += parameter_vector[0]
  return hyp_val



def 					UpperCAmelCase__     (   lowerCamelCase,	lowerCamelCase      ):
  if data_set == "train":
    return train_data[example_no][1]
  elif data_set == "test":
    return test_data[example_no][1]
  return None



def 					UpperCAmelCase__     (   lowerCamelCase,	lowerCamelCase      ):
  if data_set == "train":
    return _hypothesis_value(train_data[example_no][0]      )
  elif data_set == "test":
    return _hypothesis_value(test_data[example_no][0]      )
  return None



def 					UpperCAmelCase__     (   lowerCamelCase,	lowerCamelCase=m      ):
  lowercase			:Union[str, Any]							=  0
  for i in range(lowerCamelCase      ):
    if index == -1:
      summation_value += _error(lowerCamelCase      )
    else:
      summation_value += _error(lowerCamelCase      ) * train_data[i][0][index]
  return summation_value



def 					UpperCAmelCase__     (   lowerCamelCase      ):
  lowercase			:int							=  summation_of_cost_derivative(lowerCamelCase,	lowerCamelCase      ) / m
  return cost_derivative_value



def 					UpperCAmelCase__     (   ):
  global parameter_vector
  # Tune these values to set a tolerance value for predicted output
  lowercase			:str							=  0.000_002
  lowercase			:Tuple							=  0
  lowercase			:Optional[int]							=  0
  while True:
    j += 1
    lowercase			:Union[str, Any]							=  [0, 0, 0, 0]
    for i in range(0,	len(lowerCamelCase      )      ):
      lowercase			:Dict							=  get_cost_derivative(i - 1      )
      lowercase			:Optional[Any]							=  (
          parameter_vector[i] - LEARNING_RATE * cost_derivative
      )
    if numpy.allclose(
        lowerCamelCase,	lowerCamelCase,	atol=lowerCamelCase,	rtol=lowerCamelCase,	):
      break
    lowercase			:Union[str, Any]							=  temp_parameter_vector
  print(("Number of iterations:", j)      )



def 					UpperCAmelCase__     (   ):
  for i in range(len(lowerCamelCase      )      ):
    print(("Actual output value:", output(lowerCamelCase,	"test"      ))      )
    print(("Hypothesis output:", calculate_hypothesis_value(lowerCamelCase,	"test"      ))      )


if __name__ == "__main__":
     run_gradient_descent()
     print("\nTesting gradient descent for a linear hypothesis function.\n")
     test_gradient_descent()



