


'''simple docstring'''
from typing import Optional, Tuple, Union

import tensorflow as tf

from ...activations_tf import ACTaFN
from ...modeling_tf_outputs import (
    TFBaseModelOutputWithNoAttention,
    TFBaseModelOutputWithPoolingAndNoAttention,
    TFImageClassifierOutputWithNoAttention,
)
from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs
from ...tf_utils import shape_list
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_resnet import ResNetConfig


lowerCAmelCase  =				logging.get_logger(__name__)

# General docstring
lowerCAmelCase  =				"""ResNetConfig"""

# Base docstring
lowerCAmelCase  =				"""microsoft/resnet-50"""
lowerCAmelCase  =				[1, 2_048, 7, 7]

# Image classification docstring
lowerCAmelCase  =				"""microsoft/resnet-50"""
lowerCAmelCase  =				"""tiger cat"""

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






class 					lowerCAmelCase_     (    tf.keras.layers.Layer  ):






       def __init__(   self ,    _UpperCamelCase ,    _UpperCamelCase = 3 ,    _UpperCamelCase = 1 ,    _UpperCamelCase = "relu" ,    **_UpperCamelCase      )->			None:
            super().__init__(**_UpperCamelCase      )
            _A				=       kernel_size // 2
            _A				=       tf.keras.layers.ConvaD(
                _UpperCamelCase ,    kernel_size=_UpperCamelCase ,    strides=_UpperCamelCase ,    padding='valid' ,    use_bias=_UpperCamelCase ,    name='convolution'      )
            # Use same default momentum and epsilon as PyTorch equivalent
            _A				=       tf.keras.layers.BatchNormalization(epsilon=1e-5 ,    momentum=0.9 ,    name='normalization'      )
            _A				=       ACTaFN[activation] if activation is not None else tf.keras.layers.Activation('linear'      )






       def      UpperCamelCase	(   self ,    _UpperCamelCase      )->			tf.Tensor:
            # Pad to match that done in the PyTorch Conv2D model
            _A				=       _A				=       (self.pad_value, self.pad_value)
            _A				=       tf.pad(_UpperCamelCase ,    [(0, 0), height_pad, width_pad, (0, 0)]      )
            _A				=       self.conv(_UpperCamelCase      )
            return hidden_state






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase = False      )->			tf.Tensor:
            _A				=       self.convolution(_UpperCamelCase      )
            _A				=       self.normalization(_UpperCamelCase ,    training=_UpperCamelCase      )
            _A				=       self.activation(_UpperCamelCase      )
            return hidden_state






class 					lowerCAmelCase_     (    tf.keras.layers.Layer  ):






       def __init__(   self ,    _UpperCamelCase ,    **_UpperCamelCase      )->			None:
            super().__init__(**_UpperCamelCase      )
            _A				=       TFResNetConvLayer(
                config.embedding_size ,    kernel_size=7 ,    stride=2 ,    activation=config.hidden_act ,    name='embedder' ,    )
            _A				=       tf.keras.layers.MaxPoolaD(pool_size=3 ,    strides=2 ,    padding='valid' ,    name='pooler'      )
            _A				=       config.num_channels






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase = False      )->			tf.Tensor:
            _A		,							_A		,							_A		,							_A				=       shape_list(_UpperCamelCase      )
            if tf.executing_eagerly() and 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.'      )
            _A				=       pixel_values
            _A				=       self.embedder(_UpperCamelCase      )
            _A				=       tf.pad(_UpperCamelCase ,    [[0, 0], [1, 1], [1, 1], [0, 0]]      )
            _A				=       self.pooler(_UpperCamelCase      )
            return hidden_state






class 					lowerCAmelCase_     (    tf.keras.layers.Layer  ):






       def __init__(   self ,    _UpperCamelCase ,    _UpperCamelCase = 2 ,    **_UpperCamelCase      )->			None:
            super().__init__(**_UpperCamelCase      )
            _A				=       tf.keras.layers.ConvaD(
                _UpperCamelCase ,    kernel_size=1 ,    strides=_UpperCamelCase ,    use_bias=_UpperCamelCase ,    name='convolution'      )
            # Use same default momentum and epsilon as PyTorch equivalent
            _A				=       tf.keras.layers.BatchNormalization(epsilon=1e-5 ,    momentum=0.9 ,    name='normalization'      )






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase = False      )->			tf.Tensor:
            _A				=       x
            _A				=       self.convolution(_UpperCamelCase      )
            _A				=       self.normalization(_UpperCamelCase ,    training=_UpperCamelCase      )
            return hidden_state






class 					lowerCAmelCase_     (    tf.keras.layers.Layer  ):






       def __init__(   self ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase = 1 ,    _UpperCamelCase = "relu" ,    **_UpperCamelCase      )->			None:
            super().__init__(**_UpperCamelCase      )
            _A				=       in_channels != out_channels or stride != 1
            _A				=       TFResNetConvLayer(_UpperCamelCase ,    stride=_UpperCamelCase ,    name='layer.0'      )
            _A				=       TFResNetConvLayer(_UpperCamelCase ,    activation=_UpperCamelCase ,    name='layer.1'      )
            _A				=       (
                TFResNetShortCut(_UpperCamelCase ,    stride=_UpperCamelCase ,    name='shortcut'      )
                if should_apply_shortcut
                else tf.keras.layers.Activation('linear' ,    name='shortcut'      )
            )
            _A				=       ACTaFN[activation]






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase = False      )->			tf.Tensor:
            _A				=       hidden_state
            _A				=       self.conva(_UpperCamelCase ,    training=_UpperCamelCase      )
            _A				=       self.conva(_UpperCamelCase ,    training=_UpperCamelCase      )
            _A				=       self.shortcut(_UpperCamelCase ,    training=_UpperCamelCase      )
            hidden_state += residual
            _A				=       self.activation(_UpperCamelCase      )
            return hidden_state






class 					lowerCAmelCase_     (    tf.keras.layers.Layer  ):






       def __init__(   self ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase = 1 ,    _UpperCamelCase = "relu" ,    _UpperCamelCase = 4 ,    **_UpperCamelCase ,    )->			None:
            super().__init__(**_UpperCamelCase      )
            _A				=       in_channels != out_channels or stride != 1
            _A				=       out_channels // reduction
            _A				=       TFResNetConvLayer(_UpperCamelCase ,    kernel_size=1 ,    name='layer.0'      )
            _A				=       TFResNetConvLayer(_UpperCamelCase ,    stride=_UpperCamelCase ,    name='layer.1'      )
            _A				=       TFResNetConvLayer(_UpperCamelCase ,    kernel_size=1 ,    activation=_UpperCamelCase ,    name='layer.2'      )
            _A				=       (
                TFResNetShortCut(_UpperCamelCase ,    stride=_UpperCamelCase ,    name='shortcut'      )
                if should_apply_shortcut
                else tf.keras.layers.Activation('linear' ,    name='shortcut'      )
            )
            _A				=       ACTaFN[activation]






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase = False      )->			tf.Tensor:
            _A				=       hidden_state
            _A				=       self.conva(_UpperCamelCase ,    training=_UpperCamelCase      )
            _A				=       self.conva(_UpperCamelCase ,    training=_UpperCamelCase      )
            _A				=       self.conva(_UpperCamelCase ,    training=_UpperCamelCase      )
            _A				=       self.shortcut(_UpperCamelCase ,    training=_UpperCamelCase      )
            hidden_state += residual
            _A				=       self.activation(_UpperCamelCase      )
            return hidden_state






class 					lowerCAmelCase_     (    tf.keras.layers.Layer  ):






       def __init__(   self ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase = 2 ,    _UpperCamelCase = 2 ,    **_UpperCamelCase      )->			None:
            super().__init__(**_UpperCamelCase      )

            _A				=       TFResNetBottleNeckLayer if config.layer_type == 'bottleneck' else TFResNetBasicLayer

            _A				=       [layer(_UpperCamelCase ,    _UpperCamelCase ,    stride=_UpperCamelCase ,    activation=config.hidden_act ,    name='layers.0'      )]
            layers += [
                layer(_UpperCamelCase ,    _UpperCamelCase ,    activation=config.hidden_act ,    name=F'layers.{i + 1}'      )
                for i in range(depth - 1      )
            ]
            _A				=       layers






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase = False      )->			tf.Tensor:
            for layer in self.stage_layers:
                 _A				=       layer(_UpperCamelCase ,    training=_UpperCamelCase      )
            return hidden_state






class 					lowerCAmelCase_     (    tf.keras.layers.Layer  ):






       def __init__(   self ,    _UpperCamelCase ,    **_UpperCamelCase      )->			None:
            super().__init__(**_UpperCamelCase      )
            # based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input
            _A				=       [
                TFResNetStage(
                    _UpperCamelCase ,    config.embedding_size ,    config.hidden_sizes[0] ,    stride=2 if config.downsample_in_first_stage else 1 ,    depth=config.depths[0] ,    name='stages.0' ,    )
            ]
            for i, (in_channels, out_channels, depth) in enumerate(
                zip(config.hidden_sizes ,    config.hidden_sizes[1:] ,    config.depths[1:]      )      ):
                 self.stages.append(TFResNetStage(_UpperCamelCase ,    _UpperCamelCase ,    _UpperCamelCase ,    depth=_UpperCamelCase ,    name=F'stages.{i + 1}'      )      )






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase = False ,    _UpperCamelCase = True ,    _UpperCamelCase = False ,    )->			TFBaseModelOutputWithNoAttention:
            _A				=       () if output_hidden_states else None

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

                 _A				=       stage_module(_UpperCamelCase ,    training=_UpperCamelCase      )

            if output_hidden_states:
                 _A				=       hidden_states + (hidden_state,)

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

            return TFBaseModelOutputWithNoAttention(last_hidden_state=_UpperCamelCase ,    hidden_states=_UpperCamelCase      )






class 					lowerCAmelCase_     (    UpperCAmelCase  ):
       __UpperCAmelCase				=ResNetConfig
       __UpperCAmelCase				='resnet'
       __UpperCAmelCase				='pixel_values'






       @property
       def      UpperCamelCase	(   self      )->			Union[str, Any]:
            return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224) ,    dtype=tf.floataa      )}






lowerCAmelCase  =				R"""
    This model is a TensorFlow
    [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a
    regular TensorFlow Module and refer to the TensorFlow 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 [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.
"""


lowerCAmelCase  =				R"""
    Args:
        pixel_values (`tf.Tensor` 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.
"""






@keras_serializable
class 					lowerCAmelCase_     (    tf.keras.layers.Layer  ):
       __UpperCAmelCase				=ResNetConfig






       def __init__(   self ,    _UpperCamelCase ,    **_UpperCamelCase      )->			None:
            super().__init__(**_UpperCamelCase      )
            _A				=       config
            _A				=       TFResNetEmbeddings(_UpperCamelCase ,    name='embedder'      )
            _A				=       TFResNetEncoder(_UpperCamelCase ,    name='encoder'      )
            _A				=       tf.keras.layers.GlobalAveragePoolingaD(keepdims=_UpperCamelCase      )






       @unpack_inputs
       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase = None ,    _UpperCamelCase = None ,    _UpperCamelCase = False ,    )->			Union[Tuple[tf.Tensor], TFBaseModelOutputWithPoolingAndNoAttention]:
            _A				=       (
                output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
            )
            _A				=       return_dict if return_dict is not None else self.config.use_return_dict

            # TF 2.0 image layers can't use NCHW format when running on CPU.
            # We transpose to NHWC format and then transpose back after the full forward pass.
            # (batch_size, num_channels, height, width) -> (batch_size, height, width, num_channels)
            _A				=       tf.transpose(_UpperCamelCase ,    perm=[0, 2, 3, 1]      )
            _A				=       self.embedder(_UpperCamelCase ,    training=_UpperCamelCase      )

            _A				=       self.encoder(
                _UpperCamelCase ,    output_hidden_states=_UpperCamelCase ,    return_dict=_UpperCamelCase ,    training=_UpperCamelCase      )

            _A				=       encoder_outputs[0]

            _A				=       self.pooler(_UpperCamelCase      )

            # Transpose all the outputs to the NCHW format
            # (batch_size, height, width, num_channels) -> (batch_size, num_channels, height, width)
            _A				=       tf.transpose(_UpperCamelCase ,    (0, 3, 1, 2)      )
            _A				=       tf.transpose(_UpperCamelCase ,    (0, 3, 1, 2)      )
            _A				=       ()
            for hidden_state in encoder_outputs[1:]:
                 _A				=       hidden_states + tuple(tf.transpose(_UpperCamelCase ,    (0, 3, 1, 2)      ) for h in hidden_state      )

            if not return_dict:
                 return (last_hidden_state, pooled_output) + hidden_states

            _A				=       hidden_states if output_hidden_states else None

            return TFBaseModelOutputWithPoolingAndNoAttention(
                last_hidden_state=_UpperCamelCase ,    pooler_output=_UpperCamelCase ,    hidden_states=_UpperCamelCase ,    )






@add_start_docstrings(
    'The bare ResNet model outputting raw features without any specific head on top.'			,       UpperCAmelCase			,       )
class 					lowerCAmelCase_     (    UpperCAmelCase  ):






       def __init__(   self ,    _UpperCamelCase ,    **_UpperCamelCase      )->			None:
            super().__init__(_UpperCamelCase ,    **_UpperCamelCase      )
            _A				=       TFResNetMainLayer(config=_UpperCamelCase ,    name='resnet'      )






       @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 ,    )
       @unpack_inputs
       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase = None ,    _UpperCamelCase = None ,    _UpperCamelCase = False ,    )->			Union[Tuple[tf.Tensor], TFBaseModelOutputWithPoolingAndNoAttention]:
            _A				=       (
                output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
            )
            _A				=       return_dict if return_dict is not None else self.config.use_return_dict

            _A				=       self.resnet(
                pixel_values=_UpperCamelCase ,    output_hidden_states=_UpperCamelCase ,    return_dict=_UpperCamelCase ,    training=_UpperCamelCase ,    )
            return resnet_outputs






@add_start_docstrings(
    '\n    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    '			,       UpperCAmelCase			,       )
class 					lowerCAmelCase_     (    UpperCAmelCase			,       UpperCAmelCase  ):






       def __init__(   self ,    _UpperCamelCase ,    **_UpperCamelCase      )->			None:
            super().__init__(_UpperCamelCase ,    **_UpperCamelCase      )
            _A				=       config.num_labels
            _A				=       TFResNetMainLayer(_UpperCamelCase ,    name='resnet'      )
            # classification head
            _A				=       (
                tf.keras.layers.Dense(config.num_labels ,    name='classifier.1'      )
                if config.num_labels > 0
                else tf.keras.layers.Activation('linear' ,    name='classifier.1'      )
            )






       def      UpperCamelCase	(   self ,    _UpperCamelCase      )->			tf.Tensor:
            _A				=       tf.keras.layers.Flatten()(_UpperCamelCase      )
            _A				=       self.classifier_layer(_UpperCamelCase      )
            return logits






       @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 ,    )
       @unpack_inputs
       def      UpperCamelCase	(   self ,    _UpperCamelCase = None ,    _UpperCamelCase = None ,    _UpperCamelCase = None ,    _UpperCamelCase = None ,    _UpperCamelCase = False ,    )->			Union[Tuple[tf.Tensor], TFImageClassifierOutputWithNoAttention]:
            _A				=       return_dict if return_dict is not None else self.config.use_return_dict

            _A				=       self.resnet(
                _UpperCamelCase ,    output_hidden_states=_UpperCamelCase ,    return_dict=_UpperCamelCase ,    training=_UpperCamelCase      )

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

            _A				=       self.classifier(_UpperCamelCase      )

            _A				=       None if labels is None else self.hf_compute_loss(_UpperCamelCase ,    _UpperCamelCase      )

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

            return TFImageClassifierOutputWithNoAttention(loss=_UpperCamelCase ,    logits=_UpperCamelCase ,    hidden_states=outputs.hidden_states      )









'''simple docstring'''
import numpy as np
import requests

def 				lowerCamelCase_   (   )				->  int:







     """simple docstring"""
     _A				=       requests.get(
         'https://raw.githubusercontent.com/yashLadha/The_Math_of_Intelligence/'
         'master/Week1/ADRvsRating.csv'		)
     _A				=       response.text.splitlines()
     _A				=       []
     for item in lines:
          _A				=       item.split(','		)
          data.append(__UpperCamelCase		)
     data.pop(0		)  # This is for removing the labels from the list
     _A				=       np.matrix(__UpperCamelCase		)
     return dataset

def 				lowerCamelCase_   (   __UpperCamelCase				:					Union[str, Any]       ,						__UpperCamelCase				:					Tuple       ,						__UpperCamelCase				:					int       ,						__UpperCamelCase				:					Tuple       ,						__UpperCamelCase				:					Dict		)				->  int:







     """simple docstring"""
     _A				=       len_data

     _A				=       np.dot(__UpperCamelCase       ,						data_x.transpose()		)
     prod -= data_y.transpose()
     _A				=       np.dot(__UpperCamelCase       ,						__UpperCamelCase		)
     _A				=       theta - (alpha / n) * sum_grad
     return theta

def 				lowerCamelCase_   (   __UpperCamelCase				:					Optional[int]       ,						__UpperCamelCase				:					Any       ,						__UpperCamelCase				:					List[str]       ,						__UpperCamelCase				:					Optional[int]		)				->  Dict:







     """simple docstring"""
     _A				=       np.dot(__UpperCamelCase       ,						data_x.transpose()		)
     prod -= data_y.transpose()
     _A				=       np.sum(np.square(__UpperCamelCase		)		)
     _A				=       sum_elem / (2 * len_data)
     return error

def 				lowerCamelCase_   (   __UpperCamelCase				:					Dict       ,						__UpperCamelCase				:					List[str]		)				->  int:







     """simple docstring"""
     _A				=       1_0_0_0_0_0
     _A				=       0.0_00_15_50

     _A				=       data_x.shape[1]
     _A				=       data_x.shape[0] - 1

     _A				=       np.zeros((1, no_features)		)

     for i in range(0       ,						__UpperCamelCase		):
          _A				=       run_steep_gradient_descent(__UpperCamelCase       ,						__UpperCamelCase       ,						__UpperCamelCase       ,						__UpperCamelCase       ,						__UpperCamelCase		)
          _A				=       sum_of_square_error(__UpperCamelCase       ,						__UpperCamelCase       ,						__UpperCamelCase       ,						__UpperCamelCase		)
          print(F'At Iteration {i + 1} - Error is {error:.5f}'		)

     return theta

def 				lowerCamelCase_   (   __UpperCamelCase				:					Dict       ,						__UpperCamelCase				:					Tuple		)				->  Union[str, Any]:







     """simple docstring"""
     _A				=       sum(abs(y - predicted_y[i]		) for i, y in enumerate(__UpperCamelCase		)		)
     return total / len(__UpperCamelCase		)

def 				lowerCamelCase_   (   )				->  Optional[Any]:







     """simple docstring"""
     _A				=       collect_dataset()

     _A				=       data.shape[0]
     _A				=       np.c_[np.ones(__UpperCamelCase		), data[:, :-1]].astype(__UpperCamelCase		)
     _A				=       data[:, -1].astype(__UpperCamelCase		)

     _A				=       run_linear_regression(__UpperCamelCase       ,						__UpperCamelCase		)
     _A				=       theta.shape[1]
     print('Resultant Feature vector : '		)
     for i in range(0       ,						__UpperCamelCase		):
          print(F'{theta[0, i]:.5f}'		)


if __name__ == "__main__":
    main()





