Created
June 30, 2022 12:16
-
-
Save tomonari-masada/913d6a910c13c3a9f733364c7be124ff to your computer and use it in GitHub Desktop.
torch_erf.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "torch_erf.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyO7ZPv+XfCDUHidfyQ3b9au", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/tomonari-masada/913d6a910c13c3a9f733364c7be124ff/torch_erf.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import numpy as np\n", | |
"import torch\n", | |
"from torch.special import erf\n", | |
"\n", | |
"# 微分係数を計算する位置\n", | |
"a = 1.0\n", | |
"\n", | |
"# erfの自動微分\n", | |
"x = torch.tensor([a], requires_grad=True)\n", | |
"y = erf(x)\n", | |
"y.backward()\n", | |
"print(x.grad.item())\n", | |
"\n", | |
"# 答え合わせ(erfを式の計算で微分しておいたものにaを当てはめる。)\n", | |
"print(2 / np.sqrt(np.pi) * np.exp(- a ** 2))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "X24lm_yqPJvd", | |
"outputId": "6197b06a-38e5-4612-a3e3-f88859402d88" | |
}, | |
"execution_count": 34, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"0.41510751843452454\n", | |
"0.4151074974205947\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"" | |
], | |
"metadata": { | |
"id": "hu5XKjrGez0A" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment