

'''simple docstring'''


import gc
import unittest

from transformers import MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, FillMaskPipeline, pipeline
from transformers.pipelines import PipelineException
from transformers.testing_utils import (
    is_pipeline_test,
    is_torch_available,
    nested_simplify,
    require_tf,
    require_torch,
    require_torch_gpu,
    slow,
)

from .test_pipelines_common import ANY



@is_pipeline_test
class 					lowercase						(  unittest.TestCase ):
							_lowerCamelCase		:      Optional[int]=   MODEL_FOR_MASKED_LM_MAPPING
							_lowerCamelCase		:      Dict=   TF_MODEL_FOR_MASKED_LM_MAPPING


							def 						_snake_case							(			self)							->				Optional[Any]:
								super().tearDown()
								# clean-up as much as possible GPU memory occupied by PyTorch
								gc.collect()
								if is_torch_available():
									import torch

									torch.cuda.empty_cache()


							@require_tf
							def 						_snake_case							(			self)							->				Optional[Any]:
								UpperCAmelCase_		:      str       	=					pipeline(task='fill-mask'    ,			model='sshleifer/tiny-distilroberta-base'    ,			top_k=2    ,			framework='tf')
								UpperCAmelCase_		:      Tuple       	=					unmasker('My name is <mask>')
								self.assertEqual(
								    nested_simplify(_snake_case    ,			decimals=6)    ,			[
								        {'sequence': 'My name is grouped', 'score': 2.1e-05, 'token': 3_8015, 'token_str': ' grouped'},
								        {'sequence': 'My name is accuser', 'score': 2.1e-05, 'token': 2_5506, 'token_str': ' accuser'},
								    ]    ,			)

								UpperCAmelCase_		:      int       	=					unmasker('The largest city in France is <mask>')
								self.assertEqual(
								    nested_simplify(_snake_case    ,			decimals=6)    ,			[
								        {
								            'sequence': 'The largest city in France is grouped',
								            'score': 2.1e-05,
								            'token': 3_8015,
								            'token_str': ' grouped',
								        },
								        {
								            'sequence': 'The largest city in France is accuser',
								            'score': 2.1e-05,
								            'token': 2_5506,
								            'token_str': ' accuser',
								        },
								    ]    ,			)

								UpperCAmelCase_		:      Any       	=					unmasker('My name is <mask>'    ,			targets=[' Patrick', ' Clara', ' Teven']    ,			top_k=3)
								self.assertEqual(
								    nested_simplify(_snake_case    ,			decimals=6)    ,			[
								        {'sequence': 'My name is Clara', 'score': 2e-05, 'token': 1_3606, 'token_str': ' Clara'},
								        {'sequence': 'My name is Patrick', 'score': 2e-05, 'token': 3499, 'token_str': ' Patrick'},
								        {'sequence': 'My name is Te', 'score': 1.9e-05, 'token': 2941, 'token_str': ' Te'},
								    ]    ,			)


							@require_torch
							def 						_snake_case							(			self)							->				Optional[Any]:
								UpperCAmelCase_		:      List[Any]       	=					pipeline(task='fill-mask'    ,			model='sshleifer/tiny-distilroberta-base'    ,			top_k=2    ,			framework='pt')

								UpperCAmelCase_		:      Optional[int]       	=					unmasker('My name is <mask>')
								self.assertEqual(
								    nested_simplify(_snake_case    ,			decimals=6)    ,			[
								        {'sequence': 'My name is Maul', 'score': 2.2e-05, 'token': 3_5676, 'token_str': ' Maul'},
								        {'sequence': 'My name isELS', 'score': 2.2e-05, 'token': 1_6416, 'token_str': 'ELS'},
								    ]    ,			)

								UpperCAmelCase_		:      Optional[Any]       	=					unmasker('The largest city in France is <mask>')
								self.assertEqual(
								    nested_simplify(_snake_case    ,			decimals=6)    ,			[
								        {
								            'sequence': 'The largest city in France is Maul',
								            'score': 2.2e-05,
								            'token': 3_5676,
								            'token_str': ' Maul',
								        },
								        {'sequence': 'The largest city in France isELS', 'score': 2.2e-05, 'token': 1_6416, 'token_str': 'ELS'},
								    ]    ,			)

								UpperCAmelCase_		:      Tuple       	=					unmasker('My name is <mask>'    ,			targets=[' Patrick', ' Clara', ' Teven']    ,			top_k=3)
								self.assertEqual(
								    nested_simplify(_snake_case    ,			decimals=6)    ,			[
								        {'sequence': 'My name is Patrick', 'score': 2.1e-05, 'token': 3499, 'token_str': ' Patrick'},
								        {'sequence': 'My name is Te', 'score': 2e-05, 'token': 2941, 'token_str': ' Te'},
								        {'sequence': 'My name is Clara', 'score': 2e-05, 'token': 1_3606, 'token_str': ' Clara'},
								    ]    ,			)

								UpperCAmelCase_		:      Any       	=					unmasker('My name is <mask> <mask>'    ,			top_k=2)

								self.assertEqual(
								    nested_simplify(_snake_case    ,			decimals=6)    ,			[
								        [
								            {
								                'score': 2.2e-05,
								                'token': 3_5676,
								                'token_str': ' Maul',
								                'sequence': '<s>My name is Maul<mask></s>',
								            },
								            {'score': 2.2e-05, 'token': 1_6416, 'token_str': 'ELS', 'sequence': '<s>My name isELS<mask></s>'},
								        ],
								        [
								            {
								                'score': 2.2e-05,
								                'token': 3_5676,
								                'token_str': ' Maul',
								                'sequence': '<s>My name is<mask> Maul</s>',
								            },
								            {'score': 2.2e-05, 'token': 1_6416, 'token_str': 'ELS', 'sequence': '<s>My name is<mask>ELS</s>'},
								        ],
								    ]    ,			)


							@require_torch_gpu
							def 						_snake_case							(			self)							->				int:
								UpperCAmelCase_		:      int       	=					pipeline('fill-mask'    ,			model='hf-internal-testing/tiny-random-distilbert'    ,			device=0    ,			framework='pt')

								# convert model to fp16
								pipe.model.half()

								UpperCAmelCase_		:      Tuple       	=					pipe('Paris is the [MASK] of France.')
								# We actually don't care about the result, we just want to make sure
								# it works, meaning the float16 tensor got casted back to float32
								# for postprocessing.
								self.assertIsInstance(_snake_case    ,			_snake_case)


							@slow
							@require_torch
							def 						_snake_case							(			self)							->				str:
								UpperCAmelCase_		:      Dict       	=					pipeline(task='fill-mask'    ,			model='distilroberta-base'    ,			top_k=2    ,			framework='pt')
								self.run_large_test(_snake_case)


							@slow
							@require_tf
							def 						_snake_case							(			self)							->				Any:
								UpperCAmelCase_		:      Dict       	=					pipeline(task='fill-mask'    ,			model='distilroberta-base'    ,			top_k=2    ,			framework='tf')
								self.run_large_test(_snake_case)


							def 						_snake_case							(			self    ,			_snake_case)							->				Any:
								UpperCAmelCase_		:      int       	=					unmasker('My name is <mask>')
								self.assertEqual(
								    nested_simplify(_snake_case)    ,			[
								        {'sequence': 'My name is John', 'score': 0.008, 'token': 610, 'token_str': ' John'},
								        {'sequence': 'My name is Chris', 'score': 0.007, 'token': 1573, 'token_str': ' Chris'},
								    ]    ,			)
								UpperCAmelCase_		:      str       	=					unmasker('The largest city in France is <mask>')
								self.assertEqual(
								    nested_simplify(_snake_case)    ,			[
								        {
								            'sequence': 'The largest city in France is Paris',
								            'score': 0.251,
								            'token': 2201,
								            'token_str': ' Paris',
								        },
								        {
								            'sequence': 'The largest city in France is Lyon',
								            'score': 0.214,
								            'token': 1_2790,
								            'token_str': ' Lyon',
								        },
								    ]    ,			)

								UpperCAmelCase_		:      Optional[int]       	=					unmasker('My name is <mask>'    ,			targets=[' Patrick', ' Clara', ' Teven']    ,			top_k=3)
								self.assertEqual(
								    nested_simplify(_snake_case)    ,			[
								        {'sequence': 'My name is Patrick', 'score': 0.005, 'token': 3499, 'token_str': ' Patrick'},
								        {'sequence': 'My name is Clara', 'score': 0.000, 'token': 1_3606, 'token_str': ' Clara'},
								        {'sequence': 'My name is Te', 'score': 0.000, 'token': 2941, 'token_str': ' Te'},
								    ]    ,			)


							@require_torch
							def 						_snake_case							(			self)							->				int:
								UpperCAmelCase_		:      Dict       	=					pipeline(task='fill-mask'    ,			model='sshleifer/tiny-distilroberta-base'    ,			framework='pt')
								UpperCAmelCase_		:      List[Any]       	=					None
								UpperCAmelCase_		:      List[str]       	=					None
								self.run_pipeline_test(_snake_case    ,			[])


							@require_tf
							def 						_snake_case							(			self)							->				Optional[int]:
								UpperCAmelCase_		:      Dict       	=					pipeline(task='fill-mask'    ,			model='sshleifer/tiny-distilroberta-base'    ,			framework='tf')
								UpperCAmelCase_		:      str       	=					None
								UpperCAmelCase_		:      int       	=					None
								self.run_pipeline_test(_snake_case    ,			[])


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				Optional[int]:
								if tokenizer is None or tokenizer.mask_token_id is None:
									self.skipTest('The provided tokenizer has no mask token, (probably reformer or wav2vec2)')

								UpperCAmelCase_		:      Dict       	=					FillMaskPipeline(model=_snake_case    ,			tokenizer=_snake_case)
								UpperCAmelCase_		:      Any       	=					[
								    F"""This is another {tokenizer.mask_token} test""",
								]
								return fill_masker, examples


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case)							->				Tuple:
								UpperCAmelCase_		:      Optional[int]       	=					fill_masker.tokenizer
								UpperCAmelCase_		:      Any       	=					fill_masker.model

								UpperCAmelCase_		:      Union[str, Any]       	=					fill_masker(
								    F"""This is a {tokenizer.mask_token}"""    ,			)
								self.assertEqual(
								    _snake_case    ,			[
								        {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								        {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								        {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								        {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								        {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								    ]    ,			)

								UpperCAmelCase_		:      Any       	=					fill_masker([F"""This is a {tokenizer.mask_token}"""])
								self.assertEqual(
								    _snake_case    ,			[
								        {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								        {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								        {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								        {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								        {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								    ]    ,			)

								UpperCAmelCase_		:      Tuple       	=					fill_masker([F"""This is a {tokenizer.mask_token}""", F"""Another {tokenizer.mask_token} great test."""])
								self.assertEqual(
								    _snake_case    ,			[
								        [
								            {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								            {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								            {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								            {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								            {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								        ],
								        [
								            {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								            {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								            {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								            {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								            {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								        ],
								    ]    ,			)

								with self.assertRaises(_snake_case):
									fill_masker([None])
								# No mask_token is not supported
								with self.assertRaises(_snake_case):
									fill_masker('This is')

								self.run_test_top_k(_snake_case    ,			_snake_case)
								self.run_test_targets(_snake_case    ,			_snake_case)
								self.run_test_top_k_targets(_snake_case    ,			_snake_case)
								self.fill_mask_with_duplicate_targets_and_top_k(_snake_case    ,			_snake_case)
								self.fill_mask_with_multiple_masks(_snake_case    ,			_snake_case)


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case)							->				Optional[int]:
								UpperCAmelCase_		:      List[str]       	=					tokenizer.get_vocab()
								UpperCAmelCase_		:      Optional[int]       	=					sorted(vocab.keys())[:2]
								# Pipeline argument
								UpperCAmelCase_		:      Union[str, Any]       	=					FillMaskPipeline(model=_snake_case    ,			tokenizer=_snake_case    ,			targets=_snake_case)
								UpperCAmelCase_		:      Optional[int]       	=					fill_masker(F"""This is a {tokenizer.mask_token}""")
								self.assertEqual(
								    _snake_case    ,			[
								        {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								        {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								    ]    ,			)
								UpperCAmelCase_		:      Optional[Any]       	=					{vocab[el] for el in targets}
								self.assertEqual({el['token'] for el in outputs}    ,			_snake_case)
								UpperCAmelCase_		:      Dict       	=					[tokenizer.decode([x]) for x in target_ids]
								self.assertEqual({el['token_str'] for el in outputs}    ,			set(_snake_case))

								# Call argument
								UpperCAmelCase_		:      Optional[int]       	=					FillMaskPipeline(model=_snake_case    ,			tokenizer=_snake_case)
								UpperCAmelCase_		:      List[Any]       	=					fill_masker(F"""This is a {tokenizer.mask_token}"""    ,			targets=_snake_case)
								self.assertEqual(
								    _snake_case    ,			[
								        {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								        {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								    ]    ,			)
								UpperCAmelCase_		:      List[Any]       	=					{vocab[el] for el in targets}
								self.assertEqual({el['token'] for el in outputs}    ,			_snake_case)
								UpperCAmelCase_		:      Tuple       	=					[tokenizer.decode([x]) for x in target_ids]
								self.assertEqual({el['token_str'] for el in outputs}    ,			set(_snake_case))

								# Score equivalence
								UpperCAmelCase_		:      Any       	=					fill_masker(F"""This is a {tokenizer.mask_token}"""    ,			targets=_snake_case)
								UpperCAmelCase_		:      Union[str, Any]       	=					[top_mask['token_str'] for top_mask in outputs]
								UpperCAmelCase_		:      Union[str, Any]       	=					[top_mask['score'] for top_mask in outputs]

								# For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`.
								if set(_snake_case) == set(_snake_case):
									UpperCAmelCase_		:      List[Any]       	=					fill_masker(F"""This is a {tokenizer.mask_token}"""    ,			targets=_snake_case)
									UpperCAmelCase_		:      List[Any]       	=					[top_mask['score'] for top_mask in unmasked_targets]
									self.assertEqual(nested_simplify(_snake_case)    ,			nested_simplify(_snake_case))

								# Raises with invalid
								with self.assertRaises(_snake_case):
									UpperCAmelCase_		:      int       	=					fill_masker(F"""This is a {tokenizer.mask_token}"""    ,			targets=[])
								# For some tokenizers, `""` is actually in the vocabulary and the expected error won't raised
								if "" not in tokenizer.get_vocab():
									with self.assertRaises(_snake_case):
										UpperCAmelCase_		:      List[Any]       	=					fill_masker(F"""This is a {tokenizer.mask_token}"""    ,			targets=[''])
									with self.assertRaises(_snake_case):
										UpperCAmelCase_		:      Optional[int]       	=					fill_masker(F"""This is a {tokenizer.mask_token}"""    ,			targets='')


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case)							->				Union[str, Any]:
								UpperCAmelCase_		:      List[Any]       	=					FillMaskPipeline(model=_snake_case    ,			tokenizer=_snake_case    ,			top_k=2)
								UpperCAmelCase_		:      Tuple       	=					fill_masker(F"""This is a {tokenizer.mask_token}""")
								self.assertEqual(
								    _snake_case    ,			[
								        {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								        {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								    ]    ,			)

								UpperCAmelCase_		:      Tuple       	=					FillMaskPipeline(model=_snake_case    ,			tokenizer=_snake_case)
								UpperCAmelCase_		:      Optional[int]       	=					fill_masker(F"""This is a {tokenizer.mask_token}"""    ,			top_k=2)
								self.assertEqual(
								    _snake_case    ,			[
								        {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								        {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								    ]    ,			)
								self.assertEqual(nested_simplify(_snake_case)    ,			nested_simplify(_snake_case))


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case)							->				Tuple:
								UpperCAmelCase_		:      List[Any]       	=					tokenizer.get_vocab()
								UpperCAmelCase_		:      Tuple       	=					FillMaskPipeline(model=_snake_case    ,			tokenizer=_snake_case)

								# top_k=2, ntargets=3
								UpperCAmelCase_		:      Optional[int]       	=					sorted(vocab.keys())[:3]
								UpperCAmelCase_		:      Union[str, Any]       	=					fill_masker(F"""This is a {tokenizer.mask_token}"""    ,			top_k=2    ,			targets=_snake_case)

								# If we use the most probably targets, and filter differently, we should still
								# have the same results
								UpperCAmelCase_		:      Tuple       	=					[el['token_str'] for el in sorted(_snake_case    ,			key=lambda _snake_case: x["score"]    ,			reverse=_snake_case)]
								# For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`.
								if set(_snake_case).issubset(_snake_case):
									UpperCAmelCase_		:      Union[str, Any]       	=					fill_masker(F"""This is a {tokenizer.mask_token}"""    ,			top_k=3    ,			targets=_snake_case)
									# They should yield exactly the same result
									self.assertEqual(nested_simplify(_snake_case)    ,			nested_simplify(_snake_case))


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case)							->				Union[str, Any]:
								UpperCAmelCase_		:      List[Any]       	=					FillMaskPipeline(model=_snake_case    ,			tokenizer=_snake_case)
								UpperCAmelCase_		:      Any       	=					tokenizer.get_vocab()
								# String duplicates + id duplicates
								UpperCAmelCase_		:      Tuple       	=					sorted(vocab.keys())[:3]
								UpperCAmelCase_		:      Dict       	=					[targets[0], targets[1], targets[0], targets[2], targets[1]]
								UpperCAmelCase_		:      Any       	=					fill_masker(F"""My name is {tokenizer.mask_token}"""    ,			targets=_snake_case    ,			top_k=10)

								# The target list contains duplicates, so we can't output more
								# than them
								self.assertEqual(len(_snake_case)    ,			3)





							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case)							->				Dict:
								UpperCAmelCase_		:      Union[str, Any]       	=					FillMaskPipeline(model=_snake_case    ,			tokenizer=_snake_case)

								UpperCAmelCase_		:      str       	=					fill_masker(
								    F"""This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}"""    ,			top_k=2)
								self.assertEqual(
								    _snake_case    ,			[
								        [
								            {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								            {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								        ],
								        [
								            {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								            {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								        ],
								        [
								            {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								            {'sequence': ANY(_snake_case), 'score': ANY(_snake_case), 'token': ANY(_snake_case), 'token_str': ANY(_snake_case)},
								        ],
								    ]    ,			)









'''simple docstring'''


import numpy



class 					lowercase						:


							def __init__(			self    ,			_snake_case    ,			_snake_case)							->				None:
								UpperCAmelCase_		:      Optional[Any]       	=					input_array

								# Random initial weights are assigned where first argument is the
								# number of nodes in previous layer and second argument is the
								# number of nodes in the next layer.

								# Random initial weights are assigned.
								# self.input_array.shape[1] is used to represent number of nodes in input layer.
								# First hidden layer consists of 4 nodes.
								UpperCAmelCase_		:      Tuple       	=					numpy.random.rand(
								    self.input_array.shape[1]    ,			4)

								# Random initial values for the first hidden layer.
								# First hidden layer has 4 nodes.
								# Second hidden layer has 3 nodes.
								UpperCAmelCase_		:      List[str]       	=					numpy.random.rand(
								    4    ,			3)

								# Random initial values for the second hidden layer.
								# Second hidden layer has 3 nodes.
								# Output layer has 1 node.
								UpperCAmelCase_		:      Dict       	=					numpy.random.rand(3    ,			1)

								# Real output values provided.
								UpperCAmelCase_		:      str       	=					output_array

								# Predicted output values by the neural network.
								# Predicted_output array initially consists of zeroes.
								UpperCAmelCase_		:      Union[str, Any]       	=					numpy.zeros(output_array.shape)


							def 						_snake_case							(			self)							->				numpy.ndarray:
								UpperCAmelCase_		:      Any       	=					sigmoid(
								    numpy.dot(self.input_array    ,			self.input_layer_and_first_hidden_layer_weights))

								# layer_between_first_hidden_layer_and_second_hidden_layer is the layer
								# connecting the first hidden set of nodes with the second hidden set of nodes.
								UpperCAmelCase_		:      Tuple       	=					sigmoid(
								    numpy.dot(
								        self.layer_between_input_and_first_hidden_layer    ,			self.first_hidden_layer_and_second_hidden_layer_weights    ,			))

								# layer_between_second_hidden_layer_and_output is the layer connecting
								# second hidden layer with the output node.
								UpperCAmelCase_		:      int       	=					sigmoid(
								    numpy.dot(
								        self.layer_between_first_hidden_layer_and_second_hidden_layer    ,			self.second_hidden_layer_and_output_layer_weights    ,			))

								return self.layer_between_second_hidden_layer_and_output


							def 						_snake_case							(			self)							->				None:
								UpperCAmelCase_		:      Optional[int]       	=					numpy.dot(
								    self.layer_between_first_hidden_layer_and_second_hidden_layer.T    ,			2
								    * (self.output_array - self.predicted_output)
								    * sigmoid_derivative(self.predicted_output)    ,			)
								UpperCAmelCase_		:      Any       	=					numpy.dot(
								    self.layer_between_input_and_first_hidden_layer.T    ,			numpy.dot(
								        2
								        * (self.output_array - self.predicted_output)
								        * sigmoid_derivative(self.predicted_output)    ,			self.second_hidden_layer_and_output_layer_weights.T    ,			)
								    * sigmoid_derivative(
								        self.layer_between_first_hidden_layer_and_second_hidden_layer)    ,			)
								UpperCAmelCase_		:      Tuple       	=					numpy.dot(
								    self.input_array.T    ,			numpy.dot(
								        numpy.dot(
								            2
								            * (self.output_array - self.predicted_output)
								            * sigmoid_derivative(self.predicted_output)    ,			self.second_hidden_layer_and_output_layer_weights.T    ,			)
								        * sigmoid_derivative(
								            self.layer_between_first_hidden_layer_and_second_hidden_layer)    ,			self.first_hidden_layer_and_second_hidden_layer_weights.T    ,			)
								    * sigmoid_derivative(self.layer_between_input_and_first_hidden_layer)    ,			)

								self.input_layer_and_first_hidden_layer_weights += (
								    updated_input_layer_and_first_hidden_layer_weights
								)
								self.first_hidden_layer_and_second_hidden_layer_weights += (
								    updated_first_hidden_layer_and_second_hidden_layer_weights
								)
								self.second_hidden_layer_and_output_layer_weights += (
								    updated_second_hidden_layer_and_output_layer_weights
								)


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				None:
								for iteration in range(1    ,			iterations + 1):
									UpperCAmelCase_		:      int       	=					self.feedforward()
									self.back_propagation()
									if give_loss:
										UpperCAmelCase_		:      List[Any]       	=					numpy.mean(numpy.square(output - self.feedforward()))
										print(F"""Iteration {iteration} Loss: {loss}""")





							def 						_snake_case							(			self    ,			_snake_case)							->				int:
								UpperCAmelCase_		:      Optional[int]       	=					input_arr

								UpperCAmelCase_		:      Tuple       	=					sigmoid(
								    numpy.dot(self.array    ,			self.input_layer_and_first_hidden_layer_weights))

								UpperCAmelCase_		:      Optional[int]       	=					sigmoid(
								    numpy.dot(
								        self.layer_between_input_and_first_hidden_layer    ,			self.first_hidden_layer_and_second_hidden_layer_weights    ,			))

								UpperCAmelCase_		:      int       	=					sigmoid(
								    numpy.dot(
								        self.layer_between_first_hidden_layer_and_second_hidden_layer    ,			self.second_hidden_layer_and_output_layer_weights    ,			))

								return int(self.layer_between_second_hidden_layer_and_output > 0.6)







def 						SCREAMING_SNAKE_CASE(  UpperCamelCase  )    ->    numpy.ndarray:
	return 1 / (1 + numpy.exp(-value  ))







def 						SCREAMING_SNAKE_CASE(  UpperCamelCase  )    ->    numpy.ndarray:
	return (value) * (1 - (value))







def 						SCREAMING_SNAKE_CASE(  )    ->    int:
	UpperCAmelCase_		:      Optional[int]       	=					numpy.array(
	    (
	        [0, 0, 0],
	        [0, 0, 1],
	        [0, 1, 0],
	        [0, 1, 1],
	        [1, 0, 0],
	        [1, 0, 1],
	        [1, 1, 0],
	        [1, 1, 1],
	    )   ,dtype=numpy.floataa   ,)

	# True output values for the given input values.
	UpperCAmelCase_		:      Dict       	=					numpy.array(([0], [1], [1], [0], [1], [0], [0], [1])   ,dtype=numpy.floataa  )

	# Calling neural network class.
	UpperCAmelCase_		:      List[str]       	=					TwoHiddenLayerNeuralNetwork(
	    input_array=UpperCamelCase   ,output_array=UpperCamelCase  )

	# Calling training function.
	# Set give_loss to True if you want to see loss in every iteration.
	neural_network.train(output=UpperCamelCase   ,iterations=1_0   ,give_loss=UpperCamelCase  )

	return neural_network.predict(numpy.array(([1, 1, 1])   ,dtype=numpy.floataa  )  )


if __name__ == "__main__":
						example()






