"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/data/dataset/map) 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"
]
},
{
...
...
@@ -412,8 +421,8 @@
"cell_type": "code",
"source": [
"def pack_features_vector(features,labels):\n",
" features = tf.stack([features[name] for name in feature_names],\n",
" axis=1)\n",
" values = [value for value in features.values()]\n",
"for features,labels in train_dataset.take(1):\n",
" print(features[:5])"
],
"execution_count": 0,
"outputs": []
...
...
@@ -539,8 +547,8 @@
},
"cell_type": "code",
"source": [
"prediction = model(features)\n",
"prediction[:5]"
"predictions = model(features)\n",
"predictions[:5]"
],
"execution_count": 0,
"outputs": []
...
...
@@ -554,7 +562,32 @@
"source": [
"For each example it returns a *[logit](https://developers.google.com/machine-learning/crash-course/glossary#logits)* score 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.\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."
]
},
{
"metadata": {
"id": "2fas18iHoiGB",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"prob = tf.nn.softmax(predictions[:5])\n",
"\n",
"prob"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "uRZmchElo481",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"Taking the `tf.argmax` across the `classes` axis would give us the predicted class index.\n",
"\n",
"The model hasn't been trained yet, so these aren't very good predictions."