Created
October 9, 2019 08:26
-
-
Save jkrukowski/62e539095df342cfd7d872f3e1104a3c to your computer and use it in GitHub Desktop.
differentiation.ipynb
This file contains 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": "differentiation.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "swift", | |
"display_name": "Swift" | |
}, | |
"accelerator": "GPU" | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/jkrukowski/62e539095df342cfd7d872f3e1104a3c/differentiation.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "taisDn5ItGXF", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"# Basic differentiation with Swift for TensorFlow" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "kZRlD4utdPuX", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"// Functions\n", | |
"\n", | |
"func powOf2(_ x: Float) -> Float {\n", | |
" return x * x\n", | |
"}\n", | |
"\n", | |
"func powOf3(_ x: Float) -> Float {\n", | |
" return powOf2(x) * x\n", | |
"}\n", | |
"\n", | |
"func powOf4(_ x: Float) -> Float {\n", | |
" return powOf3(x) * x\n", | |
"}\n", | |
"\n", | |
"let doublePowerOf3: @differentiable (Float) -> Float = {\n", | |
" return 2*powOf3($0)\n", | |
"}\n", | |
"\n", | |
"// Gradients\n", | |
"\n", | |
"let gradPowOf4 = gradient(of: powOf4) // 3 to the power of 4\n", | |
"let gradDoublePowerOf3At2 = gradient(at: 2, in: doublePowerOf3) // 2 times 2 to the power of 3\n", | |
"\n", | |
"let input: Float = 10.0\n", | |
"let (value, grad) = input.valueWithGradient {\n", | |
" return powOf2($0)\n", | |
"}" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "sK5ptA9Bq2EB", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"print(\"3 to the power of 4 = \\(powOf4(3))\")\n", | |
"print(\"𝛁(3 to the power of 4) = \\(gradPowOf4(3)), type: \\(type(of: gradPowOf4))\")\n", | |
"print(\"2 times 2 to the power of 3 = \\(doublePowerOf3(2))\")\n", | |
"print(\"𝛁(2 times 2 to the power of 3) = \\(gradDoublePowerOf3At2), type: \\(type(of: gradDoublePowerOf3At2))\")\n", | |
"print(value, grad)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "xS6KDpjICycR", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment