[go: nahoru, domu]

TensorFlow.js: tfjs-data

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
tf.data.generator()
. 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
model.fitDataset()
, 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 to win.

How to use this app.

  1. Familiarize yourself with the rules of the game below.
  2. Simulate the game by pressing the simulate-game button.
  3. Select how many cards each player gets using the drop-down menu.
  4. See how games are generated, wins are calculated, and notice that the simulation count increases.
  5. Familiarize yourself with the tensor-valued features calculated from player 1's hand.
  6. See what batches of data looks like by clicking dataset-to-array.
  7. Before training a model, select a batchSize, the number of batchesPerEpoch, and how many epochs to train on.
  8. When you are ready, train a model on the generated features by clicking 'train-model-using-fit-dataset'.
  9. Once the model is trained, make predictions by entering values for the user's hand, and clicking predict.

Game Simulation

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..

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 =
0
)

player 1
opponent
win?

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.

Features:
Click "sample" to run
Label:

Data Pipeline

tf.dataFromGenerator(simulation↑)

.map(gameToFeaturesAndLabel)

.batch( )

.take( )

.toArray()

Train & Evaluate Model

batchesPerEpoch

Epochs to train

Expected simulations = batchSize * batchesPerEpoch * epochs =

Training Progress

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.