azure ML how should my score script look like to deploy my ml model

Yu, Hazel (APEX SYSTEMS LLC) 6 Reputation points
2022-01-27T21:08:39.657+00:00

Hi,
I've created made an basic ml model just for the demo purpose and
here is the sample output from the model that I want to send to the eventhub from azure ml,

169203-image.png

I know I need score.py script when deploying the model and I wonder how the score script should be like to get the desired output that I want.

any help would be very appreciated.

Azure Machine Learning
Azure Machine Learning
An Azure machine learning service for building and deploying models.
2,560 questions
{count} votes

1 answer

Sort by: Most helpful
  1. romungi-MSFT 41,961 Reputation points Microsoft Employee
    2022-01-28T06:31:01.693+00:00

    @Yu, Hazel (APEX SYSTEMS LLC) If you have already able to test your model then your scoring script is essentially should try to load the model and define a input and output schema based on the input/output value types. This will validate your input data and generate a swagger document when you deploy your model. For example, I think your scoring script can be defined as below:

    import joblib  
    import numpy as np  
    import os  
      
    from inference_schema.schema_decorators import input_schema, output_schema  
    from inference_schema.parameter_types.numpy_parameter_type import NumpyParameterType  
      
      
    # The init() method is called once, when the web service starts up.  
    #  
    # Typically you would deserialize the model file, as shown here using joblib,  
    # and store it in a global variable so your run() method can access it later.  
    def init():  
        global model  
      
        # The AZUREML_MODEL_DIR environment variable indicates  
        # a directory containing the model file you registered.  
        model_filename = 'your_model.pkl'  
        model_path = os.path.join(os.environ['AZUREML_MODEL_DIR'], model_filename)  
      
        model = joblib.load(model_path)  
      
      
    # The run() method is called each time a request is made to the scoring API.  
    #  
    # Shown here are the optional input_schema and output_schema decorators  
    # from the inference-schema pip package. Using these decorators on your  
    # run() method parses and validates the incoming payload against  
    # the example input you provide here. This will also generate a Swagger  
    # API document for your web service.  
    @input_schema('data', NumpyParameterType(np.array([[0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0]])))  
    @output_schema(NumpyParameterType(np.array([4429.929236457418])))  
    def run(data):  
        # Use the model object loaded by init().  
        result = model.predict(data)  
      
        # You can return any JSON-serializable object.  
        return result.tolist()  
    

    Ref: Scoring script from Azure ML Notebooks github repo.

    If an answer is helpful, please click on 130616-image.png or upvote 130671-image.png which might help other community members reading this thread.

    1 person found this answer helpful.
    0 comments No comments