Book Image

Distributed Machine Learning with Python

By : Guanhua Wang
Book Image

Distributed Machine Learning with Python

By: Guanhua Wang

Overview of this book

Reducing time cost in machine learning leads to a shorter waiting time for model training and a faster model updating cycle. Distributed machine learning enables machine learning practitioners to shorten model training and inference time by orders of magnitude. With the help of this practical guide, you'll be able to put your Python development knowledge to work to get up and running with the implementation of distributed machine learning, including multi-node machine learning systems, in no time. You'll begin by exploring how distributed systems work in the machine learning area and how distributed machine learning is applied to state-of-the-art deep learning models. As you advance, you'll see how to use distributed systems to enhance machine learning model training and serving speed. You'll also get to grips with applying data parallel and model parallel approaches before optimizing the in-parallel model training and serving pipeline in local clusters or cloud environments. By the end of this book, you'll have gained the knowledge and skills needed to build and deploy an efficient data processing pipeline for machine learning model training and inference in a distributed manner.
Table of Contents (17 chapters)
1
Section 1 – Data Parallelism
6
Section 2 – Model Parallelism
11
Section 3 – Advanced Parallelism Paradigms

NLP model serving

Now, we will discuss NLP model serving. We will assume that the model has been successfully trained with your own custom data:

  1. First, we define our questions and answers, as follows:
    string1 = "Packt is a publisher"
    string2 = "Who is Packt ?"
    index_tokens = tokenizer.encode(string1, string2, add_special_tokens=True)

Basically, we define a question-and-answer pair as string1 and string2 and then tokenize both strings.

  1. Then, we convert the preceding tokens to torch tensors, as follows:
    tokens_tensors = torch.tensor([index_tokens])
  2. Then, we can conduct NLP model serving for question answering, as follows:
    with torch.no_grad():
        out = model(tokens_tensors, 
                    token_type_ids = segments_tensors)
    ans = tokenizer.decode(index_tokens
               [torch.argmax(out...