{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "b518b04cbfe0" }, "source": [ "##### Copyright 2020 The TensorFlow Authors." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "cellView": "form", "execution": { "iopub.execute_input": "2024-01-11T19:46:58.290572Z", "iopub.status.busy": "2024-01-11T19:46:58.290350Z", "iopub.status.idle": "2024-01-11T19:46:58.294049Z", "shell.execute_reply": "2024-01-11T19:46:58.293390Z" }, "id": "906e07f6e562" }, "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": "3e19705694d6" }, "source": [ "# Sequential モデル" ] }, { "cell_type": "markdown", "metadata": { "id": "defbb10e8ae3" }, "source": [ "\n", " \n", " \n", " \n", " \n", "
TensorFlow.org で実行 \tGoogle Colabで実行 GitHubでソースを表示 ノートブックをダウンロード
" ] }, { "cell_type": "markdown", "metadata": { "id": "8d4ac441b1fc" }, "source": [ "## MNIST モデルをビルドする" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-01-11T19:46:58.297745Z", "iopub.status.busy": "2024-01-11T19:46:58.297146Z", "iopub.status.idle": "2024-01-11T19:47:00.670854Z", "shell.execute_reply": "2024-01-11T19:47:00.670111Z" }, "id": "0472bf67b2bf" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2024-01-11 19:46:58.726279: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n", "2024-01-11 19:46:58.726330: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n", "2024-01-11 19:46:58.727948: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n" ] } ], "source": [ "import tensorflow as tf\n", "from tensorflow import keras\n", "from tensorflow.keras import layers" ] }, { "cell_type": "markdown", "metadata": { "id": "80f7713c6b92" }, "source": [ "## Sequential モデルの使用が適している場合\n", "\n", "`Sequential`モデルは、各レイヤーに** 1 つの入力テンソルと 1 つの出力テンソルのみ**がある**レイヤーのプレーンスタック**に適しています。\n", "\n", "概略的には、以下の`Sequential`モデルを参照してください。" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-01-11T19:47:00.675103Z", "iopub.status.busy": "2024-01-11T19:47:00.674708Z", "iopub.status.idle": "2024-01-11T19:47:03.061684Z", "shell.execute_reply": "2024-01-11T19:47:03.060856Z" }, "id": "f536515be229" }, "outputs": [], "source": [ "# Define Sequential model with 3 layers\n", "model = keras.Sequential(\n", " [\n", " layers.Dense(2, activation=\"relu\", name=\"layer1\"),\n", " layers.Dense(3, activation=\"relu\", name=\"layer2\"),\n", " layers.Dense(4, name=\"layer3\"),\n", " ]\n", ")\n", "# Call model on a test input\n", "x = tf.ones((3, 3))\n", "y = model(x)" ] }, { "cell_type": "markdown", "metadata": { "id": "7d81502d9753" }, "source": [ "上記は次の関数と同じです。" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-01-11T19:47:03.065718Z", "iopub.status.busy": "2024-01-11T19:47:03.065421Z", "iopub.status.idle": "2024-01-11T19:47:03.083039Z", "shell.execute_reply": "2024-01-11T19:47:03.082457Z" }, "id": "7059a8890f72" }, "outputs": [], "source": [ "# Create 3 layers\n", "layer1 = layers.Dense(2, activation=\"relu\", name=\"layer1\")\n", "layer2 = layers.Dense(3, activation=\"relu\", name=\"layer2\")\n", "layer3 = layers.Dense(4, name=\"layer3\")\n", "\n", "# Call layers on a test input\n", "x = tf.ones((3, 3))\n", "y = layer3(layer2(layer1(x)))" ] }, { "cell_type": "markdown", "metadata": { "id": "cdf6d2932c31" }, "source": [ "以下の場合、Sequential モデルは**適切ではありません**。\n", "\n", "- モデルに複数の入力または複数の出力がある場合\n", "- レイヤーに複数の入力または複数の出力がある場合\n", "- レイヤーの共有を行う必要がある場合\n", "- 非線形トポロジーが必要な場合(残差を用いた接続、複数の分岐点をもつモデルなど)" ] }, { "cell_type": "markdown", "metadata": { "id": "c5706d9f8eb8" }, "source": [ "## Sequential モデルの作成\n", "\n", "レイヤーのリストを Sequential コンストラクタに渡すことにより、Sequential モデルを作成できます。" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-01-11T19:47:03.086285Z", "iopub.status.busy": "2024-01-11T19:47:03.086047Z", "iopub.status.idle": "2024-01-11T19:47:03.094032Z", "shell.execute_reply": "2024-01-11T19:47:03.093451Z" }, "id": "8b3eee00f80d" }, "outputs": [], "source": [ "model = keras.Sequential(\n", " [\n", " layers.Dense(2, activation=\"relu\"),\n", " layers.Dense(3, activation=\"relu\"),\n", " layers.Dense(4),\n", " ]\n", ")" ] }, { "cell_type": "markdown", "metadata": { "id": "1939a7a4a66c" }, "source": [ "そのレイヤーには、`layers`属性を介してアクセスできます。" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-01-11T19:47:03.097175Z", "iopub.status.busy": "2024-01-11T19:47:03.096935Z", "iopub.status.idle": "2024-01-11T19:47:03.103266Z", "shell.execute_reply": "2024-01-11T19:47:03.102718Z" }, "id": "49c0448b6da2" }, "outputs": [ { "data": { "text/plain": [ "[,\n", " ,\n", " ]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.layers" ] }, { "cell_type": "markdown", "metadata": { "id": "b4c7957e9913" }, "source": [ "また、`add()`メソッドを使用して Sequential モデルを作成することもできます。" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-01-11T19:47:03.106603Z", "iopub.status.busy": "2024-01-11T19:47:03.106076Z", "iopub.status.idle": "2024-01-11T19:47:03.114386Z", "shell.execute_reply": "2024-01-11T19:47:03.113819Z" }, "id": "d54fde401054" }, "outputs": [], "source": [ "model = keras.Sequential()\n", "model.add(layers.Dense(2, activation=\"relu\"))\n", "model.add(layers.Dense(3, activation=\"relu\"))\n", "model.add(layers.Dense(4))" ] }, { "cell_type": "markdown", "metadata": { "id": "d16278f5a1dc" }, "source": [ "また、レイヤーを削除するには、 `pop()` メソッドが対応します。Sequential モデルは、レイヤーのリストのように動作します。" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-01-11T19:47:03.118017Z", "iopub.status.busy": "2024-01-11T19:47:03.117347Z", "iopub.status.idle": "2024-01-11T19:47:03.120952Z", "shell.execute_reply": "2024-01-11T19:47:03.120321Z" }, "id": "e89f35b73979" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "model.pop()\n", "print(len(model.layers)) # 2" ] }, { "cell_type": "markdown", "metadata": { "id": "99cb1c9a7c7a" }, "source": [ "また、Sequential コンストラクタは、Keras のレイヤーやモデルと同様に、`name`引数を受け入れます。これは、セマンティックに意味のある命名で TensorBoard グラフに注釈を付けるのに役立ちます。" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2024-01-11T19:47:03.124125Z", "iopub.status.busy": "2024-01-11T19:47:03.123898Z", "iopub.status.idle": "2024-01-11T19:47:03.132557Z", "shell.execute_reply": "2024-01-11T19:47:03.131974Z" }, "id": "068c2f82e7cb" }, "outputs": [], "source": [ "model = keras.Sequential(name=\"my_sequential\")\n", "model.add(layers.Dense(2, activation=\"relu\", name=\"layer1\"))\n", "model.add(layers.Dense(3, activation=\"relu\", name=\"layer2\"))\n", "model.add(layers.Dense(4, name=\"layer3\"))" ] }, { "cell_type": "markdown", "metadata": { "id": "a6247ff17d3a" }, "source": [ "## 事前に入力形状を指定\n", "\n", "一般的に、Keras のすべてのレイヤーは、重みを作成できるようにするために、入力の形状を知る必要があります。このようなレイヤーを作成する場合、当初は重みがありません。" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2024-01-11T19:47:03.135698Z", "iopub.status.busy": "2024-01-11T19:47:03.135467Z", "iopub.status.idle": "2024-01-11T19:47:03.140331Z", "shell.execute_reply": "2024-01-11T19:47:03.139764Z" }, "id": "373ecbb5c6bd" }, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "layer = layers.Dense(3)\n", "layer.weights # Empty" ] }, { "cell_type": "markdown", "metadata": { "id": "da150335961e" }, "source": [ "重みの形状は入力の形状に依存するため、入力時に最初に呼び出されたときに重みが作成されます。" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2024-01-11T19:47:03.143386Z", "iopub.status.busy": "2024-01-11T19:47:03.143170Z", "iopub.status.idle": "2024-01-11T19:47:03.160424Z", "shell.execute_reply": "2024-01-11T19:47:03.159855Z" }, "id": "bf28829ce193" }, "outputs": [ { "data": { "text/plain": [ "[,\n", " ]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Call layer on a test input\n", "x = tf.ones((1, 4))\n", "y = layer(x)\n", "layer.weights # Now it has weights, of shape (4, 3) and (3,)" ] }, { "cell_type": "markdown", "metadata": { "id": "e50f21c5f247" }, "source": [ "これは Sequential モデルでも同じです。入力形状なしで Sequential モデルをインスタンス化すると、重みがないために「構築」されません (`model.weights`を呼び出すと、これを示すエラーが発生します)。重みは、モデルが最初に入力データを確認したときに作成されます。" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2024-01-11T19:47:03.163374Z", "iopub.status.busy": "2024-01-11T19:47:03.163147Z", "iopub.status.idle": "2024-01-11T19:47:03.202563Z", "shell.execute_reply": "2024-01-11T19:47:03.201941Z" }, "id": "04479960526c" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of weights after calling the model: 6\n" ] } ], "source": [ "model = keras.Sequential(\n", " [\n", " layers.Dense(2, activation=\"relu\"),\n", " layers.Dense(3, activation=\"relu\"),\n", " layers.Dense(4),\n", " ]\n", ") # No weights at this stage!\n", "\n", "# At this point, you can't do this:\n", "# model.weights\n", "\n", "# You also can't do this:\n", "# model.summary()\n", "\n", "# Call the model on a test input\n", "x = tf.ones((1, 4))\n", "y = model(x)\n", "print(\"Number of weights after calling the model:\", len(model.weights)) # 6" ] }, { "cell_type": "markdown", "metadata": { "id": "2837e3d2c798" }, "source": [ "モデルが「構築」されたら、その`summary()`メソッドを呼び出して、その内容を表示できます。" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2024-01-11T19:47:03.206254Z", "iopub.status.busy": "2024-01-11T19:47:03.205616Z", "iopub.status.idle": "2024-01-11T19:47:03.216290Z", "shell.execute_reply": "2024-01-11T19:47:03.215703Z" }, "id": "1368bd27f679" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model: \"sequential_3\"\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "_________________________________________________________________\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " Layer (type) Output Shape Param # \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "=================================================================\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " dense_7 (Dense) (1, 2) 10 \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " dense_8 (Dense) (1, 3) 9 \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " dense_9 (Dense) (1, 4) 16 \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "=================================================================\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Total params: 35 (140.00 Byte)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Trainable params: 35 (140.00 Byte)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Non-trainable params: 0 (0.00 Byte)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "_________________________________________________________________\n" ] } ], "source": [ "model.summary()" ] }, { "cell_type": "markdown", "metadata": { "id": "08cf1da27edb" }, "source": [ "ただし、Sequential モデルを段階的に構築する場合、その時点の出力形状を含め、それまでのモデルの概要を表示できると非常に便利です。 この場合、モデルに`Input`オブジェクトを渡してモデルを開始し、最初から入力形状がわかるようにする必要があります。" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2024-01-11T19:47:03.223959Z", "iopub.status.busy": "2024-01-11T19:47:03.223716Z", "iopub.status.idle": "2024-01-11T19:47:03.245656Z", "shell.execute_reply": "2024-01-11T19:47:03.245068Z" }, "id": "e3d2024cfeeb" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model: \"sequential_4\"\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "_________________________________________________________________\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " Layer (type) Output Shape Param # \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "=================================================================\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " dense_10 (Dense) (None, 2) 10 \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "=================================================================\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Total params: 10 (40.00 Byte)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Trainable params: 10 (40.00 Byte)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Non-trainable params: 0 (0.00 Byte)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "_________________________________________________________________\n" ] } ], "source": [ "model = keras.Sequential()\n", "model.add(keras.Input(shape=(4,)))\n", "model.add(layers.Dense(2, activation=\"relu\"))\n", "\n", "model.summary()" ] }, { "cell_type": "markdown", "metadata": { "id": "3d965e3761a8" }, "source": [ "`Input`オブジェクトはレイヤーではないため、`model.layers`の一部として表示されないことに注意してください。" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "execution": { "iopub.execute_input": "2024-01-11T19:47:03.251519Z", "iopub.status.busy": "2024-01-11T19:47:03.250880Z", "iopub.status.idle": "2024-01-11T19:47:03.254857Z", "shell.execute_reply": "2024-01-11T19:47:03.254308Z" }, "id": "8e3b0d58e7ed" }, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.layers" ] }, { "cell_type": "markdown", "metadata": { "id": "8a057b1baf72" }, "source": [ "簡単な代替手段として、最初のレイヤーに`input_shape`引数を渡すこともできます。" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "execution": { "iopub.execute_input": "2024-01-11T19:47:03.258257Z", "iopub.status.busy": "2024-01-11T19:47:03.257794Z", "iopub.status.idle": "2024-01-11T19:47:03.279278Z", "shell.execute_reply": "2024-01-11T19:47:03.278713Z" }, "id": "1c6ab83d68ea" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model: \"sequential_5\"\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "_________________________________________________________________\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " Layer (type) Output Shape Param # \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "=================================================================\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " dense_11 (Dense) (None, 2) 10 \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "=================================================================\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Total params: 10 (40.00 Byte)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Trainable params: 10 (40.00 Byte)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Non-trainable params: 0 (0.00 Byte)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "_________________________________________________________________\n" ] } ], "source": [ "model = keras.Sequential()\n", "model.add(layers.Dense(2, activation=\"relu\", input_shape=(4,)))\n", "\n", "model.summary()" ] }, { "cell_type": "markdown", "metadata": { "id": "40c14619d283" }, "source": [ "このような事前定義された入力形状で構築されたモデルは、常に (データが確認される前でも) 重みを持ち、常に定義された出力形状をもっています。\n", "\n", "一般的に、Sequential モデルの入力形状がわかっている場合は、常に事前に指定しておくことをお勧めします。" ] }, { "cell_type": "markdown", "metadata": { "id": "843f6b6505b3" }, "source": [ "## 一般的なデバッグワークフロー:`add()` + `summary()`\n", "\n", "新しい Sequential アーキテクチャを構築する場合、`add()`を使用して段階的にレイヤーを積み重ね、モデルの概要を頻繁に出力することをお勧めします。これにより、`Conv2D`レイヤーと`MaxPooling2D`レイヤーのスタックが画像特徴量マップをどのようにダウンサンプリングしているかを監視できます。" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "execution": { "iopub.execute_input": "2024-01-11T19:47:03.285304Z", "iopub.status.busy": "2024-01-11T19:47:03.285053Z", "iopub.status.idle": "2024-01-11T19:47:03.418350Z", "shell.execute_reply": "2024-01-11T19:47:03.417693Z" }, "id": "46bfb8f7dc6e" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model: \"sequential_6\"\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "_________________________________________________________________\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " Layer (type) Output Shape Param # \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "=================================================================\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " conv2d (Conv2D) (None, 123, 123, 32) 2432 \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " conv2d_1 (Conv2D) (None, 121, 121, 32) 9248 \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " max_pooling2d (MaxPooling2 (None, 40, 40, 32) 0 \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " D) \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "=================================================================\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Total params: 11680 (45.62 KB)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Trainable params: 11680 (45.62 KB)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Non-trainable params: 0 (0.00 Byte)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "_________________________________________________________________\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Model: \"sequential_6\"\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "_________________________________________________________________\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " Layer (type) Output Shape Param # \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "=================================================================\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " conv2d (Conv2D) (None, 123, 123, 32) 2432 \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " conv2d_1 (Conv2D) (None, 121, 121, 32) 9248 \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " max_pooling2d (MaxPooling2 (None, 40, 40, 32) 0 \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " D) \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " conv2d_2 (Conv2D) (None, 38, 38, 32) 9248 \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " conv2d_3 (Conv2D) (None, 36, 36, 32) 9248 \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " max_pooling2d_1 (MaxPoolin (None, 12, 12, 32) 0 \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " g2D) \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " conv2d_4 (Conv2D) (None, 10, 10, 32) 9248 \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " conv2d_5 (Conv2D) (None, 8, 8, 32) 9248 \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " max_pooling2d_2 (MaxPoolin (None, 4, 4, 32) 0 \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " g2D) \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "=================================================================\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Total params: 48672 (190.12 KB)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Trainable params: 48672 (190.12 KB)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Non-trainable params: 0 (0.00 Byte)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "_________________________________________________________________\n" ] } ], "source": [ "model = keras.Sequential()\n", "model.add(keras.Input(shape=(250, 250, 3))) # 250x250 RGB images\n", "model.add(layers.Conv2D(32, 5, strides=2, activation=\"relu\"))\n", "model.add(layers.Conv2D(32, 3, activation=\"relu\"))\n", "model.add(layers.MaxPooling2D(3))\n", "\n", "# Can you guess what the current output shape is at this point? Probably not.\n", "# Let's just print it:\n", "model.summary()\n", "\n", "# The answer was: (40, 40, 32), so we can keep downsampling...\n", "\n", "model.add(layers.Conv2D(32, 3, activation=\"relu\"))\n", "model.add(layers.Conv2D(32, 3, activation=\"relu\"))\n", "model.add(layers.MaxPooling2D(3))\n", "model.add(layers.Conv2D(32, 3, activation=\"relu\"))\n", "model.add(layers.Conv2D(32, 3, activation=\"relu\"))\n", "model.add(layers.MaxPooling2D(2))\n", "\n", "# And now?\n", "model.summary()\n", "\n", "# Now that we have 4x4 feature maps, time to apply global max pooling.\n", "model.add(layers.GlobalMaxPooling2D())\n", "\n", "# Finally, we add a classification layer.\n", "model.add(layers.Dense(10))" ] }, { "cell_type": "markdown", "metadata": { "id": "a2d3335a90fa" }, "source": [ "これは、非常に実用的なワークフローです。\n" ] }, { "cell_type": "markdown", "metadata": { "id": "46addede37f3" }, "source": [ "## モデルの準備ができたら\n", "\n", "モデルアーキテクチャの準備ができたら、以下を行います。\n", "\n", "- モデルをトレーニング、評価し、推論を実行します。[「トレーニングと組み込みループを使用した評価のガイド」](https://www.tensorflow.org/guide/keras/train_and_evaluate/)をご覧ください。\n", "- モデルをディスクに保存して復元します。[「シリアル化と保存のガイド」](https://www.tensorflow.org/guide/keras/save_and_serialize/)をご覧ください。\n", "- 複数の GPU を活用してモデルを迅速にトレーニングします。[「マルチ GPU と分散トレーニングのガイド」](https://keras.io/guides/distributed_training/)をご覧ください。" ] }, { "cell_type": "markdown", "metadata": { "id": "608f3b03669c" }, "source": [ "## Sequential モデルによる特徴量の抽出\n", "\n", "Sequential モデルが構築されると、[Functional API モデル](https://www.tensorflow.org/guide/keras/functional/)のように動作します。つまり、すべてのレイヤーに`input`および`output`属性があります。これらの属性を使用すると、Sequential モデルのすべての中間層の出力を抽出するモデルを迅速に作成したりできます。" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "execution": { "iopub.execute_input": "2024-01-11T19:47:03.422155Z", "iopub.status.busy": "2024-01-11T19:47:03.421889Z", "iopub.status.idle": "2024-01-11T19:47:04.033841Z", "shell.execute_reply": "2024-01-11T19:47:04.033041Z" }, "id": "a5888d753301" }, "outputs": [], "source": [ "initial_model = keras.Sequential(\n", " [\n", " keras.Input(shape=(250, 250, 3)),\n", " layers.Conv2D(32, 5, strides=2, activation=\"relu\"),\n", " layers.Conv2D(32, 3, activation=\"relu\"),\n", " layers.Conv2D(32, 3, activation=\"relu\"),\n", " ]\n", ")\n", "feature_extractor = keras.Model(\n", " inputs=initial_model.inputs,\n", " outputs=[layer.output for layer in initial_model.layers],\n", ")\n", "\n", "# Call feature extractor on test input.\n", "x = tf.ones((1, 250, 250, 3))\n", "features = feature_extractor(x)" ] }, { "cell_type": "markdown", "metadata": { "id": "4abef35355d3" }, "source": [ "以下の例では、1 つのレイヤーからのみ特徴量を抽出します。" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "execution": { "iopub.execute_input": "2024-01-11T19:47:04.038189Z", "iopub.status.busy": "2024-01-11T19:47:04.037685Z", "iopub.status.idle": "2024-01-11T19:47:04.080953Z", "shell.execute_reply": "2024-01-11T19:47:04.080277Z" }, "id": "fc404c7ac90e" }, "outputs": [], "source": [ "initial_model = keras.Sequential(\n", " [\n", " keras.Input(shape=(250, 250, 3)),\n", " layers.Conv2D(32, 5, strides=2, activation=\"relu\"),\n", " layers.Conv2D(32, 3, activation=\"relu\", name=\"my_intermediate_layer\"),\n", " layers.Conv2D(32, 3, activation=\"relu\"),\n", " ]\n", ")\n", "feature_extractor = keras.Model(\n", " inputs=initial_model.inputs,\n", " outputs=initial_model.get_layer(name=\"my_intermediate_layer\").output,\n", ")\n", "# Call feature extractor on test input.\n", "x = tf.ones((1, 250, 250, 3))\n", "features = feature_extractor(x)" ] }, { "cell_type": "markdown", "metadata": { "id": "4e2fb64f0676" }, "source": [ "## Sequential モデルによる転移学習\n", "\n", "転移学習では、モデルの最下層を凍結し、最上層のみをトレーニングします。転移学習の詳細については、[転移学習のガイド](https://www.tensorflow.org/guide/keras/transfer_learning/)を参照してください。\n", "\n", "以下は、Sequential モデルを含む 2 つの一般的な転移学習の例です。\n", "\n", "まず、Sequential モデルで、最後のレイヤーを除くすべてのレイヤーを凍結するとします。この場合、`model.layers` を繰り返し処理し、最後のレイヤーを除く各レイヤーに `layer.trainable = False` を設定します。 以下に例を示します。\n", "\n", "```python\n", "model = keras.Sequential([\n", " keras.Input(shape=(784)),\n", " layers.Dense(32, activation='relu'),\n", " layers.Dense(32, activation='relu'),\n", " layers.Dense(32, activation='relu'),\n", " layers.Dense(10),\n", "])\n", "\n", "# Presumably you would want to first load pre-trained weights.\n", "model.load_weights(...)\n", "\n", "# Freeze all layers except the last one.\n", "for layer in model.layers[:-1]:\n", " layer.trainable = False\n", "\n", "# Recompile and train (this will only update the weights of the last layer).\n", "model.compile(...)\n", "model.fit(...)\n", "```\n", "\n", "もう 1 つの一般的な例は、Sequential モデルを使用して、事前にトレーニングされたモデルといくつかの新しく初期化された分類レイヤーをスタックすることです。以下に例を示します。\n", "\n", "```python\n", "# Load a convolutional base with pre-trained weights\n", "base_model = keras.applications.Xception(\n", " weights='imagenet',\n", " include_top=False,\n", " pooling='avg')\n", "\n", "# Freeze the base model\n", "base_model.trainable = False\n", "\n", "# Use a Sequential model to add a trainable classifier on top\n", "model = keras.Sequential([\n", " base_model,\n", " layers.Dense(1000),\n", "])\n", "\n", "# Compile & train\n", "model.compile(...)\n", "model.fit(...)\n", "```\n", "\n", "転移学習を行う場合、おそらくこれら 2 つのパターンを頻繁に使用されていることに気付くでしょう。" ] }, { "cell_type": "markdown", "metadata": { "id": "fcffc33b61e5" }, "source": [ "Sequential モデルについての説明は以上です。\n", "\n", "Keras でのモデル構築の詳細については、以下を参照してください。\n", "\n", "- [ Functional API のガイド](https://www.tensorflow.org/guide/keras/functional/)\n", "- [サブクラス化でレイヤとモデルをゼロから作成する方法](https://www.tensorflow.org/guide/keras/custom_layers_and_models/)" ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "sequential_model.ipynb", "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "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.9.18" } }, "nbformat": 4, "nbformat_minor": 0 }