Skip to content

Local mode support for Generic Estimators #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions src/sagemaker/estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ class Estimator(EstimatorBase):
def __init__(self, image_name, role, train_instance_count, train_instance_type,
train_volume_size=30, train_max_run=24 * 60 * 60, input_mode='File',
output_path=None, output_kms_key=None, base_job_name=None, sagemaker_session=None,
hyperparameters=None):
hyperparameters=None, local_mode=False):
"""Initialize an ``Estimator`` instance.

Args:
Expand Down Expand Up @@ -414,10 +414,11 @@ def __init__(self, image_name, role, train_instance_count, train_instance_type,
hyperparameters (dict): Dictionary containing the hyperparameters to initialize this estimator with.
"""
self.image_name = image_name
self.hyperparam_dict = hyperparameters.copy() if hyperparameters else {}
self._hyperparameters = hyperparameters or {}

super(Estimator, self).__init__(role, train_instance_count, train_instance_type,
train_volume_size, train_max_run, input_mode,
output_path, output_kms_key, base_job_name, sagemaker_session)
output_path, output_kms_key, base_job_name, sagemaker_session, local_mode)

def train_image(self):
"""
Expand All @@ -428,16 +429,16 @@ def train_image(self):
"""
return self.image_name

def set_hyperparameters(self, **kwargs):
for k, v in kwargs.items():
self.hyperparam_dict[k] = v

def hyperparameters(self):
"""Returns the hyperparameters as a dictionary to use for training.
"""Return the hyperparameters as a dictionary to use for training.

The fit() method, that does the model training, calls this method to find the hyperparameters you specified.


The fit() method, that does the model training, calls this method to find the hyperparameters you specified.
Returns:
dict[str, str]: The hyperparameters.
"""
return self.hyperparam_dict
return _json_encode_hyperparameters(self._hyperparameters)

def create_model(self, image=None, predictor_cls=None, serializer=None, deserializer=None,
content_type=None, accept=None, **kwargs):
Expand Down Expand Up @@ -589,7 +590,7 @@ def hyperparameters(self):
Returns:
dict[str, str]: The hyperparameters.
"""
return self._json_encode_hyperparameters(self._hyperparameters)
return _json_encode_hyperparameters(self._hyperparameters)

@classmethod
def _prepare_init_params_from_job_description(cls, job_details):
Expand Down Expand Up @@ -647,10 +648,6 @@ def attach(cls, training_job_name, sagemaker_session=None):
estimator.uploaded_code = UploadedCode(estimator.source_dir, estimator.entry_point)
return estimator

@staticmethod
def _json_encode_hyperparameters(hyperparameters):
return {str(k): json.dumps(v) for (k, v) in hyperparameters.items()}

@classmethod
def _update_init_params(cls, hp, tf_arguments):
updated_params = {}
Expand All @@ -660,3 +657,7 @@ def _update_init_params(cls, hp, tf_arguments):
value = json.loads(value)
updated_params[argument] = value
return updated_params


def _json_encode_hyperparameters(hyperparameters):
return {str(k): json.dumps(v) for (k, v) in hyperparameters.items()}
6 changes: 5 additions & 1 deletion src/sagemaker/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,11 @@ def _download_folder(bucket_name, prefix, target, session=None):
except:
pass

obj.download_file(file_path)
try:
obj.download_file(file_path)
except OSError as e:
if e.errno == 21: # it is a directory
pass


def _compose(tmpdir, instance_type, detached=False):
Expand Down
16 changes: 0 additions & 16 deletions src/sagemaker/keras/__init__.py

This file was deleted.

53 changes: 0 additions & 53 deletions src/sagemaker/keras/estimator.py

This file was deleted.

53 changes: 0 additions & 53 deletions src/sagemaker/keras/model.py

This file was deleted.

3 changes: 1 addition & 2 deletions src/sagemaker/local_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, boto_session=None):
self.created_endpoint = False

def create_training_job(self, TrainingJobName, AlgorithmSpecification, RoleArn, InputDataConfig, OutputDataConfig,
ResourceConfig, StoppingCondition, HyperParameters, Tags=None):
ResourceConfig, StoppingCondition, HyperParameters={}, Tags=None):
self.train_instance_count = ResourceConfig['InstanceCount']

self.s3_model_artifacts = train(AlgorithmSpecification, InputDataConfig, ResourceConfig, HyperParameters,
Expand Down Expand Up @@ -94,7 +94,6 @@ def create_endpoint(self, EndpointName, EndpointConfigName):
print("Container is up")
return


def delete_endpoint(self, EndpointName):
self.container.down()

Expand Down
4 changes: 1 addition & 3 deletions src/sagemaker/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def train(self, image, input_mode, input_config, role, job_name, output_config,
'TrainingImage': image,
'TrainingInputMode': input_mode
},
# 'HyperParameters': hyperparameters,
'HyperParameters': hyperparameters or {},
'InputDataConfig': input_config,
'OutputDataConfig': output_config,
'TrainingJobName': job_name,
Expand All @@ -232,8 +232,6 @@ def train(self, image, input_mode, input_config, role, job_name, output_config,
"RoleArn": role,
}

if hyperparameters and len(hyperparameters) > 0:
train_request['HyperParameters'] = hyperparameters
LOGGER.info('Creating training-job with name: {}'.format(job_name))
LOGGER.debug('train request: {}'.format(json.dumps(train_request, indent=4)))
self.sagemaker_client.create_training_job(**train_request)
Expand Down