





"""simple docstring"""
import os
import socket
from contextlib import contextmanager

import torch

from ..commands.config.default import write_basic_config  # noqa: F401
from ..state import PartialState
from .dataclasses import DistributedType
from .imports import is_deepspeed_available, is_tpu_available
from .transformer_engine import convert_model
from .versions import is_torch_version


if is_deepspeed_available():
  from deepspeed import DeepSpeedEngine

if is_tpu_available(check_device=False):
  import torch_xla.core.xla_model as xm



def 					__lowerCamelCase       (							a_					:		str							)      ->				List[str]:
      if is_torch_version('''<'''			,       '''2.0.0'''							) or not hasattr(a_			,       '''_dynamo'''							):
            return False
      return isinstance(a_			,       torch._dynamo.eval_frame.OptimizedModule							)



def 					__lowerCamelCase       (							a_					:		int			,       a_					:		bool = True							)      ->				Optional[int]:
      __SCREAMING_SNAKE_CASE       :Tuple		    =  (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

      __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  is_compiled_module(a_							)
      if is_compiled:
            __SCREAMING_SNAKE_CASE       :Tuple		    =  model
            __SCREAMING_SNAKE_CASE       :Tuple		    =  model._orig_mod

      if is_deepspeed_available():
            options += (DeepSpeedEngine,)

      while isinstance(a_			,       a_							):
            __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  model.module

      if not keep_fpaa_wrapper:
            __SCREAMING_SNAKE_CASE       :Dict		    =  getattr(a_			,       '''forward'''							)
            __SCREAMING_SNAKE_CASE       :Any		    =  model.__dict__.pop('''_original_forward'''			,       a_							)
            if original_forward is not None:
                  while hasattr(a_			,       '''__wrapped__'''							):
                        __SCREAMING_SNAKE_CASE       :List[str]		    =  forward.__wrapped__
                        if forward == original_forward:
                              break
                  __SCREAMING_SNAKE_CASE       :Any		    =  forward
            if getattr(a_			,       '''_converted_to_transformer_engine'''			,       a_							):
                  convert_model(a_			,       to_transformer_engine=a_							)

      if is_compiled:
            __SCREAMING_SNAKE_CASE       :Optional[int]		    =  model
            __SCREAMING_SNAKE_CASE       :Optional[int]		    =  compiled_model

      return model



def 					__lowerCamelCase       (							)      ->				str:
      PartialState().wait_for_everyone()



def 					__lowerCamelCase       (							a_					:		Union[str, Any]			,       a_					:		Optional[int]							)      ->				int:
      if PartialState().distributed_type == DistributedType.TPU:
            xm.save(a_			,       a_							)
      elif PartialState().local_process_index == 0:
            torch.save(a_			,       a_							)



@contextmanager
def 					__lowerCamelCase       (							**a_					:		int							)      ->				Tuple:
      for key, value in kwargs.items():
            __SCREAMING_SNAKE_CASE       :Any		    =  str(a_							)

      yield

      for key in kwargs:
            if key.upper() in os.environ:
                  del os.environ[key.upper()]



def 					__lowerCamelCase       (							a_					:		List[str]							)      ->				List[Any]:
      if not hasattr(a_			,       '''__qualname__'''							) and not hasattr(a_			,       '''__name__'''							):
            __SCREAMING_SNAKE_CASE       :List[Any]		    =  getattr(a_			,       '''__class__'''			,       a_							)
      if hasattr(a_			,       '''__qualname__'''							):
            return obj.__qualname__
      if hasattr(a_			,       '''__name__'''							):
            return obj.__name__
      return str(a_							)



def 					__lowerCamelCase       (							a_					:		List[Any]			,       a_					:		List[str]							)      ->				Tuple:
      for key, value in source.items():
            if isinstance(a_			,       a_							):
                  __SCREAMING_SNAKE_CASE       :List[str]		    =  destination.setdefault(a_			,       {}							)
                  merge_dicts(a_			,       a_							)
            else:
                  __SCREAMING_SNAKE_CASE       :str		    =  value

      return destination







def 					__lowerCamelCase       (							a_					:		int = None							)      ->				bool:
      if port is None:
            __SCREAMING_SNAKE_CASE       :Dict		    =  2_95_00
      with socket.socket(socket.AF_INET			,       socket.SOCK_STREAM							) as s:
            return s.connect_ex(('''localhost''', port)							) == 0






"""simple docstring"""
import math
import random



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


# Initial Value
lowerCamelCase_       =		0.02



def 					__lowerCamelCase       (							a_					:		int			,       a_					:		int							)      ->				float:
      __SCREAMING_SNAKE_CASE       :Any		    =  float(2 * (random.randint(1			,       1_00							)) - 1							)

      for _ in range(a_							):
            # Forward propagation
            __SCREAMING_SNAKE_CASE       :Any		    =  sigmoid_function(INITIAL_VALUE * weight							)
            # How much did we miss?
            __SCREAMING_SNAKE_CASE       :Tuple		    =  (expected / 1_00) - layer_a
            # Error delta
            __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  layer_1_error * sigmoid_function(a_			,       a_							)
            # Update weight
            weight += INITIAL_VALUE * layer_1_delta

      return layer_a * 1_00


if __name__ == "__main__":
  import doctest

  doctest.testmod()

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