import math






class 						__a :



	def 		UpperCamelCase							(						self							:    Optional[Any]				,	snake_case_							:    list[list[float]]				,	snake_case_							:    list[int])->							int:
						__lowerCAmelCase	   =0.0
						__lowerCAmelCase	   =0.0
						for i in range(len(snake_case_)):
											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 		UpperCamelCase							(						self							:    Optional[int]				,	snake_case_							:    list[list[int | float]]				,	snake_case_							:    list[int]				,	snake_case_							:    int				,	snake_case_							:    float)->							list[list[int | float]]:
						for i in range(len(snake_case_)):
											weights[j][i] += alpha * (sample[i] - weights[j][i])
						return weights






def 					__lowerCAmelCase		(  )  ->						None:
					# Training Examples ( m, n )
					__lowerCAmelCase	   =[[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]]

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

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

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

															# Compute the winning vector
															__lowerCAmelCase	   =self_organizing_map.get_winner(__lowerCamelCase				,   __lowerCamelCase	)

															# Update the winning vector
															__lowerCAmelCase	   =self_organizing_map.update(__lowerCamelCase				,   __lowerCamelCase				,   __lowerCamelCase				,   __lowerCamelCase	)

    # classify test sample
					__lowerCAmelCase	   =[0, 0, 0, 1]
					__lowerCAmelCase	   =self_organizing_map.get_winner(__lowerCamelCase				,   __lowerCamelCase	)

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









import PIL.Image
import PIL.ImageOps
from packaging import version
from PIL import Image


if version.parse(version.parse(PIL.__version__).base_version) >= version.parse('''9.1.0'''):
				lowercase_	       =			{
				    '''linear''': PIL.Image.Resampling.BILINEAR,
				    '''bilinear''': PIL.Image.Resampling.BILINEAR,
				    '''bicubic''': PIL.Image.Resampling.BICUBIC,
				    '''lanczos''': PIL.Image.Resampling.LANCZOS,
				    '''nearest''': PIL.Image.Resampling.NEAREST,
				}
else:
				lowercase_	       =			{
				    '''linear''': PIL.Image.LINEAR,
				    '''bilinear''': PIL.Image.BILINEAR,
				    '''bicubic''': PIL.Image.BICUBIC,
				    '''lanczos''': PIL.Image.LANCZOS,
				    '''nearest''': PIL.Image.NEAREST,
				}






def 					__lowerCAmelCase		(  __lowerCamelCase	:    List[str]	)  ->						int:
					__lowerCAmelCase	   =(images / 2 + 0.5).clamp(0				,   1	)
					__lowerCAmelCase	   =images.cpu().permute(0				,   2				,   3				,   1	).float().numpy()
					__lowerCAmelCase	   =numpy_to_pil(__lowerCamelCase	)
					return images






def 					__lowerCAmelCase		(  __lowerCamelCase	:    str	)  ->						str:
					if images.ndim == 3:
										__lowerCAmelCase	   =images[None, ...]
					__lowerCAmelCase	   =(images * 255).round().astype("""uint8"""	)
					if images.shape[-1] == 1:
										# special case for grayscale (single channel) images
										__lowerCAmelCase	   =[Image.fromarray(image.squeeze()				,   mode="""L"""	) for image in images]
					else:
										__lowerCAmelCase	   =[Image.fromarray(__lowerCamelCase	) for image in images]

					return pil_images




