"To simplify the model building, let's repackage the features dictionary into an array with shape ``(batch_size,num_features)`.\n",
"To simplify the model building, let's repackage the features dictionary into an array with shape `(batch_size,num_features)`.\n",
"\n",
"To do this we'll write a simple function using the [tf.stack](https://www.tensorflow.org/api_docs/python/tf/stack) method to pack the features into a single array. Then we'll use the [tf.data.Dataset.map](https://www.tensorflow.org/api_docs/python/tf/data/dataset/map) method to apply this function to each `(features,label)` pair in the dataset. :\n"
"To do this we'll write a simple function using the [tf.stack](https://www.tensorflow.org/api_docs/python/tf/stack) method to pack the features into a single array.\n",
"\n",
"Stack takes a list of tensors, and stacks them along a new axis, like this:\n"
]
},
{
"metadata": {
"id": "lSI2KLB4CAtc",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"tf.stack(list(features.values()), axis=1)[:10]"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "V1Vuph_eDl8x",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"Then we'll use the [tf.data.Dataset.map](https://www.tensorflow.org/api_docs/python/tf/data/dataset/map) method to stack the `features` in each `(features,label)` pair in the dataset. "
]
},
{
...
...
@@ -437,8 +487,9 @@
},
"cell_type": "code",
"source": [
"for features,labels in train_dataset.take(1):\n",
" print(features[:5])"
"features, labels = next(iter(train_dataset))\n",
"\n",
"print(features[:10])"
],
"execution_count": 0,
"outputs": []
...
...
@@ -525,6 +576,8 @@
},
"cell_type": "markdown",
"source": [
"### Using the model\n",
"\n",
"Let's have a quick look at what this model does to a batch of features:"
]
},
...
...
@@ -549,22 +602,20 @@
},
"cell_type": "markdown",
"source": [
"For each example it returns a *[logit](https://developers.google.com/machine-learning/crash-course/glossary#logits)* score for each class. \n",
"For each example it returns a [logit](https://developers.google.com/machine-learning/crash-course/glossary#logit) for each class. \n",
"\n",
"You can convert logits to probabilities for each class using the [tf.nn.softmax](https://www.tensorflow.org/api_docs/python/tf/nn/softmax) function."
"To convert to a probability for each class, for each example, we use the [softmax](https://developers.google.com/machine-learning/crash-course/glossary#softmax) function:"
]
},
{
"metadata": {
"id": "2fas18iHoiGB",
"id": "_tRwHZmTNTX2",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"prob = tf.nn.softmax(predictions[:5])\n",
"\n",
"prob"
"tf.nn.softmax(predictions[:5])"
],
"execution_count": 0,
"outputs": []
...
...
@@ -576,7 +627,7 @@
},
"cell_type": "markdown",
"source": [
"Taking the `tf.argmax` across the `classes` axis would give us the predicted class index.\n",
"Taking the `tf.argmax` across the classes would give us the predicted class index.\n",
"\n",
"The model hasn't been trained yet, so these aren't very good predictions."
]
...
...
@@ -632,7 +683,7 @@
"\n",
"Both training and evaluation stages need to calculate the model's *[loss](https://developers.google.com/machine-learning/crash-course/glossary#loss)*. This measures how off a model's predictions are from the desired label, in other words, how bad the model is performing. We want to minimize, or optimize, this value.\n",
"\n",
"Our model will calculate its loss using the [tf.losses.sparse_softmax_cross_entropy](https://www.tensorflow.org/api_docs/python/tf/losses/sparse_softmax_cross_entropy) function which takes the model's prediction and the desired label, and returns the average loss across the examples."
"Our model will calculate its loss using the [tf.keras.losses.categorical_crossentropy](https://www.tensorflow.org/api_docs/python/tf/losses/sparse_softmax_cross_entropy) function which takes the model's class probability predictions and the desired label, and returns the average loss across the examples."