Skip to content

Instantly share code, notes, and snippets.

@arunmallya
Created August 17, 2018 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arunmallya/804d06cb54401d69fb6ecee43cc2bc24 to your computer and use it in GitHub Desktop.
Save arunmallya/804d06cb54401d69fb6ecee43cc2bc24 to your computer and use it in GitHub Desktop.
Normal operation on small tensor
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.4.1\n"
]
}
],
"source": [
"import torch\n",
"print(torch.__version__)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[0.2906, 0.7618, 0.0318, 0.6305, 0.3093],\n",
" [0.3360, 0.3016, 0.7235, 0.1689, 0.7401],\n",
" [0.4158, 0.5294, 0.1691, 0.4574, 0.1047],\n",
" [0.1103, 0.1274, 0.8512, 0.3206, 0.2296],\n",
" [0.4130, 0.5525, 0.9446, 0.6805, 0.6599],\n",
" [0.4207, 0.9097, 0.1605, 0.9655, 0.4070],\n",
" [0.5831, 0.1240, 0.9495, 0.3958, 0.5785],\n",
" [0.9375, 0.8432, 0.1809, 0.7320, 0.1422],\n",
" [0.2422, 0.8776, 0.7639, 0.4937, 0.0692],\n",
" [0.7932, 0.7240, 0.3930, 0.9832, 0.7180]])\n"
]
}
],
"source": [
"a = torch.rand(10, 5)\n",
"b = a.clone()\n",
"c = a.clone()\n",
"d = a.clone()\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[ 0.2906, 0.7618, 0.0318, 0.6305, 0.3093],\n",
" [ 0.3360, 0.3016, 0.7235, 0.1689, 0.7401],\n",
" [ 0.4158, 0.5294, 0.1691, 0.4574, 0.1047],\n",
" [ 0.1103, 0.1274, 0.8512, 0.3206, 0.2296],\n",
" [ 0.4130, 0.5525, 0.9446, 0.6805, 0.6599],\n",
" [ 0.4207, 0.9097, 0.1605, 0.9655, 0.4070],\n",
" [ 0.5831, 0.1240, 0.9495, 0.3958, 0.5785],\n",
" [ 0.9375, 0.8432, 0.1809, 0.7320, 0.1422],\n",
" [ 0.2422, 0.8776, 0.7639, 0.4937, 0.0692],\n",
" [-0.4388, -0.2975, 0.6762, 0.7267, -0.8753]])\n"
]
}
],
"source": [
"a[9:10].normal_(0, 1)\n",
"print(a)\n",
"# Note that last row has changed."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[ 0.2906, 0.7618, 0.0318, 0.6305, 0.3093],\n",
" [ 0.3360, 0.3016, 0.7235, 0.1689, 0.7401],\n",
" [ 0.4158, 0.5294, 0.1691, 0.4574, 0.1047],\n",
" [ 0.1103, 0.1274, 0.8512, 0.3206, 0.2296],\n",
" [ 0.4130, 0.5525, 0.9446, 0.6805, 0.6599],\n",
" [ 0.4207, 0.9097, 0.1605, 0.9655, 0.4070],\n",
" [ 0.5831, 0.1240, 0.9495, 0.3958, 0.5785],\n",
" [ 0.9375, 0.8432, 0.1809, 0.7320, 0.1422],\n",
" [ 0.2422, 0.8776, 0.7639, 0.4937, 0.0692],\n",
" [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000]])\n"
]
}
],
"source": [
"b[9:10] = -1\n",
"print(b)\n",
"# Note that last row has changed."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[ 0.2906, 0.7618, 0.0318, 0.6305, 0.3093],\n",
" [ 0.3360, 0.3016, 0.7235, 0.1689, 0.7401],\n",
" [ 0.4158, 0.5294, 0.1691, 0.4574, 0.1047],\n",
" [ 0.1103, 0.1274, 0.8512, 0.3206, 0.2296],\n",
" [ 0.4130, 0.5525, 0.9446, 0.6805, 0.6599],\n",
" [ 0.4207, 0.9097, 0.1605, 0.9655, 0.4070],\n",
" [ 0.5831, 0.1240, 0.9495, 0.3958, 0.5785],\n",
" [ 0.9375, 0.8432, 0.1809, 0.7320, 0.1422],\n",
" [ 0.2422, 0.8776, 0.7639, 0.4937, 0.0692],\n",
" [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000]])\n"
]
}
],
"source": [
"c[9:10].copy_(torch.rand(1, 5).fill_(-1))\n",
"print(c)\n",
"# Note that last row has changed."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[ 0.2906, 0.7618, 0.0318, 0.6305, 0.3093],\n",
" [ 0.3360, 0.3016, 0.7235, 0.1689, 0.7401],\n",
" [ 0.4158, 0.5294, 0.1691, 0.4574, 0.1047],\n",
" [ 0.1103, 0.1274, 0.8512, 0.3206, 0.2296],\n",
" [ 0.4130, 0.5525, 0.9446, 0.6805, 0.6599],\n",
" [ 0.4207, 0.9097, 0.1605, 0.9655, 0.4070],\n",
" [ 0.5831, 0.1240, 0.9495, 0.3958, 0.5785],\n",
" [ 0.9375, 0.8432, 0.1809, 0.7320, 0.1422],\n",
" [ 0.2422, 0.8776, 0.7639, 0.4937, 0.0692],\n",
" [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000]])\n"
]
}
],
"source": [
"d[9:10].fill_(-1)\n",
"print(d)\n",
"# Note that last row has changed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment