Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion site/en/tutorials/eager/custom_training.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "Custom training: basics",
"name": "custom_training.ipynb",
"private_outputs": true,
"provenance": [],
"toc_visible": true,
Expand Down
341 changes: 341 additions & 0 deletions site/ko/tutorials/eager/automatic_differentiation.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,341 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "automatic_differentiation.ipynb",
"version": "0.3.2",
"provenance": [],
"private_outputs": true,
"collapsed_sections": [],
"toc_visible": true
},
"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.1"
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
}
},
"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": [
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://www.tensorflow.org/tutorials/eager/automatic_differentiation\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />TensorFlow.org에서 보기</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/docs/blob/master/site/ko/tutorials/eager/automatic_differentiation.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />구글 코랩(Colab)에서 실행하기</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://github.com/tensorflow/docs/blob/master/site/ko/tutorials/eager/automatic_differentiation.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />깃허브(GitHub) 소스 보기</a>\n",
" </td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Q9_NaXPWxEd8",
"colab_type": "text"
},
"source": [
"Note: 이 문서는 텐서플로 커뮤니티에서 번역했습니다. 커뮤니티 번역 활동의 특성상 정확한 번역과 최신 내용을 반영하기 위해 노력함에도\n",
"불구하고 [공식 영문 문서](https://www.tensorflow.org/?hl=en)의 내용과 일치하지 않을 수 있습니다.\n",
"이 번역에 개선할 부분이 있다면\n",
"[tensorflow/docs](https://github.com/tensorflow/docs) 깃헙 저장소로 풀 리퀘스트를 보내주시기 바랍니다.\n",
"문서 번역이나 리뷰에 참여하려면\n",
"[docs-ko@tensorflow.org](https://groups.google.com/a/tensorflow.org/forum/#!forum/docs-ko)로\n",
"메일을 보내주시기 바랍니다."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "vDJ4XzMqodTy"
},
"source": [
"이전 튜토리얼에서는 텐서(tensor)와 텐서의 연산에 대해서 알아보았습니다. 이번 튜토리얼에서는 머신러닝 모델을 최적화할 수 있는 주요 기술 중 하나인 [자동 미분(automatic differentiation)](https://en.wikipedia.org/wiki/Automatic_differentiation)에 대해 알아보겠습니다."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "GQJysDM__Qb0"
},
"source": [
"## 설정\n"
]
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"id": "OiMPZStlibBv",
"colab": {}
},
"source": [
"from __future__ import absolute_import, division, print_function, unicode_literals\n",
"\n",
"import tensorflow as tf\n",
"\n",
"tf.enable_eager_execution()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "1CLWJl0QliB0"
},
"source": [
"## 그래디언트 테이프\n",
"\n",
"텐서플로는 자동 미분(주어진 입력 변수에 대한 연산의 그래디언트(gradient)를 계산하는 것)을 위한 [tf.GradientTape](https://www.tensorflow.org/api_docs/python/tf/GradientTape) API를 제공합니다. `tf.GradientTape`는 안에서 실행된 모든 연산을 테이프(tape)에 \"기록\"합니다. 그리고 [후진 방식 자동 미분(reverse mode differentiation)](https://en.wikipedia.org/wiki/Automatic_differentiation)을 사용하여 각각의 기록된 연산과 관련된 그래디언트와 테이프를 사용하여 기록된 연산의 그래디언트를 계산합니다. \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에 대한 도함수를 계산합니다. \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": [
"기본적으로 GradientTape.gradient() 메서드가 호출되면 GradientTape에 포함된 리소스가 해제됩니다. 동일한 연산 대해 여러 그래디언트를 계산하려면, `지속성있는(persistent)` 그래디언트 테이프를 생성하면 됩니다. 이 그래디언트 테이프는 `gradient()` 메서드의 다중 호출을 허용합니다. 테이프 객체가 쓰레기 수집(garbage collection)될때 리소스는 해체됩니다.\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`, `for`문 같은)이 자연스럽게 처리됩니다. "
]
},
{
"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\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "DK05KXrAAld3"
},
"source": [
"### 고계도(Higher-order) 그래디언트\n",
"\n",
"`GradientTape` 컨텍스트 매니저안에 있는 연산들은 자동미분을 위해 기록됩니다. 만약 그래디언트가 컨텍스트 안에서 계산되면 그 그래디언트 연산 또한 기록되어집니다. 그 결과 똑같은 API가 고계도 그래디언트에서도 잘 작동합니다. 예를 들면:"
]
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"id": "cPQgthZ7ugRJ",
"colab": {}
},
"source": [
"x = tf.Variable(1.0) # 1.0으로 초기화된 텐서플로 변수를 생성합니다.\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": []
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "4U1KKzUpNl58"
},
"source": [
"## 다음 단계\n",
"\n",
"이번 튜토리얼에서는 텐서플로에서 그래디언트 계산법을 다루었습니다. 이를 통해 신경망(neural network)을 구축하고 훈련시키는데 필요한 많은 기본 요소를 배웠습니다."
]
}
]
}
Loading