




import os
import tempfile
from functools import partial
from unittest import TestCase
from unittest.mock import patch

import datasets
import datasets.config

from .utils import require_beam




class       __lowerCAmelCase				(							datasets.BeamBasedBuilder):






       def     SCREAMING_SNAKE_CASE			(      self:    str ):
         return datasets.DatasetInfo(
             features=datasets.Features({"content": datasets.Value("string" )} )				,    supervised_keys=_lowerCAmelCase				,    )






       def     SCREAMING_SNAKE_CASE			(      self:    Dict				,    _lowerCAmelCase:    Union[str, Any]				,    _lowerCAmelCase:    Optional[Any] ):
         return [datasets.SplitGenerator(name=datasets.Split.TRAIN				,    gen_kwargs={"examples": get_test_dummy_examples()} )]






       def     SCREAMING_SNAKE_CASE			(      self:    Dict				,    _lowerCAmelCase:    List[str]				,    _lowerCAmelCase:    int ):
         import apache_beam as beam

         return pipeline | "Load Examples" >> beam.Create(_lowerCAmelCase )







class       __lowerCAmelCase				(							datasets.BeamBasedBuilder):






       def     SCREAMING_SNAKE_CASE			(      self:    Dict ):
         return datasets.DatasetInfo(
             features=datasets.Features({"a": datasets.Sequence({"b": datasets.Value("string" )} )} )				,    supervised_keys=_lowerCAmelCase				,    )






       def     SCREAMING_SNAKE_CASE			(      self:    Any				,    _lowerCAmelCase:    Optional[int]				,    _lowerCAmelCase:    List[Any] ):
         return [
             datasets.SplitGenerator(name=datasets.Split.TRAIN				,    gen_kwargs={"examples": get_test_nested_examples()} )
         ]






       def     SCREAMING_SNAKE_CASE			(      self:    Dict				,    _lowerCAmelCase:    str				,    _lowerCAmelCase:    List[str] ):
         import apache_beam as beam

         return pipeline | "Load Examples" >> beam.Create(_lowerCAmelCase )







def 					UpperCAmelCase__     (   ):
  return [(i, {"content": content}) for i, content in enumerate(["foo", "bar", "foobar"]      )]



def 					UpperCAmelCase__     (   ):
  return [(i, {"a": {"b": [content]}}) for i, content in enumerate(["foo", "bar", "foobar"]      )]







class       __lowerCAmelCase				(							lowerCAmelCase):






       @require_beam
       def     SCREAMING_SNAKE_CASE			(      self:    Tuple ):
         lowercase			:List[str]							=  len(get_test_dummy_examples() )
         with tempfile.TemporaryDirectory() as tmp_cache_dir:
           lowercase			:Tuple							=  DummyBeamDataset(cache_dir=_lowerCAmelCase				,    beam_runner="DirectRunner" )
           builder.download_and_prepare()
           self.assertTrue(
               os.path.exists(
                   os.path.join(_lowerCAmelCase				,    builder.name				,    "default"				,    "0.0.0"				,    F"{builder.name}-train.arrow" ) ) )
           self.assertDictEqual(builder.info.features				,    datasets.Features({"content": datasets.Value("string" )} ) )
           lowercase			:List[Any]							=  builder.as_dataset()
           self.assertEqual(dset["train"].num_rows				,    _lowerCAmelCase )
           self.assertEqual(dset["train"].info.splits["train"].num_examples				,    _lowerCAmelCase )
           self.assertDictEqual(dset["train"][0]				,    get_test_dummy_examples()[0][1] )
           self.assertDictEqual(
               dset["train"][expected_num_examples - 1]				,    get_test_dummy_examples()[expected_num_examples - 1][1] )
           self.assertTrue(
               os.path.exists(os.path.join(_lowerCAmelCase				,    builder.name				,    "default"				,    "0.0.0"				,    "dataset_info.json" ) ) )
           del dset






       @require_beam
       def     SCREAMING_SNAKE_CASE			(      self:    Optional[Any] ):
         import apache_beam as beam

         lowercase			:List[Any]							=  beam.io.parquetio.WriteToParquet

         lowercase			:List[Any]							=  len(get_test_dummy_examples() )
         with tempfile.TemporaryDirectory() as tmp_cache_dir:
           lowercase			:Optional[Any]							=  DummyBeamDataset(cache_dir=_lowerCAmelCase				,    beam_runner="DirectRunner" )
           with patch("apache_beam.io.parquetio.WriteToParquet" ) as write_parquet_mock:
             lowercase			:Tuple							=  partial(_lowerCAmelCase				,    num_shards=2 )
             builder.download_and_prepare()
           self.assertTrue(
               os.path.exists(
                   os.path.join(
                       _lowerCAmelCase				,    builder.name				,    "default"				,    "0.0.0"				,    F"{builder.name}-train-00000-of-00002.arrow" ) ) )
           self.assertTrue(
               os.path.exists(
                   os.path.join(
                       _lowerCAmelCase				,    builder.name				,    "default"				,    "0.0.0"				,    F"{builder.name}-train-00000-of-00002.arrow" ) ) )
           self.assertDictEqual(builder.info.features				,    datasets.Features({"content": datasets.Value("string" )} ) )
           lowercase			:Any							=  builder.as_dataset()
           self.assertEqual(dset["train"].num_rows				,    _lowerCAmelCase )
           self.assertEqual(dset["train"].info.splits["train"].num_examples				,    _lowerCAmelCase )
           # Order is not preserved when sharding, so we just check that all the elements are there
           self.assertListEqual(sorted(dset["train"]["content"] )				,    sorted(["foo", "bar", "foobar"] ) )
           self.assertTrue(
               os.path.exists(os.path.join(_lowerCAmelCase				,    builder.name				,    "default"				,    "0.0.0"				,    "dataset_info.json" ) ) )
           del dset






       @require_beam
       def     SCREAMING_SNAKE_CASE			(      self:    List[Any] ):
         with tempfile.TemporaryDirectory() as tmp_cache_dir:
           lowercase			:Optional[int]							=  DummyBeamDataset(cache_dir=_lowerCAmelCase )
           self.assertRaises(datasets.builder.MissingBeamOptions				,    builder.download_and_prepare )






       @require_beam
       def     SCREAMING_SNAKE_CASE			(      self:    str ):
         lowercase			:Union[str, Any]							=  len(get_test_nested_examples() )
         with tempfile.TemporaryDirectory() as tmp_cache_dir:
           lowercase			:Any							=  NestedBeamDataset(cache_dir=_lowerCAmelCase				,    beam_runner="DirectRunner" )
           builder.download_and_prepare()
           self.assertTrue(
               os.path.exists(
                   os.path.join(_lowerCAmelCase				,    builder.name				,    "default"				,    "0.0.0"				,    F"{builder.name}-train.arrow" ) ) )
           self.assertDictEqual(
               builder.info.features				,    datasets.Features({"a": datasets.Sequence({"b": datasets.Value("string" )} )} ) )
           lowercase			:List[Any]							=  builder.as_dataset()
           self.assertEqual(dset["train"].num_rows				,    _lowerCAmelCase )
           self.assertEqual(dset["train"].info.splits["train"].num_examples				,    _lowerCAmelCase )
           self.assertDictEqual(dset["train"][0]				,    get_test_nested_examples()[0][1] )
           self.assertDictEqual(
               dset["train"][expected_num_examples - 1]				,    get_test_nested_examples()[expected_num_examples - 1][1] )
           self.assertTrue(
               os.path.exists(os.path.join(_lowerCAmelCase				,    builder.name				,    "default"				,    "0.0.0"				,    "dataset_info.json" ) ) )
           del dset









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()



