Skip to content

Keep column name, and add an axis, move some summaries. #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 48 additions & 31 deletions site/en/tutorials/keras/regression.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,7 @@
"test_features = test_dataset.copy()\n",
"\n",
"train_labels = train_features.pop('MPG')\n",
"test_labels = test_features.pop('MPG')",
"\n",
"train_features = tf.convert_to_tensor(train_features, dtype=tf.float32)\n",
"test_features = tf.convert_to_tensor(test_features, dtype=tf.float32)"
"test_labels = test_features.pop('MPG')"
]
},
{
Expand Down Expand Up @@ -492,7 +489,7 @@
},
"outputs": [],
"source": [
"first = np.array(train_features[:1])\n",
"first = np.array(train_features[:1], dtype=float)\n",
"\n",
"with np.printoptions(precision=2, suppress=True):\n",
" print('First example:', first)\n",
Expand Down Expand Up @@ -548,9 +545,11 @@
},
"outputs": [],
"source": [
"horsepower = np.array(train_features[:, 2])\n",
"horsepower = np.array(train_features['Horsepower'], dtype=float)\n",
"horsepower = horsepower[:, None]\n",
"print(horsepower.shape)\n",
"\n",
"horsepower_normalizer = layers.Normalization(input_shape=[1,], axis=None)\n",
"horsepower_normalizer = layers.Normalization(axis=1, input_shape=[1])\n",
"horsepower_normalizer.adapt(horsepower)"
]
},
Expand All @@ -574,9 +573,7 @@
"horsepower_model = tf.keras.Sequential([\n",
" horsepower_normalizer,\n",
" layers.Dense(units=1)\n",
"])\n",
"\n",
"horsepower_model.summary()"
"])"
]
},
{
Expand All @@ -601,6 +598,17 @@
"horsepower_model.predict(horsepower[:10])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "eNDk5U4S-WVR"
},
"outputs": [],
"source": [
"horsepower_model.summary()"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -642,7 +650,7 @@
"source": [
"%%time\n",
"history = horsepower_model.fit(\n",
" train_features[:, 2],\n",
" train_features['Horsepower'],\n",
" train_labels,\n",
" epochs=100,\n",
" # Suppress logging.\n",
Expand Down Expand Up @@ -722,7 +730,7 @@
"test_results = {}\n",
"\n",
"test_results['horsepower_model'] = horsepower_model.evaluate(\n",
" test_features[:, 2],\n",
" test_features['Horsepower'],\n",
" test_labels, verbose=0)"
]
},
Expand Down Expand Up @@ -756,7 +764,7 @@
"outputs": [],
"source": [
"def plot_horsepower(x, y):\n",
" plt.scatter(train_features[:, 2], train_labels, label='Data')\n",
" plt.scatter(train_features['Horsepower'], train_labels, label='Data')\n",
" plt.plot(x, y, color='k', label='Predictions')\n",
" plt.xlabel('Horsepower')\n",
" plt.ylabel('MPG')\n",
Expand Down Expand Up @@ -1026,17 +1034,6 @@
"This model has quite a few more trainable parameters than the linear models:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ReAD0n6MsFK-"
},
"outputs": [],
"source": [
"dnn_horsepower_model.summary()"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand All @@ -1056,12 +1053,23 @@
"source": [
"%%time\n",
"history = dnn_horsepower_model.fit(\n",
" train_features[:, 2],\n",
" train_features['Horsepower'],\n",
" train_labels,\n",
" validation_split=0.2,\n",
" verbose=0, epochs=100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ReAD0n6MsFK-"
},
"outputs": [],
"source": [
"dnn_horsepower_model.summary()"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -1132,7 +1140,7 @@
"outputs": [],
"source": [
"test_results['dnn_horsepower_model'] = dnn_horsepower_model.evaluate(\n",
" test_features[:, 2], test_labels,\n",
" test_features['Horsepower'], test_labels,\n",
" verbose=0)"
]
},
Expand Down Expand Up @@ -1162,8 +1170,7 @@
},
"outputs": [],
"source": [
"dnn_model = build_and_compile_model(normalizer)\n",
"dnn_model.summary()"
"dnn_model = build_and_compile_model(normalizer)"
]
},
{
Expand All @@ -1182,6 +1189,17 @@
" verbose=0, epochs=100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "5R9yfjpC_qcT"
},
"outputs": [],
"source": [
"dnn_model.summary()"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -1324,7 +1342,7 @@
},
"outputs": [],
"source": [
"dnn_model.save('dnn_model.tf', save_format='tf')"
"dnn_model.save('dnn_model.keras')"
]
},
{
Expand All @@ -1344,7 +1362,7 @@
},
"outputs": [],
"source": [
"reloaded = tf.keras.models.load_model('dnn_model.tf')\n",
"reloaded = tf.keras.models.load_model('dnn_model.keras')\n",
"\n",
"test_results['reloaded'] = reloaded.evaluate(\n",
" test_features, test_labels, verbose=0)"
Expand Down Expand Up @@ -1380,7 +1398,6 @@
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "regression.ipynb",
"toc_visible": true
},
Expand Down