Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between PyTorch, PyTorchModel in sagemaker.pytorch

I am trying to create a model using pytorch in sagemaker. I tried deploying using - PyTorch module in sagemaker.pytorch [from sagemaker.pytorch import PyTorch].

But, I want to understand what is PyTorchModel in sagemaker.pytorch [from sagemaker.pytorch import PyTorchModel]. They both have deploy() . And I followed the link https://sagemaker.readthedocs.io/en/stable/using_pytorch.html to create and deploy the model . Where I don't see using "PyTorchModel" anywhere. I would like to know the difference and when to use what.

I tried the following so far.

Step 1 : I called pytorch estimator

pytorch_model = PyTorch(entry_point='entry_v1.py',
                        train_instance_type='ml.m5.4xlarge',
                        role = role,
                        train_instance_count=1,
                        output_path = "s3://model-output-bucket/test",
                        framework_version='1.1',
                        hyperparameters = {'epochs': 10,'learning-rate': 0.01})

Step2 : I called fit method

pytorch_model.fit({'train': 's3://training-data/train_data.csv',
                  'test':'s3://testing-data/test_data.csv'})

Step3: I called deploy method.

predictor = pytorch_model.deploy(instance_type='ml.m4.xlarge', initial_instance_count=1)

I would like to know when to call create_model() here.

I got some understanding over here. We use [from sagemaker.pytorch import PyTorch] for end to end process where we train a model with .fit() and then we can deploy the model with .deploy()

But, with [from sagemaker.pytorch import PyTorchModel] we can use the model which is already trained.

Step1:

pytorch_model = PyTorchModel(model_data='s3://model-output-bucket/sagemaker-pytorch-2019-08-20-16-54-32-500/output/model.tar.gz', role=role,entry_point=entry_v1.py,sagemaker_session=sagemaker_session)

Step2:

predictor = pytorch_model.deploy(instance_type='ml.c4.xlarge', initial_instance_count=1)

Also, .create_model() of PyTorch Estimator will return an object of PyTorchModel.

Please correct me if i am wrong anywhere.

like image 214
Vaibhav Nellore Avatar asked May 13 '26 18:05

Vaibhav Nellore


1 Answers

PyTorch class is inherited from the Framework class whereas PyTorchModel is inherited from the FrameworkModel class.

The difference between these two is that: Framework is used to perform the end to end training and deployment of a model FrameworkModel is used to create an Estimator from a pretrained model and then use it to deploy an endpoint using the deploy() method. This does not involve training of the model.

In the PyTorch class, you cannot directly call the deploy model. You'll first have to invoke the fit() method which will be followed by the deploy() method.

You can read the following blog on how you can bring your own Pretrained model to Sagemaker https://aws.amazon.com/blogs/machine-learning/bring-your-own-pre-trained-mxnet-or-tensorflow-models-into-amazon-sagemaker/

Regarding the create_model() method, you need not invoke it in your script if you'd like to directly deploy an endpoint after training. It is generally used in scenarios where you require to create a pipeline for inferences through multiple models

like image 196
Ujjwal Bhardwaj Avatar answered May 15 '26 11:05

Ujjwal Bhardwaj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!