Skip to content

Instantly share code, notes, and snippets.

@nathanhubens
Created March 27, 2019 16:28
Show Gist options
  • Save nathanhubens/5a9fc090dcfbf03759068ae0fc3df1c9 to your computer and use it in GitHub Desktop.
Save nathanhubens/5a9fc090dcfbf03759068ae0fc3df1c9 to your computer and use it in GitHub Desktop.
Forward vs Call.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Forward vs Call.ipynb",
"version": "0.3.2",
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/nathanhubens/5a9fc090dcfbf03759068ae0fc3df1c9/forward-vs-call.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"metadata": {
"id": "P8bU6AqJ9Z5U",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"import torch\n",
"import torch.nn as nn\n",
"import torch.nn.functional as F"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "m3HJTzwy9hj-",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"class Net(nn.Module):\n",
"\n",
" def __init__(self):\n",
" super(Net, self).__init__()\n",
" self.fc1 = nn.Linear(1, 10)\n",
"\n",
" def forward(self, x):\n",
" x = self.fc1(x)\n",
" return x"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "Zk_jlJGJ-OuE",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"net = Net()"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "Tk2L0q2u9ztm",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 357
},
"outputId": "ac095735-01db-4dfa-d773-f30c7d77e653"
},
"cell_type": "code",
"source": [
"input = torch.randn(10, 1)\n",
"out = net(input); out"
],
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"tensor([[-0.3731, 1.3823, 0.0515, -0.5198, 0.5836, -1.0378, 0.5496, 0.4065,\n",
" 1.0506, 0.3349],\n",
" [ 0.5514, 0.1857, -0.4676, -0.2912, 0.3555, 0.0757, 0.8639, -0.6369,\n",
" 0.0750, 0.7080],\n",
" [ 0.1788, 0.6679, -0.2584, -0.3833, 0.4474, -0.3730, 0.7373, -0.2165,\n",
" 0.4681, 0.5576],\n",
" [ 0.8737, -0.2315, -0.6486, -0.2115, 0.2759, 0.4639, 0.9735, -1.0007,\n",
" -0.2652, 0.8381],\n",
" [ 1.0532, -0.4638, -0.7494, -0.1671, 0.2317, 0.6801, 1.0345, -1.2033,\n",
" -0.4546, 0.9106],\n",
" [ 0.7490, -0.0701, -0.5785, -0.2424, 0.3067, 0.3137, 0.9311, -0.8600,\n",
" -0.1336, 0.7878],\n",
" [ 1.2528, -0.7222, -0.8615, -0.1178, 0.1824, 0.9205, 1.1024, -1.4286,\n",
" -0.6653, 0.9912],\n",
" [ 0.9843, -0.3747, -0.7107, -0.1842, 0.2486, 0.5972, 1.0111, -1.1256,\n",
" -0.3819, 0.8828],\n",
" [ 0.8114, -0.1510, -0.6136, -0.2269, 0.2913, 0.3889, 0.9524, -0.9305,\n",
" -0.1995, 0.8130],\n",
" [ 0.9214, -0.2933, -0.6754, -0.1997, 0.2642, 0.5214, 0.9897, -1.0546,\n",
" -0.3155, 0.8574]], grad_fn=<AddmmBackward>)"
]
},
"metadata": {
"tags": []
},
"execution_count": 6
}
]
},
{
"metadata": {
"id": "yosLq6xW9xKQ",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"class Net(nn.Module):\n",
"\n",
" def __init__(self):\n",
" super(Net, self).__init__()\n",
" self.fc1 = nn.Linear(1, 10)\n",
"\n",
" def __call__(self, x):\n",
" x = self.fc1(x)\n",
" return x"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "XMNi5RdH-Wta",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"net = Net()"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "qaxe6pTA-EEO",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 357
},
"outputId": "37cd68ed-83ef-4e61-dcad-8f4951183b43"
},
"cell_type": "code",
"source": [
"out = net(input); out"
],
"execution_count": 11,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"tensor([[-2.0674, -0.6294, 1.9423, 0.0444, -0.4427, -0.5925, -1.7611, 0.2161,\n",
" -1.3422, 0.4794],\n",
" [-0.7436, -0.1677, 0.3849, -0.5462, -0.7569, 0.1370, -0.3460, -0.1350,\n",
" -0.3681, 0.1131],\n",
" [-1.2770, -0.3538, 1.0125, -0.3082, -0.6303, -0.1570, -0.9162, 0.0065,\n",
" -0.7606, 0.2607],\n",
" [-0.2820, -0.0067, -0.1581, -0.7521, -0.8665, 0.3913, 0.1473, -0.2574,\n",
" -0.0284, -0.0147],\n",
" [-0.0250, 0.0829, -0.4604, -0.8667, -0.9275, 0.5329, 0.4220, -0.3255,\n",
" 0.1607, -0.0858],\n",
" [-0.4606, -0.0690, 0.0520, -0.6724, -0.8241, 0.2929, -0.0436, -0.2100,\n",
" -0.1598, 0.0348],\n",
" [ 0.2609, 0.1826, -0.7968, -0.9942, -0.9953, 0.6905, 0.7276, -0.4014,\n",
" 0.3710, -0.1649],\n",
" [-0.1235, 0.0485, -0.3445, -0.8227, -0.9041, 0.4786, 0.3167, -0.2994,\n",
" 0.0881, -0.0585],\n",
" [-0.3711, -0.0378, -0.0532, -0.7123, -0.8453, 0.3422, 0.0521, -0.2338,\n",
" -0.0940, 0.0100],\n",
" [-0.2136, 0.0171, -0.2385, -0.7825, -0.8827, 0.4290, 0.2204, -0.2755,\n",
" 0.0219, -0.0336]], grad_fn=<AddmmBackward>)"
]
},
"metadata": {
"tags": []
},
"execution_count": 11
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment