diff --git a/site/ja/tutorials/customization/autodiff.ipynb b/site/ja/tutorials/customization/autodiff.ipynb index fa202371dc7..541108af8f3 100644 --- a/site/ja/tutorials/customization/autodiff.ipynb +++ b/site/ja/tutorials/customization/autodiff.ipynb @@ -1,364 +1,382 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "t09eeeR5prIJ" - }, - "source": [ - "##### Copyright 2018 The TensorFlow Authors." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cellView": "form", - "colab": {}, - "colab_type": "code", - "id": "GCCk8_dHpuNf" - }, - "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": { - "colab_type": "text", - "id": "xh8WkEwWpnm7" - }, - "source": [ - "# 自動微分と勾配テープ" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "idv0bPeCp325" - }, - "source": [ - "\n", - " \n", - " \n", - " \n", - " \n", - "
\n", - " View on TensorFlow.org\n", - " \n", - " Run in Google Colab\n", - " \n", - " View source on GitHub\n", - " \n", - " Download notebook\n", - "
" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Note: これらのドキュメントは私たちTensorFlowコミュニティが翻訳したものです。コミュニティによる 翻訳は**ベストエフォート**であるため、この翻訳が正確であることや[英語の公式ドキュメント](https://www.tensorflow.org/?hl=en)の 最新の状態を反映したものであることを保証することはできません。 この翻訳の品質を向上させるためのご意見をお持ちの方は、GitHubリポジトリ[tensorflow/docs](https://github.com/tensorflow/docs)にプルリクエストをお送りください。 コミュニティによる翻訳やレビューに参加していただける方は、 [docs-ja@tensorflow.org メーリングリスト](https://groups.google.com/a/tensorflow.org/forum/#!forum/docs-ja)にご連絡ください。" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "vDJ4XzMqodTy" - }, - "source": [ - "これまでのチュートリアルでは、テンソルとその演算について紹介しました。このチュートリアルでは、機械学習モデルの最適化の鍵となる手法である[自動微分](https://ja.wikipedia.org/wiki/自動微分)を扱います。" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "GQJysDM__Qb0" - }, - "source": [ - "## 設定" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "cxzaxo6ff2y3" - }, - "outputs": [], - "source": [ - "from __future__ import absolute_import, division, print_function, unicode_literals\n", - "\n", - "!pip install tensorflow==2.0.0-beta1\n", - "import tensorflow as tf" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "1CLWJl0QliB0" - }, - "source": [ - "## 勾配テープ\n", - "\n", - "TensorFlow には、自動微分、すなわち、入力変数に対する計算結果の勾配を計算するための[tf.GradientTape](https://www.tensorflow.org/api_docs/python/tf/GradientTape) API があります。TensorFlow は、`tf.GradientTape` のコンテキスト内で行われる演算すべてを「テープ」に「記録」します。その後 TensorFlow は、そのテープと、そこに記録された演算ひとつひとつに関連する勾配を使い、[トップダウン型自動微分(リバースモード)](https://ja.wikipedia.org/wiki/自動微分)を使用して、「記録」された計算の勾配を計算します。\n", - "\n", - "例を示します。" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "bAFeIE8EuVIq" - }, - "outputs": [], - "source": [ - "x = tf.ones((2, 2))\n", - " \n", - "with tf.GradientTape() as t:\n", - " t.watch(x)\n", - " y = tf.reduce_sum(x)\n", - " z = tf.multiply(y, y)\n", - "\n", - "# 元の入力テンソル x に対する z の微分\n", - "dz_dx = t.gradient(z, x)\n", - "for i in [0, 1]:\n", - " for j in [0, 1]:\n", - " assert dz_dx[i][j].numpy() == 8.0" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "N4VlqKFzzGaC" - }, - "source": [ - "「記録された」`tf.GradientTape` のコンテキスト中の、中間の値に対する出力の勾配を求めることもできます。" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "7XaPRAwUyYms" - }, - "outputs": [], - "source": [ - "x = tf.ones((2, 2))\n", - " \n", - "with tf.GradientTape() as t:\n", - " t.watch(x)\n", - " y = tf.reduce_sum(x)\n", - " z = tf.multiply(y, y)\n", - "\n", - "# テープを使って中間値 y に対する z の微分を計算\n", - "dz_dy = t.gradient(z, y)\n", - "assert dz_dy.numpy() == 8.0" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "ISkXuY7YzIcS" - }, - "source": [ - "デフォルトでは、ある GrandientTape に保持されたリソースは、GradientTape.gradient() メソッドが呼び出されると解放されます。一つの計算で複数の勾配を計算したい場合には、永続的(`persistent`)な勾配テープを作成します。こうすると、リソースはテープオブジェクトがガベージコレクションによって廃棄されるときに解放されるため、`gradient()` メソッドを複数回呼び出すことができます。 \n", - "\n", - "例を示します。" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "zZaCm3-9zVCi" - }, - "outputs": [], - "source": [ - "x = tf.constant(3.0)\n", - "with tf.GradientTape(persistent=True) as t:\n", - " t.watch(x)\n", - " y = x * x\n", - " z = y * y\n", - "dz_dx = t.gradient(z, x) # 108.0 (4*x^3 at x = 3)\n", - "dy_dx = t.gradient(y, x) # 6.0\n", - "del t # テープへの参照を削除" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "6kADybtQzYj4" - }, - "source": [ - "### 制御フローの記録\n", - "\n", - "勾配テープは演算を実行の都度記録するため、(たとえば `if` や `while` を使った)Python の制御フローも自然に扱われます。" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "9FViq92UX7P8" - }, - "outputs": [], - "source": [ - "def f(x, y):\n", - " output = 1.0\n", - " for i in range(y):\n", - " if i > 1 and i < 5:\n", - " output = tf.multiply(output, x)\n", - " return output\n", - "\n", - "def grad(x, y):\n", - " with tf.GradientTape() as t:\n", - " t.watch(x)\n", - " out = f(x, y)\n", - " return t.gradient(out, x) \n", - "\n", - "x = tf.convert_to_tensor(2.0)\n", - "\n", - "assert grad(x, 6).numpy() == 12.0\n", - "assert grad(x, 5).numpy() == 12.0\n", - "assert grad(x, 4).numpy() == 4.0\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "DK05KXrAAld3" - }, - "source": [ - "### 高次勾配\n", - "\n", - "`GradientTape` コンテキストマネージャの内側で行われた演算は、自動微分のために記録されます。このコンテキスト内で勾配が計算された場合、勾配計算も同様に記録されます。結果として、まったくおなじAPIが高次勾配にも使えることになります。例を示します。" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "cPQgthZ7ugRJ" - }, - "outputs": [], - "source": [ - "x = tf.Variable(1.0) # 1.0 で初期化された TensorFlow 変数を作成\n", - "\n", - "with tf.GradientTape() as t:\n", - " with tf.GradientTape() as t2:\n", - " y = x * x * x\n", - " # ’t’ コンテキストマネジャー内で勾配を計算\n", - " # これは勾配計算も同様に微分可能であるということ\n", - " dy_dx = t2.gradient(y, x)\n", - "d2y_dx2 = t.gradient(dy_dx, x)\n", - "\n", - "assert dy_dx.numpy() == 3.0\n", - "assert d2y_dx2.numpy() == 6.0" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "4U1KKzUpNl58" - }, - "source": [ - "## 次のステップ\n", - "\n", - "このチュートリアルでは、TensorFlow の勾配計算を扱いました。これで、ニューラルネットワークの構築と訓練のために必要な構成要素が揃いました。" - ] - } - ], - "metadata": { - "colab": { - "collapsed_sections": [], - "name": "automatic_differentiation.ipynb", - "private_outputs": true, - "provenance": [], - "toc_visible": true, - "version": "0.3.2" - }, - "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.7.4" + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "autodiff.ipynb", + "version": "0.3.2", + "provenance": [], + "private_outputs": true, + "collapsed_sections": [], + "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.7.4" + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + } }, - "varInspector": { - "cols": { - "lenName": 16, - "lenType": 16, - "lenVar": 40 - }, - "kernels_config": { - "python": { - "delete_cmd_postfix": "", - "delete_cmd_prefix": "del ", - "library": "var_list.py", - "varRefreshCmd": "print(var_dic_list())" + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "t09eeeR5prIJ" + }, + "source": [ + "##### Copyright 2018 The TensorFlow Authors." + ] + }, + { + "cell_type": "code", + "metadata": { + "cellView": "form", + "colab_type": "code", + "id": "GCCk8_dHpuNf", + "colab": {} + }, + "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." + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "xh8WkEwWpnm7" + }, + "source": [ + "# 自動微分と勾配テープ" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "idv0bPeCp325" + }, + "source": [ + "\n", + " \n", + " \n", + " \n", + " \n", + "
\n", + " View on TensorFlow.org\n", + " \n", + " Run in Google Colab\n", + " \n", + " View source on GitHub\n", + " \n", + " Download notebook\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Qk0cqFB6lPZ9", + "colab_type": "text" + }, + "source": [ + "Note: これらのドキュメントは私たちTensorFlowコミュニティが翻訳したものです。コミュニティによる 翻訳は**ベストエフォート**であるため、この翻訳が正確であることや[英語の公式ドキュメント](https://www.tensorflow.org/?hl=en)の 最新の状態を反映したものであることを保証することはできません。 この翻訳の品質を向上させるためのご意見をお持ちの方は、GitHubリポジトリ[tensorflow/docs](https://github.com/tensorflow/docs)にプルリクエストをお送りください。 コミュニティによる翻訳やレビューに参加していただける方は、 [docs-ja@tensorflow.org メーリングリスト](https://groups.google.com/a/tensorflow.org/forum/#!forum/docs-ja)にご連絡ください。" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "vDJ4XzMqodTy" + }, + "source": [ + "これまでのチュートリアルでは、テンソルとその演算について紹介しました。このチュートリアルでは、機械学習モデルの最適化の鍵となる手法である[自動微分](https://ja.wikipedia.org/wiki/自動微分)を扱います。" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "GQJysDM__Qb0" + }, + "source": [ + "## 設定" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "O8u4t_ptlu_u", + "colab_type": "code", + "colab": {} + }, + "source": [ + "try:\n", + " %tensorflow_version 2.x\n", + "except Exception:\n", + " pass" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "cxzaxo6ff2y3", + "colab": {} + }, + "source": [ + "from __future__ import absolute_import, division, print_function, unicode_literals\n", + "\n", + "import tensorflow as tf" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "1CLWJl0QliB0" + }, + "source": [ + "## 勾配テープ\n", + "\n", + "TensorFlow には、自動微分、すなわち、入力変数に対する計算結果の勾配を計算するための[tf.GradientTape](https://www.tensorflow.org/api_docs/python/tf/GradientTape) API があります。TensorFlow は、`tf.GradientTape` のコンテキスト内で行われる演算すべてを「テープ」に「記録」します。その後 TensorFlow は、そのテープと、そこに記録された演算ひとつひとつに関連する勾配を使い、[トップダウン型自動微分(リバースモード)](https://ja.wikipedia.org/wiki/自動微分)を使用して、「記録」された計算の勾配を計算します。\n", + "\n", + "例を示します。" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "bAFeIE8EuVIq", + "colab": {} + }, + "source": [ + "x = tf.ones((2, 2))\n", + " \n", + "with tf.GradientTape() as t:\n", + " t.watch(x)\n", + " y = tf.reduce_sum(x)\n", + " z = tf.multiply(y, y)\n", + "\n", + "# 元の入力テンソル x に対する z の微分\n", + "dz_dx = t.gradient(z, x)\n", + "for i in [0, 1]:\n", + " for j in [0, 1]:\n", + " assert dz_dx[i][j].numpy() == 8.0" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "N4VlqKFzzGaC" + }, + "source": [ + "「記録された」`tf.GradientTape` のコンテキスト中の、中間の値に対する出力の勾配を求めることもできます。" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "7XaPRAwUyYms", + "colab": {} + }, + "source": [ + "x = tf.ones((2, 2))\n", + " \n", + "with tf.GradientTape() as t:\n", + " t.watch(x)\n", + " y = tf.reduce_sum(x)\n", + " z = tf.multiply(y, y)\n", + "\n", + "# テープを使って中間値 y に対する z の微分を計算\n", + "dz_dy = t.gradient(z, y)\n", + "assert dz_dy.numpy() == 8.0" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "ISkXuY7YzIcS" + }, + "source": [ + "デフォルトでは、ある GrandientTape に保持されたリソースは、GradientTape.gradient() メソッドが呼び出されると解放されます。一つの計算で複数の勾配を計算したい場合には、永続的(`persistent`)な勾配テープを作成します。こうすると、リソースはテープオブジェクトがガベージコレクションによって廃棄されるときに解放されるため、`gradient()` メソッドを複数回呼び出すことができます。 \n", + "\n", + "例を示します。" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "zZaCm3-9zVCi", + "colab": {} + }, + "source": [ + "x = tf.constant(3.0)\n", + "with tf.GradientTape(persistent=True) as t:\n", + " t.watch(x)\n", + " y = x * x\n", + " z = y * y\n", + "dz_dx = t.gradient(z, x) # 108.0 (4*x^3 at x = 3)\n", + "dy_dx = t.gradient(y, x) # 6.0\n", + "del t # テープへの参照を削除" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "6kADybtQzYj4" + }, + "source": [ + "### 制御フローの記録\n", + "\n", + "勾配テープは演算を実行の都度記録するため、(たとえば `if` や `while` を使った)Python の制御フローも自然に扱われます。" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "9FViq92UX7P8", + "colab": {} + }, + "source": [ + "def f(x, y):\n", + " output = 1.0\n", + " for i in range(y):\n", + " if i > 1 and i < 5:\n", + " output = tf.multiply(output, x)\n", + " return output\n", + "\n", + "def grad(x, y):\n", + " with tf.GradientTape() as t:\n", + " t.watch(x)\n", + " out = f(x, y)\n", + " return t.gradient(out, x) \n", + "\n", + "x = tf.convert_to_tensor(2.0)\n", + "\n", + "assert grad(x, 6).numpy() == 12.0\n", + "assert grad(x, 5).numpy() == 12.0\n", + "assert grad(x, 4).numpy() == 4.0" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "DK05KXrAAld3" + }, + "source": [ + "### 高次勾配\n", + "\n", + "`GradientTape` コンテキストマネージャの内側で行われた演算は、自動微分のために記録されます。このコンテキスト内で勾配が計算された場合、勾配計算も同様に記録されます。結果として、まったくおなじAPIが高次勾配にも使えることになります。例を示します。" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "cPQgthZ7ugRJ", + "colab": {} + }, + "source": [ + "x = tf.Variable(1.0) # 1.0 で初期化された TensorFlow 変数を作成\n", + "\n", + "with tf.GradientTape() as t:\n", + " with tf.GradientTape() as t2:\n", + " y = x * x * x\n", + " # ’t’ コンテキストマネジャー内で勾配を計算\n", + " # これは勾配計算も同様に微分可能であるということ\n", + " dy_dx = t2.gradient(y, x)\n", + "d2y_dx2 = t.gradient(dy_dx, x)\n", + "\n", + "assert dy_dx.numpy() == 3.0\n", + "assert d2y_dx2.numpy() == 6.0" + ], + "execution_count": 0, + "outputs": [] }, - "r": { - "delete_cmd_postfix": ") ", - "delete_cmd_prefix": "rm(", - "library": "var_list.r", - "varRefreshCmd": "cat(var_dic_list()) " + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "4U1KKzUpNl58" + }, + "source": [ + "## 次のステップ\n", + "\n", + "このチュートリアルでは、TensorFlow の勾配計算を扱いました。これで、ニューラルネットワークの構築と訓練のために必要な構成要素が揃いました。" + ] } - }, - "types_to_exclude": [ - "module", - "function", - "builtin_function_or_method", - "instance", - "_Feature" - ], - "window_display": false - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} + ] +} \ No newline at end of file