294 lines
5.9 KiB
Plaintext
294 lines
5.9 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# 202 Variable\n",
|
|
"\n",
|
|
"View more, visit my tutorial page: https://mofanpy.com/tutorials/\n",
|
|
"My Youtube Channel: https://www.youtube.com/user/MorvanZhou\n",
|
|
"\n",
|
|
"Dependencies:\n",
|
|
"* torch: 0.1.11\n",
|
|
"\n",
|
|
"Variable in torch is to build a computational graph,\n",
|
|
"but this graph is dynamic compared with a static graph in Tensorflow or Theano.\n",
|
|
"So torch does not have placeholder, torch can just pass variable to the computational graph.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {
|
|
"collapsed": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import torch\n",
|
|
"from torch.autograd import Variable"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
" 1 2\n",
|
|
" 3 4\n",
|
|
"[torch.FloatTensor of size 2x2]\n",
|
|
"\n",
|
|
"Variable containing:\n",
|
|
" 1 2\n",
|
|
" 3 4\n",
|
|
"[torch.FloatTensor of size 2x2]\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"tensor = torch.FloatTensor([[1,2],[3,4]]) # build a tensor\n",
|
|
"variable = Variable(tensor, requires_grad=True) # build a variable, usually for compute gradients\n",
|
|
"\n",
|
|
"print(tensor) # [torch.FloatTensor of size 2x2]\n",
|
|
"print(variable) # [torch.FloatTensor of size 2x2]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Till now the tensor and variable seem the same.\n",
|
|
"\n",
|
|
"However, the variable is a part of the graph, it's a part of the auto-gradient.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"7.5\n",
|
|
"Variable containing:\n",
|
|
" 7.5000\n",
|
|
"[torch.FloatTensor of size 1]\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"t_out = torch.mean(tensor*tensor) # x^2\n",
|
|
"v_out = torch.mean(variable*variable) # x^2\n",
|
|
"print(t_out)\n",
|
|
"print(v_out)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {
|
|
"collapsed": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"v_out.backward() # backpropagation from v_out"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$ v_{out} = {{1} \\over {4}} sum(variable^2) $$\n",
|
|
"\n",
|
|
"the gradients w.r.t the variable, \n",
|
|
"\n",
|
|
"$$ {d(v_{out}) \\over d(variable)} = {{1} \\over {4}} 2 variable = {variable \\over 2}$$\n",
|
|
"\n",
|
|
"let's check the result pytorch calculated for us below:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"Variable containing:\n",
|
|
" 0.5000 1.0000\n",
|
|
" 1.5000 2.0000\n",
|
|
"[torch.FloatTensor of size 2x2]"
|
|
]
|
|
},
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"variable.grad"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"Variable containing:\n",
|
|
" 1 2\n",
|
|
" 3 4\n",
|
|
"[torch.FloatTensor of size 2x2]"
|
|
]
|
|
},
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"variable # this is data in variable format"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"\n",
|
|
" 1 2\n",
|
|
" 3 4\n",
|
|
"[torch.FloatTensor of size 2x2]"
|
|
]
|
|
},
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"variable.data # this is data in tensor format"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"array([[ 1., 2.],\n",
|
|
" [ 3., 4.]], dtype=float32)"
|
|
]
|
|
},
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"variable.data.numpy() # numpy format"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Note that we did `.backward()` on `v_out` but `variable` has been assigned new values on it's `grad`.\n",
|
|
"\n",
|
|
"As this line \n",
|
|
"```\n",
|
|
"v_out = torch.mean(variable*variable)\n",
|
|
"``` \n",
|
|
"will make a new variable `v_out` and connect it with `variable` in computation graph."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"torch.autograd.variable.Variable"
|
|
]
|
|
},
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"type(v_out)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"torch.FloatTensor"
|
|
]
|
|
},
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"type(v_out.data)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true
|
|
},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"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.5.2"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|