






'''simple docstring'''







import numpy

# List of input, output pairs
__lowerCAmelCase									=   (
    ((5, 2, 3), 15),
    ((6, 5, 9), 25),
    ((11, 12, 13), 41),
    ((1, 1, 1), 8),
    ((11, 12, 13), 41),
)
__lowerCAmelCase									=   (((515, 22, 13), 555), ((61, 35, 49), 150))
__lowerCAmelCase									=   [2, 4, 1, 5]
__lowerCAmelCase									=   len(train_data)
__lowerCAmelCase									=   0.009





def 		_UpperCAmelCase (				__A				: str    ,		__A				: int="train"   ):
				return calculate_hypothesis_value(__A    ,		__A   ) - output(
				    __A    ,		__A   )





def 		_UpperCAmelCase (				__A				: Tuple   ):
				a_		:	List[str]									=   0
				for i in range(len(__A   ) - 1   ):
								hyp_val += data_input_tuple[i] * parameter_vector[i + 1]
				hyp_val += parameter_vector[0]
				return hyp_val





def 		_UpperCAmelCase (				__A				: Optional[Any]    ,		__A				: Optional[Any]   ):
				if data_set == "train":
								return train_data[example_no][1]
				elif data_set == "test":
								return test_data[example_no][1]
				return None





def 		_UpperCAmelCase (				__A				: Dict    ,		__A				: Optional[int]   ):
				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 (				__A				: List[Any]    ,		__A				: Optional[Any]=m   ):
				a_		:	List[str]									=   0
				for i in range(__A   ):
								if index == -1:
												summation_value += _error(__A   )
								else:
												summation_value += _error(__A   ) * train_data[i][0][index]
				return summation_value





def 		_UpperCAmelCase (				__A				: Optional[Any]   ):
				a_		:	Optional[int]									=   summation_of_cost_derivative(__A    ,		__A   ) / m
				return cost_derivative_value





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





def 		_UpperCAmelCase (				):
				for i in range(len(__A   )   ):
								print(('''Actual output value:''', output(__A    ,		'''test'''   ))   )
								print(('''Hypothesis output:''', calculate_hypothesis_value(__A    ,		'''test'''   ))   )


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












'''simple docstring'''







import re

from flax.core.frozen_dict import freeze
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.experimental import PartitionSpec as P


# Sentinels
__lowerCAmelCase									=   object()

# For specifying empty leaf dict `{}`
__lowerCAmelCase									=   object()





def 		_UpperCAmelCase (				__A				: List[str]    ,		__A				: Tuple   ):
				a_		:	List[Any]									=   tuple((re.compile(x + '''$'''   ) for x in qs)   )
				for i in range(len(__A   ) - len(__A   ) + 1   ):
								a_		:	Union[str, Any]									=   [x.match(__A   ) for x, y in zip(__A    ,		ks[i:]   )]
								if matches and all(__A   ):
												return True
				return False





def 		_UpperCAmelCase (				__A				: List[str]   ):
				def replace(__A				: int    ,		__A				: Union[str, Any]   ):
								for rule, replacement in rules:
												if _match(__A    ,		__A   ):
																return replacement
								return val

				return replace





def 		_UpperCAmelCase (				):
				return [
				    # embeddings
				    (("transformer", "wpe", "embedding"), P('''mp'''    ,		__A   )),
				    (("transformer", "wte", "embedding"), P('''mp'''    ,		__A   )),
				    # atention
				    (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(__A    ,		'''mp'''   )),
				    (("attention", "out_proj", "kernel"), P('''mp'''    ,		__A   )),
				    (("attention", "out_proj", "bias"), None),
				    # mlp
				    (("mlp", "c_fc", "kernel"), P(__A    ,		'''mp'''   )),
				    (("mlp", "c_fc", "bias"), P('''mp'''   )),
				    (("mlp", "c_proj", "kernel"), P('''mp'''    ,		__A   )),
				    (("mlp", "c_proj", "bias"), None),
				    # layer norms
				    ((r"ln_\d+", "bias"), None),
				    ((r"\d+", r"ln_\d+", "scale"), None),
				    (("ln_f", "bias"), None),
				    (("ln_f", "scale"), None),
				]





def 		_UpperCAmelCase (				__A				: Union[str, Any]   ):
				a_		:	Tuple									=   _get_partition_rules()
				a_		:	Tuple									=   _replacement_rules(__A   )
				a_		:	Optional[Any]									=   {k: _unmatched for k in flatten_dict(__A   )}
				a_		:	Optional[Any]									=   {k: replace(__A    ,		__A   ) for k, v in initd.items()}
				assert _unmatched not in result.values(), "Incomplete partition spec."
				return freeze(unflatten_dict(__A   )   )




