




'''simple docstring'''






import numpy as np
from matplotlib import pyplot as plt




def 		a_( lowerCamelCase      )				->List[str]:






       '''simple docstring'''




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

class   SCREAMING_SNAKE_CASE_		:



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






        '''simple docstring'''




        lowercase  :		Any						=      units
        lowercase  :		Dict						=      None
        lowercase  :		int						=      None
        lowercase  :		str						=      activation
        if learning_rate is None:
               lowercase  :		Dict						=      0.3
        lowercase  :		int						=      learning_rate
        lowercase  :		int						=      is_input_layer



 def    SCREAMING_SNAKE_CASE(  self   ,		A ):






        '''simple docstring'''




        lowercase  :		Union[str, Any]						=      np.asmatrix(np.random.normal(0   ,		0.5   ,		(self.units, back_units) ) )
        lowercase  :		List[str]						=      np.asmatrix(np.random.normal(0   ,		0.5   ,		self.units ) ).T
        if self.activation is None:
               lowercase  :		int						=      sigmoid



 def    SCREAMING_SNAKE_CASE(  self ):






        '''simple docstring'''




        if self.activation == sigmoid:
               lowercase  :		Tuple						=      np.dot(self.output   ,		(1 - self.output).T )
               lowercase  :		Union[str, Any]						=      np.diag(np.diag(A ) )
        else:
               lowercase  :		Dict						=      1
        return gradient_activation



 def    SCREAMING_SNAKE_CASE(  self   ,		A ):






        '''simple docstring'''




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





 def    SCREAMING_SNAKE_CASE(  self   ,		A ):






        '''simple docstring'''




        lowercase  :		List[Any]						=      self.cal_gradient()  # i * i 维
        lowercase  :		Union[str, Any]						=      np.asmatrix(np.dot(gradient.T   ,		A ) )

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

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



 def __init__(  self ):






        '''simple docstring'''




        lowercase  :		Tuple						=      []
        lowercase  :		Tuple						=      []
        lowercase  :		str						=      plt.figure()
        lowercase  :		Any						=      self.fig_loss.add_subplot(1   ,		1   ,		1 )



 def    SCREAMING_SNAKE_CASE(  self   ,		A ):






        '''simple docstring'''




        self.layers.append(A )



 def    SCREAMING_SNAKE_CASE(  self ):






        '''simple docstring'''




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



 def    SCREAMING_SNAKE_CASE(  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    SCREAMING_SNAKE_CASE(  self   ,		A   ,		A   ,		A   ,		A ):






        '''simple docstring'''




        lowercase  :		Any						=      train_round
        lowercase  :		Any						=      accuracy

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

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

                      # forward propagation
                      for layer in self.layers:
                             lowercase  :		Dict						=      layer.forward_propagation(_xdata )

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

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

               lowercase  :		Dict						=      all_loss / x_shape[0]
               self.train_mse.append(A )

               self.plot_loss()

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



 def    SCREAMING_SNAKE_CASE(  self   ,		A   ,		A ):






        '''simple docstring'''




        lowercase  :		Any						=      np.sum(np.power((ydata - ydata_)   ,		2 ) )
        lowercase  :		Tuple						=      2 * (ydata_ - ydata)
        # vector (shape is the same as _ydata.shape)
        return self.loss, self.loss_gradient





 def    SCREAMING_SNAKE_CASE(  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 		a_( )				->Tuple:






       '''simple docstring'''




       lowercase  :		Dict						=      np.random.randn(10   ,				10      )
       lowercase  :		Tuple						=      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],
           ]      )
       lowercase  :		str						=      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'''






import numpy as np
import torch
import torch.nn as nn
from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel

from ...utils import logging


lowercase	:      Optional[Any]				=       logging.get_logger(__name__)




def 		a_( lowerCamelCase   ,				lowerCamelCase      )				->Optional[int]:






       '''simple docstring'''




       lowercase  :		List[str]						=      nn.functional.normalize(lowerCamelCase      )
       lowercase  :		Optional[int]						=      nn.functional.normalize(lowerCamelCase      )
       return torch.mm(lowerCamelCase   ,				normalized_text_embeds.t()      )





class   SCREAMING_SNAKE_CASE_		(       lowerCamelCase_				):
 __snake_case	:     List[str]			   =      CLIPConfig

 __snake_case	:     Optional[int]			   =      ["CLIPEncoderLayer"]



 def __init__(  self   ,		A ):






        '''simple docstring'''




        super().__init__(A )

        lowercase  :		Optional[Any]						=      CLIPVisionModel(config.vision_config )
        lowercase  :		Tuple						=      nn.Linear(config.vision_config.hidden_size   ,		config.projection_dim   ,		bias=A )

        lowercase  :		Any						=      nn.Parameter(torch.ones(17   ,		config.projection_dim )   ,		requires_grad=A )
        lowercase  :		int						=      nn.Parameter(torch.ones(3   ,		config.projection_dim )   ,		requires_grad=A )

        lowercase  :		List[str]						=      nn.Parameter(torch.ones(17 )   ,		requires_grad=A )
        lowercase  :		str						=      nn.Parameter(torch.ones(3 )   ,		requires_grad=A )



 @torch.no_grad()
 def    SCREAMING_SNAKE_CASE(  self   ,		A   ,		A ):






        '''simple docstring'''




        lowercase  :		Union[str, Any]						=      self.vision_model(A )[1]  # pooled_output
        lowercase  :		List[Any]						=      self.visual_projection(A )

        # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
        lowercase  :		Optional[Any]						=      cosine_distance(A   ,		self.special_care_embeds ).cpu().float().numpy()
        lowercase  :		int						=      cosine_distance(A   ,		self.concept_embeds ).cpu().float().numpy()

        lowercase  :		Dict						=      []
        lowercase  :		str						=      image_embeds.shape[0]
        for i in range(A ):
               lowercase  :		List[str]						=      {'''special_scores''': {}, '''special_care''': [], '''concept_scores''': {}, '''bad_concepts''': []}

               # increase this value to create a stronger `nfsw` filter
               # at the cost of increasing the possibility of filtering benign images
               lowercase  :		str						=      0.0

               for concept_idx in range(len(special_cos_dist[0] ) ):
                      lowercase  :		List[str]						=      special_cos_dist[i][concept_idx]
                      lowercase  :		List[Any]						=      self.special_care_embeds_weights[concept_idx].item()
                      lowercase  :		Optional[Any]						=      round(concept_cos - concept_threshold + adjustment   ,		3 )
                      if result_img["special_scores"][concept_idx] > 0:
                             result_img["special_care"].append({concept_idx, result_img['''special_scores'''][concept_idx]} )
                             lowercase  :		List[str]						=      0.0_1

               for concept_idx in range(len(cos_dist[0] ) ):
                      lowercase  :		List[Any]						=      cos_dist[i][concept_idx]
                      lowercase  :		Optional[Any]						=      self.concept_embeds_weights[concept_idx].item()
                      lowercase  :		Any						=      round(concept_cos - concept_threshold + adjustment   ,		3 )
                      if result_img["concept_scores"][concept_idx] > 0:
                             result_img["bad_concepts"].append(A )

               result.append(A )

        lowercase  :		Dict						=      [len(res['''bad_concepts'''] ) > 0 for res in result]

        for idx, has_nsfw_concept in enumerate(A ):
               if has_nsfw_concept:
                      if torch.is_tensor(A ) or torch.is_tensor(images[0] ):
                             lowercase  :		Any						=      torch.zeros_like(images[idx] )  # black image
                      else:
                             lowercase  :		Tuple						=      np.zeros(images[idx].shape )  # black image

        if any(A ):
               logger.warning(
                   '''Potential NSFW content was detected in one or more images. A black image will be returned instead.'''
                   ''' Try again with a different prompt and/or seed.''' )

        return images, has_nsfw_concepts





 @torch.no_grad()
 def    SCREAMING_SNAKE_CASE(  self   ,		A   ,		A ):






        '''simple docstring'''




        lowercase  :		Dict						=      self.vision_model(A )[1]  # pooled_output
        lowercase  :		Optional[int]						=      self.visual_projection(A )

        lowercase  :		Tuple						=      cosine_distance(A   ,		self.special_care_embeds )
        lowercase  :		List[str]						=      cosine_distance(A   ,		self.concept_embeds )

        # increase this value to create a stronger `nsfw` filter
        # at the cost of increasing the possibility of filtering benign images
        lowercase  :		List[str]						=      0.0

        lowercase  :		Optional[Any]						=      special_cos_dist - self.special_care_embeds_weights + adjustment
        # special_scores = special_scores.round(decimals=3)
        lowercase  :		Union[str, Any]						=      torch.any(special_scores > 0   ,		dim=1 )
        lowercase  :		Union[str, Any]						=      special_care * 0.0_1
        lowercase  :		Any						=      special_adjustment.unsqueeze(1 ).expand(-1   ,		cos_dist.shape[1] )

        lowercase  :		Union[str, Any]						=      (cos_dist - self.concept_embeds_weights) + special_adjustment
        # concept_scores = concept_scores.round(decimals=3)
        lowercase  :		Tuple						=      torch.any(concept_scores > 0   ,		dim=1 )

        lowercase  :		Dict						=      0.0  # black image

        return images, has_nsfw_concepts




