Get startedGet started for free

Two-output models

1. Two-output models

In this chapter, I will cover neural networks with 2 outputs. These sorts of networks make predictions for 2 targets at once. For example, you could use a single model to predict the scores of both teams in a basketball game or use a single model to predict both the score difference and the win/loss outcome of that game. To me, this is the most interesting chapter in the class, because it teaches you things that only neural networks can do. At the end of the chapter, I will show you how to make a single model that is both a classifier and a regressor.

2. Simple model with 2 outputs

To create a model with 2 outputs, we start with an input layer, as with all keras models. In this example, there is only one predictor, so I use an Input layer with one input. To make a 2 output model, I simply make a Dense layer with 2 units for the output layer. The model will now make 2 predictions! Contrast this model with models from previous chapters, which all had single outputs, and therefore Dense output layers with a single unit. The only difference between the 2 output model and the one output model is the size of the output layer.

3. Simple model with 2 outputs

The API for creating a 2-output model and compiling it is exactly the same as for a single output model. Wrap the input and the output tensors in your call to Model(), and then compile it using the adam optimizer and mean absolute error.

4. Fitting a model with 2 outputs

To fit a model with 2 outputs, you use a dataset with 2 columns for the y variable. In this case, the training set has the seed difference for the two teams, as well the team's scores for the game. The model's single input is seed difference, and the 2 outputs are the scores for each team. The fit call is then exactly the same as a single input, single output model. The difference is that the y variable has 2 columns now. This particular model takes a while to converge, so I use 500 epochs in the fit. For the exercises, you will use some additional data in the model that will help it converge faster.

5. Inspecting a 2 output model

Now that the model is fit, you can take a look at what it learned. The dense layer has two weights and two biases. The weights indicate that each additional unit of seed difference for the input data equals about .60 additional points for team 1 (and .60 fewer points for team 2). The bias, or intercept term for each team is about 70 points, indicating that we expect an average basketball team to score about 70 points in an average game. In other words, 2 teams with a 1 point seed difference would be expected to have a score of about 69 to 71, while 2 teams with a 10 point seed difference would be expected to have a score of about 64 to 76.

6. Evaluating a model with 2 outputs

Evaluating a model with two outputs is very similar to evaluating a model with 1 output, except you provide the evaluation function a dataset with 2 columns of data for the target. In this case, the model performs reasonably well on the test set, but in the exercises, you will add some more data to get better predictions.

7. Let's practice!

Now that I've shown you how to fit a model with 2 outputs, you can practice doing it yourself.