
'''simple docstring'''







import inspect
import unittest

from huggingface_hub import hf_hub_download

from transformers import ConvNextConfig, UperNetConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import UperNetForSemanticSegmentation
							from transformers.models.upernet.modeling_upernet import UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor






class  lowerCAmelCase__   :






					def __init__(   self				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE=13				,							__SCREAMING_SNAKE_CASE=32				,							__SCREAMING_SNAKE_CASE=3				,							__SCREAMING_SNAKE_CASE=4				,							__SCREAMING_SNAKE_CASE=[10, 20, 30, 40]				,							__SCREAMING_SNAKE_CASE=[2, 2, 3, 2]				,							__SCREAMING_SNAKE_CASE=True				,							__SCREAMING_SNAKE_CASE=True				,							__SCREAMING_SNAKE_CASE=37				,							__SCREAMING_SNAKE_CASE="gelu"				,							__SCREAMING_SNAKE_CASE=10				,							__SCREAMING_SNAKE_CASE=0.02				,							__SCREAMING_SNAKE_CASE=["stage2", "stage3", "stage4"]				,							__SCREAMING_SNAKE_CASE=3				,							__SCREAMING_SNAKE_CASE=None				,							):


											"""simple docstring"""





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






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     List[str]													=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

											lowercase_	:     int													=       None
											if self.use_labels:
																	lowercase_	:     Union[str, Any]													=       ids_tensor([self.batch_size]				,							self.type_sequence_label_size		)

											lowercase_	:     Tuple													=       self.get_config()

											return config, pixel_values, labels






					def 	_snake_case    (   self		):


											"""simple docstring"""





											return ConvNextConfig(
											    num_channels=self.num_channels				,							num_stages=self.num_stages				,							hidden_sizes=self.hidden_sizes				,							depths=self.depths				,							is_training=self.is_training				,							intermediate_size=self.intermediate_size				,							hidden_act=self.hidden_act				,							out_features=self.out_features				,							)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											return UperNetConfig(
											    backbone_config=self.get_backbone_config()				,							hidden_size=5_12				,							pool_scales=[1, 2, 3, 6]				,							use_auxiliary_head=__SCREAMING_SNAKE_CASE				,							auxiliary_loss_weight=0.4				,							auxiliary_in_channels=40				,							auxiliary_channels=2_56				,							auxiliary_num_convs=1				,							auxiliary_concat_input=__SCREAMING_SNAKE_CASE				,							loss_ignore_index=2_55				,							num_labels=self.num_labels				,							)






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_	:     Tuple													=       UperNetForSemanticSegmentation(config=__SCREAMING_SNAKE_CASE		)
											model.to(__SCREAMING_SNAKE_CASE		)
											model.eval()
											lowercase_	:     Optional[Any]													=       model(__SCREAMING_SNAKE_CASE		)
											self.parent.assertEqual(
											    result.logits.shape				,							(self.batch_size, self.num_labels, self.image_size, self.image_size)		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Dict													=       self.prepare_config_and_inputs()
											(
											    lowercase_							
											)	:     List[Any]													=       config_and_inputs
											lowercase_	:     Dict													=       {'pixel_values': pixel_values}
											return config, inputs_dict







@require_torch
class  lowerCAmelCase__   (  __UpperCAmelCase    ,						__UpperCAmelCase    ,						unittest.TestCase     ):
					lowerCAmelCase_        =     (UperNetForSemanticSegmentation,) if is_torch_available() else ()
					lowerCAmelCase_        =     {'''image-segmentation''': UperNetForSemanticSegmentation} if is_torch_available() else {}
					lowerCAmelCase_        =     False
					lowerCAmelCase_        =     False
					lowerCAmelCase_        =     False
					lowerCAmelCase_        =     False
					lowerCAmelCase_        =     False
					lowerCAmelCase_        =     False






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Tuple													=       UperNetModelTester(self		)
											lowercase_	:     List[str]													=       ConfigTester(self				,							config_class=__SCREAMING_SNAKE_CASE				,							has_text_modality=__SCREAMING_SNAKE_CASE				,							hidden_size=37		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											self.create_and_test_config_common_properties()
											self.config_tester.create_and_test_config_to_json_string()
											self.config_tester.create_and_test_config_to_json_file()
											self.config_tester.create_and_test_config_from_and_save_pretrained()
											self.config_tester.create_and_test_config_with_num_labels()
											self.config_tester.check_config_can_be_init_without_params()
											self.config_tester.check_config_arguments_init()






					def 	_snake_case    (   self		):


											"""simple docstring"""





											return






					def 	_snake_case    (   self		):


											"""simple docstring"""





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

											for model_class in self.all_model_classes:
																	lowercase_	:     str													=       model_class(__SCREAMING_SNAKE_CASE		)
																	lowercase_	:     Optional[Any]													=       inspect.signature(model.forward		)
																	# signature.parameters is an OrderedDict => so arg_names order is deterministic
																	lowercase_	:     List[Any]													=       [*signature.parameters.keys()]

																	lowercase_	:     str													=       ['pixel_values']
																	self.assertListEqual(arg_names[:1]				,							__SCREAMING_SNAKE_CASE		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Optional[int]													=       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_semantic_segmentation(*__SCREAMING_SNAKE_CASE		)






					@unittest.skip(reason='''UperNet does not use inputs_embeds'''		)
					def 	_snake_case    (   self		):


											"""simple docstring"""





											pass






					@unittest.skip(reason='''UperNet does not support input and output embeddings'''		)
					def 	_snake_case    (   self		):


											"""simple docstring"""





											pass






					@unittest.skip(reason='''UperNet does not have a base model'''		)
					def 	_snake_case    (   self		):


											"""simple docstring"""





											pass






					@unittest.skip(reason='''UperNet does not have a base model'''		)
					def 	_snake_case    (   self		):


											"""simple docstring"""





											pass






					@require_torch_multi_gpu
					@unittest.skip(reason='''UperNet has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''		)
					def 	_snake_case    (   self		):


											"""simple docstring"""





											pass






					@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''		)
					def 	_snake_case    (   self		):


											"""simple docstring"""





											pass






					def 	_snake_case    (   self		):


											"""simple docstring"""





											def check_hidden_states_output(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		):
																	lowercase_	:     Optional[int]													=       model_class(__SCREAMING_SNAKE_CASE		)
																	model.to(__SCREAMING_SNAKE_CASE		)
																	model.eval()

																	with torch.no_grad():
																							lowercase_	:     Dict													=       model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)		)

																	lowercase_	:     str													=       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																	lowercase_	:     Tuple													=       self.model_tester.num_stages
																	self.assertEqual(len(__SCREAMING_SNAKE_CASE		)				,							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]				,							)

											lowercase_	:     Any													=       self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																	lowercase_	:     List[Any]													=       True
																	check_hidden_states_output(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)

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

																	check_hidden_states_output(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Any													=       self.model_tester.prepare_config_and_inputs_for_common()

											lowercase_	:     List[Any]													=       _config_zero_init(__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Dict													=       _config_zero_init(configs_no_init.backbone_config		)
											for model_class in self.all_model_classes:
																	lowercase_	:     Any													=       model_class(config=__SCREAMING_SNAKE_CASE		)
																	for name, param in model.named_parameters():
																							if param.requires_grad:
																													self.assertIn(
																													    ((param.data.mean() * 1E9).round() / 1E9).item()				,							[0.0, 1.0]				,							msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''				,							)






					@unittest.skip(reason='''UperNet does not have tied weights'''		)
					def 	_snake_case    (   self		):


											"""simple docstring"""





											pass






					@slow
					def 	_snake_case    (   self		):


											"""simple docstring"""





											for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase_	:     Union[str, Any]													=       UperNetForSemanticSegmentation.from_pretrained(__SCREAMING_SNAKE_CASE		)
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)







def 					snake_case_		(				):



						"""simple docstring"""





						lowercase_	:     Dict													=       hf_hub_download(
						    repo_id='''hf-internal-testing/fixtures_ade20k'''      ,      repo_type='''dataset'''      ,      filename='''ADE_val_00000001.jpg'''							)
						lowercase_	:     Tuple													=       Image.open(A__							).convert('''RGB'''							)
						return image






@require_torch
@require_vision
@slow
class  lowerCAmelCase__   (  unittest.TestCase     ):






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Any													=       AutoImageProcessor.from_pretrained('''openmmlab/upernet-swin-tiny'''		)
											lowercase_	:     List[str]													=       UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-swin-tiny'''		).to(__SCREAMING_SNAKE_CASE		)

											lowercase_	:     str													=       prepare_img()
											lowercase_	:     List[str]													=       processor(images=__SCREAMING_SNAKE_CASE				,							return_tensors='''pt'''		).to(__SCREAMING_SNAKE_CASE		)

											with torch.no_grad():
																	lowercase_	:     List[str]													=       model(**__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Optional[int]													=       torch.Size((1, model.config.num_labels, 5_12, 5_12)		)
											self.assertEqual(outputs.logits.shape				,							__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Dict													=       torch.tensor(
											    [[-7.5_958, -7.5_958, -7.4_302], [-7.5_958, -7.5_958, -7.4_302], [-7.4_797, -7.4_797, -7.3_068]]		).to(__SCREAMING_SNAKE_CASE		)
											self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]				,							__SCREAMING_SNAKE_CASE				,							atol=1E-4		)		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Union[str, Any]													=       AutoImageProcessor.from_pretrained('''openmmlab/upernet-convnext-tiny'''		)
											lowercase_	:     Tuple													=       UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-convnext-tiny'''		).to(__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Optional[int]													=       prepare_img()
											lowercase_	:     Any													=       processor(images=__SCREAMING_SNAKE_CASE				,							return_tensors='''pt'''		).to(__SCREAMING_SNAKE_CASE		)

											with torch.no_grad():
																	lowercase_	:     Any													=       model(**__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Tuple													=       torch.Size((1, model.config.num_labels, 5_12, 5_12)		)
											self.assertEqual(outputs.logits.shape				,							__SCREAMING_SNAKE_CASE		)

											lowercase_	:     List[Any]													=       torch.tensor(
											    [[-8.8_110, -8.8_110, -8.6_521], [-8.8_110, -8.8_110, -8.6_521], [-8.7_746, -8.7_746, -8.6_130]]		).to(__SCREAMING_SNAKE_CASE		)
											self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]				,							__SCREAMING_SNAKE_CASE				,							atol=1E-4		)		)










import math
import random



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


# Initial Value
lowercase      :       Optional[Any]									=  0.02



def A_	(		A__		,      A__				)  ->		float:
       a__						:	int =				float(2 * (random.randint(1		,      100				)) - 1				)

       for _ in range(A__				):
              # Forward propagation
              a__						:	Union[str, Any] =				sigmoid_function(INITIAL_VALUE * weight				)
              # How much did we miss?
              a__						:	Optional[Any] =				(expected / 100) - layer_a
              # Error delta
              a__						:	Optional[int] =				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()

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


