
"""simple docstring"""




import unittest

import numpy as np
import torch

from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()







class     lowercase(unittest.TestCase       ):



     @property
     def 			lowercase__  (							self   )		->     Optional[Any]:







         """simple docstring"""



         torch.manual_seed(0   )
         a__		=			UNetaDModel(
             block_out_channels=(3_2, 6_4)     ,				layers_per_block=2     ,				sample_size=3_2     ,				in_channels=3     ,				out_channels=3     ,				down_block_types=('DownBlock2D', 'AttnDownBlock2D')     ,				up_block_types=('AttnUpBlock2D', 'UpBlock2D')     ,				)
         return model




     def 			lowercase__  (							self   )		->     str:







         """simple docstring"""



         a__		=			self.dummy_uncond_unet
         a__		=			PNDMScheduler()

         a__		=			PNDMPipeline(unet=__SCREAMING_SNAKE_CASE     ,				scheduler=__SCREAMING_SNAKE_CASE   )
         pndm.to(__SCREAMING_SNAKE_CASE   )
         pndm.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE   )

         a__		=			torch.manual_seed(0   )
         a__		=			pndm(generator=__SCREAMING_SNAKE_CASE     ,				num_inference_steps=2_0     ,				output_type='numpy'   ).images

         a__		=			torch.manual_seed(0   )
         a__		=			pndm(generator=__SCREAMING_SNAKE_CASE     ,				num_inference_steps=2_0     ,				output_type='numpy'     ,				return_dict=__SCREAMING_SNAKE_CASE   )[0]

         a__		=			image[0, -3:, -3:, -1]
         a__		=			image_from_tuple[0, -3:, -3:, -1]

         assert image.shape == (1, 3_2, 3_2, 3)
         a__		=			np.array([1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0]   )

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







@slow
@require_torch
class     lowercase(unittest.TestCase       ):



     def 			lowercase__  (							self   )		->     int:







         """simple docstring"""



         a__		=			'google/ddpm-cifar10-32'

         a__		=			UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE   )
         a__		=			PNDMScheduler()

         a__		=			PNDMPipeline(unet=__SCREAMING_SNAKE_CASE     ,				scheduler=__SCREAMING_SNAKE_CASE   )
         pndm.to(__SCREAMING_SNAKE_CASE   )
         pndm.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE   )
         a__		=			torch.manual_seed(0   )
         a__		=			pndm(generator=__SCREAMING_SNAKE_CASE     ,				output_type='numpy'   ).images

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

         assert image.shape == (1, 3_2, 3_2, 3)
         a__		=			np.array([0.15_64, 0.1_46_45, 0.14_06, 0.1_47_15, 0.1_24_25, 0.1_40_45, 0.1_31_15, 0.1_21_75, 0.1_25]   )

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




"""simple docstring"""




import math







class     lowercase:



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )		->     int:







         """simple docstring"""



         a__		=			0.0
         a__		=			0.0
         for i in range(len(__SCREAMING_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 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )		->     list[list[int | float]]:







         """simple docstring"""



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




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

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

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

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

            # Compute the winning vector
            a__		=			self_organizing_map.get_winner(UpperCamelCase							,		UpperCamelCase     )

            # Update the winning vector
            a__		=			self_organizing_map.update(UpperCamelCase							,		UpperCamelCase							,		UpperCamelCase							,		UpperCamelCase     )

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


