




'''simple docstring'''

from __future__ import annotations

import unittest

from transformers import MobileBertConfig, is_tf_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
				import tensorflow as tf

				from transformers import (
				    TF_MODEL_FOR_PRETRAINING_MAPPING,
				    TFMobileBertForMaskedLM,
				    TFMobileBertForMultipleChoice,
				    TFMobileBertForNextSentencePrediction,
				    TFMobileBertForPreTraining,
				    TFMobileBertForQuestionAnswering,
				    TFMobileBertForSequenceClassification,
				    TFMobileBertForTokenClassification,
				    TFMobileBertModel,
				)

@require_tf
class __snake_case(				__lowercase		,							__lowercase		,							unittest.TestCase ):


						'''simple docstring'''



						UpperCAmelCase							:   Optional[Any]									=		(
						    (
						        TFMobileBertModel,
						        TFMobileBertForMaskedLM,
						        TFMobileBertForNextSentencePrediction,
						        TFMobileBertForPreTraining,
						        TFMobileBertForQuestionAnswering,
						        TFMobileBertForSequenceClassification,
						        TFMobileBertForTokenClassification,
						        TFMobileBertForMultipleChoice,
						    )
						    if is_tf_available()
						    else ()
						)
						UpperCAmelCase							:   Tuple									=		(
						    {
						        "feature-extraction": TFMobileBertModel,
						        "fill-mask": TFMobileBertForMaskedLM,
						        "question-answering": TFMobileBertForQuestionAnswering,
						        "text-classification": TFMobileBertForSequenceClassification,
						        "token-classification": TFMobileBertForTokenClassification,
						        "zero-shot": TFMobileBertForSequenceClassification,
						    }
						    if is_tf_available()
						    else {}
						)
						UpperCAmelCase							:   int									=		False
						UpperCAmelCase							:   Dict									=		False


						def     __snake_case   ( self     ,	A_     ,	A_     ,	A_=False				)     ->     Any:
							lowerCAmelCase   =     super()._prepare_for_class(snake_case_     ,	snake_case_     ,	return_labels=snake_case_				)

							if return_labels:
								if model_class in get_values(snake_case_				):
									lowerCAmelCase   =     tf.zeros(self.model_tester.batch_size     ,	dtype=tf.intaa				)

							return inputs_dict

						class __snake_case(				__lowercase ):


												'''simple docstring'''



												def __init__( self     ,	A_     ,	A_=13     ,	A_=7     ,	A_=True     ,	A_=True     ,	A_=True     ,	A_=True     ,	A_=99     ,	A_=32     ,	A_=32     ,	A_=2     ,	A_=4     ,	A_=37     ,	A_="gelu"     ,	A_=0.1     ,	A_=0.1     ,	A_=512     ,	A_=16     ,	A_=2     ,	A_=0.0_2     ,	A_=3     ,	A_=4     ,	A_=None     ,	)     ->     Any:
													lowerCAmelCase   =     parent
													lowerCAmelCase   =     batch_size
													lowerCAmelCase   =     seq_length
													lowerCAmelCase   =     is_training
													lowerCAmelCase   =     use_input_mask
													lowerCAmelCase   =     use_token_type_ids
													lowerCAmelCase   =     use_labels
													lowerCAmelCase   =     vocab_size
													lowerCAmelCase   =     hidden_size
													lowerCAmelCase   =     num_hidden_layers
													lowerCAmelCase   =     num_attention_heads
													lowerCAmelCase   =     intermediate_size
													lowerCAmelCase   =     hidden_act
													lowerCAmelCase   =     hidden_dropout_prob
													lowerCAmelCase   =     attention_probs_dropout_prob
													lowerCAmelCase   =     max_position_embeddings
													lowerCAmelCase   =     type_vocab_size
													lowerCAmelCase   =     type_sequence_label_size
													lowerCAmelCase   =     initializer_range
													lowerCAmelCase   =     num_labels
													lowerCAmelCase   =     num_choices
													lowerCAmelCase   =     scope
													lowerCAmelCase   =     embedding_size


												def     __snake_case   ( self				)     ->     List[Any]:
													lowerCAmelCase   =     ids_tensor([self.batch_size, self.seq_length]     ,	self.vocab_size				)

													lowerCAmelCase   =     None
													if self.use_input_mask:
														lowerCAmelCase   =     random_attention_mask([self.batch_size, self.seq_length]				)

													lowerCAmelCase   =     None
													if self.use_token_type_ids:
														lowerCAmelCase   =     ids_tensor([self.batch_size, self.seq_length]     ,	self.type_vocab_size				)

													lowerCAmelCase   =     None
													lowerCAmelCase   =     None
													lowerCAmelCase   =     None
													if self.use_labels:
														lowerCAmelCase   =     ids_tensor([self.batch_size]     ,	self.type_sequence_label_size				)
														lowerCAmelCase   =     ids_tensor([self.batch_size, self.seq_length]     ,	self.num_labels				)
														lowerCAmelCase   =     ids_tensor([self.batch_size]     ,	self.num_choices				)

													lowerCAmelCase   =     MobileBertConfig(
													    vocab_size=self.vocab_size     ,	hidden_size=self.hidden_size     ,	num_hidden_layers=self.num_hidden_layers     ,	num_attention_heads=self.num_attention_heads     ,	intermediate_size=self.intermediate_size     ,	hidden_act=self.hidden_act     ,	hidden_dropout_prob=self.hidden_dropout_prob     ,	attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,	max_position_embeddings=self.max_position_embeddings     ,	type_vocab_size=self.type_vocab_size     ,	initializer_range=self.initializer_range     ,	embedding_size=self.embedding_size     ,	)

													return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels


												def     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     Tuple:
													lowerCAmelCase   =     TFMobileBertModel(config=snake_case_				)
													lowerCAmelCase   =     {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
													lowerCAmelCase   =     model(snake_case_				)

													lowerCAmelCase   =     [input_ids, input_mask]
													lowerCAmelCase   =     model(snake_case_				)

													lowerCAmelCase   =     model(snake_case_				)

													self.parent.assertEqual(
													    result.last_hidden_state.shape     ,	(self.batch_size, self.seq_length, self.hidden_size)				)
													self.parent.assertEqual(result.pooler_output.shape     ,	(self.batch_size, self.hidden_size)				)


												def     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     Union[str, Any]:
													lowerCAmelCase   =     TFMobileBertForMaskedLM(config=snake_case_				)
													lowerCAmelCase   =     {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
													lowerCAmelCase   =     model(snake_case_				)
													self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, self.seq_length, self.vocab_size)				)


												def     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     Optional[int]:
													lowerCAmelCase   =     TFMobileBertForNextSentencePrediction(config=snake_case_				)
													lowerCAmelCase   =     {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
													lowerCAmelCase   =     model(snake_case_				)
													self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, 2)				)


												def     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     List[str]:
													lowerCAmelCase   =     TFMobileBertForPreTraining(config=snake_case_				)
													lowerCAmelCase   =     {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
													lowerCAmelCase   =     model(snake_case_				)
													self.parent.assertEqual(
													    result.prediction_logits.shape     ,	(self.batch_size, self.seq_length, self.vocab_size)				)
													self.parent.assertEqual(result.seq_relationship_logits.shape     ,	(self.batch_size, 2)				)


												def     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     Optional[int]:
													lowerCAmelCase   =     self.num_labels
													lowerCAmelCase   =     TFMobileBertForSequenceClassification(config=snake_case_				)
													lowerCAmelCase   =     {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
													lowerCAmelCase   =     model(snake_case_				)
													self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, self.num_labels)				)


												def     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     Optional[int]:
													lowerCAmelCase   =     self.num_choices
													lowerCAmelCase   =     TFMobileBertForMultipleChoice(config=snake_case_				)
													lowerCAmelCase   =     tf.tile(tf.expand_dims(snake_case_     ,	1				)     ,	(1, self.num_choices, 1)				)
													lowerCAmelCase   =     tf.tile(tf.expand_dims(snake_case_     ,	1				)     ,	(1, self.num_choices, 1)				)
													lowerCAmelCase   =     tf.tile(tf.expand_dims(snake_case_     ,	1				)     ,	(1, self.num_choices, 1)				)
													lowerCAmelCase   =     {
													    '''input_ids''': multiple_choice_inputs_ids,
													    '''attention_mask''': multiple_choice_input_mask,
													    '''token_type_ids''': multiple_choice_token_type_ids,
													}
													lowerCAmelCase   =     model(snake_case_				)
													self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, self.num_choices)				)


												def     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     Dict:
													lowerCAmelCase   =     self.num_labels
													lowerCAmelCase   =     TFMobileBertForTokenClassification(config=snake_case_				)
													lowerCAmelCase   =     {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
													lowerCAmelCase   =     model(snake_case_				)
													self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, self.seq_length, self.num_labels)				)


												def     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     Dict:
													lowerCAmelCase   =     TFMobileBertForQuestionAnswering(config=snake_case_				)
													lowerCAmelCase   =     {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
													lowerCAmelCase   =     model(snake_case_				)
													self.parent.assertEqual(result.start_logits.shape     ,	(self.batch_size, self.seq_length)				)
													self.parent.assertEqual(result.end_logits.shape     ,	(self.batch_size, self.seq_length)				)




												def     __snake_case   ( self				)     ->     Any:
													lowerCAmelCase   =     self.prepare_config_and_inputs()
													(
													    lowerCAmelCase	
													)   =     config_and_inputs
													lowerCAmelCase   =     {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
													return config, inputs_dict







						def     __snake_case   ( self				)     ->     List[Any]:
							lowerCAmelCase   =     TFMobileBertModelTest.TFMobileBertModelTester(self				)
							lowerCAmelCase   =     ConfigTester(self     ,	config_class=snake_case_     ,	hidden_size=37				)


						def     __snake_case   ( self				)     ->     int:
							self.config_tester.run_common_tests()


						def     __snake_case   ( self				)     ->     Tuple:
							lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_model(*snake_case_				)


						def     __snake_case   ( self				)     ->     int:
							lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_masked_lm(*snake_case_				)


						def     __snake_case   ( self				)     ->     Optional[int]:
							lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_multiple_choice(*snake_case_				)


						def     __snake_case   ( self				)     ->     Any:
							lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*snake_case_				)


						def     __snake_case   ( self				)     ->     Any:
							lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_pretraining(*snake_case_				)


						def     __snake_case   ( self				)     ->     Union[str, Any]:
							lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_question_answering(*snake_case_				)


						def     __snake_case   ( self				)     ->     Tuple:
							lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_sequence_classification(*snake_case_				)


						def     __snake_case   ( self				)     ->     str:
							lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_token_classification(*snake_case_				)




						@slow
						def     __snake_case   ( self				)     ->     Optional[Any]:
							# for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							for model_name in ["google/mobilebert-uncased"]:
								lowerCAmelCase   =     TFMobileBertModel.from_pretrained(snake_case_				)
								self.assertIsNotNone(snake_case_				)







@require_tf
class __snake_case(				unittest.TestCase ):


						'''simple docstring'''



						@slow
						def     __snake_case   ( self				)     ->     Union[str, Any]:
							lowerCAmelCase   =     TFMobileBertForPreTraining.from_pretrained("""google/mobilebert-uncased"""				)
							lowerCAmelCase   =     tf.constant([[0, 1, 2, 3, 4, 5]]				)
							lowerCAmelCase   =     model(snake_case_				)[0]

							lowerCAmelCase   =     [1, 6, 3_0522]
							self.assertEqual(output.shape     ,	snake_case_				)

							lowerCAmelCase   =     tf.constant(
							    [
							        [
							            [-4.5_9_1_9_5_4_7, -9.2_4_8_2_9_5, -9.6_4_5_2_5_6],
							            [-6.7_3_0_6_1_7_5, -6.4_4_0_2_8_4, -6.6_0_5_2_8_3_7],
							            [-7.2_7_4_3_5_0_6, -6.7_8_4_7_9_1_5, -6.0_2_4_6_7_3],
							        ]
							    ]				)
							tf.debugging.assert_near(output[:, :3, :3]     ,	snake_case_     ,	atol=1e-4				)




'''simple docstring'''

import numpy

class __snake_case:


						'''simple docstring'''



						def __init__( self     ,	A_     ,	A_				)     ->     None:
							lowerCAmelCase   =     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.
							lowerCAmelCase   =     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.
							lowerCAmelCase   =     numpy.random.rand(
							    4     ,	3				)

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

							# Real output values provided.
							lowerCAmelCase   =     output_array

							# Predicted output values by the neural network.
							# Predicted_output array initially consists of zeroes.
							lowerCAmelCase   =     numpy.zeros(output_array.shape				)


						def     __snake_case   ( self				)     ->     numpy.ndarray:
							lowerCAmelCase   =     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.
							lowerCAmelCase   =     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.
							lowerCAmelCase   =     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:
							lowerCAmelCase   =     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				)     ,	)
							lowerCAmelCase   =     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				)     ,	)
							lowerCAmelCase   =     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     ,	A_     ,	A_     ,	A_				)     ->     None:
							for iteration in range(1     ,	iterations + 1				):
								lowerCAmelCase   =     self.feedforward()
								self.back_propagation()
								if give_loss:
									lowerCAmelCase   =     numpy.mean(numpy.square(output - self.feedforward()				)				)
									print(f'Iteration {iteration} Loss: {loss}'				)




						def     __snake_case   ( self     ,	A_				)     ->     int:
							lowerCAmelCase   =     input_arr

							lowerCAmelCase   =     sigmoid(
							    numpy.dot(self.array     ,	self.input_layer_and_first_hidden_layer_weights				)				)

							lowerCAmelCase   =     sigmoid(
							    numpy.dot(
							        self.layer_between_input_and_first_hidden_layer     ,	self.first_hidden_layer_and_second_hidden_layer_weights     ,	)				)

							lowerCAmelCase   =     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 		_snake_case		( _SCREAMING_SNAKE_CASE					:						numpy.ndarray							)      ->						numpy.ndarray:







	"""simple docstring"""
	return 1 / (1 + numpy.exp(-value							))






def 		_snake_case		( _SCREAMING_SNAKE_CASE					:						numpy.ndarray							)      ->						numpy.ndarray:







	"""simple docstring"""
	return (value) * (1 - (value))






def 		_snake_case		( )      ->						int:







	"""simple docstring"""
	lowerCAmelCase   =     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.
	lowerCAmelCase   =     numpy.array(([0], [1], [1], [0], [1], [0], [0], [1])							,		dtype=numpy.floataa							)

	# Calling neural network class.
	lowerCAmelCase   =     TwoHiddenLayerNeuralNetwork(
	    input_array=_SCREAMING_SNAKE_CASE							,		output_array=_SCREAMING_SNAKE_CASE							)

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

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


if __name__ == "__main__":
				example()