pytorch-stuff/Pytorch_1.ipynb

357 lines
7.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import torch"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import torchvision"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"torch.cuda.is_available()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Torch tensors"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([2, 2, 1])\n"
]
}
],
"source": [
"# This is a 1D tensor\n",
"a = torch.tensor([2,2,1])\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[ 2, 3, 4],\n",
" [ 1, 2, 3],\n",
" [ 0, 8, 7],\n",
" [10, 11, 12]])\n"
]
}
],
"source": [
"# This is a 2D tensor\n",
"b = torch.tensor([[2,3,4],[1,2,3],[0,8,7],[10,11,12]])\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([3])\n",
"torch.Size([3])\n",
"torch.Size([4, 3])\n",
"torch.Size([4, 3])\n"
]
}
],
"source": [
"# the size of a tensor\n",
"print(a.shape)\n",
"print(a.size())\n",
"print(b.shape)\n",
"print(b.size())"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n"
]
}
],
"source": [
"# get the number of rows\n",
"print(b.shape[0])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"c = torch.FloatTensor([[2,3,4],[1,2,3],[0,8,7],[10,11,12]])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"d = torch.DoubleTensor([[2,3,4],[1,2,3],[0,8,7],[10,11,12]])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[ 2., 3., 4.],\n",
" [ 1., 2., 3.],\n",
" [ 0., 8., 7.],\n",
" [10., 11., 12.]])\n",
"torch.float32\n"
]
}
],
"source": [
"print(c)\n",
"print(c.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[ 2., 3., 4.],\n",
" [ 1., 2., 3.],\n",
" [ 0., 8., 7.],\n",
" [10., 11., 12.]], dtype=torch.float64)\n",
"torch.float64\n"
]
}
],
"source": [
"print(d)\n",
"print(d.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor(5.2500)\n"
]
}
],
"source": [
"print(c.mean())"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor(5.2500, dtype=torch.float64)\n"
]
}
],
"source": [
"print(d.mean())"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor(4.1588)\n"
]
}
],
"source": [
"print(c.std())"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor(4.1588, dtype=torch.float64)\n"
]
}
],
"source": [
"print(d.std())"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[ 2],\n",
" [ 3],\n",
" [ 4],\n",
" [ 1],\n",
" [ 2],\n",
" [ 3],\n",
" [ 0],\n",
" [ 8],\n",
" [ 7],\n",
" [10],\n",
" [11],\n",
" [12]])\n",
"tensor([ 2, 3, 4, 1, 2, 3, 0, 8, 7, 10, 11, 12])\n",
"tensor([[ 2, 3, 4, 1],\n",
" [ 2, 3, 0, 8],\n",
" [ 7, 10, 11, 12]])\n",
"tensor([[ 2, 3, 4, 1, 2, 3, 0, 8, 7, 10, 11, 12]])\n",
"torch.Size([1, 12])\n",
"\n",
"\n",
"\n",
"\n",
"tensor([[[ 2.3594, 0.3172, -2.0944, 0.5019],\n",
" [ 0.4300, 0.4082, -0.1532, 0.6677],\n",
" [-1.0891, 1.3434, -0.2586, -1.2845]],\n",
"\n",
" [[-2.2463, -0.6665, 0.5781, 1.1152],\n",
" [-1.9292, -0.5129, -0.2301, -0.1059],\n",
" [-0.5839, 2.9470, -0.6610, 1.1021]]])\n",
"tensor([[ 2.3594, 0.3172, -2.0944, 0.5019, 0.4300, 0.4082, -0.1532, 0.6677,\n",
" -1.0891, 1.3434, -0.2586, -1.2845],\n",
" [-2.2463, -0.6665, 0.5781, 1.1152, -1.9292, -0.5129, -0.2301, -0.1059,\n",
" -0.5839, 2.9470, -0.6610, 1.1021]])\n",
"tensor([[ 2.3594, 0.3172, -2.0944, 0.5019, 0.4300, 0.4082, -0.1532, 0.6677,\n",
" -1.0891, 1.3434, -0.2586, -1.2845],\n",
" [-2.2463, -0.6665, 0.5781, 1.1152, -1.9292, -0.5129, -0.2301, -0.1059,\n",
" -0.5839, 2.9470, -0.6610, 1.1021]])\n"
]
}
],
"source": [
"print(b.view(-1,1))\n",
"print(b.view(12))\n",
"print(b.view(-1,4))\n",
"\n",
"b = b.view(1,-1)\n",
"print(b)\n",
"print(b.shape)\n",
"print(\"\\n\")\n",
"#this is the format of a Tensor (channels,rows,columns)\n",
"three_dim = torch.randn(2,3,4)\n",
"print(\"\\n\")\n",
"print(three_dim)\n",
"print(three_dim.view(2,12))\n",
"print(three_dim.view(2,-1))"
]
},
{
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}