Building a tf.data.Dataset from a generator function.
How to use this demo.
This demo is meant to illustrate how to train a model using a tf.data.Dataset
constructed from a generator using
In this case, we will predict the likelihood of
winning a game. The game itself is described below, and implmented in game.js.
The game exports a function we will use as a "generator" function to create a
dataset. We will then fit a model to this dataset using
, with the goal of predicting
the "win" state from features calculated from Player 1's hand. The output
logit value will be larger for hands which the model considers more likely
How to use this app.
Familiarize yourself with the rules of the game below.
Simulate the game by pressing the simulate-game button.
Select how many cards each player gets using the drop-down menu.
See how games are generated, wins are calculated, and notice that the
simulation count increases.
Familiarize yourself with the tensor-valued features calculated from
player 1's hand.
See what batches of data looks like by clicking dataset-to-array.
Before training a model, select a batchSize, the number of
batchesPerEpoch, and how many epochs to train on.
When you are ready, train a model on the generated features by
Once the model is trained, make predictions by entering values
for the user's hand, and clicking predict.
Click "Simulate Game" to run one play of the game. Three numbers will be
randomly selected for each player. The "win" status will indicate
whether player one's 'win' status according to the following rules..
The player with the largest group of same-valued cards wins. E.g.,
if player 1 has three-of-a-kind, and player 2 only has a pair,
player 1 wins.
If both players have the same sized maximal group, then the player
with the group with the largest face
value wins. E.g., A pair of 5s beats a pair of 4s.
If neither player even has a pair, the player with the highest
single card wins.
Ties are settled randomly, 50/50.
Number of cards per hand
Simulation Results (Simulations so far =
Game to features and label. Note that the features fed into
the model only include values visible to player
1, since we want to predict whether player 1 will win.
Note that since each player has an equal chance of winning, we
expect that a completely naive estimator will have an accuracy of
0.5. An estimator with perfect accuracy is not possible, since the
estimator does not have access to the opponent player's hand.
Use Trained Model
Output of model:
Note that this prediction is larger for hands that the model
considers more likely to win, but are not calibrated probabilities.