






'''simple docstring'''







import copy
import os
from typing import TYPE_CHECKING, List, Union


if TYPE_CHECKING:
							pass

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCamelCase    :      str  			=		logging.get_logger(__name__)

UpperCamelCase    :      List[str]  			=		{
    'kakaobrain/align-base': 'https://huggingface.co/kakaobrain/align-base/resolve/main/config.json',
}




class 		UpperCamelCase__    (a			):




		'''simple docstring'''





		_UpperCamelCase				  =					'align_text_model'






		def __init__(      self   ,_lowerCAmelCase=3_05_22   ,_lowerCAmelCase=7_68   ,_lowerCAmelCase=12   ,_lowerCAmelCase=12   ,_lowerCAmelCase=30_72   ,_lowerCAmelCase="gelu"   ,_lowerCAmelCase=0.1   ,_lowerCAmelCase=0.1   ,_lowerCAmelCase=5_12   ,_lowerCAmelCase=2   ,_lowerCAmelCase=0.02   ,_lowerCAmelCase=1E-12   ,_lowerCAmelCase=0   ,_lowerCAmelCase="absolute"   ,_lowerCAmelCase=True   ,**_lowerCAmelCase   ,):
								super().__init__(**_lowerCAmelCase       )

								lowerCamelCase__         =  vocab_size
								lowerCamelCase__         =  hidden_size
								lowerCamelCase__         =  num_hidden_layers
								lowerCamelCase__         =  num_attention_heads
								lowerCamelCase__         =  hidden_act
								lowerCamelCase__         =  intermediate_size
								lowerCamelCase__         =  hidden_dropout_prob
								lowerCamelCase__         =  attention_probs_dropout_prob
								lowerCamelCase__         =  max_position_embeddings
								lowerCamelCase__         =  type_vocab_size
								lowerCamelCase__         =  initializer_range
								lowerCamelCase__         =  layer_norm_eps
								lowerCamelCase__         =  position_embedding_type
								lowerCamelCase__         =  use_cache
								lowerCamelCase__         =  pad_token_id






		@classmethod
		def 			UpperCamelCase_					(      cls   ,_lowerCAmelCase   ,**_lowerCAmelCase       ):
								cls._set_token_in_kwargs(_lowerCAmelCase       )

								lowerCamelCase__       ,			lowerCamelCase__         =  cls.get_config_dict(_lowerCAmelCase   ,**_lowerCAmelCase       )

								# get the text config dict if we are loading from AlignConfig
								if config_dict.get("""model_type"""       ) == "align":
														lowerCamelCase__         =  config_dict["""text_config"""]

								if "model_type" in config_dict and hasattr(cls   ,"""model_type"""       ) and config_dict["model_type"] != cls.model_type:
														logger.warning(
														    F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
														    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''       )

								return cls.from_dict(_lowerCAmelCase   ,**_lowerCAmelCase       )




class 		UpperCamelCase__    (a			):




		'''simple docstring'''





		_UpperCamelCase				  =					'align_vision_model'






		def __init__(      self   ,_lowerCAmelCase = 3   ,_lowerCAmelCase = 6_00   ,_lowerCAmelCase = 2.0   ,_lowerCAmelCase = 3.1   ,_lowerCAmelCase = 8   ,_lowerCAmelCase = [3, 3, 5, 3, 5, 5, 3]   ,_lowerCAmelCase = [32, 16, 24, 40, 80, 1_12, 1_92]   ,_lowerCAmelCase = [16, 24, 40, 80, 1_12, 1_92, 3_20]   ,_lowerCAmelCase = []   ,_lowerCAmelCase = [1, 2, 2, 2, 1, 2, 1]   ,_lowerCAmelCase = [1, 2, 2, 3, 3, 4, 1]   ,_lowerCAmelCase = [1, 6, 6, 6, 6, 6, 6]   ,_lowerCAmelCase = 0.25   ,_lowerCAmelCase = "swish"   ,_lowerCAmelCase = 25_60   ,_lowerCAmelCase = "mean"   ,_lowerCAmelCase = 0.02   ,_lowerCAmelCase = 0.001   ,_lowerCAmelCase = 0.99   ,_lowerCAmelCase = 0.2   ,**_lowerCAmelCase   ,):
								super().__init__(**_lowerCAmelCase       )

								lowerCamelCase__         =  num_channels
								lowerCamelCase__         =  image_size
								lowerCamelCase__         =  width_coefficient
								lowerCamelCase__         =  depth_coefficient
								lowerCamelCase__         =  depth_divisor
								lowerCamelCase__         =  kernel_sizes
								lowerCamelCase__         =  in_channels
								lowerCamelCase__         =  out_channels
								lowerCamelCase__         =  depthwise_padding
								lowerCamelCase__         =  strides
								lowerCamelCase__         =  num_block_repeats
								lowerCamelCase__         =  expand_ratios
								lowerCamelCase__         =  squeeze_expansion_ratio
								lowerCamelCase__         =  hidden_act
								lowerCamelCase__         =  hidden_dim
								lowerCamelCase__         =  pooling_type
								lowerCamelCase__         =  initializer_range
								lowerCamelCase__         =  batch_norm_eps
								lowerCamelCase__         =  batch_norm_momentum
								lowerCamelCase__         =  drop_connect_rate
								lowerCamelCase__         =  sum(_lowerCAmelCase       ) * 4






		@classmethod
		def 			UpperCamelCase_					(      cls   ,_lowerCAmelCase   ,**_lowerCAmelCase       ):
								cls._set_token_in_kwargs(_lowerCAmelCase       )

								lowerCamelCase__       ,			lowerCamelCase__         =  cls.get_config_dict(_lowerCAmelCase   ,**_lowerCAmelCase       )

								# get the vision config dict if we are loading from AlignConfig
								if config_dict.get("""model_type"""       ) == "align":
														lowerCamelCase__         =  config_dict["""vision_config"""]

								if "model_type" in config_dict and hasattr(cls   ,"""model_type"""       ) and config_dict["model_type"] != cls.model_type:
														logger.warning(
														    F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
														    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''       )

								return cls.from_dict(_lowerCAmelCase   ,**_lowerCAmelCase       )




class 		UpperCamelCase__    (a			):




		'''simple docstring'''





		_UpperCamelCase				  =					'align'
		_UpperCamelCase				  =					True






		def __init__(      self   ,_lowerCAmelCase=None   ,_lowerCAmelCase=None   ,_lowerCAmelCase=6_40   ,_lowerCAmelCase=1.0   ,_lowerCAmelCase=0.02   ,**_lowerCAmelCase   ,):
								super().__init__(**_lowerCAmelCase       )

								if text_config is None:
														lowerCamelCase__         =  {}
														logger.info("""text_config is None. Initializing the AlignTextConfig with default values."""       )

								if vision_config is None:
														lowerCamelCase__         =  {}
														logger.info("""vision_config is None. Initializing the AlignVisionConfig with default values."""       )

								lowerCamelCase__         =  AlignTextConfig(**_lowerCAmelCase       )
								lowerCamelCase__         =  AlignVisionConfig(**_lowerCAmelCase       )

								lowerCamelCase__         =  projection_dim
								lowerCamelCase__         =  temperature_init_value
								lowerCamelCase__         =  initializer_range






		@classmethod
		def 			UpperCamelCase_					(      cls   ,_lowerCAmelCase   ,_lowerCAmelCase   ,**_lowerCAmelCase       ):
								return cls(text_config=text_config.to_dict()   ,vision_config=vision_config.to_dict()   ,**_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  copy.deepcopy(self.__dict__       )
								lowerCamelCase__         =  self.text_config.to_dict()
								lowerCamelCase__         =  self.vision_config.to_dict()
								lowerCamelCase__         =  self.__class__.model_type
								return output









'''simple docstring'''







import numpy

# List of input, output pairs
UpperCamelCase    :      List[Any]  			=		(
    ((5, 2, 3), 15),
    ((6, 5, 9), 25),
    ((11, 12, 13), 41),
    ((1, 1, 1), 8),
    ((11, 12, 13), 41),
)
UpperCamelCase    :      Optional[int]  			=		(((5_15, 22, 13), 5_55), ((61, 35, 49), 1_50))
UpperCamelCase    :      int  			=		[2, 4, 1, 5]
UpperCamelCase    :      int  			=		len(train_data)
UpperCamelCase    :      Dict  			=		0.009
def 						A__			(				__lowerCAmelCase		:			int   ,    __lowerCAmelCase		:			str="train"						):
						return calculate_hypothesis_value(__lowerCAmelCase   ,    __lowerCAmelCase						) - output(
						    __lowerCAmelCase   ,    __lowerCAmelCase						)
def 						A__			(				__lowerCAmelCase		:			Any						):
						lowerCamelCase__         =  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 						A__			(				__lowerCAmelCase		:			Optional[Any]   ,    __lowerCAmelCase		:			Tuple						):
						if data_set == "train":
												return train_data[example_no][1]
						elif data_set == "test":
												return test_data[example_no][1]
						return None
def 						A__			(				__lowerCAmelCase		:			int   ,    __lowerCAmelCase		:			Dict						):
						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 						A__			(				__lowerCAmelCase		:			List[Any]   ,    __lowerCAmelCase		:			Optional[Any]=m						):
						lowerCamelCase__         =  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 						A__			(				__lowerCAmelCase		:			List[Any]						):
						lowerCamelCase__         =  summation_of_cost_derivative(__lowerCAmelCase   ,    __lowerCAmelCase						) / m
						return cost_derivative_value
def 						A__			(				):
						global parameter_vector
						# Tune these values to set a tolerance value for predicted output
						lowerCamelCase__         =  0.00_0002
						lowerCamelCase__         =  0
						lowerCamelCase__         =  0
						while True:
												j += 1
												lowerCamelCase__         =  [0, 0, 0, 0]
												for i in range(0   ,    len(__lowerCAmelCase						)						):
																		lowerCamelCase__         =  get_cost_derivative(i - 1						)
																		lowerCamelCase__         =  (
																		    parameter_vector[i] - LEARNING_RATE * cost_derivative
																		)
												if numpy.allclose(
												    __lowerCAmelCase   ,    __lowerCAmelCase   ,    atol=__lowerCAmelCase   ,    rtol=__lowerCAmelCase   ,    ):
																		break
												lowerCamelCase__         =  temp_parameter_vector
						print(("""Number of iterations:""", j)						)
def 						A__			(				):
						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()

