"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",
"\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"
"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 @@
...
@@ -412,8 +421,8 @@
"cell_type": "code",
"cell_type": "code",
"source": [
"source": [
"def pack_features_vector(features,labels):\n",
"def pack_features_vector(features,labels):\n",
" features = tf.stack([features[name] for name in feature_names],\n",
" values = [value for value in features.values()]\n",
"for features,labels in train_dataset.take(1):\n",
" \n",
" print(features[:5])"
"features[:5]"
],
],
"execution_count": 0,
"execution_count": 0,
"outputs": []
"outputs": []
...
@@ -539,8 +547,8 @@
...
@@ -539,8 +547,8 @@
},
},
"cell_type": "code",
"cell_type": "code",
"source": [
"source": [
"prediction = model(features)\n",
"predictions = model(features)\n",
"prediction[:5]"
"predictions[:5]"
],
],
"execution_count": 0,
"execution_count": 0,
"outputs": []
"outputs": []
...
@@ -554,7 +562,32 @@
...
@@ -554,7 +562,32 @@
"source": [
"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#logits)* score for each class. \n",
"\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",
"\n",
"The model hasn't been trained yet, so these aren't very good predictions."
"The model hasn't been trained yet, so these aren't very good predictions."