





from random import shuffle

import tensorflow as tf
from numpy import array





def   lowerCamelCase__       (       UpperCamelCase__     :    str    ,      UpperCamelCase__     :    List[str]  )     ->	Optional[Any]:



     '''simple docstring'''




     _snake_case													=			int(lowercase_  )
     assert noofclusters < len(lowercase_  )

     # Find out the dimensionality
     _snake_case													=			len(vectors[0]  )

     # Will help select random centroids from among the available vectors
     _snake_case													=			list(range(len(lowercase_  )  )  )
     shuffle(lowercase_  )

     # GRAPH OF COMPUTATION
     # We initialize a new graph and set it as the default during each run
     # of this algorithm. This ensures that as this function is called
     # multiple times, the default graph doesn't keep getting crowded with
     # unused ops and Variables from previous function calls.

     _snake_case													=			tf.Graph()

     with graph.as_default():
          # SESSION OF COMPUTATION

          _snake_case													=			tf.Session()

          ##CONSTRUCTING THE ELEMENTS OF COMPUTATION

          ##First lets ensure we have a Variable vector for each centroid,
          ##initialized to one of the vectors from the available data points
          _snake_case													=			[
              tf.Variable(vectors[vector_indices[i]]  ) for i in range(lowercase_  )
          ]
          ##These nodes will assign the centroid Variables the appropriate
          ##values
          _snake_case													=			tf.placeholder('float64'    ,      [dim]  )
          _snake_case													=			[]
          for centroid in centroids:
               cent_assigns.append(tf.assign(lowercase_    ,      lowercase_  )  )

          ##Variables for cluster assignments of individual vectors(initialized
          ##to 0 at first)
          _snake_case													=			[tf.Variable(0  ) for i in range(len(lowercase_  )  )]
          ##These nodes will assign an assignment Variable the appropriate
          ##value
          _snake_case													=			tf.placeholder('int32'  )
          _snake_case													=			[]
          for assignment in assignments:
               cluster_assigns.append(tf.assign(lowercase_    ,      lowercase_  )  )

          ##Now lets construct the node that will compute the mean
          # The placeholder for the input
          _snake_case													=			tf.placeholder('float'    ,      [None, dim]  )
          # The Node/op takes the input and computes a mean along the 0th
          # dimension, i.e. the list of input vectors
          _snake_case													=			tf.reduce_mean(lowercase_    ,      0  )

          ##Node for computing Euclidean distances
          # Placeholders for input
          _snake_case													=			tf.placeholder('float'    ,      [dim]  )
          _snake_case													=			tf.placeholder('float'    ,      [dim]  )
          _snake_case													=			tf.sqrt(tf.reduce_sum(tf.pow(tf.sub(lowercase_    ,      lowercase_  )    ,      2  )  )  )

          ##This node will figure out which cluster to assign a vector to,
          ##based on Euclidean distances of the vector from the centroids.
          # Placeholder for input
          _snake_case													=			tf.placeholder('float'    ,      [noofclusters]  )
          _snake_case													=			tf.argmin(lowercase_    ,      0  )

          ##INITIALIZING STATE VARIABLES

          ##This will help initialization of all Variables defined with respect
          ##to the graph. The Variable-initializer should be defined after
          ##all the Variables have been constructed, so that each of them
          ##will be included in the initialization.
          _snake_case													=			tf.initialize_all_variables()

          # Initialize all variables
          sess.run(lowercase_  )

          ##CLUSTERING ITERATIONS

          # Now perform the Expectation-Maximization steps of K-Means clustering
          # iterations. To keep things simple, we will only do a set number of
          # iterations, instead of using a Stopping Criterion.
          _snake_case													=			100
          for _ in range(lowercase_  ):
               ##EXPECTATION STEP
               ##Based on the centroid locations till last iteration, compute
               ##the _expected_ centroid assignments.
               # Iterate over each vector
               for vector_n in range(len(lowercase_  )  ):
                    _snake_case													=			vectors[vector_n]
                    # Compute Euclidean distance between this vector and each
                    # centroid. Remember that this list cannot be named
                    #'centroid_distances', since that is the input to the
                    # cluster assignment node.
                    _snake_case													=			[
                        sess.run(lowercase_    ,      feed_dict={va: vect, va: sess.run(lowercase_  )}  )
                        for centroid in centroids
                    ]
                    # Now use the cluster assignment node, with the distances
                    # as the input
                    _snake_case													=			sess.run(
                        lowercase_    ,      feed_dict={centroid_distances: distances}  )
                    # Now assign the value to the appropriate state variable
                    sess.run(
                        cluster_assigns[vector_n]    ,      feed_dict={assignment_value: assignment}  )

               ##MAXIMIZATION STEP
               # Based on the expected state computed from the Expectation Step,
               # compute the locations of the centroids so as to maximize the
               # overall objective of minimizing within-cluster Sum-of-Squares
               for cluster_n in range(lowercase_  ):
                    # Collect all the vectors assigned to this cluster
                    _snake_case													=			[
                        vectors[i]
                        for i in range(len(lowercase_  )  )
                        if sess.run(assignments[i]  ) == cluster_n
                    ]
                    # Compute new centroid location
                    _snake_case													=			sess.run(
                        lowercase_    ,      feed_dict={mean_input: array(lowercase_  )}  )
                    # Assign value to appropriate variable
                    sess.run(
                        cent_assigns[cluster_n]    ,      feed_dict={centroid_value: new_location}  )

        # Return centroids and assignments
          _snake_case													=			sess.run(lowercase_  )
          _snake_case													=			sess.run(lowercase_  )
          return centroids, assignments










import unittest
from typing import Tuple

import torch

from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device
from diffusers.utils.testing_utils import require_torch
@require_torch
class      UpperCamelCase_						:




					@property
					def 		lowerCAmelCase     (						self					)						->							int:
										return self.get_dummy_input()




					@property
					def 		lowerCAmelCase     (						self					)						->							Optional[Any]:
										if self.block_type == "down":
															return (4, 32, 16, 16)
										elif self.block_type == "mid":
															return (4, 32, 32, 32)
										elif self.block_type == "up":
															return (4, 32, 64, 64)

										raise ValueError(F'''\'{self.block_type}\' is not a supported block_type. Set it to \'up\', \'mid\', or \'down\'.'''					)




					def 		lowerCAmelCase     (						self			,  lowerCAmelCase_=True			,  lowerCAmelCase_=False			,  lowerCAmelCase_=False			,  lowerCAmelCase_=False			,  )						->							List[str]:
										_snake_case													=			4
										_snake_case													=			32
										_snake_case													=			(32, 32)

										_snake_case													=			torch.manual_seed(0					)
										_snake_case													=			torch.device(lowerCAmelCase_					)
										_snake_case													=			(batch_size, num_channels) + sizes
										_snake_case													=			randn_tensor(lowerCAmelCase_			,  generator=lowerCAmelCase_			,  device=lowerCAmelCase_					)
										_snake_case													=			{'hidden_states': hidden_states}

										if include_temb:
															_snake_case													=			128
															_snake_case													=			randn_tensor((batch_size, temb_channels)			,  generator=lowerCAmelCase_			,  device=lowerCAmelCase_					)

										if include_res_hidden_states_tuple:
															_snake_case													=			torch.manual_seed(1					)
															_snake_case													=			(randn_tensor(lowerCAmelCase_			,  generator=lowerCAmelCase_			,  device=lowerCAmelCase_					),)

										if include_encoder_hidden_states:
															_snake_case													=			floats_tensor((batch_size, 32, 32)					).to(lowerCAmelCase_					)

										if include_skip_sample:
															_snake_case													=			randn_tensor(((batch_size, 3) + sizes)			,  generator=lowerCAmelCase_			,  device=lowerCAmelCase_					)

										return dummy_input




					def 		lowerCAmelCase     (						self					)						->							Tuple:
										_snake_case													=			{
										    'in_channels': 32,
										    'out_channels': 32,
										    'temb_channels': 128,
										}
										if self.block_type == "up":
															_snake_case													=			32

										if self.block_type == "mid":
															init_dict.pop('out_channels'					)

										_snake_case													=			self.dummy_input
										return init_dict, inputs_dict




					def 		lowerCAmelCase     (						self			,  lowerCAmelCase_					)						->							Optional[int]:
										_snake_case							,       _snake_case													=			self.prepare_init_args_and_inputs_for_common()
										_snake_case													=			self.block_class(**lowerCAmelCase_					)
										unet_block.to(lowerCAmelCase_					)
										unet_block.eval()

										with torch.no_grad():
															_snake_case													=			unet_block(**lowerCAmelCase_					)

										if isinstance(lowerCAmelCase_			,  lowerCAmelCase_					):
															_snake_case													=			output[0]

										self.assertEqual(output.shape			,  self.output_shape					)

										_snake_case													=			output[0, -1, -3:, -3:]
										_snake_case													=			torch.tensor(lowerCAmelCase_					).to(lowerCAmelCase_					)
										assert torch_all_close(output_slice.flatten()			,  lowerCAmelCase_			,  atol=5E-3					)






					@unittest.skipIf(torch_device == 'mps'			,  'Training is not supported in mps'					)
					def 		lowerCAmelCase     (						self					)						->							Tuple:
										_snake_case							,       _snake_case													=			self.prepare_init_args_and_inputs_for_common()
										_snake_case													=			self.block_class(**lowerCAmelCase_					)
										model.to(lowerCAmelCase_					)
										model.train()
										_snake_case													=			model(**lowerCAmelCase_					)

										if isinstance(lowerCAmelCase_			,  lowerCAmelCase_					):
															_snake_case													=			output[0]

										_snake_case													=			torch.device(lowerCAmelCase_					)
										_snake_case													=			randn_tensor(output.shape			,  device=lowerCAmelCase_					)
										_snake_case													=			torch.nn.functional.mse_loss(lowerCAmelCase_			,  lowerCAmelCase_					)
										loss.backward()



