This part is analogous to Train your own model with TensorFlow. If your own problem has already been solved satisfactorily with a PhotonAI model, you can skip this chapter. On the other hand, the creation of a model is dealt with even more briefly in this chapter and it may well make sense to read the previous chapter first. Apart from that, this is also not an introduction to PyTorch or the creation of machine learning models in general. Rather, the aim is merely to create a simple model that will serve as an example in the deployment and publication process in the following parts. The PyTorch Tutorials will help you learn more about creating machine learning models with PyTorch.
PyTorch is an open-source deep learning framework that is particularly known for its flexible and dynamic computational graph structure. It facilitates the training of neural networks through an intuitive interface and offers extensive support for multidimensional data processing. PyTorch allows developers to deploy models to various platforms, including mobile devices. The framework offers a wide range of functions and modules to meet individual requirements.
For Python, PyTorch can be installed directly in the terminal via pip:
Firstly, we define our data and project structure again. In this case, we are in the /incubaitor/1_3_Torch/notebooks/ folder with the notebook. Relative to this, we have now loaded the data into the folder /incubaitor/1_3_Torch/data/ and will save our models in /incubaitor/1_3_Torch/models/. This allows us to use the relative paths ../data/ and ../models/. If there are problems here, explicit paths can also be used as in the PhotonAI example.
After we have imported all libraries and functions, we first prepare our data analogous to the TensorFlow tutorial. It is important here that we pack our data into the data structure torch.tensor(), with which PyTorch can perform all calculations efficiently.
Similar to TensorFlow, we use the container nn.Sequential, with which we create a simple multilayer perceptron (MLP) with two hidden layers of size 64 and with ReLU activation:
Unlike TensorFlow, PyTorch does not have the compile() and fit() functions to train the MLP. Instead, we have to implement the training ourselves with the train_model() function. We also specify a loss function and an optimizer, as well as the number of epochs and the size of our batches.
During training, we are regularly informed about the training progress in the console. This allows us to estimate how long the training will take and to check that the loss is actually decreasing and that our model is converging.
To evaluate the model, we again write a function evaluate_model(), which outputs the test loss in two different metrics. We also want to visualize our results again and do this analogously to the TensorFlow tutorial.
To save the model, we can simply use torch.save(), which saves the model at the desired path. We also save all our encoders and scalers again so that we can prepare our input data correctly.
To use the model for prediction, we load it with torch.load() and create a DataFrame in which we store sample data. Again, make sure that the data is wrapped in torch.tensor().
Finally, we can predict the price of the example car:
The result is about 26788.51, which is close to the result of the PhotonAI model and the TensorFlow model and also corresponds to a realistic price.
We were therefore able to design, train and now even use our own model with PyTorch. Although this required significantly more manual steps, which were previously taken away from us by the PhotonAI Hyperpipe, we were also able to use categorical features for our prediction.