How to use the sagemaker.session.Session function in sagemaker

To help you get started, we’ve selected a few sagemaker examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github aws / sagemaker-python-sdk / src / sagemaker / model_monitor / model_monitoring.py View on Github external
def attach(cls, monitor_schedule_name, sagemaker_session=None):
        """Sets this object's schedule name to point to the Amazon Sagemaker Monitoring Schedule
        name provided. This allows subsequent describe_schedule or list_executions calls to point
        to the given schedule.

        Args:
            monitor_schedule_name (str): The name of the schedule to attach to.
            sagemaker_session (sagemaker.session.Session): Session object which
                manages interactions with Amazon SageMaker APIs and any other
                AWS services needed. If not specified, one is created using
                the default AWS configuration chain.

        """
        sagemaker_session = sagemaker_session or Session()
        schedule_desc = sagemaker_session.describe_monitoring_schedule(
            monitoring_schedule_name=monitor_schedule_name
        )

        role = schedule_desc["MonitoringScheduleConfig"]["MonitoringJobDefinition"]["RoleArn"]
        instance_count = schedule_desc["MonitoringScheduleConfig"]["MonitoringJobDefinition"][
            "MonitoringResources"
        ]["ClusterConfig"]["InstanceCount"]
        instance_type = schedule_desc["MonitoringScheduleConfig"]["MonitoringJobDefinition"][
            "MonitoringResources"
        ]["ClusterConfig"]["InstanceType"]
        volume_size_in_gb = schedule_desc["MonitoringScheduleConfig"]["MonitoringJobDefinition"][
            "MonitoringResources"
        ]["ClusterConfig"]["VolumeSizeInGB"]
        volume_kms_key = schedule_desc["MonitoringScheduleConfig"]["MonitoringJobDefinition"][
            "MonitoringResources"
github aws / sagemaker-python-sdk / src / sagemaker / model_monitor / monitoring_files.py View on Github external
Args:
            constraints_file_string (str): The uri of the constraints JSON file.
            kms_key (str): The kms key to be used to encrypt the file in S3.
            file_name (str): The file name to use when uploading to S3.
            sagemaker_session (sagemaker.session.Session): A SageMaker Session
                object, used for SageMaker interactions (default: None). If not
                specified, one is created using the default AWS configuration
                chain.

        Returns:
            sagemaker.model_monitor.Constraints: The instance of Constraints generated from
                the s3 uri.

        """
        sagemaker_session = sagemaker_session or Session()
        file_name = file_name or "constraints.json"
        desired_s3_uri = os.path.join(
            "s3://", sagemaker_session.default_bucket(), "monitoring", str(uuid.uuid4()), file_name
        )
        s3_uri = S3Uploader.upload_string_as_file_body(
            body=constraints_file_string,
            desired_s3_uri=desired_s3_uri,
            kms_key=kms_key,
            session=sagemaker_session,
        )

        return Constraints.from_s3_uri(
            constraints_file_s3_uri=s3_uri, kms_key=kms_key, sagemaker_session=sagemaker_session
        )
github aws / sagemaker-python-sdk / src / sagemaker / amazon / object2vec.py View on Github external
def __init__(self, model_data, role, sagemaker_session=None, **kwargs):
        """
        Args:
            model_data:
            role:
            sagemaker_session:
            **kwargs:
        """
        sagemaker_session = sagemaker_session or Session()
        repo = "{}:{}".format(Object2Vec.repo_name, Object2Vec.repo_version)
        image = "{}/{}".format(
            registry(sagemaker_session.boto_session.region_name, Object2Vec.repo_name), repo
        )
        super(Object2VecModel, self).__init__(
            model_data,
            image,
            role,
            predictor_cls=RealTimePredictor,
            sagemaker_session=sagemaker_session,
            **kwargs
        )
github aws / sagemaker-python-sdk / src / sagemaker / amazon / ntm.py View on Github external
def __init__(self, model_data, role, sagemaker_session=None, **kwargs):
        """
        Args:
            model_data:
            role:
            sagemaker_session:
            **kwargs:
        """
        sagemaker_session = sagemaker_session or Session()
        repo = "{}:{}".format(NTM.repo_name, NTM.repo_version)
        image = "{}/{}".format(
            registry(sagemaker_session.boto_session.region_name, NTM.repo_name), repo
        )
        super(NTMModel, self).__init__(
            model_data,
            image,
            role,
            predictor_cls=NTMPredictor,
            sagemaker_session=sagemaker_session,
            **kwargs
        )
github aws / sagemaker-python-sdk / src / sagemaker / amazon / lda.py View on Github external
def __init__(self, model_data, role, sagemaker_session=None, **kwargs):
        """
        Args:
            model_data:
            role:
            sagemaker_session:
            **kwargs:
        """
        sagemaker_session = sagemaker_session or Session()
        repo = "{}:{}".format(LDA.repo_name, LDA.repo_version)
        image = "{}/{}".format(
            registry(sagemaker_session.boto_session.region_name, LDA.repo_name), repo
        )
        super(LDAModel, self).__init__(
            model_data,
            image,
            role,
            predictor_cls=LDAPredictor,
            sagemaker_session=sagemaker_session,
            **kwargs
        )
github Meeshkan / meeshkan-client / meeshkan / core / sagemaker_monitor.py View on Github external
"""
        if self.connection_tried:
            if self.connection_succeeded:
                return
            if not self.connection_succeeded:
                raise SageMakerNotAvailableException(message=self._error_message or None)

        self.connection_tried = True

        self.client = self.client or SageMakerHelper.build_client_or_none()

        if not self.client:
            self._error_message = "Could not create boto client. Check your credentials"
            raise SageMakerNotAvailableException(self._error_message)

        self.sagemaker_session = self.sagemaker_session or sagemaker.session.Session(sagemaker_client=self.client)

        try:
            self.client.list_training_jobs()
            LOGGER.info("SageMaker client successfully verified.")
        except Exception:  # pylint:disable=broad-except
            LOGGER.exception("Could not verify SageMaker connection")
            self._error_message = "Could not connect to SageMaker. Check your authorization."
            raise SageMakerNotAvailableException(self._error_message)

        self.connection_succeeded = True
github aws / sagemaker-python-sdk / src / sagemaker / transformer.py View on Github external
def attach(cls, transform_job_name, sagemaker_session=None):
        """Attach an existing transform job to a new Transformer instance

        Args:
            transform_job_name (str): Name for the transform job to be attached.
            sagemaker_session (sagemaker.session.Session): Session object which
                manages interactions with Amazon SageMaker APIs and any other
                AWS services needed. If not specified, one will be created using
                the default AWS configuration chain.

        Returns:
            sagemaker.transformer.Transformer: The Transformer instance with the
            specified transform job attached.
        """
        sagemaker_session = sagemaker_session or Session()

        job_details = sagemaker_session.sagemaker_client.describe_transform_job(
            TransformJobName=transform_job_name
        )
        init_params = cls._prepare_init_params_from_job_description(job_details)
        transformer = cls(sagemaker_session=sagemaker_session, **init_params)
        transformer.latest_transform_job = _TransformJob(
            sagemaker_session=sagemaker_session, job_name=init_params["base_transform_job_name"]
        )

        return transformer
github aws / sagemaker-python-sdk / src / sagemaker / automl / automl.py View on Github external
self.volume_kms_key = volume_kms_key
        self.encrypt_inter_container_traffic = encrypt_inter_container_traffic
        self.vpc_config = vpc_config
        self.problem_type = problem_type
        self.max_candidate = max_candidates
        self.max_runtime_per_training_job_in_seconds = max_runtime_per_training_job_in_seconds
        self.total_job_runtime_in_seconds = total_job_runtime_in_seconds
        self.target_attribute_name = target_attribute_name
        self.job_objective = job_objective
        self.generate_candidate_definitions_only = generate_candidate_definitions_only
        self.tags = tags

        self.current_job_name = None
        self._auto_ml_job_desc = None
        self._best_candidate = None
        self.sagemaker_session = sagemaker_session or Session()

        self._check_problem_type_and_job_objective(self.problem_type, self.job_objective)
github aws / sagemaker-python-sdk / src / sagemaker / s3.py View on Github external
def read_file(s3_uri, session=None):
        """Static method that returns the contents of an s3 uri file body as a string.

        Args:
            s3_uri (str): An S3 uri that refers to a single file.
            session (sagemaker.session.Session): AWS session to use. Automatically
                generates one if not provided.

        Returns:
            str: The body of the file.

        """
        sagemaker_session = session or Session()
        bucket, key_prefix = parse_s3_url(url=s3_uri)

        return sagemaker_session.read_s3_file(bucket=bucket, key_prefix=key_prefix)
github aws / sagemaker-python-sdk / src / sagemaker / amazon / randomcutforest.py View on Github external
def __init__(self, model_data, role, sagemaker_session=None, **kwargs):
        """
        Args:
            model_data:
            role:
            sagemaker_session:
            **kwargs:
        """
        sagemaker_session = sagemaker_session or Session()
        repo = "{}:{}".format(RandomCutForest.repo_name, RandomCutForest.repo_version)
        image = "{}/{}".format(
            registry(sagemaker_session.boto_session.region_name, RandomCutForest.repo_name), repo
        )
        super(RandomCutForestModel, self).__init__(
            model_data,
            image,
            role,
            predictor_cls=RandomCutForestPredictor,
            sagemaker_session=sagemaker_session,
            **kwargs
        )