



import numpy as np
from matplotlib import pyplot as plt







def  _snake_case			(							__snake_case     )  ->  Dict:






      '''simple docstring'''







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


class 	snake_case_			:




       """simple docstring"""






       def __init__(  self  ,lowercase  ,lowercase=None  ,lowercase=None  ,lowercase=False):

             """simple docstring"""





             UpperCAmelCase_	:						List[Any]					   =		units
             UpperCAmelCase_	:						int					   =		None
             UpperCAmelCase_	:						int					   =		None
             UpperCAmelCase_	:						Union[str, Any]					   =		activation
             if learning_rate is None:
                   UpperCAmelCase_	:						Any					   =		0.3
             UpperCAmelCase_	:						List[Any]					   =		learning_rate
             UpperCAmelCase_	:						str					   =		is_input_layer



       def 			A_ (  self  ,lowercase):

             """simple docstring"""





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



       def 			A_ (  self):

             """simple docstring"""





             if self.activation == sigmoid:
                   UpperCAmelCase_	:						Optional[int]					   =		np.dot(self.output  ,(1 - self.output).T)
                   UpperCAmelCase_	:						List[Any]					   =		np.diag(np.diag(lowercase))
             else:
                   UpperCAmelCase_	:						Union[str, Any]					   =		1
             return gradient_activation



       def 			A_ (  self  ,lowercase):

             """simple docstring"""





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



       def 			A_ (  self  ,lowercase):

             """simple docstring"""





             UpperCAmelCase_	:						Dict					   =		self.cal_gradient()  # i * i 维
             UpperCAmelCase_	:						Tuple					   =		np.asmatrix(np.dot(gradient.T  ,lowercase))

             UpperCAmelCase_	:						int					   =		np.asmatrix(self.xdata)
             UpperCAmelCase_	:						Union[str, Any]					   =		-1
             UpperCAmelCase_	:						str					   =		self.weight

             UpperCAmelCase_	:						List[str]					   =		np.dot(gradient.T  ,self._gradient_weight.T)
             UpperCAmelCase_	:						Any					   =		gradient * self._gradient_bias
             UpperCAmelCase_	:						Dict					   =		np.dot(lowercase  ,self._gradient_x).T
             # upgrade: the Negative gradient direction
             UpperCAmelCase_	:						List[str]					   =		self.weight - self.learn_rate * self.gradient_weight
             UpperCAmelCase_	:						Union[str, Any]					   =		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 	snake_case_			:




       """simple docstring"""






       def __init__(  self):

             """simple docstring"""





             UpperCAmelCase_	:						List[Any]					   =		[]
             UpperCAmelCase_	:						Union[str, Any]					   =		[]
             UpperCAmelCase_	:						List[str]					   =		plt.figure()
             UpperCAmelCase_	:						Optional[int]					   =		self.fig_loss.add_subplot(1  ,1  ,1)



       def 			A_ (  self  ,lowercase):

             """simple docstring"""





             self.layers.append(lowercase)



       def 			A_ (  self):

             """simple docstring"""





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



       def 			A_ (  self):

             """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 			A_ (  self  ,lowercase  ,lowercase  ,lowercase  ,lowercase):

             """simple docstring"""





             UpperCAmelCase_	:						Tuple					   =		train_round
             UpperCAmelCase_	:						Optional[int]					   =		accuracy

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

             UpperCAmelCase_	:						Dict					   =		np.shape(lowercase)
             for _ in range(lowercase):
                   UpperCAmelCase_	:						List[str]					   =		0
                   for row in range(x_shape[0]):
                         UpperCAmelCase_	:						List[str]					   =		np.asmatrix(xdata[row, :]).T
                         UpperCAmelCase_	:						Optional[int]					   =		np.asmatrix(ydata[row, :]).T

                         # forward propagation
                         for layer in self.layers:
                               UpperCAmelCase_	:						Union[str, Any]					   =		layer.forward_propagation(_xdata)

                         UpperCAmelCase_   ,   UpperCAmelCase_	:						int					   =		self.cal_loss(_ydata  ,_xdata)
                         UpperCAmelCase_	:						Dict					   =		all_loss + loss

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

                   UpperCAmelCase_	:						Optional[Any]					   =		all_loss / x_shape[0]
                   self.train_mse.append(lowercase)

                   self.plot_loss()

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



       def 			A_ (  self  ,lowercase  ,lowercase):

             """simple docstring"""





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



       def 			A_ (  self):

             """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  _snake_case			(							)  ->  int:






      '''simple docstring'''







      UpperCAmelCase_	:						int					   =		np.random.randn(1_0       ,	1_0     )
      UpperCAmelCase_	:						Union[str, Any]					   =		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],
          ]     )
      UpperCAmelCase_	:						int					   =		BPNN()
      for i in (1_0, 2_0, 3_0, 2):
            model.add_layer(DenseLayer(__snake_case     )     )
      model.build()
      model.summary()
      model.train(xdata=__snake_case       ,	ydata=__snake_case       ,	train_round=1_0_0       ,	accuracy=0.01     )


if __name__ == "__main__":
   example()





import logging
import os
import sys
from dataclasses import dataclass, field
from typing import Optional

import transformers
from transformers import (
    AutoConfig,
    AutoModelForQuestionAnswering,
    AutoTokenizer,
    DataCollatorWithPadding,
    HfArgumentParser,
    SquadDataset,
    Trainer,
    TrainingArguments,
)
from transformers import SquadDataTrainingArguments as DataTrainingArguments
from transformers.trainer_utils import is_main_process


__lowerCamelCase								=      logging.getLogger(__name__)


@dataclass
class 	snake_case_			:




       """simple docstring"""






       _lowerCamelCase           =					field(
           metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""}	)
       _lowerCamelCase           =					field(
           default=lowercase__			,			metadata={"""help""": """Pretrained config name or path if not the same as model_name"""}	)
       _lowerCamelCase           =					field(
           default=lowercase__			,			metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""}	)
       _lowerCamelCase           =					field(default=lowercase__			,			metadata={"""help""": """Set this flag to use fast tokenization."""}	)
       # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script,
       # or just modify its tokenizer_config.json.
       _lowerCamelCase           =					field(
           default=lowercase__			,			metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""}			,			)







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






      '''simple docstring'''







      UpperCAmelCase_	:						Union[str, Any]					   =		HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)     )

      if len(sys.argv     ) == 2 and sys.argv[1].endswith(".json"     ):
            # If we pass only one argument to the script and it's the path to a json file,
            # let's parse it to get our arguments.
            UpperCAmelCase_   ,   UpperCAmelCase_   ,   UpperCAmelCase_	:						Any					   =		parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]     )     )
      else:
            UpperCAmelCase_   ,   UpperCAmelCase_   ,   UpperCAmelCase_	:						Union[str, Any]					   =		parser.parse_args_into_dataclasses()

      if (
          os.path.exists(training_args.output_dir     )
          and os.listdir(training_args.output_dir     )
          and training_args.do_train
          and not training_args.overwrite_output_dir
      ):
            raise ValueError(
                F"""Output directory ({training_args.output_dir}) already exists and is not empty. Use"""
                " --overwrite_output_dir to overcome."     )

      # Setup logging
      logging.basicConfig(
          format="%(asctime)s - %(levelname)s - %(name)s - %(message)s"       ,	datefmt="%m/%d/%Y %H:%M:%S"       ,	level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN       ,	)
      logger.warning(
          "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s"       ,	training_args.local_rank       ,	training_args.device       ,	training_args.n_gpu       ,	bool(training_args.local_rank != -1     )       ,	training_args.fpaa       ,	)
      # Set the verbosity to info of the Transformers logger (on main process only):
      if is_main_process(training_args.local_rank     ):
            transformers.utils.logging.set_verbosity_info()
            transformers.utils.logging.enable_default_handler()
            transformers.utils.logging.enable_explicit_format()
      logger.info("Training/evaluation parameters %s"       ,	__snake_case     )

      # Prepare Question-Answering task
      # Load pretrained model and tokenizer
      #
      # Distributed training:
      # The .from_pretrained methods guarantee that only one local process can concurrently
      # download model & vocab.

      UpperCAmelCase_	:						Any					   =		AutoConfig.from_pretrained(
          model_args.config_name if model_args.config_name else model_args.model_name_or_path       ,	cache_dir=model_args.cache_dir       ,	)
      UpperCAmelCase_	:						List[str]					   =		AutoTokenizer.from_pretrained(
          model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path       ,	cache_dir=model_args.cache_dir       ,	use_fast=__snake_case       ,	)
      UpperCAmelCase_	:						Optional[int]					   =		AutoModelForQuestionAnswering.from_pretrained(
          model_args.model_name_or_path       ,	from_tf=bool(".ckpt" in model_args.model_name_or_path     )       ,	config=__snake_case       ,	cache_dir=model_args.cache_dir       ,	)

      # Get datasets
      UpperCAmelCase_	:						str					   =		hasattr(model.config       ,	"lang2id"     )
      UpperCAmelCase_	:						Optional[Any]					   =		(
          SquadDataset(
              __snake_case       ,	tokenizer=__snake_case       ,	is_language_sensitive=__snake_case       ,	cache_dir=model_args.cache_dir     )
          if training_args.do_train
          else None
      )
      UpperCAmelCase_	:						Optional[Any]					   =		(
          SquadDataset(
              __snake_case       ,	tokenizer=__snake_case       ,	mode="dev"       ,	is_language_sensitive=__snake_case       ,	cache_dir=model_args.cache_dir       ,	)
          if training_args.do_eval
          else None
      )

      # Data collator
      UpperCAmelCase_	:						Optional[Any]					   =		DataCollatorWithPadding(__snake_case       ,	pad_to_multiple_of=8     ) if training_args.fpaa else None

      # Initialize our Trainer
      UpperCAmelCase_	:						Any					   =		Trainer(
          model=__snake_case       ,	args=__snake_case       ,	train_dataset=__snake_case       ,	eval_dataset=__snake_case       ,	data_collator=__snake_case       ,	)

      # Training
      if training_args.do_train:
            trainer.train(
                model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path     ) else None     )
            trainer.save_model()
            # For convenience, we also re-save the tokenizer to the same directory,
            # so that you can share your model easily on huggingface.co/models =)
            if trainer.is_world_master():
                  tokenizer.save_pretrained(training_args.output_dir     )







def  _snake_case			(							__snake_case     )  ->  List[str]:






      '''simple docstring'''







      main()


if __name__ == "__main__":
   main()
