{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "wJcYs_ERTnnI" }, "source": [ "##### Copyright 2021 The TensorFlow Authors." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "cellView": "form", "execution": { "iopub.execute_input": "2022-12-14T22:24:43.743890Z", "iopub.status.busy": "2022-12-14T22:24:43.743253Z", "iopub.status.idle": "2022-12-14T22:24:43.747067Z", "shell.execute_reply": "2022-12-14T22:24:43.746542Z" }, "id": "HMUDt0CiUJk9" }, "outputs": [], "source": [ "#@title 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." ] }, { "cell_type": "markdown", "metadata": { "id": "77z2OchJTk0l" }, "source": [ "# Estimator から Keras API に移行する\n", "\n", "
![]() | \n",
" ![]() | \n",
" ![]() | \n",
" ![]() | \n",
"
tf.keras.layers.Dense
)レイヤーを持つ単純な Keras Sequential モデルを定義します。\n",
"- Adagrad オプティマイザをインスタンス化します(`tf.keras.optimizers.Adagrad`)。\n",
"- `optimizer` 変数と平均二乗誤差(`\"mse\"`)損失を `Model.compile` に渡して、トレーニング用のモデルを構成します。"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"execution": {
"iopub.execute_input": "2022-12-14T22:24:50.488564Z",
"iopub.status.busy": "2022-12-14T22:24:50.487956Z",
"iopub.status.idle": "2022-12-14T22:24:50.518528Z",
"shell.execute_reply": "2022-12-14T22:24:50.517901Z"
},
"id": "atVciNgPs0fw"
},
"outputs": [],
"source": [
"dataset = tf.data.Dataset.from_tensor_slices((features, labels)).batch(1)\n",
"eval_dataset = tf.data.Dataset.from_tensor_slices(\n",
" (eval_features, eval_labels)).batch(1)\n",
"\n",
"model = tf.keras.models.Sequential([tf.keras.layers.Dense(1)])\n",
"optimizer = tf.keras.optimizers.Adagrad(learning_rate=0.05)\n",
"\n",
"model.compile(optimizer=optimizer, loss=\"mse\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ed17a6291959"
},
"source": [
"これで、`Model.fit` を呼び出してモデルをトレーニングする準備が整いました。"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"execution": {
"iopub.execute_input": "2022-12-14T22:24:50.522081Z",
"iopub.status.busy": "2022-12-14T22:24:50.521514Z",
"iopub.status.idle": "2022-12-14T22:24:51.043078Z",
"shell.execute_reply": "2022-12-14T22:24:51.042327Z"
},
"id": "a0b732534501"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
"1/3 [=========>....................] - ETA: 0s - loss: 3.7549"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\r",
"3/3 [==============================] - 0s 5ms/step - loss: 10.1551\n"
]
},
{
"data": {
"text/plain": [
"