{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "intro_to_neural_nets.ipynb", "version": "0.3.2", "views": {}, "default_view": {}, "provenance": [], "collapsed_sections": [ "O2q5RRCKqYaU", "vvT2jDWjrKew", "copyright-notice" ] } }, "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "copyright-notice" }, "source": [ "#### Copyright 2017 Google LLC." ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "copyright-notice2", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } }, "cellView": "both" }, "source": [ "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ], "outputs": [], "execution_count": 0 }, { "metadata": { "id": "eV16J6oUY-HN", "colab_type": "text", "slideshow": { "slide_type": "slide" } }, "cell_type": "markdown", "source": [ " # \u795e\u7ecf\u7f51\u7edc\u7b80\u4ecb" ] }, { "metadata": { "id": "_wIcUFLSKNdx", "colab_type": "text" }, "cell_type": "markdown", "source": [ " **\u5b66\u4e60\u76ee\u6807\uff1a**\n", " * \u4f7f\u7528 TensorFlow `DNNRegressor` \u7c7b\u5b9a\u4e49\u795e\u7ecf\u7f51\u7edc (NN) \u53ca\u5176\u9690\u85cf\u5c42\n", " * \u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u5b66\u4e60\u6570\u636e\u96c6\u4e2d\u7684\u975e\u7ebf\u6027\u89c4\u5f8b\uff0c\u5e76\u5b9e\u73b0\u6bd4\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u66f4\u597d\u7684\u6548\u679c" ] }, { "metadata": { "id": "_ZZ7f7prKNdy", "colab_type": "text" }, "cell_type": "markdown", "source": [ " \u5728\u4e4b\u524d\u7684\u7ec3\u4e60\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5408\u6210\u7279\u5f81\u6765\u5e2e\u52a9\u6a21\u578b\u5b66\u4e60\u975e\u7ebf\u6027\u89c4\u5f8b\u3002\n", "\n", "\u4e00\u7ec4\u91cd\u8981\u7684\u975e\u7ebf\u6027\u5173\u7cfb\u662f\u7eac\u5ea6\u548c\u7ecf\u5ea6\u7684\u5173\u7cfb\uff0c\u4f46\u4e5f\u53ef\u80fd\u5b58\u5728\u5176\u4ed6\u975e\u7ebf\u6027\u5173\u7cfb\u3002\n", "\n", "\u73b0\u5728\u6211\u4eec\u4ece\u4e4b\u524d\u7ec3\u4e60\u4e2d\u7684\u903b\u8f91\u56de\u5f52\u4efb\u52a1\u56de\u5230\u6807\u51c6\u7684\uff08\u7ebf\u6027\uff09\u56de\u5f52\u4efb\u52a1\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u5c06\u76f4\u63a5\u9884\u6d4b `median_house_value`\u3002" ] }, { "metadata": { "id": "J2kqX6VZTHUy", "colab_type": "text" }, "cell_type": "markdown", "source": [ " ## \u8bbe\u7f6e\n", "\n", "\u9996\u5148\u52a0\u8f7d\u548c\u51c6\u5907\u6570\u636e\u3002" ] }, { "metadata": { "id": "AGOM1TUiKNdz", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "source": [ "from __future__ import print_function\n", "\n", "import math\n", "\n", "from IPython import display\n", "from matplotlib import cm\n", "from matplotlib import gridspec\n", "from matplotlib import pyplot as plt\n", "import numpy as np\n", "import pandas as pd\n", "from sklearn import metrics\n", "import tensorflow as tf\n", "from tensorflow.python.data import Dataset\n", "\n", "tf.logging.set_verbosity(tf.logging.ERROR)\n", "pd.options.display.max_rows = 10\n", "pd.options.display.float_format = '{:.1f}'.format\n", "\n", "california_housing_dataframe = pd.read_csv(\"https://storage.googleapis.com/mledu-datasets/california_housing_train.csv\", sep=\",\")\n", "\n", "california_housing_dataframe = california_housing_dataframe.reindex(\n", " np.random.permutation(california_housing_dataframe.index))" ], "cell_type": "code", "execution_count": 0, "outputs": [] }, { "metadata": { "id": "2I8E2qhyKNd4", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "source": [ "def preprocess_features(california_housing_dataframe):\n", " \"\"\"Prepares input features from California housing data set.\n", "\n", " Args:\n", " california_housing_dataframe: A Pandas DataFrame expected to contain data\n", " from the California housing data set.\n", " Returns:\n", " A DataFrame that contains the features to be used for the model, including\n", " synthetic features.\n", " \"\"\"\n", " selected_features = california_housing_dataframe[\n", " [\"latitude\",\n", " \"longitude\",\n", " \"housing_median_age\",\n", " \"total_rooms\",\n", " \"total_bedrooms\",\n", " \"population\",\n", " \"households\",\n", " \"median_income\"]]\n", " processed_features = selected_features.copy()\n", " # Create a synthetic feature.\n", " processed_features[\"rooms_per_person\"] = (\n", " california_housing_dataframe[\"total_rooms\"] /\n", " california_housing_dataframe[\"population\"])\n", " return processed_features\n", "\n", "def preprocess_targets(california_housing_dataframe):\n", " \"\"\"Prepares target features (i.e., labels) from California housing data set.\n", "\n", " Args:\n", " california_housing_dataframe: A Pandas DataFrame expected to contain data\n", " from the California housing data set.\n", " Returns:\n", " A DataFrame that contains the target feature.\n", " \"\"\"\n", " output_targets = pd.DataFrame()\n", " # Scale the target to be in units of thousands of dollars.\n", " output_targets[\"median_house_value\"] = (\n", " california_housing_dataframe[\"median_house_value\"] / 1000.0)\n", " return output_targets" ], "cell_type": "code", "execution_count": 0, "outputs": [] }, { "metadata": { "id": "pQzcj2B1T5dA", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "source": [ "# Choose the first 12000 (out of 17000) examples for training.\n", "training_examples = preprocess_features(california_housing_dataframe.head(12000))\n", "training_targets = preprocess_targets(california_housing_dataframe.head(12000))\n", "\n", "# Choose the last 5000 (out of 17000) examples for validation.\n", "validation_examples = preprocess_features(california_housing_dataframe.tail(5000))\n", "validation_targets = preprocess_targets(california_housing_dataframe.tail(5000))\n", "\n", "# Double-check that we've done the right thing.\n", "print(\"Training examples summary:\")\n", "display.display(training_examples.describe())\n", "print(\"Validation examples summary:\")\n", "display.display(validation_examples.describe())\n", "\n", "print(\"Training targets summary:\")\n", "display.display(training_targets.describe())\n", "print(\"Validation targets summary:\")\n", "display.display(validation_targets.describe())" ], "cell_type": "code", "execution_count": 0, "outputs": [] }, { "metadata": { "id": "RWq0xecNKNeG", "colab_type": "text" }, "cell_type": "markdown", "source": [ " ## \u6784\u5efa\u795e\u7ecf\u7f51\u7edc\n", "\n", "\u795e\u7ecf\u7f51\u7edc\u7531 [DNNRegressor](https://www.tensorflow.org/api_docs/python/tf/contrib/learn/DNNRegressor) \u7c7b\u5b9a\u4e49\u3002\n", "\n", "\u4f7f\u7528 **`hidden_units`** \u5b9a\u4e49\u795e\u7ecf\u7f51\u7edc\u7684\u7ed3\u6784\u3002`hidden_units` \u53c2\u6570\u4f1a\u521b\u5efa\u4e00\u4e2a\u6574\u6570\u5217\u8868\uff0c\u5176\u4e2d\u6bcf\u4e2a\u6574\u6570\u5bf9\u5e94\u4e00\u4e2a\u9690\u85cf\u5c42\uff0c\u8868\u793a\u5176\u4e2d\u7684\u8282\u70b9\u6570\u3002\u4ee5\u4e0b\u9762\u7684\u8d4b\u503c\u4e3a\u4f8b\uff1a\n", "\n", "`hidden_units=[3,10]`\n", "\n", "\u4e0a\u8ff0\u8d4b\u503c\u4e3a\u795e\u7ecf\u7f51\u7edc\u6307\u5b9a\u4e86\u4e24\u4e2a\u9690\u85cf\u5c42\uff1a\n", "\n", "* \u7b2c\u4e00\u4e2a\u9690\u85cf\u5c42\u5305\u542b 3 \u4e2a\u8282\u70b9\u3002\n", "* \u7b2c\u4e8c\u4e2a\u9690\u85cf\u5c42\u5305\u542b 10 \u4e2a\u8282\u70b9\u3002\n", "\n", "\u5982\u679c\u6211\u4eec\u60f3\u8981\u6dfb\u52a0\u66f4\u591a\u5c42\uff0c\u53ef\u4ee5\u5411\u8be5\u5217\u8868\u6dfb\u52a0\u66f4\u591a\u6574\u6570\u3002\u4f8b\u5982\uff0c`hidden_units=[10,20,30,40]` \u4f1a\u521b\u5efa 4 \u4e2a\u5206\u522b\u5305\u542b 10\u300120\u300130 \u548c 40 \u4e2a\u5355\u5143\u7684\u9690\u85cf\u5c42\u3002\n", "\n", "\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u9690\u85cf\u5c42\u90fd\u4f1a\u4f7f\u7528 ReLu \u6fc0\u6d3b\u51fd\u6570\uff0c\u4e14\u662f\u5168\u8fde\u63a5\u5c42\u3002" ] }, { "metadata": { "id": "ni0S6zHcTb04", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "source": [ "def construct_feature_columns(input_features):\n", " \"\"\"Construct the TensorFlow Feature Columns.\n", "\n", " Args:\n", " input_features: The names of the numerical input features to use.\n", " Returns:\n", " A set of feature columns\n", " \"\"\" \n", " return set([tf.feature_column.numeric_column(my_feature)\n", " for my_feature in input_features])" ], "cell_type": "code", "execution_count": 0, "outputs": [] }, { "metadata": { "id": "zvCqgNdzpaFg", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "source": [ "def my_input_fn(features, targets, batch_size=1, shuffle=True, num_epochs=None):\n", " \"\"\"Trains a linear regression model of one feature.\n", " \n", " Args:\n", " features: pandas DataFrame of features\n", " targets: pandas DataFrame of targets\n", " batch_size: Size of batches to be passed to the model\n", " shuffle: True or False. Whether to shuffle the data.\n", " num_epochs: Number of epochs for which data should be repeated. None = repeat indefinitely\n", " Returns:\n", " Tuple of (features, labels) for next data batch\n", " \"\"\"\n", " \n", " # Convert pandas data into a dict of np arrays.\n", " features = {key:np.array(value) for key,value in dict(features).items()} \n", " \n", " # Construct a dataset, and configure batching/repeating\n", " ds = Dataset.from_tensor_slices((features,targets)) # warning: 2GB limit\n", " ds = ds.batch(batch_size).repeat(num_epochs)\n", " \n", " # Shuffle the data, if specified\n", " if shuffle:\n", " ds = ds.shuffle(10000)\n", " \n", " # Return the next batch of data\n", " features, labels = ds.make_one_shot_iterator().get_next()\n", " return features, labels" ], "cell_type": "code", "execution_count": 0, "outputs": [] }, { "metadata": { "id": "U52Ychv9KNeH", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "source": [ "def train_nn_regression_model(\n", " learning_rate,\n", " steps,\n", " batch_size,\n", " hidden_units,\n", " training_examples,\n", " training_targets,\n", " validation_examples,\n", " validation_targets):\n", " \"\"\"Trains a neural network regression model.\n", " \n", " In addition to training, this function also prints training progress information,\n", " as well as a plot of the training and validation loss over time.\n", " \n", " Args:\n", " learning_rate: A `float`, the learning rate.\n", " steps: A non-zero `int`, the total number of training steps. A training step\n", " consists of a forward and backward pass using a single batch.\n", " batch_size: A non-zero `int`, the batch size.\n", " hidden_units: A `list` of int values, specifying the number of neurons in each layer.\n", " training_examples: A `DataFrame` containing one or more columns from\n", " `california_housing_dataframe` to use as input features for training.\n", " training_targets: A `DataFrame` containing exactly one column from\n", " `california_housing_dataframe` to use as target for training.\n", " validation_examples: A `DataFrame` containing one or more columns from\n", " `california_housing_dataframe` to use as input features for validation.\n", " validation_targets: A `DataFrame` containing exactly one column from\n", " `california_housing_dataframe` to use as target for validation.\n", " \n", " Returns:\n", " A `LinearRegressor` object trained on the training data.\n", " \"\"\"\n", "\n", " periods = 10\n", " steps_per_period = steps / periods\n", " \n", " # Create a linear regressor object.\n", " my_optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)\n", " my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer, 5.0)\n", " dnn_regressor = tf.estimator.DNNRegressor(\n", " feature_columns=construct_feature_columns(training_examples),\n", " hidden_units=hidden_units\n", " )\n", " \n", " # Create input functions\n", " training_input_fn = lambda: my_input_fn(training_examples, \n", " training_targets[\"median_house_value\"], \n", " batch_size=batch_size)\n", " predict_training_input_fn = lambda: my_input_fn(training_examples, \n", " training_targets[\"median_house_value\"], \n", " num_epochs=1, \n", " shuffle=False)\n", " predict_validation_input_fn = lambda: my_input_fn(validation_examples, \n", " validation_targets[\"median_house_value\"], \n", " num_epochs=1, \n", " shuffle=False)\n", "\n", " # Train the model, but do so inside a loop so that we can periodically assess\n", " # loss metrics.\n", " print(\"Training model...\")\n", " print(\"RMSE (on training data):\")\n", " training_rmse = []\n", " validation_rmse = []\n", " for period in range (0, periods):\n", " # Train the model, starting from the prior state.\n", " dnn_regressor.train(\n", " input_fn=training_input_fn,\n", " steps=steps_per_period\n", " )\n", " # Take a break and compute predictions.\n", " training_predictions = dnn_regressor.predict(input_fn=predict_training_input_fn)\n", " training_predictions = np.array([item['predictions'][0] for item in training_predictions])\n", " \n", " validation_predictions = dnn_regressor.predict(input_fn=predict_validation_input_fn)\n", " validation_predictions = np.array([item['predictions'][0] for item in validation_predictions])\n", " \n", " # Compute training and validation loss.\n", " training_root_mean_squared_error = math.sqrt(\n", " metrics.mean_squared_error(training_predictions, training_targets))\n", " validation_root_mean_squared_error = math.sqrt(\n", " metrics.mean_squared_error(validation_predictions, validation_targets))\n", " # Occasionally print the current loss.\n", " print(\" period %02d : %0.2f\" % (period, training_root_mean_squared_error))\n", " # Add the loss metrics from this period to our list.\n", " training_rmse.append(training_root_mean_squared_error)\n", " validation_rmse.append(validation_root_mean_squared_error)\n", " print(\"Model training finished.\")\n", "\n", " # Output a graph of loss metrics over periods.\n", " plt.ylabel(\"RMSE\")\n", " plt.xlabel(\"Periods\")\n", " plt.title(\"Root Mean Squared Error vs. Periods\")\n", " plt.tight_layout()\n", " plt.plot(training_rmse, label=\"training\")\n", " plt.plot(validation_rmse, label=\"validation\")\n", " plt.legend()\n", "\n", " print(\"Final RMSE (on training data): %0.2f\" % training_root_mean_squared_error)\n", " print(\"Final RMSE (on validation data): %0.2f\" % validation_root_mean_squared_error)\n", "\n", " return dnn_regressor" ], "cell_type": "code", "execution_count": 0, "outputs": [] }, { "metadata": { "id": "2QhdcCy-Y8QR", "colab_type": "text", "slideshow": { "slide_type": "slide" } }, "cell_type": "markdown", "source": [ " ## \u4efb\u52a1 1\uff1a\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\n", "\n", "**\u8c03\u6574\u8d85\u53c2\u6570\uff0c\u76ee\u6807\u662f\u5c06 RMSE \u964d\u5230 110 \u4ee5\u4e0b\u3002**\n", "\n", "\u8fd0\u884c\u4ee5\u4e0b\u4ee3\u7801\u5757\u6765\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u3002\n", "\n", "\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\uff0c\u5728\u4f7f\u7528\u4e86\u5f88\u591a\u7279\u5f81\u7684\u7ebf\u6027\u56de\u5f52\u7ec3\u4e60\u4e2d\uff0c110 \u5de6\u53f3\u7684 RMSE \u5df2\u7ecf\u662f\u76f8\u5f53\u4e0d\u9519\u7684\u7ed3\u679c\u3002\u6211\u4eec\u5c06\u5f97\u5230\u6bd4\u5b83\u66f4\u597d\u7684\u7ed3\u679c\u3002\n", "\n", "\u5728\u6b64\u7ec3\u4e60\u4e2d\uff0c\u60a8\u7684\u4efb\u52a1\u662f\u4fee\u6539\u5404\u79cd\u5b66\u4e60\u8bbe\u7f6e\uff0c\u4ee5\u63d0\u9ad8\u5728\u9a8c\u8bc1\u6570\u636e\u4e0a\u7684\u51c6\u786e\u7387\u3002\n", "\n", "\u5bf9\u4e8e\u795e\u7ecf\u7f51\u7edc\u800c\u8a00\uff0c\u8fc7\u62df\u5408\u662f\u4e00\u79cd\u771f\u6b63\u7684\u6f5c\u5728\u5371\u9669\u3002\u60a8\u53ef\u4ee5\u67e5\u770b\u8bad\u7ec3\u6570\u636e\u635f\u5931\u4e0e\u9a8c\u8bc1\u6570\u636e\u635f\u5931\u4e4b\u95f4\u7684\u5dee\u503c\uff0c\u4ee5\u5e2e\u52a9\u5224\u65ad\u6a21\u578b\u662f\u5426\u6709\u8fc7\u62df\u5408\u7684\u8d8b\u52bf\u3002\u5982\u679c\u5dee\u503c\u5f00\u59cb\u53d8\u5927\uff0c\u5219\u901a\u5e38\u53ef\u4ee5\u80af\u5b9a\u5b58\u5728\u8fc7\u62df\u5408\u3002\n", "\n", "\u7531\u4e8e\u5b58\u5728\u5f88\u591a\u4e0d\u540c\u7684\u53ef\u80fd\u8bbe\u7f6e\uff0c\u5f3a\u70c8\u5efa\u8bae\u60a8\u8bb0\u5f55\u6bcf\u6b21\u8bd5\u9a8c\uff0c\u4ee5\u5728\u5f00\u53d1\u6d41\u7a0b\u4e2d\u8fdb\u884c\u53c2\u8003\u3002\n", "\n", "\u6b64\u5916\uff0c\u83b7\u5f97\u6548\u679c\u51fa\u8272\u7684\u8bbe\u7f6e\u540e\uff0c\u5c1d\u8bd5\u591a\u6b21\u8fd0\u884c\u8be5\u8bbe\u7f6e\uff0c\u770b\u770b\u7ed3\u679c\u7684\u91cd\u590d\u7a0b\u5ea6\u3002\u7531\u4e8e\u795e\u7ecf\u7f51\u7edc\u6743\u91cd\u901a\u5e38\u4f1a\u521d\u59cb\u5316\u4e3a\u8f83\u5c0f\u7684\u968f\u673a\u503c\uff0c\u56e0\u6b64\u6bcf\u6b21\u8fd0\u884c\u7ed3\u679c\u5e94\u8be5\u5b58\u5728\u5dee\u5f02\u3002\n", "" ] }, { "metadata": { "id": "rXmtSW1yKNeK", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "source": [ "dnn_regressor = train_nn_regression_model(\n", " learning_rate=0.01,\n", " steps=500,\n", " batch_size=10,\n", " hidden_units=[10, 2],\n", " training_examples=training_examples,\n", " training_targets=training_targets,\n", " validation_examples=validation_examples,\n", " validation_targets=validation_targets)" ], "cell_type": "code", "execution_count": 0, "outputs": [] }, { "metadata": { "id": "O2q5RRCKqYaU", "colab_type": "text" }, "cell_type": "markdown", "source": [ " ### \u89e3\u51b3\u65b9\u6848\n", "\n", "\u70b9\u51fb\u4e0b\u65b9\u5373\u53ef\u67e5\u770b\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848" ] }, { "metadata": { "id": "j2Yd5VfrqcC3", "colab_type": "text" }, "cell_type": "markdown", "source": [ " **\u6ce8\u610f**\uff1a\u5728\u672c\u6b21\u7ec3\u4e60\u4e2d\uff0c\u53c2\u6570\u7684\u9009\u62e9\u6709\u70b9\u968f\u610f\u3002\u6211\u4eec\u5c1d\u8bd5\u4e86\u8d8a\u6765\u8d8a\u590d\u6742\u7684\u7ec4\u5408\uff0c\u5e76\u8fdb\u884c\u4e86\u8f83\u957f\u65f6\u95f4\u7684\u8bad\u7ec3\uff0c\u76f4\u5230\u8bef\u5dee\u964d\u5230\u76ee\u6807\u4e4b\u4e0b\u3002\u8fd9\u51b3\u4e0d\u662f\u6700\u4f73\u7ec4\u5408\uff1b\u5176\u4ed6\u7ec4\u5408\u53ef\u80fd\u4f1a\u83b7\u5f97\u66f4\u4f4e\u7684 RMSE\u3002\u5982\u679c\u60a8\u7684\u76ee\u6807\u662f\u627e\u5230\u53ef\u4ee5\u4ea7\u751f\u6700\u5c0f\u8bef\u5dee\u7684\u6a21\u578b\uff0c\u90a3\u4e48\u60a8\u9700\u8981\u4f7f\u7528\u66f4\u4e25\u683c\u7684\u6d41\u7a0b\uff0c\u4f8b\u5982\u53c2\u6570\u641c\u7d22\u3002" ] }, { "metadata": { "id": "IjkpSqmxqnSM", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "source": [ "dnn_regressor = train_nn_regression_model(\n", " learning_rate=0.001,\n", " steps=2000,\n", " batch_size=100,\n", " hidden_units=[10, 10],\n", " training_examples=training_examples,\n", " training_targets=training_targets,\n", " validation_examples=validation_examples,\n", " validation_targets=validation_targets)" ], "cell_type": "code", "execution_count": 0, "outputs": [] }, { "metadata": { "id": "c6diezCSeH4Y", "colab_type": "text", "slideshow": { "slide_type": "slide" } }, "cell_type": "markdown", "source": [ " ## \u4efb\u52a1 2\uff1a\u7528\u6d4b\u8bd5\u6570\u636e\u8fdb\u884c\u8bc4\u4f30\n", "\n", "**\u786e\u8ba4\u60a8\u7684\u9a8c\u8bc1\u6548\u679c\u7ed3\u679c\u7ecf\u53d7\u5f97\u4f4f\u6d4b\u8bd5\u6570\u636e\u7684\u68c0\u9a8c\u3002**\n", "\n", "\u83b7\u5f97\u6ee1\u610f\u7684\u6a21\u578b\u540e\uff0c\u7528\u6d4b\u8bd5\u6570\u636e\u8bc4\u4f30\u8be5\u6a21\u578b\uff0c\u4ee5\u4e0e\u9a8c\u8bc1\u6548\u679c\u8fdb\u884c\u6bd4\u8f83\u3002\n", "\n", "\u63d0\u793a\uff1a\u6d4b\u8bd5\u6570\u636e\u96c6\u4f4d\u4e8e[\u6b64\u5904](https://storage.googleapis.com/mledu-datasets/california_housing_test.csv)\u3002" ] }, { "metadata": { "id": "icEJIl5Vp51r", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 }, "test": { "output": "ignore", "timeout": 600 } }, "cellView": "both" }, "source": [ "california_housing_test_data = pd.read_csv(\"https://storage.googleapis.com/mledu-datasets/california_housing_test.csv\", sep=\",\")\n", "\n", "# YOUR CODE HERE" ], "cell_type": "code", "execution_count": 0, "outputs": [] }, { "metadata": { "id": "vvT2jDWjrKew", "colab_type": "text" }, "cell_type": "markdown", "source": [ " ### \u89e3\u51b3\u65b9\u6848\n", "\n", "\u70b9\u51fb\u4e0b\u65b9\u5373\u53ef\u67e5\u770b\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\u3002" ] }, { "metadata": { "id": "FyDh7Qy6rQb0", "colab_type": "text" }, "cell_type": "markdown", "source": [ " \u4e0e\u9876\u90e8\u4ee3\u7801\u7c7b\u4f3c\uff0c\u6211\u4eec\u53ea\u9700\u52a0\u8f7d\u5408\u9002\u7684\u6570\u636e\u6587\u4ef6\u3001\u5bf9\u5176\u8fdb\u884c\u9884\u5904\u7406\u5e76\u8c03\u7528\u9884\u6d4b\u548c mean_squared_error \u5373\u53ef\u3002\n", "\n", "\u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u6211\u4eec\u4f1a\u4f7f\u7528\u6240\u6709\u8bb0\u5f55\uff0c\u56e0\u6b64\u65e0\u9700\u5bf9\u6d4b\u8bd5\u6570\u636e\u8fdb\u884c\u968f\u673a\u5316\u5904\u7406\u3002" ] }, { "metadata": { "id": "vhb0CtdvrWZx", "colab_type": "code", "colab": { "autoexec": { "startup": false, "wait_interval": 0 } } }, "source": [ "california_housing_test_data = pd.read_csv(\"https://storage.googleapis.com/mledu-datasets/california_housing_test.csv\", sep=\",\")\n", "\n", "test_examples = preprocess_features(california_housing_test_data)\n", "test_targets = preprocess_targets(california_housing_test_data)\n", "\n", "predict_testing_input_fn = lambda: my_input_fn(test_examples, \n", " test_targets[\"median_house_value\"], \n", " num_epochs=1, \n", " shuffle=False)\n", "\n", "test_predictions = dnn_regressor.predict(input_fn=predict_testing_input_fn)\n", "test_predictions = np.array([item['predictions'][0] for item in test_predictions])\n", "\n", "root_mean_squared_error = math.sqrt(\n", " metrics.mean_squared_error(test_predictions, test_targets))\n", "\n", "print(\"Final RMSE (on test data): %0.2f\" % root_mean_squared_error)" ], "cell_type": "code", "execution_count": 0, "outputs": [] } ] }