


"""simple docstring"""


import math


class 			A__	:


     """simple docstring"""

     def 		a__						(	self:						str     ,		__a:						list[list[float]]     ,		__a:						list[int]	)->    int:
           lowerCamelCase					:					Dict        =	0.0
           lowerCamelCase					:					Tuple        =	0.0
           for i in range(len(__a	)	):
                 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 		a__						(	self:						Dict     ,		__a:						list[list[int | float]]     ,		__a:						list[int]     ,		__a:						int     ,		__a:						float	)->    list[list[int | float]]:
           for i in range(len(__a	)	):
                 weights[j][i] += alpha * (sample[i] - weights[j][i])
           return weights




def   snake_case		(     )   ->      None:
      # Training Examples ( m, n )
      lowerCamelCase					:					Union[str, Any]        =	[[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]]

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

      # training
      lowerCamelCase					:					Optional[int]        =	SelfOrganizingMap()
      lowerCamelCase					:					List[Any]        =	3
      lowerCamelCase					:					Tuple        =	0.5

      for _ in range(UpperCamelCase__    ):
            for j in range(len(UpperCamelCase__    )    ):
                  # training sample
                  lowerCamelCase					:					Any        =	training_samples[j]

                  # Compute the winning vector
                  lowerCamelCase					:					Optional[int]        =	self_organizing_map.get_winner(UpperCamelCase__			,				UpperCamelCase__    )

                  # Update the winning vector
                  lowerCamelCase					:					List[Any]        =	self_organizing_map.update(UpperCamelCase__			,				UpperCamelCase__			,				UpperCamelCase__			,				UpperCamelCase__    )

    # classify test sample
      lowerCamelCase					:					Dict        =	[0, 0, 0, 1]
      lowerCamelCase					:					Dict        =	self_organizing_map.get_winner(UpperCamelCase__			,				UpperCamelCase__    )

      # 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()








"""simple docstring"""


import unittest

from transformers import EsmConfig, is_torch_available
from transformers.testing_utils import TestCasePlus, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import EsmForMaskedLM, EsmForSequenceClassification, EsmForTokenClassification, EsmModel
    from transformers.models.esm.modeling_esm import (
        ESM_PRETRAINED_MODEL_ARCHIVE_LIST,
        EsmEmbeddings,
        create_position_ids_from_input_ids,
    )


class 			A__	:


     """simple docstring"""

     def __init__(	self:						List[str]     ,		__a:						List[str]     ,		__a:						Dict=13     ,		__a:						Tuple=7     ,		__a:						Dict=False     ,		__a:						str=True     ,		__a:						List[Any]=False     ,		__a:						Dict=True     ,		__a:						Any=33     ,		__a:						Optional[Any]=32     ,		__a:						List[Any]=5     ,		__a:						Any=4     ,		__a:						Dict=37     ,		__a:						str="gelu"     ,		__a:						str=0.1     ,		__a:						int=0.1     ,		__a:						Optional[int]=512     ,		__a:						List[Any]=16     ,		__a:						int=2     ,		__a:						int=0.02     ,		__a:						Optional[int]=3     ,		__a:						str=4     ,		__a:						Tuple=None     ,		)->    Tuple:
           lowerCamelCase					:					Union[str, Any]        =	parent
           lowerCamelCase					:					Tuple        =	batch_size
           lowerCamelCase					:					Any        =	seq_length
           lowerCamelCase					:					Any        =	is_training
           lowerCamelCase					:					Tuple        =	use_input_mask
           lowerCamelCase					:					int        =	use_token_type_ids
           lowerCamelCase					:					List[str]        =	use_labels
           lowerCamelCase					:					Optional[int]        =	vocab_size
           lowerCamelCase					:					Tuple        =	hidden_size
           lowerCamelCase					:					List[str]        =	num_hidden_layers
           lowerCamelCase					:					Optional[int]        =	num_attention_heads
           lowerCamelCase					:					Optional[Any]        =	intermediate_size
           lowerCamelCase					:					Optional[Any]        =	hidden_act
           lowerCamelCase					:					Union[str, Any]        =	hidden_dropout_prob
           lowerCamelCase					:					Optional[Any]        =	attention_probs_dropout_prob
           lowerCamelCase					:					Any        =	max_position_embeddings
           lowerCamelCase					:					str        =	type_vocab_size
           lowerCamelCase					:					List[Any]        =	type_sequence_label_size
           lowerCamelCase					:					Optional[Any]        =	initializer_range
           lowerCamelCase					:					Union[str, Any]        =	num_labels
           lowerCamelCase					:					Optional[Any]        =	num_choices
           lowerCamelCase					:					Any        =	scope

     def 		a__						(	self:						Optional[int]	)->    List[Any]:
           lowerCamelCase					:					List[Any]        =	ids_tensor([self.batch_size, self.seq_length]     ,		self.vocab_size	)

           lowerCamelCase					:					Dict        =	None
           if self.use_input_mask:
                 lowerCamelCase					:					Tuple        =	random_attention_mask([self.batch_size, self.seq_length]	)

           lowerCamelCase					:					Any        =	None
           lowerCamelCase					:					int        =	None
           lowerCamelCase					:					Union[str, Any]        =	None
           if self.use_labels:
                 lowerCamelCase					:					Optional[Any]        =	ids_tensor([self.batch_size]     ,		self.type_sequence_label_size	)
                 lowerCamelCase					:					Optional[int]        =	ids_tensor([self.batch_size, self.seq_length]     ,		self.num_labels	)
                 lowerCamelCase					:					Union[str, Any]        =	ids_tensor([self.batch_size]     ,		self.num_choices	)

           lowerCamelCase					:					List[str]        =	self.get_config()

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

     def 		a__						(	self:						Tuple	)->    Union[str, Any]:
           return EsmConfig(
               vocab_size=self.vocab_size     ,		hidden_size=self.hidden_size     ,		pad_token_id=1     ,		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     ,		)

     def 		a__						(	self:						List[Any]     ,		__a:						List[str]     ,		__a:						str     ,		__a:						Tuple     ,		__a:						List[str]     ,		__a:						List[str]     ,		__a:						str	)->    int:
           lowerCamelCase					:					Optional[int]        =	EsmModel(config=__a	)
           model.to(__a	)
           model.eval()
           lowerCamelCase					:					int        =	model(__a     ,		attention_mask=__a	)
           lowerCamelCase					:					str        =	model(__a	)
           lowerCamelCase					:					Optional[Any]        =	model(__a	)

           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 		a__						(	self:						int     ,		__a:						Union[str, Any]     ,		__a:						Optional[int]     ,		__a:						List[str]     ,		__a:						str     ,		__a:						List[str]     ,		__a:						Tuple	)->    int:
           lowerCamelCase					:					str        =	EsmForMaskedLM(config=__a	)
           model.to(__a	)
           model.eval()
           lowerCamelCase					:					List[Any]        =	model(__a     ,		attention_mask=__a     ,		labels=__a	)
           self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.seq_length, self.vocab_size)	)

     def 		a__						(	self:						List[str]     ,		__a:						List[Any]     ,		__a:						List[str]     ,		__a:						int     ,		__a:						Union[str, Any]     ,		__a:						List[Any]     ,		__a:						Tuple	)->    List[str]:
           lowerCamelCase					:					Tuple        =	self.num_labels
           lowerCamelCase					:					Dict        =	EsmForTokenClassification(config=__a	)
           model.to(__a	)
           model.eval()
           lowerCamelCase					:					int        =	model(__a     ,		attention_mask=__a     ,		labels=__a	)
           self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.seq_length, self.num_labels)	)







     def 		a__						(	self:						Optional[int]	)->    Optional[int]:
           lowerCamelCase					:					Any        =	self.prepare_config_and_inputs()
           (
               (
               lowerCamelCase
           )     ,			(
               lowerCamelCase
           )     ,			(
               lowerCamelCase
           )     ,			(
               lowerCamelCase
           )     ,			(
               lowerCamelCase
           )     ,			(
               lowerCamelCase
           )     ,			
           )					:					Tuple        =	config_and_inputs
           lowerCamelCase					:					List[Any]        =	{"""input_ids""": input_ids, """attention_mask""": input_mask}
           return config, inputs_dict


@require_torch
class 			A__	(						__lowercase ,     __lowercase ,     unittest.TestCase):


     """simple docstring"""
     snake_case__	:					Any													=False

     snake_case__	:					Dict													=(
         (
             EsmForMaskedLM,
             EsmModel,
             EsmForSequenceClassification,
             EsmForTokenClassification,
         )
         if is_torch_available()
         else ()
     )
     snake_case__	:					Dict													=()
     snake_case__	:					Optional[int]													=(
         {
             '''feature-extraction''': EsmModel,
             '''fill-mask''': EsmForMaskedLM,
             '''text-classification''': EsmForSequenceClassification,
             '''token-classification''': EsmForTokenClassification,
             '''zero-shot''': EsmForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     snake_case__	:					Any													=True

     def 		a__						(	self:						Optional[int]	)->    Optional[int]:
           lowerCamelCase					:					Optional[Any]        =	EsmModelTester(self	)
           lowerCamelCase					:					Any        =	ConfigTester(self     ,		config_class=__a     ,		hidden_size=37	)

     def 		a__						(	self:						List[Any]	)->    Optional[Any]:
           self.config_tester.run_common_tests()

     def 		a__						(	self:						int	)->    Optional[Any]:
           lowerCamelCase					:					Union[str, Any]        =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__a	)

     def 		a__						(	self:						Tuple	)->    Any:
           lowerCamelCase					:					List[str]        =	self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
                 lowerCamelCase					:					Tuple        =	type
                 self.model_tester.create_and_check_model(*__a	)

     def 		a__						(	self:						List[str]	)->    Tuple:
           lowerCamelCase					:					Optional[int]        =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*__a	)

     def 		a__						(	self:						int	)->    Optional[Any]:
           lowerCamelCase					:					List[Any]        =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*__a	)

     @slow
     def 		a__						(	self:						Any	)->    List[Any]:
           for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 lowerCamelCase					:					int        =	EsmModel.from_pretrained(__a	)
                 self.assertIsNotNone(__a	)

     def 		a__						(	self:						str	)->    List[str]:
           lowerCamelCase					:					Union[str, Any]        =	self.model_tester.prepare_config_and_inputs()[0]
           lowerCamelCase					:					Union[str, Any]        =	EsmEmbeddings(config=__a	)

           lowerCamelCase					:					List[str]        =	torch.as_tensor([[12, 31, 13, model.padding_idx]]	)
           lowerCamelCase					:					Union[str, Any]        =	torch.as_tensor(
               [
                   [
                       0 + model.padding_idx + 1,
                       1 + model.padding_idx + 1,
                       2 + model.padding_idx + 1,
                       model.padding_idx,
                   ]
               ]	)
           lowerCamelCase					:					Optional[Any]        =	create_position_ids_from_input_ids(__a     ,		model.padding_idx	)
           self.assertEqual(position_ids.shape     ,		expected_positions.shape	)
           self.assertTrue(torch.all(torch.eq(__a     ,		__a	)	)	)

     def 		a__						(	self:						Optional[int]	)->    int:
           lowerCamelCase					:					List[str]        =	self.model_tester.prepare_config_and_inputs()[0]
           lowerCamelCase					:					Any        =	EsmEmbeddings(config=__a	)

           lowerCamelCase					:					Dict        =	torch.empty(2     ,		4     ,		30	)
           lowerCamelCase					:					List[Any]        =	[
               0 + embeddings.padding_idx + 1,
               1 + embeddings.padding_idx + 1,
               2 + embeddings.padding_idx + 1,
               3 + embeddings.padding_idx + 1,
           ]
           lowerCamelCase					:					Any        =	torch.as_tensor([expected_single_positions, expected_single_positions]	)
           lowerCamelCase					:					List[str]        =	embeddings.create_position_ids_from_inputs_embeds(__a	)
           self.assertEqual(position_ids.shape     ,		expected_positions.shape	)
           self.assertTrue(torch.all(torch.eq(__a     ,		__a	)	)	)

     @unittest.skip("""Esm does not support embedding resizing"""	)
     def 		a__						(	self:						Any	)->    Optional[Any]:
           pass

     @unittest.skip("""Esm does not support embedding resizing"""	)
     def 		a__						(	self:						Dict	)->    Dict:
           pass







     @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""	)
     def 		a__						(	self:						List[str]	)->    Dict:
           pass


@require_torch
class 			A__	(						__lowercase):


     """simple docstring"""

     @slow
     def 		a__						(	self:						Any	)->    Union[str, Any]:
           with torch.no_grad():
                 lowerCamelCase					:					Union[str, Any]        =	EsmForMaskedLM.from_pretrained("""facebook/esm2_t6_8M_UR50D"""	)
                 model.eval()
                 lowerCamelCase					:					List[str]        =	torch.tensor([[0, 1, 2, 3, 4, 5]]	)
                 lowerCamelCase					:					Tuple        =	model(__a	)[0]

                 lowerCamelCase					:					Dict        =	33

                 lowerCamelCase					:					List[str]        =	torch.Size((1, 6, vocab_size)	)
                 self.assertEqual(output.shape     ,		__a	)

                 lowerCamelCase					:					Tuple        =	torch.tensor(
                     [[[8.92_15, -10.58_98, -6.46_71], [-6.39_67, -13.91_14, -1.12_12], [-7.78_12, -13.95_16, -3.74_06]]]	)
                 self.assertTrue(torch.allclose(output[:, :3, :3]     ,		__a     ,		atol=1e-4	)	)







     @slow
     def 		a__						(	self:						Dict	)->    str:
           with torch.no_grad():
                 lowerCamelCase					:					Any        =	EsmModel.from_pretrained("""facebook/esm2_t6_8M_UR50D"""	)
                 model.eval()

                 lowerCamelCase					:					Optional[Any]        =	torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]	)
                 lowerCamelCase					:					Any        =	model(__a	)[0]
                 # compare the actual values for a slice.
                 lowerCamelCase					:					Tuple        =	torch.tensor(
                     [[[0.14_44, 0.54_13, 0.32_48], [0.30_34, 0.00_53, 0.31_08], [0.32_28, -0.24_99, 0.34_15]]]	)
                 self.assertTrue(torch.allclose(output[:, :3, :3]     ,		__a     ,		atol=1e-4	)	)




