Files
fchollet--deep-learning-wit…/chapter04_classification-and-regression.ipynb
T
2026-07-13 13:25:23 +08:00

1305 lines
28 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"This is a companion notebook for the book [Deep Learning with Python, Third Edition](https://www.manning.com/books/deep-learning-with-python-third-edition). For readability, it only contains runnable code blocks and section titles, and omits everything else in the book: text paragraphs, figures, and pseudocode.\n\n**If you want to be able to follow what's going on, I recommend reading the notebook side by side with your copy of the book.**\n\nThe book's contents are available online at [deeplearningwithpython.io](https://deeplearningwithpython.io)."
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"!pip install keras keras-hub --upgrade -q"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"import os\n",
"os.environ[\"KERAS_BACKEND\"] = \"jax\""
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"cellView": "form",
"colab_type": "code"
},
"outputs": [],
"source": [
"# @title\n",
"import os\n",
"from IPython.core.magic import register_cell_magic\n",
"\n",
"@register_cell_magic\n",
"def backend(line, cell):\n",
" current, required = os.environ.get(\"KERAS_BACKEND\", \"\"), line.split()[-1]\n",
" if current == required:\n",
" get_ipython().run_cell(cell)\n",
" else:\n",
" print(\n",
" f\"This cell requires the {required} backend. To run it, change KERAS_BACKEND to \"\n",
" f\"\\\"{required}\\\" at the top of the notebook, restart the runtime, and rerun the notebook.\"\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"## Classification and regression"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"### Classifying movie reviews: A binary classification example"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### The IMDb dataset"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"from keras.datasets import imdb\n",
"\n",
"(train_data, train_labels), (test_data, test_labels) = imdb.load_data(\n",
" num_words=10000\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"train_data[0]"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"train_labels[0]"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"max([max(sequence) for sequence in train_data])"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"word_index = imdb.get_word_index()\n",
"reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])\n",
"decoded_review = \" \".join(\n",
" [reverse_word_index.get(i - 3, \"?\") for i in train_data[0]]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"decoded_review[:100]"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### Preparing the data"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"def multi_hot_encode(sequences, num_classes):\n",
" results = np.zeros((len(sequences), num_classes))\n",
" for i, sequence in enumerate(sequences):\n",
" results[i][sequence] = 1.0\n",
" return results\n",
"\n",
"x_train = multi_hot_encode(train_data, num_classes=10000)\n",
"x_test = multi_hot_encode(test_data, num_classes=10000)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"x_train[0]"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"y_train = train_labels.astype(\"float32\")\n",
"y_test = test_labels.astype(\"float32\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### Building your model"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"import keras\n",
"from keras import layers\n",
"\n",
"model = keras.Sequential(\n",
" [\n",
" layers.Dense(16, activation=\"relu\"),\n",
" layers.Dense(16, activation=\"relu\"),\n",
" layers.Dense(1, activation=\"sigmoid\"),\n",
" ]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"model.compile(\n",
" optimizer=\"adam\",\n",
" loss=\"binary_crossentropy\",\n",
" metrics=[\"accuracy\"],\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### Validating your approach"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"x_val = x_train[:10000]\n",
"partial_x_train = x_train[10000:]\n",
"y_val = y_train[:10000]\n",
"partial_y_train = y_train[10000:]"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"history = model.fit(\n",
" partial_x_train,\n",
" partial_y_train,\n",
" epochs=20,\n",
" batch_size=512,\n",
" validation_data=(x_val, y_val),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"history = model.fit(\n",
" x_train,\n",
" y_train,\n",
" epochs=20,\n",
" batch_size=512,\n",
" validation_split=0.2,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"history_dict = history.history\n",
"history_dict.keys()"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"history_dict = history.history\n",
"loss_values = history_dict[\"loss\"]\n",
"val_loss_values = history_dict[\"val_loss\"]\n",
"epochs = range(1, len(loss_values) + 1)\n",
"plt.plot(epochs, loss_values, \"r--\", label=\"Training loss\")\n",
"plt.plot(epochs, val_loss_values, \"b\", label=\"Validation loss\")\n",
"plt.title(\"[IMDB] Training and validation loss\")\n",
"plt.xlabel(\"Epochs\")\n",
"plt.xticks(epochs)\n",
"plt.ylabel(\"Loss\")\n",
"plt.legend()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"plt.clf()\n",
"acc = history_dict[\"accuracy\"]\n",
"val_acc = history_dict[\"val_accuracy\"]\n",
"plt.plot(epochs, acc, \"r--\", label=\"Training acc\")\n",
"plt.plot(epochs, val_acc, \"b\", label=\"Validation acc\")\n",
"plt.title(\"[IMDB] Training and validation accuracy\")\n",
"plt.xlabel(\"Epochs\")\n",
"plt.xticks(epochs)\n",
"plt.ylabel(\"Accuracy\")\n",
"plt.legend()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"model = keras.Sequential(\n",
" [\n",
" layers.Dense(16, activation=\"relu\"),\n",
" layers.Dense(16, activation=\"relu\"),\n",
" layers.Dense(1, activation=\"sigmoid\"),\n",
" ]\n",
")\n",
"model.compile(\n",
" optimizer=\"adam\",\n",
" loss=\"binary_crossentropy\",\n",
" metrics=[\"accuracy\"],\n",
")\n",
"model.fit(x_train, y_train, epochs=4, batch_size=512)\n",
"results = model.evaluate(x_test, y_test)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"results"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### Using a trained model to generate predictions on new data"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"model.predict(x_test)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### Further experiments"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### Wrapping up"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"### Classifying newswires: A multiclass classification example"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### The Reuters dataset"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"from keras.datasets import reuters\n",
"\n",
"(train_data, train_labels), (test_data, test_labels) = reuters.load_data(\n",
" num_words=10000\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"len(train_data)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"len(test_data)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"train_data[10]"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"word_index = reuters.get_word_index()\n",
"reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])\n",
"decoded_newswire = \" \".join(\n",
" [reverse_word_index.get(i - 3, \"?\") for i in train_data[10]]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"train_labels[10]"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### Preparing the data"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"x_train = multi_hot_encode(train_data, num_classes=10000)\n",
"x_test = multi_hot_encode(test_data, num_classes=10000)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"def one_hot_encode(labels, num_classes=46):\n",
" results = np.zeros((len(labels), num_classes))\n",
" for i, label in enumerate(labels):\n",
" results[i, label] = 1.0\n",
" return results\n",
"\n",
"y_train = one_hot_encode(train_labels)\n",
"y_test = one_hot_encode(test_labels)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"from keras.utils import to_categorical\n",
"\n",
"y_train = to_categorical(train_labels)\n",
"y_test = to_categorical(test_labels)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### Building your model"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"model = keras.Sequential(\n",
" [\n",
" layers.Dense(64, activation=\"relu\"),\n",
" layers.Dense(64, activation=\"relu\"),\n",
" layers.Dense(46, activation=\"softmax\"),\n",
" ]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"top_3_accuracy = keras.metrics.TopKCategoricalAccuracy(\n",
" k=3, name=\"top_3_accuracy\"\n",
")\n",
"model.compile(\n",
" optimizer=\"adam\",\n",
" loss=\"categorical_crossentropy\",\n",
" metrics=[\"accuracy\", top_3_accuracy],\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### Validating your approach"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"x_val = x_train[:1000]\n",
"partial_x_train = x_train[1000:]\n",
"y_val = y_train[:1000]\n",
"partial_y_train = y_train[1000:]"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"history = model.fit(\n",
" partial_x_train,\n",
" partial_y_train,\n",
" epochs=20,\n",
" batch_size=512,\n",
" validation_data=(x_val, y_val),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"loss = history.history[\"loss\"]\n",
"val_loss = history.history[\"val_loss\"]\n",
"epochs = range(1, len(loss) + 1)\n",
"plt.plot(epochs, loss, \"r--\", label=\"Training loss\")\n",
"plt.plot(epochs, val_loss, \"b\", label=\"Validation loss\")\n",
"plt.title(\"Training and validation loss\")\n",
"plt.xlabel(\"Epochs\")\n",
"plt.xticks(epochs)\n",
"plt.ylabel(\"Loss\")\n",
"plt.legend()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"plt.clf()\n",
"acc = history.history[\"accuracy\"]\n",
"val_acc = history.history[\"val_accuracy\"]\n",
"plt.plot(epochs, acc, \"r--\", label=\"Training accuracy\")\n",
"plt.plot(epochs, val_acc, \"b\", label=\"Validation accuracy\")\n",
"plt.title(\"Training and validation accuracy\")\n",
"plt.xlabel(\"Epochs\")\n",
"plt.xticks(epochs)\n",
"plt.ylabel(\"Accuracy\")\n",
"plt.legend()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"plt.clf()\n",
"acc = history.history[\"top_3_accuracy\"]\n",
"val_acc = history.history[\"val_top_3_accuracy\"]\n",
"plt.plot(epochs, acc, \"r--\", label=\"Training top-3 accuracy\")\n",
"plt.plot(epochs, val_acc, \"b\", label=\"Validation top-3 accuracy\")\n",
"plt.title(\"Training and validation top-3 accuracy\")\n",
"plt.xlabel(\"Epochs\")\n",
"plt.xticks(epochs)\n",
"plt.ylabel(\"Top-3 accuracy\")\n",
"plt.legend()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"model = keras.Sequential(\n",
" [\n",
" layers.Dense(64, activation=\"relu\"),\n",
" layers.Dense(64, activation=\"relu\"),\n",
" layers.Dense(46, activation=\"softmax\"),\n",
" ]\n",
")\n",
"model.compile(\n",
" optimizer=\"adam\",\n",
" loss=\"categorical_crossentropy\",\n",
" metrics=[\"accuracy\"],\n",
")\n",
"model.fit(\n",
" x_train,\n",
" y_train,\n",
" epochs=9,\n",
" batch_size=512,\n",
")\n",
"results = model.evaluate(x_test, y_test)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"results"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"import copy\n",
"test_labels_copy = copy.copy(test_labels)\n",
"np.random.shuffle(test_labels_copy)\n",
"hits_array = np.array(test_labels == test_labels_copy)\n",
"hits_array.mean()"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### Generating predictions on new data"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"predictions = model.predict(x_test)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"predictions[0].shape"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"np.sum(predictions[0])"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"np.argmax(predictions[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### A different way to handle the labels and the loss"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"y_train = train_labels\n",
"y_test = test_labels"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"model.compile(\n",
" optimizer=\"adam\",\n",
" loss=\"sparse_categorical_crossentropy\",\n",
" metrics=[\"accuracy\"],\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### The importance of having sufficiently large intermediate layers"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"model = keras.Sequential(\n",
" [\n",
" layers.Dense(64, activation=\"relu\"),\n",
" layers.Dense(4, activation=\"relu\"),\n",
" layers.Dense(46, activation=\"softmax\"),\n",
" ]\n",
")\n",
"model.compile(\n",
" optimizer=\"adam\",\n",
" loss=\"categorical_crossentropy\",\n",
" metrics=[\"accuracy\"],\n",
")\n",
"model.fit(\n",
" partial_x_train,\n",
" partial_y_train,\n",
" epochs=20,\n",
" batch_size=128,\n",
" validation_data=(x_val, y_val),\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### Further experiments"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### Wrapping up"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"### Predicting house prices: A regression example"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### The California Housing Price dataset"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"from keras.datasets import california_housing\n",
"\n",
"(train_data, train_targets), (test_data, test_targets) = (\n",
" california_housing.load_data(version=\"small\")\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"train_data.shape"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"test_data.shape"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"train_targets"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### Preparing the data"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"mean = train_data.mean(axis=0)\n",
"std = train_data.std(axis=0)\n",
"x_train = (train_data - mean) / std\n",
"x_test = (test_data - mean) / std"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"y_train = train_targets / 100000\n",
"y_test = test_targets / 100000"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### Building your model"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"def get_model():\n",
" model = keras.Sequential(\n",
" [\n",
" layers.Dense(64, activation=\"relu\"),\n",
" layers.Dense(64, activation=\"relu\"),\n",
" layers.Dense(1),\n",
" ]\n",
" )\n",
" model.compile(\n",
" optimizer=\"adam\",\n",
" loss=\"mean_squared_error\",\n",
" metrics=[\"mean_absolute_error\"],\n",
" )\n",
" return model"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### Validating your approach using K-fold validation"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"k = 4\n",
"num_val_samples = len(x_train) // k\n",
"num_epochs = 50\n",
"all_scores = []\n",
"for i in range(k):\n",
" print(f\"Processing fold #{i + 1}\")\n",
" fold_x_val = x_train[i * num_val_samples : (i + 1) * num_val_samples]\n",
" fold_y_val = y_train[i * num_val_samples : (i + 1) * num_val_samples]\n",
" fold_x_train = np.concatenate(\n",
" [x_train[: i * num_val_samples], x_train[(i + 1) * num_val_samples :]],\n",
" axis=0,\n",
" )\n",
" fold_y_train = np.concatenate(\n",
" [y_train[: i * num_val_samples], y_train[(i + 1) * num_val_samples :]],\n",
" axis=0,\n",
" )\n",
" model = get_model()\n",
" model.fit(\n",
" fold_x_train,\n",
" fold_y_train,\n",
" epochs=num_epochs,\n",
" batch_size=16,\n",
" verbose=0,\n",
" )\n",
" scores = model.evaluate(fold_x_val, fold_y_val, verbose=0)\n",
" val_loss, val_mae = scores\n",
" all_scores.append(val_mae)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"[round(value, 3) for value in all_scores]"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"round(np.mean(all_scores), 3)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"k = 4\n",
"num_val_samples = len(x_train) // k\n",
"num_epochs = 200\n",
"all_mae_histories = []\n",
"for i in range(k):\n",
" print(f\"Processing fold #{i + 1}\")\n",
" fold_x_val = x_train[i * num_val_samples : (i + 1) * num_val_samples]\n",
" fold_y_val = y_train[i * num_val_samples : (i + 1) * num_val_samples]\n",
" fold_x_train = np.concatenate(\n",
" [x_train[: i * num_val_samples], x_train[(i + 1) * num_val_samples :]],\n",
" axis=0,\n",
" )\n",
" fold_y_train = np.concatenate(\n",
" [y_train[: i * num_val_samples], y_train[(i + 1) * num_val_samples :]],\n",
" axis=0,\n",
" )\n",
" model = get_model()\n",
" history = model.fit(\n",
" fold_x_train,\n",
" fold_y_train,\n",
" validation_data=(fold_x_val, fold_y_val),\n",
" epochs=num_epochs,\n",
" batch_size=16,\n",
" verbose=0,\n",
" )\n",
" mae_history = history.history[\"val_mean_absolute_error\"]\n",
" all_mae_histories.append(mae_history)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"average_mae_history = [\n",
" np.mean([x[i] for x in all_mae_histories]) for i in range(num_epochs)\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"epochs = range(1, len(average_mae_history) + 1)\n",
"plt.plot(epochs, average_mae_history)\n",
"plt.xlabel(\"Epochs\")\n",
"plt.ylabel(\"Validation MAE\")\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"truncated_mae_history = average_mae_history[10:]\n",
"epochs = range(10, len(truncated_mae_history) + 10)\n",
"plt.plot(epochs, truncated_mae_history)\n",
"plt.xlabel(\"Epochs\")\n",
"plt.ylabel(\"Validation MAE\")\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"model = get_model()\n",
"model.fit(x_train, y_train, epochs=130, batch_size=16, verbose=0)\n",
"test_mean_squared_error, test_mean_absolute_error = model.evaluate(\n",
" x_test, y_test\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"round(test_mean_absolute_error, 3)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### Generating predictions on new data"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"predictions = model.predict(x_test)\n",
"predictions[0]"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"#### Wrapping up"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"name": "chapter04_classification-and-regression",
"private_outputs": false,
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}