


import unittest

import numpy as np

from transformers import AlbertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
   import jax.numpy as jnp

   from transformers.models.albert.modeling_flax_albert import (
       FlaxAlbertForMaskedLM,
       FlaxAlbertForMultipleChoice,
       FlaxAlbertForPreTraining,
       FlaxAlbertForQuestionAnswering,
       FlaxAlbertForSequenceClassification,
       FlaxAlbertForTokenClassification,
       FlaxAlbertModel,
   )






class     UpperCAmelCase__       (					unittest.TestCase		):



    """simple docstring"""






    def __init__(			self					,   A_					,   A_=13					,   A_=7					,   A_=True					,   A_=True					,   A_=True					,   A_=True					,   A_=99					,   A_=32					,   A_=5					,   A_=4					,   A_=37					,   A_="gelu"					,   A_=0.1					,   A_=0.1					,   A_=512					,   A_=16					,   A_=2					,   A_=0.02					,   A_=4					,   )  ->       Union[str, Any]:
         __UpperCamelCase    							=parent
         __UpperCamelCase    							=batch_size
         __UpperCamelCase    							=seq_length
         __UpperCamelCase    							=is_training
         __UpperCamelCase    							=use_attention_mask
         __UpperCamelCase    							=use_token_type_ids
         __UpperCamelCase    							=use_labels
         __UpperCamelCase    							=vocab_size
         __UpperCamelCase    							=hidden_size
         __UpperCamelCase    							=num_hidden_layers
         __UpperCamelCase    							=num_attention_heads
         __UpperCamelCase    							=intermediate_size
         __UpperCamelCase    							=hidden_act
         __UpperCamelCase    							=hidden_dropout_prob
         __UpperCamelCase    							=attention_probs_dropout_prob
         __UpperCamelCase    							=max_position_embeddings
         __UpperCamelCase    							=type_vocab_size
         __UpperCamelCase    							=type_sequence_label_size
         __UpperCamelCase    							=initializer_range
         __UpperCamelCase    							=num_choices






    def _a					(			self    )  ->       Dict:
         __UpperCamelCase    							=ids_tensor([self.batch_size, self.seq_length]					,   self.vocab_size    )

         __UpperCamelCase    							=None
         if self.use_attention_mask:
              __UpperCamelCase    							=random_attention_mask([self.batch_size, self.seq_length]    )

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

         __UpperCamelCase    							=AlbertConfig(
             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					,   is_decoder=A_					,   initializer_range=self.initializer_range					,   )

         return config, input_ids, token_type_ids, attention_mask






    def _a					(			self    )  ->       Optional[Any]:
         __UpperCamelCase    							=self.prepare_config_and_inputs()
         __UpperCamelCase							,      __UpperCamelCase							,      __UpperCamelCase							,      __UpperCamelCase    							=config_and_inputs
         __UpperCamelCase    							={'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': attention_mask}
         return config, inputs_dict






@require_flax
class     UpperCAmelCase__       (					A_   ,  unittest.TestCase		):



    """simple docstring"""






    UpperCAmelCase__						:		List[str]      =  (
        (
            FlaxAlbertModel,
            FlaxAlbertForPreTraining,
            FlaxAlbertForMaskedLM,
            FlaxAlbertForMultipleChoice,
            FlaxAlbertForQuestionAnswering,
            FlaxAlbertForSequenceClassification,
            FlaxAlbertForTokenClassification,
            FlaxAlbertForQuestionAnswering,
        )
        if is_flax_available()
        else ()
    )






    def _a					(			self    )  ->       Tuple:
         __UpperCamelCase    							=FlaxAlbertModelTester(self    )






    @slow
    def _a					(			self    )  ->       str:
         for model_class_name in self.all_model_classes:
              __UpperCamelCase    							=model_class_name.from_pretrained('albert-base-v2'    )
              __UpperCamelCase    							=model(np.ones((1, 1)    )    )
              self.assertIsNotNone(A_    )






@require_flax
class     UpperCAmelCase__       (					unittest.TestCase		):



    """simple docstring"""






    @slow
    def _a					(			self    )  ->       str:
         __UpperCamelCase    							=FlaxAlbertModel.from_pretrained('albert-base-v2'    )
         __UpperCamelCase    							=np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]    )
         __UpperCamelCase    							=np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]    )
         __UpperCamelCase    							=model(A_					,   attention_mask=A_    )[0]
         __UpperCamelCase    							=(1, 11, 768)
         self.assertEqual(output.shape					,   A_    )
         __UpperCamelCase    							=np.array(
             [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]]    )

         self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]					,   A_					,   atol=1E-4    )    )






"""simple docstring"""


import math


class 				a :


    def    lowerCAmelCase_     (    self		:     Tuple      , __lowerCAmelCase		:     list[list[float]]      , __lowerCAmelCase		:     list[int]    ):
      _UpperCAmelCase         =					0.0
      _UpperCAmelCase         =					0.0
      for i in range(len(__lowerCAmelCase    )    ):
        da += math.pow((sample[i] - weights[0][i])      , 2    )
        da += math.pow((sample[i] - weights[1][i])      , 2    )
        return 0 if da > da else 1
      return 0


    def    lowerCAmelCase_     (    self		:     Union[str, Any]      , __lowerCAmelCase		:     list[list[int | float]]      , __lowerCAmelCase		:     list[int]      , __lowerCAmelCase		:     int      , __lowerCAmelCase		:     float    ):
      for i in range(len(__lowerCAmelCase    )    ):
        weights[j][i] += alpha * (sample[i] - weights[j][i])
      return weights






def 			__UpperCAmelCase    (	):







  """simple docstring"""
  # Training Examples ( m, n )
  _UpperCAmelCase         =					[[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]]

  # weight initialization ( n, C )
  _UpperCAmelCase         =					[[0.2, 0.6, 0.5, 0.9], [0.8, 0.4, 0.7, 0.3]]

  # training
  _UpperCAmelCase         =					SelfOrganizingMap()
  _UpperCAmelCase         =					3
  _UpperCAmelCase         =					0.5

  for _ in range(lowercase						):
    for j in range(len(lowercase						)						):
      # training sample
      _UpperCAmelCase         =					training_samples[j]

      # Compute the winning vector
      _UpperCAmelCase         =					self_organizing_map.get_winner(lowercase      ,lowercase						)

      # Update the winning vector
      _UpperCAmelCase         =					self_organizing_map.update(lowercase      ,lowercase      ,lowercase      ,lowercase						)

    # classify test sample
  _UpperCAmelCase         =					[0, 0, 0, 1]
  _UpperCAmelCase         =					self_organizing_map.get_winner(lowercase      ,lowercase						)

  # results
  print(f'''Clusters that the test sample belongs to : {winner}'''						)
  print(f'''Weights that have been trained : {weights}'''						)


# running the main() function
if __name__ == "__main__":
    main()
