
import math







class      A__							:






				def 							_lowerCamelCase		(						self					:      int    ,     a					:      Optional[Any]    ,     a					:      List[str]   ):






							'''simple docstring'''







							lowerCAmelCase__      :      Dict         =		0.0
							lowerCAmelCase__      :      Optional[int]         =		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 							_lowerCamelCase		(						self					:      int    ,     a					:      Union[str, Any]    ,     a					:      Optional[int]    ,     a					:      Optional[Any]    ,     a					:      List[Any]   ):






							'''simple docstring'''







							for i in range(len(__a   )   ):
										weights[j][i] += alpha * (sample[i] - weights[j][i])
							return weights






def  lowerCAmelCase__			(  )				->		Tuple:
			# Training Examples ( m, n )
			lowerCAmelCase__      :      int         =		[[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]]

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

			# training
			lowerCAmelCase__      :      Optional[Any]         =		SelfOrganizingMap()
			lowerCAmelCase__      :      Any         =		3
			lowerCAmelCase__      :      Tuple         =		0.5

			for _ in range(_SCREAMING_SNAKE_CASE     ):
						for j in range(len(_SCREAMING_SNAKE_CASE     )     ):
									# training sample
									lowerCAmelCase__      :      List[str]         =		training_samples[j]

									# Compute the winning vector
									lowerCAmelCase__      :      Union[str, Any]         =		self_organizing_map.get_winner(_SCREAMING_SNAKE_CASE       ,							_SCREAMING_SNAKE_CASE     )

									# Update the winning vector
									lowerCAmelCase__      :      int         =		self_organizing_map.update(_SCREAMING_SNAKE_CASE       ,							_SCREAMING_SNAKE_CASE       ,							_SCREAMING_SNAKE_CASE       ,							_SCREAMING_SNAKE_CASE     )

    # classify test sample
			lowerCAmelCase__      :      Union[str, Any]         =		[0, 0, 0, 1]
			lowerCAmelCase__      :      Optional[int]         =		self_organizing_map.get_winner(_SCREAMING_SNAKE_CASE       ,							_SCREAMING_SNAKE_CASE     )

			# 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 torch

from transformers import AutoModel





class 							__UpperCamelCase				(		torch.nn.Module					):




	def __init__(   self						,				__a="sayef/fsner-bert-base-uncased"							):

						'''simple docstring'''





						super(__a						,				self							).__init__()

						__a			:       Tuple					    =  AutoModel.from_pretrained(__a						,				return_dict=__a							)
						__a			:       int					    =  torch.nn.CosineSimilarity(3						,				1E-0_8							)
						__a			:       Union[str, Any]					    =  torch.nn.Softmax(dim=1							)




	def       __UpperCAmelCase							(   self						,				**__a							):

						'''simple docstring'''





						return self.bert(**__a							).last_hidden_state




	def       __UpperCAmelCase							(   self						,				__a							):

						'''simple docstring'''





						return token_embeddings.sum(2						,				keepdim=__a							)




	def       __UpperCAmelCase							(   self						,				__a						,				__a						,				__a=1							):

						'''simple docstring'''





						return self.softmax(T * self.cos(__a						,				__a							)							)





	def       __UpperCAmelCase							(   self						,				__a						,				__a							):

						'''simple docstring'''





						__a			:       str					    =  W_supports['sizes'].tolist()
						__a			:       Union[str, Any]					    =  W_supports['start_token_id'].item()
						__a			:       Any					    =  W_supports['end_token_id'].item()

						del W_supports["sizes"]
						del W_supports["start_token_id"]
						del W_supports["end_token_id"]

						__a			:       Tuple					    =  self.BERT(**__a							)
						__a			:       str					    =  self.BERT(**__a							)

						__a			:       Any					    =  None
						__a			:       Dict					    =  None

						__a			:       Dict					    =  W_supports['input_ids'] == start_token_id
						__a			:       Union[str, Any]					    =  W_supports['input_ids'] == end_token_id

						for i, size in enumerate(__a							):
											if i == 0:
																__a			:       Optional[int]					    =  0
											else:
																__a			:       Union[str, Any]					    =  support_sizes[i - 1]

											__a			:       int					    =  S[s : s + size][start_token_masks[s : s + size]]
											__a			:       Union[str, Any]					    =  S[s : s + size][end_token_masks[s : s + size]]

											__a			:       Tuple					    =  torch.matmul(q[i]						,				s_start.T							).sum(1							).softmax(0							)
											__a			:       Dict					    =  torch.matmul(q[i]						,				s_end.T							).sum(1							).softmax(0							)

											if p_starts is not None:
																__a			:       str					    =  torch.vstack((p_starts, p_start)							)
																__a			:       str					    =  torch.vstack((p_ends, p_end)							)
											else:
																__a			:       List[str]					    =  p_start
																__a			:       int					    =  p_end

						return p_starts, p_ends



