
'''simple docstring'''







from typing import Optional

import torch
import torch.utils.checkpoint
from torch import Tensor, nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import (
    BackboneOutput,
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import (
    add_code_sample_docstrings,
    add_start_docstrings,
    add_start_docstrings_to_model_forward,
    logging,
    replace_return_docstrings,
)
from ...utils.backbone_utils import BackboneMixin
from .configuration_resnet import ResNetConfig


_snake_case				:   Optional[int]     =				logging.get_logger(__name__)

# General docstring
_snake_case				:   Any     =				"""ResNetConfig"""

# Base docstring
_snake_case				:   List[str]     =				"""microsoft/resnet-50"""
_snake_case				:   Tuple     =				[1, 2_048, 7, 7]

# Image classification docstring
_snake_case				:   Any     =				"""microsoft/resnet-50"""
_snake_case				:   List[Any]     =				"""tiger cat"""

_snake_case				:   Optional[Any]     =				[
    """microsoft/resnet-50""",
    # See all resnet models at https://huggingface.co/models?filter=resnet
]


class 			lowerCAmelCase (				nn.Module			):




		def __init__(				self					,						UpperCamelCase					,						UpperCamelCase					,						UpperCamelCase = 3					,						UpperCamelCase = 1					,						UpperCamelCase = "relu"      ):
								super().__init__()
								_SCREAMING_SNAKE_CASE				= nn.Convad(
								    UpperCamelCase					,						UpperCamelCase					,						kernel_size=UpperCamelCase					,						stride=UpperCamelCase					,						padding=kernel_size // 2					,						bias=UpperCamelCase      )
								_SCREAMING_SNAKE_CASE				= nn.BatchNormad(UpperCamelCase      )
								_SCREAMING_SNAKE_CASE				= ACTaFN[activation] if activation is not None else nn.Identity()




		def     lowercase       (				self					,						UpperCamelCase      ):
								_SCREAMING_SNAKE_CASE				= self.convolution(UpperCamelCase      )
								_SCREAMING_SNAKE_CASE				= self.normalization(UpperCamelCase      )
								_SCREAMING_SNAKE_CASE				= self.activation(UpperCamelCase      )
								return hidden_state


class 			lowerCAmelCase (				nn.Module			):




		def __init__(				self					,						UpperCamelCase      ):
								super().__init__()
								_SCREAMING_SNAKE_CASE				= ResNetConvLayer(
								    config.num_channels					,						config.embedding_size					,						kernel_size=7					,						stride=2					,						activation=config.hidden_act      )
								_SCREAMING_SNAKE_CASE				= nn.MaxPoolad(kernel_size=3					,						stride=2					,						padding=1      )
								_SCREAMING_SNAKE_CASE				= config.num_channels




		def     lowercase       (				self					,						UpperCamelCase      ):
								_SCREAMING_SNAKE_CASE				= pixel_values.shape[1]
								if num_channels != self.num_channels:
														raise ValueError(
														    "Make sure that the channel dimension of the pixel values match with the one set in the configuration."      )
								_SCREAMING_SNAKE_CASE				= self.embedder(UpperCamelCase      )
								_SCREAMING_SNAKE_CASE				= self.pooler(UpperCamelCase      )
								return embedding


class 			lowerCAmelCase (				nn.Module			):




		def __init__(				self					,						UpperCamelCase					,						UpperCamelCase					,						UpperCamelCase = 2      ):
								super().__init__()
								_SCREAMING_SNAKE_CASE				= nn.Convad(UpperCamelCase					,						UpperCamelCase					,						kernel_size=1					,						stride=UpperCamelCase					,						bias=UpperCamelCase      )
								_SCREAMING_SNAKE_CASE				= nn.BatchNormad(UpperCamelCase      )




		def     lowercase       (				self					,						UpperCamelCase      ):
								_SCREAMING_SNAKE_CASE				= self.convolution(UpperCamelCase      )
								_SCREAMING_SNAKE_CASE				= self.normalization(UpperCamelCase      )
								return hidden_state


class 			lowerCAmelCase (				nn.Module			):




		def __init__(				self					,						UpperCamelCase					,						UpperCamelCase					,						UpperCamelCase = 1					,						UpperCamelCase = "relu"      ):
								super().__init__()
								_SCREAMING_SNAKE_CASE				= in_channels != out_channels or stride != 1
								_SCREAMING_SNAKE_CASE				= (
								    ResNetShortCut(UpperCamelCase					,						UpperCamelCase					,						stride=UpperCamelCase      ) if should_apply_shortcut else nn.Identity()
								)
								_SCREAMING_SNAKE_CASE				= nn.Sequential(
								    ResNetConvLayer(UpperCamelCase					,						UpperCamelCase					,						stride=UpperCamelCase      )					,						ResNetConvLayer(UpperCamelCase					,						UpperCamelCase					,						activation=UpperCamelCase      )					,						)
								_SCREAMING_SNAKE_CASE				= ACTaFN[activation]




		def     lowercase       (				self					,						UpperCamelCase      ):
								_SCREAMING_SNAKE_CASE				= hidden_state
								_SCREAMING_SNAKE_CASE				= self.layer(UpperCamelCase      )
								_SCREAMING_SNAKE_CASE				= self.shortcut(UpperCamelCase      )
								hidden_state += residual
								_SCREAMING_SNAKE_CASE				= self.activation(UpperCamelCase      )
								return hidden_state


class 			lowerCAmelCase (				nn.Module			):




		def __init__(				self					,						UpperCamelCase					,						UpperCamelCase					,						UpperCamelCase = 1					,						UpperCamelCase = "relu"					,						UpperCamelCase = 4      ):
								super().__init__()
								_SCREAMING_SNAKE_CASE				= in_channels != out_channels or stride != 1
								_SCREAMING_SNAKE_CASE				= out_channels // reduction
								_SCREAMING_SNAKE_CASE				= (
								    ResNetShortCut(UpperCamelCase					,						UpperCamelCase					,						stride=UpperCamelCase      ) if should_apply_shortcut else nn.Identity()
								)
								_SCREAMING_SNAKE_CASE				= nn.Sequential(
								    ResNetConvLayer(UpperCamelCase					,						UpperCamelCase					,						kernel_size=1      )					,						ResNetConvLayer(UpperCamelCase					,						UpperCamelCase					,						stride=UpperCamelCase      )					,						ResNetConvLayer(UpperCamelCase					,						UpperCamelCase					,						kernel_size=1					,						activation=UpperCamelCase      )					,						)
								_SCREAMING_SNAKE_CASE				= ACTaFN[activation]




		def     lowercase       (				self					,						UpperCamelCase      ):
								_SCREAMING_SNAKE_CASE				= hidden_state
								_SCREAMING_SNAKE_CASE				= self.layer(UpperCamelCase      )
								_SCREAMING_SNAKE_CASE				= self.shortcut(UpperCamelCase      )
								hidden_state += residual
								_SCREAMING_SNAKE_CASE				= self.activation(UpperCamelCase      )
								return hidden_state


class 			lowerCAmelCase (				nn.Module			):




		def __init__(				self					,						UpperCamelCase					,						UpperCamelCase					,						UpperCamelCase					,						UpperCamelCase = 2					,						UpperCamelCase = 2					,						):
								super().__init__()

								_SCREAMING_SNAKE_CASE				= ResNetBottleNeckLayer if config.layer_type == "bottleneck" else ResNetBasicLayer

								_SCREAMING_SNAKE_CASE				= nn.Sequential(
								    # downsampling is done in the first layer with stride of 2
								    layer(UpperCamelCase					,						UpperCamelCase					,						stride=UpperCamelCase					,						activation=config.hidden_act      )					,						*[layer(UpperCamelCase					,						UpperCamelCase					,						activation=config.hidden_act      ) for _ in range(depth - 1      )]					,						)




		def     lowercase       (				self					,						UpperCamelCase      ):
								_SCREAMING_SNAKE_CASE				= input
								for layer in self.layers:
														_SCREAMING_SNAKE_CASE				= layer(UpperCamelCase      )
								return hidden_state


class 			lowerCAmelCase (				nn.Module			):




		def __init__(				self					,						UpperCamelCase      ):
								super().__init__()
								_SCREAMING_SNAKE_CASE				= nn.ModuleList([]      )
								# based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input
								self.stages.append(
								    ResNetStage(
								        UpperCamelCase					,						config.embedding_size					,						config.hidden_sizes[0]					,						stride=2 if config.downsample_in_first_stage else 1					,						depth=config.depths[0]					,						)      )
								_SCREAMING_SNAKE_CASE				= zip(config.hidden_sizes					,						config.hidden_sizes[1:]      )
								for (in_channels, out_channels), depth in zip(UpperCamelCase					,						config.depths[1:]      ):
														self.stages.append(ResNetStage(UpperCamelCase					,						UpperCamelCase					,						UpperCamelCase					,						depth=UpperCamelCase      )      )




		def     lowercase       (				self					,						UpperCamelCase					,						UpperCamelCase = False					,						UpperCamelCase = True      ):
								_SCREAMING_SNAKE_CASE				= () if output_hidden_states else None

								for stage_module in self.stages:
														if output_hidden_states:
																				_SCREAMING_SNAKE_CASE				= hidden_states + (hidden_state,)

														_SCREAMING_SNAKE_CASE				= stage_module(UpperCamelCase      )

								if output_hidden_states:
														_SCREAMING_SNAKE_CASE				= hidden_states + (hidden_state,)

								if not return_dict:
														return tuple(v for v in [hidden_state, hidden_states] if v is not None      )

								return BaseModelOutputWithNoAttention(
								    last_hidden_state=UpperCamelCase					,						hidden_states=UpperCamelCase					,						)


class 			lowerCAmelCase (				__UpperCAmelCase			):
		a							:					Optional[Any]       =					ResNetConfig
		a							:					str       =					"""resnet"""
		a							:					List[Any]       =					"""pixel_values"""
		a							:					Optional[int]       =					True




		def     lowercase       (				self					,						UpperCamelCase      ):
								if isinstance(UpperCamelCase					,						nn.Convad      ):
														nn.init.kaiming_normal_(module.weight					,						mode="fan_out"					,						nonlinearity="relu"      )
								elif isinstance(UpperCamelCase					,						(nn.BatchNormad, nn.GroupNorm)      ):
														nn.init.constant_(module.weight					,						1      )
														nn.init.constant_(module.bias					,						0      )




		def     lowercase       (				self					,						UpperCamelCase					,						UpperCamelCase=False      ):
								if isinstance(UpperCamelCase					,						UpperCamelCase      ):
														_SCREAMING_SNAKE_CASE				= value
_snake_case				:   Union[str, Any]     =				R"""
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`ResNetConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
"""

_snake_case				:   Optional[int]     =				R"""
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`ConvNextImageProcessor.__call__`] for details.

        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
"""


@add_start_docstrings(
    """The bare ResNet model outputting raw features without any specific head on top."""		,     __UpperCAmelCase		,     )
class 			lowerCAmelCase (				__UpperCAmelCase			):




		def __init__(				self					,						UpperCamelCase      ):
								super().__init__(UpperCamelCase      )
								_SCREAMING_SNAKE_CASE				= config
								_SCREAMING_SNAKE_CASE				= ResNetEmbeddings(UpperCamelCase      )
								_SCREAMING_SNAKE_CASE				= ResNetEncoder(UpperCamelCase      )
								_SCREAMING_SNAKE_CASE				= nn.AdaptiveAvgPoolad((1, 1)      )
								# Initialize weights and apply final processing
								self.post_init()




		@add_start_docstrings_to_model_forward(UpperCamelCase      )
		@add_code_sample_docstrings(
		    checkpoint=_CHECKPOINT_FOR_DOC					,						output_type=UpperCamelCase					,						config_class=_CONFIG_FOR_DOC					,						modality="vision"					,						expected_output=_EXPECTED_OUTPUT_SHAPE					,						)
		def     lowercase       (				self					,						UpperCamelCase					,						UpperCamelCase = None					,						UpperCamelCase = None      ):
								_SCREAMING_SNAKE_CASE				= (
								    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
								)
								_SCREAMING_SNAKE_CASE				= return_dict if return_dict is not None else self.config.use_return_dict

								_SCREAMING_SNAKE_CASE				= self.embedder(UpperCamelCase      )

								_SCREAMING_SNAKE_CASE				= self.encoder(
								    UpperCamelCase					,						output_hidden_states=UpperCamelCase					,						return_dict=UpperCamelCase      )

								_SCREAMING_SNAKE_CASE				= encoder_outputs[0]

								_SCREAMING_SNAKE_CASE				= self.pooler(UpperCamelCase      )

								if not return_dict:
														return (last_hidden_state, pooled_output) + encoder_outputs[1:]

								return BaseModelOutputWithPoolingAndNoAttention(
								    last_hidden_state=UpperCamelCase					,						pooler_output=UpperCamelCase					,						hidden_states=encoder_outputs.hidden_states					,						)


@add_start_docstrings(
    """
    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    """		,     __UpperCAmelCase		,     )
class 			lowerCAmelCase (				__UpperCAmelCase			):




		def __init__(				self					,						UpperCamelCase      ):
								super().__init__(UpperCamelCase      )
								_SCREAMING_SNAKE_CASE				= config.num_labels
								_SCREAMING_SNAKE_CASE				= ResNetModel(UpperCamelCase      )
								# classification head
								_SCREAMING_SNAKE_CASE				= nn.Sequential(
								    nn.Flatten()					,						nn.Linear(config.hidden_sizes[-1]					,						config.num_labels      ) if config.num_labels > 0 else nn.Identity()					,						)
								# initialize weights and apply final processing
								self.post_init()




		@add_start_docstrings_to_model_forward(UpperCamelCase      )
		@add_code_sample_docstrings(
		    checkpoint=_IMAGE_CLASS_CHECKPOINT					,						output_type=UpperCamelCase					,						config_class=_CONFIG_FOR_DOC					,						expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT					,						)
		def     lowercase       (				self					,						UpperCamelCase = None					,						UpperCamelCase = None					,						UpperCamelCase = None					,						UpperCamelCase = None					,						):
								_SCREAMING_SNAKE_CASE				= return_dict if return_dict is not None else self.config.use_return_dict

								_SCREAMING_SNAKE_CASE				= self.resnet(UpperCamelCase					,						output_hidden_states=UpperCamelCase					,						return_dict=UpperCamelCase      )

								_SCREAMING_SNAKE_CASE				= outputs.pooler_output if return_dict else outputs[1]

								_SCREAMING_SNAKE_CASE				= self.classifier(UpperCamelCase      )

								_SCREAMING_SNAKE_CASE				= None

								if labels is not None:
														if self.config.problem_type is None:
																				if self.num_labels == 1:
																										_SCREAMING_SNAKE_CASE				= "regression"
																				elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
																										_SCREAMING_SNAKE_CASE				= "single_label_classification"
																				else:
																										_SCREAMING_SNAKE_CASE				= "multi_label_classification"
														if self.config.problem_type == "regression":
																				_SCREAMING_SNAKE_CASE				= MSELoss()
																				if self.num_labels == 1:
																										_SCREAMING_SNAKE_CASE				= loss_fct(logits.squeeze()					,						labels.squeeze()      )
																				else:
																										_SCREAMING_SNAKE_CASE				= loss_fct(UpperCamelCase					,						UpperCamelCase      )
														elif self.config.problem_type == "single_label_classification":
																				_SCREAMING_SNAKE_CASE				= CrossEntropyLoss()
																				_SCREAMING_SNAKE_CASE				= loss_fct(logits.view(-1					,						self.num_labels      )					,						labels.view(-1      )      )
														elif self.config.problem_type == "multi_label_classification":
																				_SCREAMING_SNAKE_CASE				= BCEWithLogitsLoss()
																				_SCREAMING_SNAKE_CASE				= loss_fct(UpperCamelCase					,						UpperCamelCase      )

								if not return_dict:
														_SCREAMING_SNAKE_CASE				= (logits,) + outputs[2:]
														return (loss,) + output if loss is not None else output

								return ImageClassifierOutputWithNoAttention(loss=UpperCamelCase					,						logits=UpperCamelCase					,						hidden_states=outputs.hidden_states      )


@add_start_docstrings(
    """
    ResNet backbone, to be used with frameworks like DETR and MaskFormer.
    """		,     __UpperCAmelCase		,     )
class 			lowerCAmelCase (				__UpperCAmelCase		,     __UpperCAmelCase			):




		def __init__(				self					,						UpperCamelCase      ):
								super().__init__(UpperCamelCase      )
								super()._init_backbone(UpperCamelCase      )

								_SCREAMING_SNAKE_CASE				= [config.embedding_size] + config.hidden_sizes
								_SCREAMING_SNAKE_CASE				= ResNetEmbeddings(UpperCamelCase      )
								_SCREAMING_SNAKE_CASE				= ResNetEncoder(UpperCamelCase      )

								# initialize weights and apply final processing
								self.post_init()




		@add_start_docstrings_to_model_forward(UpperCamelCase      )
		@replace_return_docstrings(output_type=UpperCamelCase					,						config_class=_CONFIG_FOR_DOC      )
		def     lowercase       (				self					,						UpperCamelCase					,						UpperCamelCase = None					,						UpperCamelCase = None      ):
								_SCREAMING_SNAKE_CASE				= return_dict if return_dict is not None else self.config.use_return_dict
								_SCREAMING_SNAKE_CASE				= (
								    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
								)

								_SCREAMING_SNAKE_CASE				= self.embedder(UpperCamelCase      )

								_SCREAMING_SNAKE_CASE				= self.encoder(UpperCamelCase					,						output_hidden_states=UpperCamelCase					,						return_dict=UpperCamelCase      )

								_SCREAMING_SNAKE_CASE				= outputs.hidden_states

								_SCREAMING_SNAKE_CASE				= ()
								for idx, stage in enumerate(self.stage_names      ):
														if stage in self.out_features:
																				feature_maps += (hidden_states[idx],)

								if not return_dict:
														_SCREAMING_SNAKE_CASE				= (feature_maps,)
														if output_hidden_states:
																				output += (outputs.hidden_states,)
														return output

								return BackboneOutput(
								    feature_maps=UpperCamelCase					,						hidden_states=outputs.hidden_states if output_hidden_states else None					,						attentions=UpperCamelCase					,						)

'''simple docstring'''







import numpy

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



def 			_a		(	_SCREAMING_SNAKE_CASE		:   List[Any]	,			_SCREAMING_SNAKE_CASE		:   List[Any]="train"  ):
						return calculate_hypothesis_value(_SCREAMING_SNAKE_CASE	,			_SCREAMING_SNAKE_CASE  ) - output(
						    _SCREAMING_SNAKE_CASE	,			_SCREAMING_SNAKE_CASE  )



def 			_a		(	_SCREAMING_SNAKE_CASE		:   List[Any]  ):
						_SCREAMING_SNAKE_CASE				= 0
						for i in range(len(_SCREAMING_SNAKE_CASE  ) - 1  ):
												hyp_val += data_input_tuple[i] * parameter_vector[i + 1]
						hyp_val += parameter_vector[0]
						return hyp_val



def 			_a		(	_SCREAMING_SNAKE_CASE		:   str	,			_SCREAMING_SNAKE_CASE		:   Union[str, Any]  ):
						if data_set == "train":
												return train_data[example_no][1]
						elif data_set == "test":
												return test_data[example_no][1]
						return None



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



def 			_a		(	_SCREAMING_SNAKE_CASE		:   Optional[Any]  ):
						_SCREAMING_SNAKE_CASE				= summation_of_cost_derivative(_SCREAMING_SNAKE_CASE	,			_SCREAMING_SNAKE_CASE  ) / m
						return cost_derivative_value



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



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


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