Making your first neural network — Part 3: Getting predictions from the network qwertpi Follow Dec 1, 2018 · Unlisted

Homemade meme

Introduction

Last time we improved accuracy and now we are going to use your model to make predictions. Having completed the previous article is not required as all you will need for this one is a model.h5 file but is recommended as it will improve your predictions. If you did run the code from last time you will have a bunch of files beginning with checkpoint- you will need to pick one of these to use and rename it to best.h5, I would recommend picking the one with the highest number after the dash.

The code!

Imports (lines 1–2)

There are very few libraries required for this 😀

Loading the model (line 3)

3 This line is fairly self explanatory, we load the model best.h5 and call it model

Data formatting (lines 6–23)

6 You should have a moves list that you are appending to with every move in the game by both the human and computer if you are in gameplay but I’ve put dummy data in it for the purposes of this tutorial. The network takes input in the form user, computer, user, computer etc. In this example, we have user: rock computer: paper user: rock computer: paper.

8–9 We make lists for our moves in numbers and our number moves as a numpy array

11–17 We add the corresponding number of each move in our moves list to our num_moves list

19–20 We then add 0s to our num_moves until there are 18 elements in it as we specified input_dim=18 for our model in our train.py file

22 We append our num_moves array to our num_moves_numpy array, you may think this is pointless to have a list inside a list but it serves a purpose. Our input to the network in training looked a bit like [[1,2],[1,2,1,2]]. Our input (which actually would have 18 elements in each internal list) had internal lists of the moves as numbers with each new piece of data being an internal list as a new element in the main list.

Although we don’t have more than one piece of data for our prediction we have to keep to the input format our model expects or keras will throw a hissy fit.

23 We then convert the num_moves_numpy list to a numpy array

Making a prediction (line 25)

25–26 We make a prediction with num_moves_numpy as our input or X. We then store this prediction in the prediction variable and print the contents of the variable. If you run it without the .argmax() You should get something like [[0.14404249 0.28447104 0.35180148 0.21968503]]. This is a list of probabilities for each output and surprise surprise is a numpy array of a list in a list. You’ll notice that the second element (remember the first is the zeroth) has the highest number, fortunately numpy arrays have a built-in function to output the position of the highest value, this is what .argmax() is

Doing something useful with the output (lines 27–43)

27–33 We create a subroutine to convert our numerical prediction back into a move in text

35–40 We create a subroutine to convert a move into the move that would beat that move as you have to remember the network predicts the user's next move so we have to play the move that would beat that

42–43 We run the subroutines on the prediction leaving you with a move that should beat the user's next move for you to use in your gameplay

Conclusion

Well done you’ve made your first neural network and can now do something useful with it! If you want to see how the network can fit into gameplay see my code on GitHub and if you want to see a game with the neural net in action I have a live demo (which can also be found below), the code for which can be found here.

For people too lazy to click through to the live demo

Please share this post on social media if you enjoyed it/found it useful. If there are any inaccuracies in this article please ensure to let me know so I can improve my knowledge and avoid giving people wrong information. Please feel free to leave feedback in the comments so I know how to improve for the next post. If you especially liked this article send me a tip to one of the cryptocurrency addresses here or tip this post, you will get a portion of all future tips if you tip the post. Any questions or problems with the code tell me in the comments. If you want help with Keras generally use the Keras google group and me or someone else will help you