


'''simple docstring'''





import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()






@slow
@require_torch_gpu
class   __lowercase				(  unittest.TestCase  ):





      def        _lowercase				(		self						:					Tuple       )    ->   str:






         """simple docstring"""







         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()





      def        _lowercase				(		self						:					int       )    ->   Tuple:






         """simple docstring"""







         UpperCAmelCase							=     StableDiffusionKDiffusionPipeline.from_pretrained("""CompVis/stable-diffusion-v1-4"""       )
         UpperCAmelCase							=     sd_pipe.to(A_       )
         sd_pipe.set_progress_bar_config(disable=A_       )

         sd_pipe.set_scheduler("""sample_euler"""       )

         UpperCAmelCase							=     """A painting of a squirrel eating a burger"""
         UpperCAmelCase							=     torch.manual_seed(0       )
         UpperCAmelCase							=     sd_pipe([prompt]					,       generator=A_					,       guidance_scale=9.0					,       num_inference_steps=2_0					,       output_type="""np"""       )

         UpperCAmelCase							=     output.images

         UpperCAmelCase							=     image[0, -3:, -3:, -1]

         assert image.shape == (1, 5_1_2, 5_1_2, 3)
         UpperCAmelCase							=     np.array([0.0_447, 0.0_492, 0.0_468, 0.0_408, 0.0_383, 0.0_408, 0.0_354, 0.0_380, 0.0_339]       )

         assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2





      def        _lowercase				(		self						:					Optional[Any]       )    ->   Tuple:






         """simple docstring"""







         UpperCAmelCase							=     StableDiffusionKDiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""       )
         UpperCAmelCase							=     sd_pipe.to(A_       )
         sd_pipe.set_progress_bar_config(disable=A_       )

         sd_pipe.set_scheduler("""sample_euler"""       )

         UpperCAmelCase							=     """A painting of a squirrel eating a burger"""
         UpperCAmelCase							=     torch.manual_seed(0       )
         UpperCAmelCase							=     sd_pipe([prompt]					,       generator=A_					,       guidance_scale=9.0					,       num_inference_steps=2_0					,       output_type="""np"""       )

         UpperCAmelCase							=     output.images

         UpperCAmelCase							=     image[0, -3:, -3:, -1]

         assert image.shape == (1, 5_1_2, 5_1_2, 3)
         UpperCAmelCase							=     np.array([0.1_237, 0.1_320, 0.1_438, 0.1_359, 0.1_390, 0.1_132, 0.1_277, 0.1_175, 0.1_112]       )

         assert np.abs(image_slice.flatten() - expected_slice       ).max() < 5e-1





      def        _lowercase				(		self						:					List[str]       )    ->   Optional[Any]:






         """simple docstring"""







         UpperCAmelCase							=     StableDiffusionKDiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""       )
         UpperCAmelCase							=     sd_pipe.to(A_       )
         sd_pipe.set_progress_bar_config(disable=A_       )

         sd_pipe.set_scheduler("""sample_dpmpp_2m"""       )

         UpperCAmelCase							=     """A painting of a squirrel eating a burger"""
         UpperCAmelCase							=     torch.manual_seed(0       )
         UpperCAmelCase							=     sd_pipe(
             [prompt]					,       generator=A_					,       guidance_scale=7.5					,       num_inference_steps=1_5					,       output_type="""np"""					,       use_karras_sigmas=A_					,       )

         UpperCAmelCase							=     output.images

         UpperCAmelCase							=     image[0, -3:, -3:, -1]

         assert image.shape == (1, 5_1_2, 5_1_2, 3)
         UpperCAmelCase							=     np.array(
             [0.11_381_689, 0.12_112_921, 0.1_389_457, 0.12_549_606, 0.1_244_964, 0.10_831_517, 0.11_562_866, 0.10_867_816, 0.10_499_048]       )

         assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2







import math






class   __lowercase				:





      def        _lowercase				(		self						:					Union[str, Any]					,       __lowerCamelCase						:					list[list[float]]					,       __lowerCamelCase						:					list[int]       )    ->   int:






         """simple docstring"""







         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        _lowercase				(		self						:					List[Any]					,       __lowerCamelCase						:					list[list[int | float]]					,       __lowerCamelCase						:					list[int]					,       __lowerCamelCase						:					int					,       __lowerCamelCase						:					float       )    ->   list[list[int | float]]:






         """simple docstring"""







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

def   _UpperCamelCase				(						)						->None:
   # 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(lowerCAmelCase_   ):
      for j in range(len(lowerCAmelCase_   )   ):
         # training sample
         UpperCAmelCase							=     training_samples[j]

         # Compute the winning vector
         UpperCAmelCase							=     self_organizing_map.get_winner(lowerCAmelCase_  ,						lowerCAmelCase_   )

         # Update the winning vector
         UpperCAmelCase							=     self_organizing_map.update(lowerCAmelCase_  ,						lowerCAmelCase_  ,						lowerCAmelCase_  ,						lowerCAmelCase_   )

    # classify test sample
   UpperCAmelCase							=     [0, 0, 0, 1]
   UpperCAmelCase							=     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()



