


# 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   (      A__						: Optional[int]					)     ->    List[Any]:
   return 1 / (1 + np.exp(-z					))


def    __lowerCamelCase   (      A__						: List[str]						,						A__						: List[str]					)     ->    Union[str, Any]:
   return (-y * np.log(A__					) - (1 - y) * np.log(1 - h					)).mean()


def    __lowerCamelCase   (      A__						: int						,						A__						: Dict						,						A__						: List[Any]					)     ->    Dict:
   lowerCamelCase_						:   Any		= np.dot(A__						,						A__					)
   return np.sum(y * scores - np.log(1 + np.exp(A__					)					)					)


def    __lowerCamelCase   (      A__						: Optional[int]						,						A__						: Optional[int]						,						A__						: int						,						A__						: List[Any]=7_0000					)     ->    Any:
   lowerCamelCase_						:   Optional[Any]		= np.zeros(x.shape[1]					)

   for iterations in range(A__					):
      lowerCamelCase_						:   Tuple		= np.dot(A__						,						A__					)
      lowerCamelCase_						:   Dict		= sigmoid_function(A__					)
      lowerCamelCase_						:   Tuple		= np.dot(x.T						,						h - y					) / y.size
      lowerCamelCase_						:   Dict		= theta - alpha * gradient  # updating the weights
      lowerCamelCase_						:   Tuple		= np.dot(A__						,						A__					)
      lowerCamelCase_						:   Dict		= sigmoid_function(A__					)
      lowerCamelCase_						:   List[str]		= cost_function(A__						,						A__					)
      if iterations % 100 == 0:
         print(f'''loss: {j} \t'''					)  # printing the loss after every 100 iterations
   return theta


# In[68]:

if __name__ == "__main__":
      snake_case__				:	Optional[Any]					       =					datasets.load_iris()
      snake_case__				:	Tuple					       =					iris.data[:, :2]
      snake_case__				:	int					       =					(iris.target != 0) * 1

      snake_case__				:	List[str]					       =					0.1
      snake_case__				:	Dict					       =					logistic_reg(alpha, x, y, max_iterations=7_0000)
      print('theta: ', theta)  # printing the theta i.e our weights vector


      def    __lowerCamelCase   (      A__						: Optional[Any]					)     ->    List[Any]:
         return sigmoid_function(
             np.dot(A__						,						A__					)					)  # 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')
      ((snake_case__)       ,   (snake_case__))				:	str					       =					(x[:, 0].min(), x[:, 0].max())
      ((snake_case__)       ,   (snake_case__))				:	Tuple					       =					(x[:, 1].min(), x[:, 1].max())
      ((snake_case__)       ,   (snake_case__))				:	Dict					       =					np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max))
      snake_case__				:	Any					       =					np.c_[xxa.ravel(), xxa.ravel()]
      snake_case__				:	Optional[Any]					       =					predict_prob(grid).reshape(xxa.shape)
      plt.contour(xxa, xxa, probs, [0.5], linewidths=1, colors='black')

      plt.legend()



      plt.show()









from dataclasses import dataclass, field
from typing import Optional

from transformers import AutoConfig, AutoImageProcessor, AutoTokenizer, FlaxVisionEncoderDecoderModel, HfArgumentParser





@dataclass
class   SCREAMING_SNAKE_CASE_			:




       '''simple docstring'''



       _a   							=							field(
           metadata={"help": "The output directory where the model will be written."}						,       )
       _a   							=							field(
           metadata={
               "help": (
                   "The encoder model checkpoint for weights initialization."
                   "Don't set if you want to train an encoder model from scratch."
               )
           }						,       )
       _a   							=							field(
           metadata={
               "help": (
                   "The decoder model checkpoint for weights initialization."
                   "Don't set if you want to train a decoder model from scratch."
               )
           }						,       )
       _a   							=							field(
           default=a__						,       metadata={"help": "Pretrained encoder config name or path if not the same as encoder_model_name"}						)
       _a   							=							field(
           default=a__						,       metadata={"help": "Pretrained decoder config name or path if not the same as decoder_model_name"}						)







def    __lowerCamelCase   (      )     ->    Optional[Any]:
   lowerCamelCase_						:   List[Any]		= HfArgumentParser((ModelArguments,)					)
   ((lowerCamelCase_),	)						:   List[Any]		= parser.parse_args_into_dataclasses()

   # Load pretrained model and tokenizer

   # Use explicit specified encoder config
   if model_args.encoder_config_name:
      lowerCamelCase_						:   Any		= AutoConfig.from_pretrained(model_args.encoder_config_name					)
   # Use pretrained encoder model's config
   else:
      lowerCamelCase_						:   Dict		= AutoConfig.from_pretrained(model_args.encoder_model_name_or_path					)

   # Use explicit specified decoder config
   if model_args.decoder_config_name:
      lowerCamelCase_						:   Dict		= AutoConfig.from_pretrained(model_args.decoder_config_name					)
   # Use pretrained decoder model's config
   else:
      lowerCamelCase_						:   Any		= AutoConfig.from_pretrained(model_args.decoder_model_name_or_path					)

   # necessary for `from_encoder_decoder_pretrained` when `decoder_config` is passed
   lowerCamelCase_						:   Optional[Any]		= True
   lowerCamelCase_						:   List[Any]		= True

   lowerCamelCase_						:   List[Any]		= FlaxVisionEncoderDecoderModel.from_encoder_decoder_pretrained(
       encoder_pretrained_model_name_or_path=model_args.encoder_model_name_or_path						,						decoder_pretrained_model_name_or_path=model_args.decoder_model_name_or_path						,						encoder_config=A__						,						decoder_config=A__						,						)

   # GPT2 only has bos/eos tokens but not decoder_start/pad tokens
   lowerCamelCase_						:   Tuple		= decoder_config.decoder_start_token_id
   lowerCamelCase_						:   int		= decoder_config.pad_token_id
   if decoder_start_token_id is None:
      lowerCamelCase_						:   Any		= decoder_config.bos_token_id
   if pad_token_id is None:
      lowerCamelCase_						:   List[str]		= decoder_config.eos_token_id

   # This is necessary to make Flax's generate() work
   lowerCamelCase_						:   List[str]		= decoder_config.eos_token_id
   lowerCamelCase_						:   Tuple		= decoder_start_token_id
   lowerCamelCase_						:   Optional[Any]		= pad_token_id

   lowerCamelCase_						:   str		= AutoImageProcessor.from_pretrained(model_args.encoder_model_name_or_path					)

   lowerCamelCase_						:   Any		= AutoTokenizer.from_pretrained(model_args.decoder_model_name_or_path					)
   lowerCamelCase_						:   Dict		= tokenizer.convert_ids_to_tokens(model.config.pad_token_id					)

   model.save_pretrained(model_args.output_dir					)
   image_processor.save_pretrained(model_args.output_dir					)
   tokenizer.save_pretrained(model_args.output_dir					)


if __name__ == "__main__":
      main()





