Created
December 12, 2019 17:42
-
-
Save f0nzie/e2fc9846731f7d732494f3c36ce67b84 to your computer and use it in GitHub Desktop.
rmarch.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": "rmarch.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/f0nzie/e2fc9846731f7d732494f3c36ce67b84/rmarch.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "YCnh_ab8JCW8", | |
"colab_type": "code", | |
"outputId": "19e96904-1513-465c-c418-401df4cfb867", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
} | |
}, | |
"source": [ | |
"import torch\n", | |
"import numpy as np\n", | |
"\n", | |
"torch.manual_seed(123)" | |
], | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"<torch._C.Generator at 0x7f203dd48c30>" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 1 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "z585IKG3JYJY", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"# my crazy function\n", | |
"def g(x):\n", | |
" eq = x.clone()\n", | |
" eq[0] = 2*x[0] # this will produce error without cloning \"x\" first\n", | |
" # x = eq\n", | |
" return eq" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "c5lHEMeYJYXS", | |
"colab_type": "code", | |
"outputId": "97c5c53b-f800-4096-e429-5f4d76c30a53", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 71 | |
} | |
}, | |
"source": [ | |
"\n", | |
"x0 = np.array([1, 2, 3, 4], dtype=\"float\")\n", | |
"x0 = torch.tensor(x0)\n", | |
"x0 = x0.view(2,2)\n", | |
"x0.requires_grad = True\n", | |
"\n", | |
"print(x0)\n", | |
"print(x0.shape)" | |
], | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"tensor([[1., 2.],\n", | |
" [3., 4.]], dtype=torch.float64, requires_grad=True)\n", | |
"torch.Size([2, 2])\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "tVSO8P3GhdVT", | |
"colab_type": "code", | |
"outputId": "1a22f4c0-d28a-4e63-ece5-02c162b06a10", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 89 | |
} | |
}, | |
"source": [ | |
"y = g(x0)\n", | |
"print(y)\n", | |
"y.backward(x0, retain_graph=True)\n", | |
"x0.grad" | |
], | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"tensor([[2., 4.],\n", | |
" [3., 4.]], dtype=torch.float64, grad_fn=<CopySlices>)\n" | |
], | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"tensor([[2., 4.],\n", | |
" [3., 4.]], dtype=torch.float64)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 4 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "RxAu1_fYhjaC", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "khlzZp_Yhj0Y", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "PoL8PbeLOE_8", | |
"colab_type": "code", | |
"outputId": "a09c6078-e7c5-48e9-e0a3-a7bfecda8670", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
} | |
}, | |
"source": [ | |
"# another crazy function\n", | |
"w = torch.tensor([1, 2])\n", | |
"w = w.double()\n", | |
"print(w)\n", | |
"\n", | |
"def h(x):\n", | |
" return x @ w" | |
], | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"tensor([1., 2.], dtype=torch.float64)\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "i6clIvVZOFLf", | |
"colab_type": "code", | |
"outputId": "dcd0f475-06fc-414e-d58e-d640a40e5f07", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
} | |
}, | |
"source": [ | |
"y = h(x0)\n", | |
"print(y)" | |
], | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"tensor([ 5., 11.], dtype=torch.float64, grad_fn=<MvBackward>)\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "dZnpRxCaPSrg", | |
"colab_type": "code", | |
"outputId": "6eee76aa-6a64-4baa-940b-d599fb260ba6", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 53 | |
} | |
}, | |
"source": [ | |
"y[1].backward(retain_graph=True)\n", | |
"x0.grad" | |
], | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"tensor([[2., 4.],\n", | |
" [4., 6.]], dtype=torch.float64)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 7 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "uvRq0QrMPS8h", | |
"colab_type": "code", | |
"outputId": "9261b1ae-c080-4ef3-c114-7d434d6b6845", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 53 | |
} | |
}, | |
"source": [ | |
"y[0].backward(retain_graph=True)\n", | |
"x0.grad" | |
], | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"tensor([[3., 6.],\n", | |
" [4., 6.]], dtype=torch.float64)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 8 | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment