{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "ISubpr_SSsiM" }, "source": [ "##### Copyright 2020 The TensorFlow Authors.\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "cellView": "form", "execution": { "iopub.execute_input": "2022-12-14T20:34:58.385495Z", "iopub.status.busy": "2022-12-14T20:34:58.384908Z", "iopub.status.idle": "2022-12-14T20:34:58.388594Z", "shell.execute_reply": "2022-12-14T20:34:58.388067Z" }, "id": "3jTMb1dySr3V" }, "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": "6DWfyNThSziV" }, "source": [ "# モジュール、レイヤー、モデルの概要\n", "\n", "
![]() | \n",
" ![]() | \n",
" ![]() | \n",
" ![]() | \n",
"
from_config
メソッド\n",
"\n",
"詳細は、カスタムレイヤーとモデルに関する[完全ガイド](./keras/custom_layers_and_models.ipynb)をご覧ください。"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "L2kds2IHw2KD"
},
"source": [
"### Keras モデル\n",
"\n",
"モデルはネストされた Keras レイヤーとして定義できます。\n",
"\n",
"ただし、Keras は `tf.keras.Model` と呼ばれるフル機能のモデルクラスも提供します。Keras モデルは `tf.keras.layers.Layer` を継承しているため、 Keras レイヤーと同じ方法で使用、ネスト、保存することができます。Keras モデルには、トレーニング、評価、読み込み、保存、および複数のマシンでのトレーニングを容易にする追加機能があります。\n",
"\n",
"上記の `SequentialModule` をほぼ同じコードで定義できます。先ほどと同じように、`__call__` を`call()` に変換して、親を変更します。"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"execution": {
"iopub.execute_input": "2022-12-14T20:35:05.787357Z",
"iopub.status.busy": "2022-12-14T20:35:05.786846Z",
"iopub.status.idle": "2022-12-14T20:35:05.804017Z",
"shell.execute_reply": "2022-12-14T20:35:05.803403Z"
},
"id": "Hqjo1DiyrHrn"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model results: tf.Tensor([[ 0.6988172 -0.0814582]], shape=(1, 2), dtype=float32)\n"
]
}
],
"source": [
"class MySequentialModel(tf.keras.Model):\n",
" def __init__(self, name=None, **kwargs):\n",
" super().__init__(**kwargs)\n",
"\n",
" self.dense_1 = FlexibleDense(out_features=3)\n",
" self.dense_2 = FlexibleDense(out_features=2)\n",
" def call(self, x):\n",
" x = self.dense_1(x)\n",
" return self.dense_2(x)\n",
"\n",
"# You have made a Keras model!\n",
"my_sequential_model = MySequentialModel(name=\"the_model\")\n",
"\n",
"# Call it on a tensor, with random results\n",
"print(\"Model results:\", my_sequential_model(tf.constant([[2.0, 2.0, 2.0]])))\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8i-CR_h2xw3z"
},
"source": [
"追跡変数やサブモジュールなど、すべて同じ機能を利用できます。\n",
"\n",
"注意: 上記の「注意」を繰り返すと、Keras レイヤーまたはモデル内にネストされた生の `tf.Module` は、トレーニングまたは保存のために変数を収集しません。代わりに、Keras レイヤーを Keras レイヤーの内側にネストします。"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"execution": {
"iopub.execute_input": "2022-12-14T20:35:05.807121Z",
"iopub.status.busy": "2022-12-14T20:35:05.806609Z",
"iopub.status.idle": "2022-12-14T20:35:05.812864Z",
"shell.execute_reply": "2022-12-14T20:35:05.812345Z"
},
"id": "hdLQFNdMsOz1"
},
"outputs": [
{
"data": {
"text/plain": [
"[