





"""simple docstring"""


import unittest

import torch

from diffusers import VQModel
from diffusers.utils import floats_tensor, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()



class      __SCREAMING_SNAKE_CASE    (						_UpperCamelCase		,  _UpperCamelCase		,  unittest.TestCase   ):
  SCREAMING_SNAKE_CASE__							:Optional[Any]	     =					VQModel
  SCREAMING_SNAKE_CASE__							:List[Any]	     =					"sample"

  @property
  def    __SCREAMING_SNAKE_CASE				(		self   :       List[Any]					,   __a   :       Tuple=(32, 32)	)		->				int:
        _UpperCamelCase      :	List[Any]						=       4
        _UpperCamelCase      :	Dict						=       3

        _UpperCamelCase      :	Any						=       floats_tensor((batch_size, num_channels) + sizes	).to(__a	)

        return {"sample": image}

  @property
  def    __SCREAMING_SNAKE_CASE				(		self   :       Dict	)		->				Optional[Any]:
        return (3, 32, 32)

  @property
  def    __SCREAMING_SNAKE_CASE				(		self   :       Optional[int]	)		->				Any:
        return (3, 32, 32)

  def    __SCREAMING_SNAKE_CASE				(		self   :       List[Any]	)		->				List[str]:
        _UpperCamelCase      :	Tuple						=       {
            "block_out_channels": [32, 64],
            "in_channels": 3,
            "out_channels": 3,
            "down_block_types": ["DownEncoderBlock2D", "DownEncoderBlock2D"],
            "up_block_types": ["UpDecoderBlock2D", "UpDecoderBlock2D"],
            "latent_channels": 3,
        }
        _UpperCamelCase      :	List[Any]						=       self.dummy_input
        return init_dict, inputs_dict

  def    __SCREAMING_SNAKE_CASE				(		self   :       Dict	)		->				Tuple:
        pass

  def    __SCREAMING_SNAKE_CASE				(		self   :       int	)		->				Optional[Any]:
        pass

  def    __SCREAMING_SNAKE_CASE				(		self   :       Optional[int]	)		->				Tuple:
        _UpperCamelCase      :	Any						=       VQModel.from_pretrained("fusing/vqgan-dummy"					,   output_loading_info=__a	)
        self.assertIsNotNone(__a	)
        self.assertEqual(len(loading_info["missing_keys"]	)					,   0	)

        model.to(__a	)
        _UpperCamelCase      :	List[str]						=       model(**self.dummy_input	)

        assert image is not None, "Make sure output is not None"


  def    __SCREAMING_SNAKE_CASE				(		self   :       Tuple	)		->				Any:
        _UpperCamelCase      :	Dict						=       VQModel.from_pretrained("fusing/vqgan-dummy"	)
        model.to(__a	).eval()

        torch.manual_seed(0	)
        if torch.cuda.is_available():
              torch.cuda.manual_seed_all(0	)

        _UpperCamelCase      :	Dict						=       torch.randn(1					,   model.config.in_channels					,   model.config.sample_size					,   model.config.sample_size	)
        _UpperCamelCase      :	str						=       image.to(__a	)
        with torch.no_grad():
              _UpperCamelCase      :	List[str]						=       model(__a	).sample

        _UpperCamelCase      :	str						=       output[0, -1, -3:, -3:].flatten().cpu()
        # fmt: off
        _UpperCamelCase      :	str						=       torch.tensor([-0.01_53, -0.40_44, -0.18_80, -0.51_61, -0.24_18, -0.40_72, -0.16_12, -0.06_33, -0.01_43]	)
        # fmt: on
        self.assertTrue(torch.allclose(__a					,   __a					,   atol=1e-3	)	)







"""simple docstring"""


import math



class      __SCREAMING_SNAKE_CASE    :







  '''simple docstring'''


  def    __SCREAMING_SNAKE_CASE				(		self   :       Any					,   __a   :       list[list[float]]					,   __a   :       list[int]	)		->				int:
        _UpperCamelCase      :	List[Any]						=       0.0
        _UpperCamelCase      :	Union[str, Any]						=       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    __SCREAMING_SNAKE_CASE				(		self   :       Optional[Any]					,   __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 					lowercase__    (					)					->		None:




      """simple docstring"""

      _UpperCamelCase      :	Optional[int]						=       [[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]]

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

      # training
      _UpperCamelCase      :	List[Any]						=       SelfOrganizingMap()
      _UpperCamelCase      :	int						=       3
      _UpperCamelCase      :	List[Any]						=       0.5

      for _ in range(lowercase_			):
            for j in range(len(lowercase_			)			):
                  # training sample
                  _UpperCamelCase      :	int						=       training_samples[j]

                  # Compute the winning vector
                  _UpperCamelCase      :	Tuple						=       self_organizing_map.get_winner(lowercase_		,lowercase_			)

                  # Update the winning vector
                  _UpperCamelCase      :	int						=       self_organizing_map.update(lowercase_		,lowercase_		,lowercase_		,lowercase_			)

    # classify test sample
      _UpperCamelCase      :	Optional[int]						=       [0, 0, 0, 1]
      _UpperCamelCase      :	Dict						=       self_organizing_map.get_winner(lowercase_		,lowercase_			)

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