






"""simple docstring"""



import collections
import inspect
import unittest

from transformers import SwinConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch
						from torch import nn

						from transformers import SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel
						from transformers.models.swin.modeling_swin import SWIN_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import AutoImageProcessor




class 							__lowercase:






						def __init__(       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE=13     ,    _SCREAMING_SNAKE_CASE=32     ,    _SCREAMING_SNAKE_CASE=2     ,    _SCREAMING_SNAKE_CASE=3     ,    _SCREAMING_SNAKE_CASE=16     ,    _SCREAMING_SNAKE_CASE=[1, 2, 1]     ,    _SCREAMING_SNAKE_CASE=[2, 2, 4]     ,    _SCREAMING_SNAKE_CASE=2     ,    _SCREAMING_SNAKE_CASE=2.0     ,    _SCREAMING_SNAKE_CASE=True     ,    _SCREAMING_SNAKE_CASE=0.0     ,    _SCREAMING_SNAKE_CASE=0.0     ,    _SCREAMING_SNAKE_CASE=0.1     ,    _SCREAMING_SNAKE_CASE="gelu"     ,    _SCREAMING_SNAKE_CASE=False     ,    _SCREAMING_SNAKE_CASE=True     ,    _SCREAMING_SNAKE_CASE=0.02     ,    _SCREAMING_SNAKE_CASE=1E-5     ,    _SCREAMING_SNAKE_CASE=True     ,    _SCREAMING_SNAKE_CASE=None     ,    _SCREAMING_SNAKE_CASE=True     ,    _SCREAMING_SNAKE_CASE=10     ,    _SCREAMING_SNAKE_CASE=8     ,    _SCREAMING_SNAKE_CASE=["stage1", "stage2"]     ,    _SCREAMING_SNAKE_CASE=[1, 2]     ,    )      ->	List[Any]:




											"""simple docstring"""





											lowerCamelCase__					=parent
											lowerCamelCase__					=batch_size
											lowerCamelCase__					=image_size
											lowerCamelCase__					=patch_size
											lowerCamelCase__					=num_channels
											lowerCamelCase__					=embed_dim
											lowerCamelCase__					=depths
											lowerCamelCase__					=num_heads
											lowerCamelCase__					=window_size
											lowerCamelCase__					=mlp_ratio
											lowerCamelCase__					=qkv_bias
											lowerCamelCase__					=hidden_dropout_prob
											lowerCamelCase__					=attention_probs_dropout_prob
											lowerCamelCase__					=drop_path_rate
											lowerCamelCase__					=hidden_act
											lowerCamelCase__					=use_absolute_embeddings
											lowerCamelCase__					=patch_norm
											lowerCamelCase__					=layer_norm_eps
											lowerCamelCase__					=initializer_range
											lowerCamelCase__					=is_training
											lowerCamelCase__					=scope
											lowerCamelCase__					=use_labels
											lowerCamelCase__					=type_sequence_label_size
											lowerCamelCase__					=encoder_stride
											lowerCamelCase__					=out_features
											lowerCamelCase__					=out_indices






						def 		lowerCAmelCase (       self)      ->	Tuple:




											"""simple docstring"""





											lowerCamelCase__					=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

											lowerCamelCase__					=None
											if self.use_labels:
																lowerCamelCase__					=ids_tensor([self.batch_size]     ,    self.type_sequence_label_size)

											lowerCamelCase__					=self.get_config()

											return config, pixel_values, labels






						def 		lowerCAmelCase (       self)      ->	Dict:




											"""simple docstring"""





											return SwinConfig(
											    image_size=self.image_size     ,    patch_size=self.patch_size     ,    num_channels=self.num_channels     ,    embed_dim=self.embed_dim     ,    depths=self.depths     ,    num_heads=self.num_heads     ,    window_size=self.window_size     ,    mlp_ratio=self.mlp_ratio     ,    qkv_bias=self.qkv_bias     ,    hidden_dropout_prob=self.hidden_dropout_prob     ,    attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,    drop_path_rate=self.drop_path_rate     ,    hidden_act=self.hidden_act     ,    use_absolute_embeddings=self.use_absolute_embeddings     ,    path_norm=self.patch_norm     ,    layer_norm_eps=self.layer_norm_eps     ,    initializer_range=self.initializer_range     ,    encoder_stride=self.encoder_stride     ,    out_features=self.out_features     ,    out_indices=self.out_indices     ,    )






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	Dict:




											"""simple docstring"""





											lowerCamelCase__					=SwinModel(config=_SCREAMING_SNAKE_CASE)
											model.to(_SCREAMING_SNAKE_CASE)
											model.eval()
											lowerCamelCase__					=model(_SCREAMING_SNAKE_CASE)

											lowerCamelCase__					=((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths) - 1))
											lowerCamelCase__					=int(config.embed_dim * 2 ** (len(config.depths) - 1))

											self.parent.assertEqual(result.last_hidden_state.shape     ,    (self.batch_size, expected_seq_len, expected_dim))






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	int:




											"""simple docstring"""





											lowerCamelCase__					=SwinBackbone(config=_SCREAMING_SNAKE_CASE)
											model.to(_SCREAMING_SNAKE_CASE)
											model.eval()
											lowerCamelCase__					=model(_SCREAMING_SNAKE_CASE)

											# verify hidden states
											self.parent.assertEqual(len(result.feature_maps)     ,    len(config.out_features))
											self.parent.assertListEqual(list(result.feature_maps[0].shape)     ,    [self.batch_size, model.channels[0], 16, 16])

											# verify channels
											self.parent.assertEqual(len(model.channels)     ,    len(config.out_features))

											# verify backbone works with out_features=None
											lowerCamelCase__					=None
											lowerCamelCase__					=SwinBackbone(config=_SCREAMING_SNAKE_CASE)
											model.to(_SCREAMING_SNAKE_CASE)
											model.eval()
											lowerCamelCase__					=model(_SCREAMING_SNAKE_CASE)

											# verify feature maps
											self.parent.assertEqual(len(result.feature_maps)     ,    1)
											self.parent.assertListEqual(list(result.feature_maps[0].shape)     ,    [self.batch_size, model.channels[-1], 4, 4])

											# verify channels
											self.parent.assertEqual(len(model.channels)     ,    1)






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	Dict:




											"""simple docstring"""





											lowerCamelCase__					=SwinForMaskedImageModeling(config=_SCREAMING_SNAKE_CASE)
											model.to(_SCREAMING_SNAKE_CASE)
											model.eval()
											lowerCamelCase__					=model(_SCREAMING_SNAKE_CASE)
											self.parent.assertEqual(
											    result.logits.shape     ,    (self.batch_size, self.num_channels, self.image_size, self.image_size))

											# test greyscale images
											lowerCamelCase__					=1
											lowerCamelCase__					=SwinForMaskedImageModeling(_SCREAMING_SNAKE_CASE)
											model.to(_SCREAMING_SNAKE_CASE)
											model.eval()

											lowerCamelCase__					=floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
											lowerCamelCase__					=model(_SCREAMING_SNAKE_CASE)
											self.parent.assertEqual(result.logits.shape     ,    (self.batch_size, 1, self.image_size, self.image_size))






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	str:




											"""simple docstring"""





											lowerCamelCase__					=self.type_sequence_label_size
											lowerCamelCase__					=SwinForImageClassification(_SCREAMING_SNAKE_CASE)
											model.to(_SCREAMING_SNAKE_CASE)
											model.eval()
											lowerCamelCase__					=model(_SCREAMING_SNAKE_CASE     ,    labels=_SCREAMING_SNAKE_CASE)
											self.parent.assertEqual(result.logits.shape     ,    (self.batch_size, self.type_sequence_label_size))

											# test greyscale images
											lowerCamelCase__					=1
											lowerCamelCase__					=SwinForImageClassification(_SCREAMING_SNAKE_CASE)
											model.to(_SCREAMING_SNAKE_CASE)
											model.eval()

											lowerCamelCase__					=floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
											lowerCamelCase__					=model(_SCREAMING_SNAKE_CASE)
											self.parent.assertEqual(result.logits.shape     ,    (self.batch_size, self.type_sequence_label_size))






						def 		lowerCAmelCase (       self)      ->	Any:




											"""simple docstring"""





											lowerCamelCase__					=self.prepare_config_and_inputs()
											(
											    (
											    lowerCamelCase__
											)					,  (
											    lowerCamelCase__
											)					,  (
											    lowerCamelCase__
											)					,  
											)					=config_and_inputs
											lowerCamelCase__					={'pixel_values': pixel_values}
											return config, inputs_dict




@require_torch
class 							__lowercase(							_UpperCamelCase    ,				_UpperCamelCase    ,				unittest.TestCase	):
						lowerCAmelCase_		:				int=							(
						    (
						        SwinModel,
						        SwinBackbone,
						        SwinForImageClassification,
						        SwinForMaskedImageModeling,
						    )
						    if is_torch_available()
						    else ()
						)
						lowerCAmelCase_		:				Tuple=							(
						    {"""feature-extraction""": SwinModel, """image-classification""": SwinForImageClassification}
						    if is_torch_available()
						    else {}
						)
						lowerCAmelCase_		:				int=							True

						lowerCAmelCase_		:				Union[str, Any]=							False
						lowerCAmelCase_		:				List[str]=							False
						lowerCAmelCase_		:				Any=							False






						def 		lowerCAmelCase (       self)      ->	str:




											"""simple docstring"""





											lowerCamelCase__					=SwinModelTester(self)
											lowerCamelCase__					=ConfigTester(self     ,    config_class=_SCREAMING_SNAKE_CASE     ,    embed_dim=37)






						def 		lowerCAmelCase (       self)      ->	int:




											"""simple docstring"""





											self.create_and_test_config_common_properties()
											self.config_tester.create_and_test_config_to_json_string()
											self.config_tester.create_and_test_config_to_json_file()
											self.config_tester.create_and_test_config_from_and_save_pretrained()
											self.config_tester.create_and_test_config_with_num_labels()
											self.config_tester.check_config_can_be_init_without_params()
											self.config_tester.check_config_arguments_init()






						def 		lowerCAmelCase (       self)      ->	Dict:




											"""simple docstring"""





											return






						def 		lowerCAmelCase (       self)      ->	List[Any]:




											"""simple docstring"""





											lowerCamelCase__					=self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE)






						@unittest.skip(reason='Got `CUDA error: misaligned address` with PyTorch 2.0.0.')
						def 		lowerCAmelCase (       self)      ->	Dict:




											"""simple docstring"""





											pass






						def 		lowerCAmelCase (       self)      ->	Any:




											"""simple docstring"""





											super().test_training_gradient_checkpointing()






						def 		lowerCAmelCase (       self)      ->	Optional[Any]:




											"""simple docstring"""





											lowerCamelCase__					=self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_backbone(*_SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase (       self)      ->	List[str]:




											"""simple docstring"""





											lowerCamelCase__					=self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_image_modeling(*_SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase (       self)      ->	Union[str, Any]:




											"""simple docstring"""





											lowerCamelCase__					=self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE)






						@unittest.skip(reason='Swin does not use inputs_embeds')
						def 		lowerCAmelCase (       self)      ->	Tuple:




											"""simple docstring"""





											pass






						@unittest.skip(reason='Swin Transformer does not use feedforward chunking')
						def 		lowerCAmelCase (       self)      ->	List[str]:




											"""simple docstring"""





											pass






						def 		lowerCAmelCase (       self)      ->	str:




											"""simple docstring"""





											lowerCamelCase__					,  lowerCamelCase__					=self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																lowerCamelCase__					=model_class(_SCREAMING_SNAKE_CASE)
																self.assertIsInstance(model.get_input_embeddings()     ,    (nn.Module))
																lowerCamelCase__					=model.get_output_embeddings()
																self.assertTrue(x is None or isinstance(_SCREAMING_SNAKE_CASE     ,    nn.Linear))






						def 		lowerCAmelCase (       self)      ->	Union[str, Any]:




											"""simple docstring"""





											lowerCamelCase__					,  lowerCamelCase__					=self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																lowerCamelCase__					=model_class(_SCREAMING_SNAKE_CASE)
																lowerCamelCase__					=inspect.signature(model.forward)
																# signature.parameters is an OrderedDict => so arg_names order is deterministic
																lowerCamelCase__					=[*signature.parameters.keys()]

																lowerCamelCase__					=['pixel_values']
																self.assertListEqual(arg_names[:1]     ,    _SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase (       self)      ->	Union[str, Any]:




											"""simple docstring"""





											lowerCamelCase__					,  lowerCamelCase__					=self.model_tester.prepare_config_and_inputs_for_common()
											lowerCamelCase__					=True

											for model_class in self.all_model_classes:
																lowerCamelCase__					=True
																lowerCamelCase__					=False
																lowerCamelCase__					=True
																lowerCamelCase__					=model_class(_SCREAMING_SNAKE_CASE)
																model.to(_SCREAMING_SNAKE_CASE)
																model.eval()
																with torch.no_grad():
																					lowerCamelCase__					=model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE))
																lowerCamelCase__					=outputs.attentions
																lowerCamelCase__					=len(self.model_tester.depths)
																self.assertEqual(len(_SCREAMING_SNAKE_CASE)     ,    _SCREAMING_SNAKE_CASE)

																# check that output_attentions also work using config
																del inputs_dict["output_attentions"]
																lowerCamelCase__					=True
																lowerCamelCase__					=config.window_size**2
																lowerCamelCase__					=model_class(_SCREAMING_SNAKE_CASE)
																model.to(_SCREAMING_SNAKE_CASE)
																model.eval()
																with torch.no_grad():
																					lowerCamelCase__					=model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE))
																lowerCamelCase__					=outputs.attentions
																self.assertEqual(len(_SCREAMING_SNAKE_CASE)     ,    _SCREAMING_SNAKE_CASE)

																self.assertListEqual(
																    list(attentions[0].shape[-3:])     ,    [self.model_tester.num_heads[0], window_size_squared, window_size_squared]     ,    )
																lowerCamelCase__					=len(_SCREAMING_SNAKE_CASE)

																# Check attention is always last and order is fine
																lowerCamelCase__					=True
																lowerCamelCase__					=True
																lowerCamelCase__					=model_class(_SCREAMING_SNAKE_CASE)
																model.to(_SCREAMING_SNAKE_CASE)
																model.eval()
																with torch.no_grad():
																					lowerCamelCase__					=model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE))

																# also another +1 for reshaped_hidden_states
																lowerCamelCase__					=1 if model_class.__name__ == 'SwinBackbone' else 2
																self.assertEqual(out_len + added_hidden_states     ,    len(_SCREAMING_SNAKE_CASE))

																lowerCamelCase__					=outputs.attentions

																self.assertEqual(len(_SCREAMING_SNAKE_CASE)     ,    _SCREAMING_SNAKE_CASE)

																self.assertListEqual(
																    list(self_attentions[0].shape[-3:])     ,    [self.model_tester.num_heads[0], window_size_squared, window_size_squared]     ,    )






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	Tuple:




											"""simple docstring"""





											lowerCamelCase__					=model_class(_SCREAMING_SNAKE_CASE)
											model.to(_SCREAMING_SNAKE_CASE)
											model.eval()

											with torch.no_grad():
																lowerCamelCase__					=model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE))

											lowerCamelCase__					=outputs.hidden_states

											lowerCamelCase__					=getattr(
											    self.model_tester     ,    'expected_num_hidden_layers'     ,    len(self.model_tester.depths) + 1)
											self.assertEqual(len(_SCREAMING_SNAKE_CASE)     ,    _SCREAMING_SNAKE_CASE)

											# Swin has a different seq_length
											lowerCamelCase__					=(
											    config.patch_size
											    if isinstance(config.patch_size     ,    collections.abc.Iterable)
											    else (config.patch_size, config.patch_size)
											)

											lowerCamelCase__					=(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

											self.assertListEqual(
											    list(hidden_states[0].shape[-2:])     ,    [num_patches, self.model_tester.embed_dim]     ,    )

											if not model_class.__name__ == "SwinBackbone":
																lowerCamelCase__					=outputs.reshaped_hidden_states
																self.assertEqual(len(_SCREAMING_SNAKE_CASE)     ,    _SCREAMING_SNAKE_CASE)

																lowerCamelCase__					,  lowerCamelCase__					,  lowerCamelCase__					,  lowerCamelCase__					=reshaped_hidden_states[0].shape
																lowerCamelCase__					=(
																    reshaped_hidden_states[0].view(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    height * width).permute(0     ,    2     ,    1)
																)
																self.assertListEqual(
																    list(reshaped_hidden_states.shape[-2:])     ,    [num_patches, self.model_tester.embed_dim]     ,    )






						def 		lowerCAmelCase (       self)      ->	Optional[Any]:




											"""simple docstring"""





											lowerCamelCase__					,  lowerCamelCase__					=self.model_tester.prepare_config_and_inputs_for_common()

											lowerCamelCase__					=(
											    self.model_tester.image_size
											    if isinstance(self.model_tester.image_size     ,    collections.abc.Iterable)
											    else (self.model_tester.image_size, self.model_tester.image_size)
											)

											for model_class in self.all_model_classes:
																lowerCamelCase__					=True
																self.check_hidden_states_output(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)

																# check that output_hidden_states also work using config
																del inputs_dict["output_hidden_states"]
																lowerCamelCase__					=True

																self.check_hidden_states_output(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase (       self)      ->	Tuple:




											"""simple docstring"""





											lowerCamelCase__					,  lowerCamelCase__					=self.model_tester.prepare_config_and_inputs_for_common()
											lowerCamelCase__					=3

											lowerCamelCase__					=(
											    self.model_tester.image_size
											    if isinstance(self.model_tester.image_size     ,    collections.abc.Iterable)
											    else (self.model_tester.image_size, self.model_tester.image_size)
											)
											lowerCamelCase__					=(
											    config.patch_size
											    if isinstance(config.patch_size     ,    collections.abc.Iterable)
											    else (config.patch_size, config.patch_size)
											)

											lowerCamelCase__					=image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
											lowerCamelCase__					=image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

											for model_class in self.all_model_classes:
																lowerCamelCase__					=True
																self.check_hidden_states_output(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    (padded_height, padded_width))

																# check that output_hidden_states also work using config
																del inputs_dict["output_hidden_states"]
																lowerCamelCase__					=True
																self.check_hidden_states_output(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    (padded_height, padded_width))






						@slow
						def 		lowerCAmelCase (       self)      ->	Union[str, Any]:




											"""simple docstring"""





											for model_name in SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																lowerCamelCase__					=SwinModel.from_pretrained(_SCREAMING_SNAKE_CASE)
																self.assertIsNotNone(_SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase (       self)      ->	Tuple:




											"""simple docstring"""





											lowerCamelCase__					,  lowerCamelCase__					=self.model_tester.prepare_config_and_inputs_for_common()

											lowerCamelCase__					=_config_zero_init(_SCREAMING_SNAKE_CASE)
											for model_class in self.all_model_classes:
																lowerCamelCase__					=model_class(config=_SCREAMING_SNAKE_CASE)
																for name, param in model.named_parameters():
																					if "embeddings" not in name and param.requires_grad:
																										self.assertIn(
																										    ((param.data.mean() * 1E9).round() / 1E9).item()     ,    [0.0, 1.0]     ,    msg=F"Parameter {name} of model {model_class} seems not properly initialized"     ,    )




@require_vision
@require_torch
class 							__lowercase(							unittest.TestCase	):






						@cached_property
						def 		lowerCAmelCase (       self)      ->	str:




											"""simple docstring"""





											return (
											    AutoImageProcessor.from_pretrained('microsoft/swin-tiny-patch4-window7-224')
											    if is_vision_available()
											    else None
											)






						@slow
						def 		lowerCAmelCase (       self)      ->	Dict:




											"""simple docstring"""





											lowerCamelCase__					=SwinForImageClassification.from_pretrained('microsoft/swin-tiny-patch4-window7-224').to(_SCREAMING_SNAKE_CASE)
											lowerCamelCase__					=self.default_image_processor

											lowerCamelCase__					=Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png')
											lowerCamelCase__					=image_processor(images=_SCREAMING_SNAKE_CASE     ,    return_tensors='pt').to(_SCREAMING_SNAKE_CASE)

											# forward pass
											with torch.no_grad():
																lowerCamelCase__					=model(**_SCREAMING_SNAKE_CASE)

											# verify the logits
											lowerCamelCase__					=torch.Size((1, 10_00))
											self.assertEqual(outputs.logits.shape     ,    _SCREAMING_SNAKE_CASE)
											lowerCamelCase__					=torch.tensor([-0.0948, -0.6454, -0.0921]).to(_SCREAMING_SNAKE_CASE)
											self.assertTrue(torch.allclose(outputs.logits[0, :3]     ,    _SCREAMING_SNAKE_CASE     ,    atol=1E-4))




@require_torch
class 							__lowercase(							unittest.TestCase    ,				_UpperCamelCase	):
						lowerCAmelCase_		:				Any=							(SwinBackbone,) if is_torch_available() else ()
						lowerCAmelCase_		:				int=							SwinConfig






						def 		lowerCAmelCase (       self)      ->	Optional[Any]:




											"""simple docstring"""





											lowerCamelCase__					=SwinModelTester(self)







"""simple docstring"""



import random




class 							__lowercase:






						def __init__(       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE = 0.01     ,    _SCREAMING_SNAKE_CASE = 10_00     ,    _SCREAMING_SNAKE_CASE = -1     ,    )      ->	None:




											"""simple docstring"""





											lowerCamelCase__					=sample
											if len(self.sample) == 0:
																raise ValueError('Sample data can not be empty')
											lowerCamelCase__					=target
											if len(self.target) == 0:
																raise ValueError('Target data can not be empty')
											if len(self.sample) != len(self.target):
																raise ValueError('Sample data and Target data do not have matching lengths')
											lowerCamelCase__					=learning_rate
											lowerCamelCase__					=epoch_number
											lowerCamelCase__					=bias
											lowerCamelCase__					=len(_SCREAMING_SNAKE_CASE)
											lowerCamelCase__					=len(sample[0])  # number of columns in dataset
											lowerCamelCase__					=[]






						def 		lowerCAmelCase (       self)      ->	None:




											"""simple docstring"""





											for sample in self.sample:
																sample.insert(0     ,    self.bias)

											for _ in range(self.col_sample):
																self.weight.append(random.random())

											self.weight.insert(0     ,    self.bias)

											lowerCamelCase__					=0

											while True:
																lowerCamelCase__					=False
																for i in range(self.number_sample):
																					lowerCamelCase__					=0
																					for j in range(self.col_sample + 1):
																										lowerCamelCase__					=u + self.weight[j] * self.sample[i][j]
																					lowerCamelCase__					=self.sign(_SCREAMING_SNAKE_CASE)
																					if y != self.target[i]:
																										for j in range(self.col_sample + 1):
																															lowerCamelCase__					=(
																															    self.weight[j]
																															    + self.learning_rate
																															    * (self.target[i] - y)
																															    * self.sample[i][j]
																															)
																										lowerCamelCase__					=True
            # print('Epoch: \n',epoch_count)
																lowerCamelCase__					=epoch_count + 1
																# if you want control the epoch or just by error
																if not has_misclassified:
																					print(('\nEpoch:\n', epoch_count))
																					print('------------------------\n')
																					# if epoch_count > self.epoch_number or not error:
																					break






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE)      ->	None:




											"""simple docstring"""





											if len(self.sample) == 0:
																raise ValueError('Sample data can not be empty')
											sample.insert(0     ,    self.bias)
											lowerCamelCase__					=0
											for i in range(self.col_sample + 1):
																lowerCamelCase__					=u + self.weight[i] * sample[i]

											lowerCamelCase__					=self.sign(_SCREAMING_SNAKE_CASE)

											if y == -1:
																print(('Sample: ', sample))
																print('classification: P1')
											else:
																print(('Sample: ', sample))
																print('classification: P2')






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE)      ->	int:




											"""simple docstring"""





											return 1 if u >= 0 else -1



_snake_case							:   int	  =       [
    [-0.6508, 0.1097, 4.0009],
    [-1.4492, 0.8896, 4.4005],
    [2.0850, 0.6876, 12.0710],
    [0.2626, 1.1476, 7.7985],
    [0.6418, 1.0234, 7.0427],
    [0.2569, 0.6730, 8.3265],
    [1.1155, 0.6043, 7.4446],
    [0.0914, 0.3399, 7.0677],
    [0.0121, 0.5256, 4.6316],
    [-0.0429, 0.4660, 5.4323],
    [0.4340, 0.6870, 8.2287],
    [0.2735, 1.0287, 7.1934],
    [0.4839, 0.4851, 7.4850],
    [0.4089, -0.1267, 5.5019],
    [1.4391, 0.1614, 8.5843],
    [-0.9115, -0.1973, 2.1962],
    [0.3654, 1.0475, 7.4858],
    [0.2144, 0.7515, 7.1699],
    [0.2013, 1.0014, 6.5489],
    [0.6483, 0.2183, 5.8991],
    [-0.1147, 0.2242, 7.2435],
    [-0.7970, 0.8795, 3.8762],
    [-1.0625, 0.6366, 2.4707],
    [0.5307, 0.1285, 5.6883],
    [-1.2200, 0.7777, 1.7252],
    [0.3957, 0.1076, 5.6623],
    [-0.1013, 0.5989, 7.1812],
    [2.4482, 0.9455, 11.2095],
    [2.0149, 0.6192, 10.9263],
    [0.2012, 0.2611, 5.4631],
]

_snake_case							:   List[str]	  =       [
    -1,
    -1,
    -1,
    1,
    1,
    -1,
    1,
    -1,
    1,
    1,
    -1,
    1,
    -1,
    -1,
    -1,
    -1,
    1,
    1,
    1,
    1,
    -1,
    1,
    1,
    1,
    1,
    -1,
    -1,
    1,
    -1,
    1,
]


if __name__ == "__main__":
						import doctest

						doctest.testmod()

						_snake_case							:   int	  =       Perceptron(
						    sample=samples, target=target, learning_rate=0.01, epoch_number=1_000, bias=-1
						)
						network.training()
						print("Finished training perceptron")
						print("Enter values to predict or q to exit")
						while True:
												_snake_case							:   list	  =       []
												for i in range(len(samples[0])):
																		_snake_case							:   List[str]	  =       input("value: ").strip()
																		if user_input == "q":
																								break
																		_snake_case							:   Optional[Any]	  =       float(user_input)
																		sample.insert(i, observation)
												network.sort(sample)