






from typing import Optional, Tuple

import jax
import jax.numpy as jnp
from flax import linen as nn
from flax.core.frozen_dict import FrozenDict
from transformers import CLIPConfig, FlaxPreTrainedModel
from transformers.models.clip.modeling_flax_clip import FlaxCLIPVisionModule






def      _snake_case     (__lowercase     ,   __lowercase     ,   __lowercase=1e-12):
   UpperCamelCase_						=  jnp.divide(emb_a.T     ,   jnp.clip(jnp.linalg.norm(__lowercase     ,   axis=1)     ,   a_min=__lowercase)).T
   UpperCamelCase_						=  jnp.divide(emb_a.T     ,   jnp.clip(jnp.linalg.norm(__lowercase     ,   axis=1)     ,   a_min=__lowercase)).T
   return jnp.matmul(__lowercase     ,   norm_emb_a.T)


class     _a							(	nn.Module     ):






  """simple docstring"""





  A_						       = 42
  A_						       = jnp.floataa




  def  _UpperCAmelCase  (  self			)      ->			Dict:
     UpperCamelCase_						=  FlaxCLIPVisionModule(self.config.vision_config			)
     UpperCamelCase_						=  nn.Dense(self.config.projection_dim		,		use_bias=_UpperCAmelCase		,		dtype=self.dtype			)

     UpperCamelCase_						=  self.param('concept_embeds'		,		jax.nn.initializers.ones		,		(17, self.config.projection_dim)			)
     UpperCamelCase_						=  self.param(
         'special_care_embeds'		,		jax.nn.initializers.ones		,		(3, self.config.projection_dim)			)

     UpperCamelCase_						=  self.param('concept_embeds_weights'		,		jax.nn.initializers.ones		,		(17,)			)
     UpperCamelCase_						=  self.param('special_care_embeds_weights'		,		jax.nn.initializers.ones		,		(3,)			)




  def __call__(  self		,		_UpperCAmelCase			)      ->			List[str]:
     UpperCamelCase_						=  self.vision_model(_UpperCAmelCase			)[1]
     UpperCamelCase_						=  self.visual_projection(_UpperCAmelCase			)

     UpperCamelCase_						=  jax_cosine_distance(_UpperCAmelCase		,		self.special_care_embeds			)
     UpperCamelCase_						=  jax_cosine_distance(_UpperCAmelCase		,		self.concept_embeds			)

     # increase this value to create a stronger `nfsw` filter
     # at the cost of increasing the possibility of filtering benign image inputs
     UpperCamelCase_						=  0.0

     UpperCamelCase_						=  special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment
     UpperCamelCase_						=  jnp.round(_UpperCAmelCase		,		3			)
     UpperCamelCase_						=  jnp.any(special_scores > 0		,		axis=1		,		keepdims=_UpperCAmelCase			)
     # Use a lower threshold if an image has any special care concept
     UpperCamelCase_						=  is_special_care * 0.0_1

     UpperCamelCase_						=  cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
     UpperCamelCase_						=  jnp.round(_UpperCAmelCase		,		3			)
     UpperCamelCase_						=  jnp.any(concept_scores > 0		,		axis=1			)

     return has_nsfw_concepts




class     _a							(	UpperCAmelCase__     ):






  """simple docstring"""





  A_						       = CLIPConfig
  A_						       = """clip_input"""
  A_						       = FlaxStableDiffusionSafetyCheckerModule




  def __init__(  self		,		_UpperCAmelCase		,		_UpperCAmelCase = None		,		_UpperCAmelCase = 0		,		_UpperCAmelCase = jnp.floataa		,		_UpperCAmelCase = True		,		**_UpperCAmelCase		,		)      ->			Union[str, Any]:
     if input_shape is None:
        UpperCamelCase_						=  (1, 224, 224, 3)
     UpperCamelCase_						=  self.module_class(config=_UpperCAmelCase		,		dtype=_UpperCAmelCase		,		**_UpperCAmelCase			)
     super().__init__(_UpperCAmelCase		,		_UpperCAmelCase		,		input_shape=_UpperCAmelCase		,		seed=_UpperCAmelCase		,		dtype=_UpperCAmelCase		,		_do_init=_do_init			)




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase = None			)      ->			FrozenDict:
     # init input tensor
     UpperCamelCase_						=  jax.random.normal(_UpperCAmelCase		,		_UpperCAmelCase			)

     UpperCamelCase_		,   UpperCamelCase_						=  jax.random.split(_UpperCAmelCase			)
     UpperCamelCase_						=  {'params': params_rng, 'dropout': dropout_rng}

     UpperCamelCase_						=  self.module.init(_UpperCAmelCase		,		_UpperCAmelCase			)['params']

     return random_params




  def __call__(  self		,		_UpperCAmelCase		,		_UpperCAmelCase = None		,		)      ->			Optional[int]:
     UpperCamelCase_						=  jnp.transpose(_UpperCAmelCase		,		(0, 2, 3, 1)			)

     return self.module.apply(
         {'params': params or self.params}		,		jnp.array(_UpperCAmelCase		,		dtype=jnp.floataa			)		,		rngs={}		,		)













"""simple docstring"""







import math
import random


def      UpperCamelCase       ( _A  ,				_A = False )     ->       float:
				if deriv:
								return value * (1 - value)
				return 1 / (1 + math.exp(-value ))


# Initial Value
_lowerCAmelCase            =	0.02


def      UpperCamelCase       ( _A  ,				_A )     ->       float:
				lowercase    :      Any       = float(2 * (random.randint(1  ,				100 )) - 1 )

				for _ in range(_A ):
								# Forward propagation
								lowercase    :      Dict       = sigmoid_function(INITIAL_VALUE * weight )
								# How much did we miss?
								lowercase    :      Any       = (expected / 100) - layer_a
								# Error delta
								lowercase    :      Dict       = layer_1_error * sigmoid_function(_A  ,				_A )
								# Update weight
								weight += INITIAL_VALUE * layer_1_delta

				return layer_a * 100


if __name__ == "__main__":
				import doctest

				doctest.testmod()

				_lowerCAmelCase            =	int(input('Expected value: '))
				_lowerCAmelCase            =	int(input('Number of propagations: '))
				print(forward_propagation(expected, number_propagations))
