




import unittest

from transformers.utils.backbone_utils import (
    BackboneMixin,
    get_aligned_output_features_output_indices,
    verify_out_features_out_indices,
)
class 							__snake_case	(unittest.TestCase		):



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





								'''simple docstring'''




								_lowerCAmelCase     :     Any              =   ["""a""", """b""", """c"""]

								# Defaults to last layer if both are None
								_lowerCAmelCase       ,     _lowerCAmelCase     :     List[str]              =   get_aligned_output_features_output_indices(_UpperCAmelCase		,						_UpperCAmelCase		,						_UpperCAmelCase   )
								self.assertEqual(_UpperCAmelCase		,						["""c"""]   )
								self.assertEqual(_UpperCAmelCase		,						[2]   )

								# Out indices set to match out features
								_lowerCAmelCase       ,     _lowerCAmelCase     :     Any              =   get_aligned_output_features_output_indices(["""a""", """c"""]		,						_UpperCAmelCase		,						_UpperCAmelCase   )
								self.assertEqual(_UpperCAmelCase		,						["""a""", """c"""]   )
								self.assertEqual(_UpperCAmelCase		,						[0, 2]   )

								# Out features set to match out indices
								_lowerCAmelCase       ,     _lowerCAmelCase     :     str              =   get_aligned_output_features_output_indices(_UpperCAmelCase		,						[0, 2]		,						_UpperCAmelCase   )
								self.assertEqual(_UpperCAmelCase		,						["""a""", """c"""]   )
								self.assertEqual(_UpperCAmelCase		,						[0, 2]   )

								# Out features selected from negative indices
								_lowerCAmelCase       ,     _lowerCAmelCase     :     Dict              =   get_aligned_output_features_output_indices(_UpperCAmelCase		,						[-3, -1]		,						_UpperCAmelCase   )
								self.assertEqual(_UpperCAmelCase		,						["""a""", """c"""]   )
								self.assertEqual(_UpperCAmelCase		,						[-3, -1]   )



				def    SCREAMING_SNAKE_CASE							(   self   :		Dict   )	->     str:





								'''simple docstring'''




								with self.assertRaises(_UpperCAmelCase   ):
												verify_out_features_out_indices(["""a""", """b"""]		,						(0, 1)		,						_UpperCAmelCase   )

								# Out features must be a list
								with self.assertRaises(_UpperCAmelCase   ):
												verify_out_features_out_indices(("""a""", """b""")		,						(0, 1)		,						["""a""", """b"""]   )

								# Out features must be a subset of stage names
								with self.assertRaises(_UpperCAmelCase   ):
												verify_out_features_out_indices(["""a""", """b"""]		,						(0, 1)		,						["""a"""]   )

								# Out indices must be a list or tuple
								with self.assertRaises(_UpperCAmelCase   ):
												verify_out_features_out_indices(_UpperCAmelCase		,						0		,						["""a""", """b"""]   )

								# Out indices must be a subset of stage names
								with self.assertRaises(_UpperCAmelCase   ):
												verify_out_features_out_indices(_UpperCAmelCase		,						(0, 1)		,						["""a"""]   )

								# Out features and out indices must be the same length
								with self.assertRaises(_UpperCAmelCase   ):
												verify_out_features_out_indices(["""a""", """b"""]		,						(0,)		,						["""a""", """b""", """c"""]   )

								# Out features should match out indices
								with self.assertRaises(_UpperCAmelCase   ):
												verify_out_features_out_indices(["""a""", """b"""]		,						(0, 2)		,						["""a""", """b""", """c"""]   )

								# Out features and out indices should be in order
								with self.assertRaises(_UpperCAmelCase   ):
												verify_out_features_out_indices(["""b""", """a"""]		,						(0, 1)		,						["""a""", """b"""]   )

								# Check passes with valid inputs
								verify_out_features_out_indices(["""a""", """b""", """d"""]		,						(0, 1, -1)		,						["""a""", """b""", """c""", """d"""]   )



				def    SCREAMING_SNAKE_CASE							(   self   :		Tuple   )	->     str:





								'''simple docstring'''




								_lowerCAmelCase     :     str              =   BackboneMixin()

								_lowerCAmelCase     :     Optional[Any]              =   ["""a""", """b""", """c"""]
								_lowerCAmelCase     :     Dict              =   ["""a""", """c"""]
								_lowerCAmelCase     :     Optional[Any]              =   [0, 2]

								# Check that the output features and indices are set correctly
								self.assertEqual(backbone.out_features		,						["""a""", """c"""]   )
								self.assertEqual(backbone.out_indices		,						[0, 2]   )

								# Check out features and indices are updated correctly
								_lowerCAmelCase     :     Tuple              =   ["""a""", """b"""]
								self.assertEqual(backbone.out_features		,						["""a""", """b"""]   )
								self.assertEqual(backbone.out_indices		,						[0, 1]   )

								_lowerCAmelCase     :     Union[str, Any]              =   [-3, -1]
								self.assertEqual(backbone.out_features		,						["""a""", """c"""]   )
								self.assertEqual(backbone.out_indices		,						[-3, -1]   )




# Logistic Regression from scratch

# In[62]:

# In[63]:

# importing all the required libraries

import numpy as np
from matplotlib import pyplot as plt
from sklearn import datasets





def    __lowerCamelCase      (					UpperCamelCase__ ):







							'''simple docstring'''






							return 1 / (1 + np.exp(-z ))





def    __lowerCamelCase      (					UpperCamelCase__  ,				UpperCamelCase__ ):







							'''simple docstring'''






							return (-y * np.log(UpperCamelCase__ ) - (1 - y) * np.log(1 - h )).mean()





def    __lowerCamelCase      (					UpperCamelCase__  ,				UpperCamelCase__  ,				UpperCamelCase__ ):







							'''simple docstring'''






							snake_case_     							=       np.dot(UpperCamelCase__  ,				UpperCamelCase__ )
							return np.sum(y * scores - np.log(1 + np.exp(UpperCamelCase__ ) ) )





def    __lowerCamelCase      (					UpperCamelCase__  ,				UpperCamelCase__  ,				UpperCamelCase__  ,				UpperCamelCase__=70000 ):







							'''simple docstring'''






							snake_case_     							=       np.zeros(x.shape[1] )

							for iterations in range(UpperCamelCase__ ):
														snake_case_     							=       np.dot(UpperCamelCase__  ,				UpperCamelCase__ )
														snake_case_     							=       sigmoid_function(UpperCamelCase__ )
														snake_case_     							=       np.dot(x.T  ,				h - y ) / y.size
														snake_case_     							=       theta - alpha * gradient  # updating the weights
														snake_case_     							=       np.dot(UpperCamelCase__  ,				UpperCamelCase__ )
														snake_case_     							=       sigmoid_function(UpperCamelCase__ )
														snake_case_     							=       cost_function(UpperCamelCase__  ,				UpperCamelCase__ )
														if iterations % 100 == 0:
																					print(F'''loss: {j} \t''' )  # printing the loss after every 100 iterations
							return theta


# In[68]:

if __name__ == "__main__":
					_UpperCAmelCase						:  str									=			datasets.load_iris()
					_UpperCAmelCase						:  Union[str, Any]									=			iris.data[:, :2]
					_UpperCAmelCase						:  List[str]									=			(iris.target != 0) * 1

					_UpperCAmelCase						:  Optional[int]									=			0.1
					_UpperCAmelCase						:  Dict									=			logistic_reg(alpha, x, y, max_iterations=7_0000)
					print("""theta: """, theta)  # printing the theta i.e our weights vector





					def    __lowerCamelCase      (					UpperCamelCase__ ):







												'''simple docstring'''






												return sigmoid_function(
												    np.dot(UpperCamelCase__  ,				UpperCamelCase__ ) )  # predicting the value of probability from the logistic regression algorithm

					plt.figure(figsize=(10, 6))
					plt.scatter(x[y == 0][:, 0], x[y == 0][:, 1], color="""b""", label="""0""")
					plt.scatter(x[y == 1][:, 0], x[y == 1][:, 1], color="""r""", label="""1""")
					((_UpperCAmelCase)							,		(_UpperCAmelCase))						:  Any									=			(x[:, 0].min(), x[:, 0].max())
					((_UpperCAmelCase)							,		(_UpperCAmelCase))						:  Union[str, Any]									=			(x[:, 1].min(), x[:, 1].max())
					((_UpperCAmelCase)							,		(_UpperCAmelCase))						:  int									=			np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max))
					_UpperCAmelCase						:  List[Any]									=			np.c_[xxa.ravel(), xxa.ravel()]
					_UpperCAmelCase						:  Dict									=			predict_prob(grid).reshape(xxa.shape)
					plt.contour(xxa, xxa, probs, [0.5], linewidths=1, colors="""black""")

					plt.legend()


					plt.show()




