





'''simple docstring'''

import numpy as np
from matplotlib import pyplot as plt




def  _a     (				lowerCAmelCase_						):





      """simple docstring"""



      return 1 / (1 + np.exp(-1 * x						))





class lowerCamelCase    :







  def __init__(		self  ,  lowercase__  ,  lowercase__=None  ,  lowercase__=None  ,  lowercase__=False		)		->    Optional[int]:






        """simple docstring"""





        _snake_case		:						Optional[int]         =  units
        _snake_case		:						Dict         =  None
        _snake_case		:						Union[str, Any]         =  None
        _snake_case		:						Any         =  activation
        if learning_rate is None:
              _snake_case		:						Tuple         =  0.3
        _snake_case		:						List[Any]         =  learning_rate
        _snake_case		:						Dict         =  is_input_layer







  def 					UpperCAmelCase_					(		self  ,  lowercase__		)		->    Tuple:






        """simple docstring"""





        _snake_case		:						Optional[int]         =  np.asmatrix(np.random.normal(0  ,  0.5  ,  (self.units, back_units)		)		)
        _snake_case		:						Tuple         =  np.asmatrix(np.random.normal(0  ,  0.5  ,  self.units		)		).T
        if self.activation is None:
              _snake_case		:						int         =  sigmoid







  def 					UpperCAmelCase_					(		self		)		->    str:






        """simple docstring"""





        if self.activation == sigmoid:
              _snake_case		:						str         =  np.dot(self.output  ,  (1 - self.output).T		)
              _snake_case		:						Any         =  np.diag(np.diag(lowercase__		)		)
        else:
              _snake_case		:						Optional[Any]         =  1
        return gradient_activation







  def 					UpperCAmelCase_					(		self  ,  lowercase__		)		->    Dict:






        """simple docstring"""





        _snake_case		:						int         =  xdata
        if self.is_input_layer:
              # input layer
              _snake_case		:						int         =  xdata
              _snake_case		:						Any         =  xdata
              return xdata
        else:
              _snake_case		:						Dict         =  np.dot(self.weight  ,  self.xdata		) - self.bias
              _snake_case		:						str         =  self.activation(self.wx_plus_b		)
              return self.output







  def 					UpperCAmelCase_					(		self  ,  lowercase__		)		->    Union[str, Any]:






        """simple docstring"""





        _snake_case		:						List[Any]         =  self.cal_gradient()  # i * i 维
        _snake_case		:						int         =  np.asmatrix(np.dot(gradient.T  ,  lowercase__		)		)

        _snake_case		:						Any         =  np.asmatrix(self.xdata		)
        _snake_case		:						List[Any]         =  -1
        _snake_case		:						Optional[Any]         =  self.weight

        _snake_case		:						Dict         =  np.dot(gradient.T  ,  self._gradient_weight.T		)
        _snake_case		:						Optional[int]         =  gradient * self._gradient_bias
        _snake_case		:						int         =  np.dot(lowercase__  ,  self._gradient_x		).T
        # upgrade: the Negative gradient direction
        _snake_case		:						str         =  self.weight - self.learn_rate * self.gradient_weight
        _snake_case		:						Tuple         =  self.bias - self.learn_rate * self.gradient_bias.T
        # updates the weights and bias according to learning rate (0.3 if undefined)
        return self.gradient





class lowerCamelCase    :







  def __init__(		self		)		->    int:






        """simple docstring"""





        _snake_case		:						int         =  []
        _snake_case		:						str         =  []
        _snake_case		:						int         =  plt.figure()
        _snake_case		:						str         =  self.fig_loss.add_subplot(1  ,  1  ,  1		)







  def 					UpperCAmelCase_					(		self  ,  lowercase__		)		->    Union[str, Any]:






        """simple docstring"""





        self.layers.append(lowercase__		)







  def 					UpperCAmelCase_					(		self		)		->    Dict:






        """simple docstring"""





        for i, layer in enumerate(self.layers[:]		):
              if i < 1:
                    _snake_case		:						str         =  True
              else:
                    layer.initializer(self.layers[i - 1].units		)







  def 					UpperCAmelCase_					(		self		)		->    Dict:






        """simple docstring"""





        for i, layer in enumerate(self.layers[:]		):
              print(F'''------- layer {i} -------'''		)
              print('''weight.shape '''  ,  np.shape(layer.weight		)		)
              print('''bias.shape '''  ,  np.shape(layer.bias		)		)







  def 					UpperCAmelCase_					(		self  ,  lowercase__  ,  lowercase__  ,  lowercase__  ,  lowercase__		)		->    Dict:






        """simple docstring"""





        _snake_case		:						Optional[Any]         =  train_round
        _snake_case		:						Dict         =  accuracy

        self.ax_loss.hlines(self.accuracy  ,  0  ,  self.train_round * 1.1		)

        _snake_case		:						List[Any]         =  np.shape(lowercase__		)
        for _ in range(lowercase__		):
              _snake_case		:						Tuple         =  0
              for row in range(x_shape[0]		):
                    _snake_case		:						str         =  np.asmatrix(xdata[row, :]		).T
                    _snake_case		:						Any         =  np.asmatrix(ydata[row, :]		).T

                    # forward propagation
                    for layer in self.layers:
                          _snake_case		:						str         =  layer.forward_propagation(_xdata		)

                    _snake_case  ,				_snake_case		:						int         =  self.cal_loss(_ydata  ,  _xdata		)
                    _snake_case		:						Any         =  all_loss + loss

                    # back propagation: the input_layer does not upgrade
                    for layer in self.layers[:0:-1]:
                          _snake_case		:						int         =  layer.back_propagation(lowercase__		)

              _snake_case		:						Any         =  all_loss / x_shape[0]
              self.train_mse.append(lowercase__		)

              self.plot_loss()

              if mse < self.accuracy:
                    print('''----达到精度----'''		)
                    return mse
        return None







  def 					UpperCAmelCase_					(		self  ,  lowercase__  ,  lowercase__		)		->    Dict:






        """simple docstring"""





        _snake_case		:						Optional[Any]         =  np.sum(np.power((ydata - ydata_)  ,  2		)		)
        _snake_case		:						Optional[int]         =  2 * (ydata_ - ydata)
        # vector (shape is the same as _ydata.shape)
        return self.loss, self.loss_gradient







  def 					UpperCAmelCase_					(		self		)		->    List[str]:






        """simple docstring"""





        if self.ax_loss.lines:
              self.ax_loss.lines.remove(self.ax_loss.lines[0]		)
        self.ax_loss.plot(self.train_mse  ,  '''r-'''		)
        plt.ion()
        plt.xlabel('''step'''		)
        plt.ylabel('''loss'''		)
        plt.show()
        plt.pause(0.1		)




def  _a     (				):





      """simple docstring"""



      _snake_case		:						Optional[int]         =  np.random.randn(10      ,   10						)
      _snake_case		:						str         =  np.asarray(
          [
              [0.8, 0.4],
              [0.4, 0.3],
              [0.34, 0.45],
              [0.67, 0.32],
              [0.88, 0.67],
              [0.78, 0.77],
              [0.55, 0.66],
              [0.55, 0.43],
              [0.54, 0.1],
              [0.1, 0.5],
          ]						)
      _snake_case		:						Any         =  BPNN()
      for i in (10, 20, 30, 2):
            model.add_layer(DenseLayer(lowerCAmelCase_						)						)
      model.build()
      model.summary()
      model.train(xdata=lowerCAmelCase_      ,   ydata=lowerCAmelCase_      ,   train_round=100      ,   accuracy=0.01						)


if __name__ == "__main__":
   example()









'''simple docstring'''

from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import ConvNextConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import tensorflow as tf

   from transformers import TFConvNextForImageClassification, TFConvNextModel


if is_vision_available():
   from PIL import Image

   from transformers import ConvNextImageProcessor





class lowerCamelCase    :







  def __init__(		self  ,  lowercase__  ,  lowercase__=13  ,  lowercase__=32  ,  lowercase__=3  ,  lowercase__=4  ,  lowercase__=[10, 20, 30, 40]  ,  lowercase__=[2, 2, 3, 2]  ,  lowercase__=True  ,  lowercase__=True  ,  lowercase__=37  ,  lowercase__="gelu"  ,  lowercase__=10  ,  lowercase__=0.02  ,  lowercase__=3  ,  lowercase__=None  ,  )		->    Any:






        """simple docstring"""





        _snake_case		:						str         =  parent
        _snake_case		:						Any         =  batch_size
        _snake_case		:						Optional[Any]         =  image_size
        _snake_case		:						List[Any]         =  num_channels
        _snake_case		:						int         =  num_stages
        _snake_case		:						Any         =  hidden_sizes
        _snake_case		:						Dict         =  depths
        _snake_case		:						List[Any]         =  is_training
        _snake_case		:						Union[str, Any]         =  use_labels
        _snake_case		:						Dict         =  intermediate_size
        _snake_case		:						List[str]         =  hidden_act
        _snake_case		:						Optional[int]         =  type_sequence_label_size
        _snake_case		:						str         =  initializer_range
        _snake_case		:						Dict         =  scope







  def 					UpperCAmelCase_					(		self		)		->    List[str]:






        """simple docstring"""





        _snake_case		:						Dict         =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

        _snake_case		:						int         =  None
        if self.use_labels:
              _snake_case		:						Optional[int]         =  ids_tensor([self.batch_size]  ,  self.type_sequence_label_size		)

        _snake_case		:						Union[str, Any]         =  self.get_config()

        return config, pixel_values, labels







  def 					UpperCAmelCase_					(		self		)		->    Union[str, Any]:






        """simple docstring"""





        return ConvNextConfig(
            num_channels=self.num_channels  ,  hidden_sizes=self.hidden_sizes  ,  depths=self.depths  ,  num_stages=self.num_stages  ,  hidden_act=self.hidden_act  ,  is_decoder=lowercase__  ,  initializer_range=self.initializer_range  ,  )







  def 					UpperCAmelCase_					(		self  ,  lowercase__  ,  lowercase__  ,  lowercase__		)		->    int:






        """simple docstring"""





        _snake_case		:						Tuple         =  TFConvNextModel(config=lowercase__		)
        _snake_case		:						Optional[int]         =  model(lowercase__  ,  training=lowercase__		)
        # expected last hidden states: B, C, H // 32, W // 32
        self.parent.assertEqual(
            result.last_hidden_state.shape  ,  (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)  ,  )







  def 					UpperCAmelCase_					(		self  ,  lowercase__  ,  lowercase__  ,  lowercase__		)		->    Optional[Any]:






        """simple docstring"""





        _snake_case		:						int         =  self.type_sequence_label_size
        _snake_case		:						Optional[int]         =  TFConvNextForImageClassification(lowercase__		)
        _snake_case		:						Dict         =  model(lowercase__  ,  labels=lowercase__  ,  training=lowercase__		)
        self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.type_sequence_label_size)		)







  def 					UpperCAmelCase_					(		self		)		->    List[str]:






        """simple docstring"""





        _snake_case		:						Union[str, Any]         =  self.prepare_config_and_inputs()
        _snake_case  ,				_snake_case  ,				_snake_case		:						Union[str, Any]         =  config_and_inputs
        _snake_case		:						Optional[Any]         =  {'''pixel_values''': pixel_values}
        return config, inputs_dict





@require_tf
class lowerCamelCase    (a__    ,   a__    ,   unittest.TestCase       ):
  _lowercase	:						str							       =					(TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
  _lowercase	:						List[str]							       =					(
      {"""feature-extraction""": TFConvNextModel, """image-classification""": TFConvNextForImageClassification}
      if is_tf_available()
      else {}
  )

  _lowercase	:						Optional[Any]							       =					False
  _lowercase	:						str							       =					False
  _lowercase	:						Tuple							       =					False
  _lowercase	:						Union[str, Any]							       =					False
  _lowercase	:						Dict							       =					False







  def 					UpperCAmelCase_					(		self		)		->    Any:






        """simple docstring"""





        _snake_case		:						List[str]         =  TFConvNextModelTester(self		)
        _snake_case		:						Tuple         =  ConfigTester(
            self  ,  config_class=lowercase__  ,  has_text_modality=lowercase__  ,  hidden_size=37  ,  )







  @unittest.skip(reason='''ConvNext does not use inputs_embeds'''		)
  def 					UpperCAmelCase_					(		self		)		->    int:






        """simple docstring"""





        pass







  @unittest.skipIf(
      not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''		)		) == 0  ,  reason='''TF does not support backprop for grouped convolutions on CPU.'''  ,  )
  @slow
  def 					UpperCAmelCase_					(		self		)		->    int:






        """simple docstring"""





        super().test_keras_fit()







  @unittest.skip(reason='''ConvNext does not support input and output embeddings'''		)
  def 					UpperCAmelCase_					(		self		)		->    Any:






        """simple docstring"""





        pass







  def 					UpperCAmelCase_					(		self		)		->    Any:






        """simple docstring"""





        _snake_case  ,				_snake_case		:						Dict         =  self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
              _snake_case		:						int         =  model_class(lowercase__		)
              _snake_case		:						Dict         =  inspect.signature(model.call		)
              # signature.parameters is an OrderedDict => so arg_names order is deterministic
              _snake_case		:						Optional[int]         =  [*signature.parameters.keys()]

              _snake_case		:						int         =  ['''pixel_values''']
              self.assertListEqual(arg_names[:1]  ,  lowercase__		)







  def 					UpperCAmelCase_					(		self		)		->    str:






        """simple docstring"""





        _snake_case		:						Dict         =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowercase__		)







  @unittest.skipIf(
      not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''		)		) == 0  ,  reason='''TF does not support backprop for grouped convolutions on CPU.'''  ,  )
  def 					UpperCAmelCase_					(		self		)		->    Optional[Any]:






        """simple docstring"""





        super().test_dataset_conversion()







  def 					UpperCAmelCase_					(		self		)		->    List[str]:






        """simple docstring"""





        def check_hidden_states_output(lowercase__  ,  lowercase__  ,  lowercase__		):
              _snake_case		:						List[Any]         =  model_class(lowercase__		)

              _snake_case		:						Dict         =  model(**self._prepare_for_class(lowercase__  ,  lowercase__		)		)
              _snake_case		:						str         =  outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

              _snake_case		:						List[Any]         =  self.model_tester.num_stages
              self.assertEqual(len(lowercase__		)  ,  expected_num_stages + 1		)

              # ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
              self.assertListEqual(
                  list(hidden_states[0].shape[-2:]		)  ,  [self.model_tester.image_size // 4, self.model_tester.image_size // 4]  ,  )

        _snake_case  ,				_snake_case		:						Optional[Any]         =  self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
              _snake_case		:						int         =  True
              check_hidden_states_output(lowercase__  ,  lowercase__  ,  lowercase__		)

              # check that output_hidden_states also work using config
              del inputs_dict["output_hidden_states"]
              _snake_case		:						Any         =  True

              check_hidden_states_output(lowercase__  ,  lowercase__  ,  lowercase__		)







  def 					UpperCAmelCase_					(		self		)		->    Any:






        """simple docstring"""





        _snake_case  ,				_snake_case		:						Dict         =  self.model_tester.prepare_config_and_inputs_for_common()

        def check_equivalence(lowercase__  ,  lowercase__  ,  lowercase__  ,  lowercase__={}		):
              _snake_case		:						Tuple         =  model(lowercase__  ,  return_dict=lowercase__  ,  **lowercase__		)
              _snake_case		:						Any         =  model(lowercase__  ,  return_dict=lowercase__  ,  **lowercase__		).to_tuple()

              def recursive_check(lowercase__  ,  lowercase__		):
                    if isinstance(lowercase__  ,  (List, Tuple)		):
                          for tuple_iterable_value, dict_iterable_value in zip(lowercase__  ,  lowercase__		):
                                recursive_check(lowercase__  ,  lowercase__		)
                    elif tuple_object is None:
                          return
                    else:
                          self.assertTrue(
                              all(tf.equal(lowercase__  ,  lowercase__		)		)  ,  msg=(
                                  '''Tuple and dict output are not equal. Difference:'''
                                  F''' {tf.math.reduce_max(tf.abs(tuple_object - dict_object		)		)}'''
                              )  ,  )

                    recursive_check(lowercase__  ,  lowercase__		)

        for model_class in self.all_model_classes:
              _snake_case		:						Dict         =  model_class(lowercase__		)

              _snake_case		:						Union[str, Any]         =  self._prepare_for_class(lowercase__  ,  lowercase__		)
              _snake_case		:						str         =  self._prepare_for_class(lowercase__  ,  lowercase__		)
              check_equivalence(lowercase__  ,  lowercase__  ,  lowercase__		)

              _snake_case		:						List[Any]         =  self._prepare_for_class(lowercase__  ,  lowercase__  ,  return_labels=lowercase__		)
              _snake_case		:						Any         =  self._prepare_for_class(lowercase__  ,  lowercase__  ,  return_labels=lowercase__		)
              check_equivalence(lowercase__  ,  lowercase__  ,  lowercase__		)

              _snake_case		:						List[str]         =  self._prepare_for_class(lowercase__  ,  lowercase__		)
              _snake_case		:						Union[str, Any]         =  self._prepare_for_class(lowercase__  ,  lowercase__		)
              check_equivalence(lowercase__  ,  lowercase__  ,  lowercase__  ,  {'''output_hidden_states''': True}		)

              _snake_case		:						Optional[int]         =  self._prepare_for_class(lowercase__  ,  lowercase__  ,  return_labels=lowercase__		)
              _snake_case		:						Any         =  self._prepare_for_class(lowercase__  ,  lowercase__  ,  return_labels=lowercase__		)
              check_equivalence(lowercase__  ,  lowercase__  ,  lowercase__  ,  {'''output_hidden_states''': True}		)







  def 					UpperCAmelCase_					(		self		)		->    Union[str, Any]:






        """simple docstring"""





        _snake_case		:						str         =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*lowercase__		)







  @slow
  def 					UpperCAmelCase_					(		self		)		->    Optional[Any]:






        """simple docstring"""





        _snake_case		:						List[Any]         =  TFConvNextModel.from_pretrained('''facebook/convnext-tiny-224'''		)
        self.assertIsNotNone(lowercase__		)




def  _a     (				):





      """simple docstring"""



      _snake_case		:						List[str]         =  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
      return image





@require_tf
@require_vision
class lowerCamelCase    (unittest.TestCase       ):







  @cached_property
  def 					UpperCAmelCase_					(		self		)		->    Optional[int]:






        """simple docstring"""





        return ConvNextImageProcessor.from_pretrained('''facebook/convnext-tiny-224'''		) if is_vision_available() else None







  @slow
  def 					UpperCAmelCase_					(		self		)		->    Any:






        """simple docstring"""





        _snake_case		:						Dict         =  TFConvNextForImageClassification.from_pretrained('''facebook/convnext-tiny-224'''		)

        _snake_case		:						str         =  self.default_image_processor
        _snake_case		:						Optional[int]         =  prepare_img()
        _snake_case		:						str         =  image_processor(images=lowercase__  ,  return_tensors='''tf'''		)

        # forward pass
        _snake_case		:						Union[str, Any]         =  model(**lowercase__		)

        # verify the logits
        _snake_case		:						Union[str, Any]         =  tf.TensorShape((1, 1_000)		)
        self.assertEqual(outputs.logits.shape  ,  lowercase__		)

        _snake_case		:						List[str]         =  tf.constant([-0.0_260, -0.4_739, 0.1_911]		)

        tf.debugging.assert_near(outputs.logits[0, :3]  ,  lowercase__  ,  atol=1E-4		)


