Skip to content

Commit

Permalink
ee
Browse files Browse the repository at this point in the history
  • Loading branch information
An-JIeun committed Apr 2, 2024
1 parent 71cda00 commit 8e2e6ee
Show file tree
Hide file tree
Showing 151 changed files with 1,156 additions and 149 deletions.
273 changes: 273 additions & 0 deletions docs/.vitepress/codes/calculus2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 자동미분을 활용한 편도함수 계산"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"import tensorflow as tf\n",
"import numpy as np\n",
"import math"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. 함수 정의"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def f(in_x, in_y):\n",
" return in_x**2 + in_y**2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. 텐서 생성\n",
"`.requires_grad()`를 활용해 텐서에 대한 기울기 추적 기능을 켠다."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"x = torch.tensor(0.).requires_grad_()\n",
"y = torch.tensor(0.).requires_grad_()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"x, y를 앞서 정의한 f()에 넣어 foward pass가 이뤄지게 한다."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor(0., grad_fn=<AddBackward0>)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"z = f(x, y) # forward pass \n",
"z"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. 자동 미분 실행\n",
"입력 텐서에 대해 기울기 추적 기능이 켜진 상태에서, z에 대한 backward pass를 진행하여 미분이 이뤄지도록 한다."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"z.backward() # backward pass"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(tensor(0.), tensor(0.))"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x.grad, y.grad # 0인 지점에서 두 텐서의 기울기는 모두 0이다."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4. 사례 1 - 실린더 부피\n",
"\n",
"실린더 부피 $v$는 $v = \\pi r^2 l$로 계산됨. 이때 $r$은 반지름, $l$은 실린더 길이를 의미함"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"def cylinder_volume(in_r, in_l):\n",
" return math.pi * in_r**2 * in_l"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"r = torch.tensor(3.).requires_grad_()\n",
"l = torch.tensor(5.).requires_grad_()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"v = cylinder_volume(r,l)\n",
"v.backward()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(tensor(94.2478), tensor(28.2743))"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"r.grad, l.grad"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"28.2743은 l이 변화할 때의 변화량을 의미한다."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**검증**"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.827433388230787"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cylinder_volume(3,5.1) - cylinder_volume(3,5.0) "
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"delta = 1e-6"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"r이 미분된 식에도 포함되어 있으므로, $\\triangle r$은 0에 근사하는 매우 작은 수여야 한다."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"94.24779531741478"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cylinder_volume(3+delta,5) - cylinder_volume(3,5))/delta"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
24 changes: 24 additions & 0 deletions docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ export default {
collapsed : false,
link:'/contents/MATH/math-main.html',
items: [
{ text: '미적분학',
collapsed : true,
link :'/contents/MATH/calculus/cal-chap-0.html',
items:[
{text : "1. 미분", link: "/contents/MATH/calculus/cal-chap-1.html"},
{text : "2. 편미분", link: "/contents/MATH/calculus/cal-chap-2.html"},
]
},
{ text: '선형대수 - 기초',
collapsed : true,
items:[
Expand Down Expand Up @@ -185,6 +193,22 @@ function getOGTag(pageData) {
"meta",
{ property: "og:locale", content: "ko_KR" },
]);
pageData.frontmatter.head.push([
"meta",
{ property: "twitter:card", content: pageData.frontmatter.description },
]);
pageData.frontmatter.head.push([
"meta",
{ property: "twitter:title", content: pageData.frontmatter.title },
]);
pageData.frontmatter.head.push([
"meta",
{ property: "twitter:description", content: pageData.frontmatter.description },
]);
pageData.frontmatter.head.push([
"meta",
{ property: "twitter:image", content: pageData.frontmatter.description },
]);
const metaData = {
"@context": "http://schema.org",
"@type": "TechArticle",
Expand Down
Loading

0 comments on commit 8e2e6ee

Please sign in to comment.