pytorch-stuff/Pytorch_2.ipynb

320 lines
6.4 KiB
Plaintext
Raw Normal View History

2020-04-16 19:29:41 +00:00
{
"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": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"torch.cuda.is_available()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[0.2504, 0.6128, 0.9066, 0.9701],\n",
" [0.9145, 0.5638, 0.2492, 0.8657],\n",
" [0.9521, 0.5454, 0.6647, 0.9666],\n",
" [0.9705, 0.8375, 0.9598, 0.4804]])\n"
]
}
],
"source": [
"r = torch.rand(4,4)\n",
"print(r)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[-0.6323, -0.4558, -0.9853, 1.5795],\n",
" [-1.5415, 0.3864, -0.0094, 0.4048],\n",
" [ 1.2190, 0.7174, 0.0796, 0.0580],\n",
" [-0.8419, 1.5195, 0.9428, 0.5261]])\n",
"torch.float32\n"
]
}
],
"source": [
"r2 = torch.randn(4,4)\n",
"print(r2)\n",
"print(r2.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([7, 9, 9, 8, 8])\n",
"torch.int64\n"
]
}
],
"source": [
"in_array = torch.randint(6,10,(5,))\n",
"print(in_array)\n",
"print(in_array.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[6, 6, 7],\n",
" [9, 7, 6],\n",
" [9, 8, 6]])\n"
]
}
],
"source": [
"in_array_2 = torch.randint(6,10,(3,3))\n",
"print(in_array_2)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n",
"9\n"
]
}
],
"source": [
"print(torch.numel(in_array))\n",
"print(torch.numel(in_array_2))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[0, 0, 0],\n",
" [0, 0, 0],\n",
" [0, 0, 0]])\n",
"torch.int64\n",
"tensor([[1., 1., 1.],\n",
" [1., 1., 1.],\n",
" [1., 1., 1.]])\n",
"torch.float32\n"
]
}
],
"source": [
"z = torch.zeros(3,3, dtype=torch.long)\n",
"print(z)\n",
"print(z.dtype)\n",
"o = torch.ones(3,3)\n",
"print(o)\n",
"print(o.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[ 0.0830, -0.5093, -0.0874, 0.4360],\n",
" [-0.4503, 1.4857, -0.9082, 0.9302],\n",
" [-1.1958, 0.2207, 1.0333, 0.1410],\n",
" [-0.0071, -2.7461, 0.8460, 0.8057]], dtype=torch.float64)\n"
]
}
],
"source": [
"r2_like = torch.randn_like(r2, dtype=torch.double)\n",
"print(r2_like)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[-0.3819, 0.1570, -0.0787, 2.5496],\n",
" [-0.6270, 0.9503, 0.2398, 1.2705],\n",
" [ 2.1711, 1.2629, 0.7444, 1.0246],\n",
" [ 0.1286, 2.3570, 1.9026, 1.0065]])\n"
]
}
],
"source": [
"add_result = torch.add(r,r2)\n",
"print(add_result)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"torch.Size([4, 4])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add_result.size()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[-0.3819, 0.1570, -0.0787, 2.5496],\n",
" [-0.6270, 0.9503, 0.2398, 1.2705],\n",
" [ 2.1711, 1.2629, 0.7444, 1.0246],\n",
" [ 0.1286, 2.3570, 1.9026, 1.0065]])\n"
]
}
],
"source": [
"# this function reassings the value to r2\n",
"r2.add_(r)\n",
"print(r2)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([0.1570, 0.9503, 1.2629, 2.3570])\n",
"tensor([[-0.3819, 0.1570],\n",
" [-0.6270, 0.9503],\n",
" [ 2.1711, 1.2629],\n",
" [ 0.1286, 2.3570]])\n",
"tensor([[-0.3819, 0.1570, -0.0787, 2.5496],\n",
" [-0.6270, 0.9503, 0.2398, 1.2705],\n",
" [ 2.1711, 1.2629, 0.7444, 1.0246]])\n",
"tensor(1.0246)\n",
"1.0245939493179321\n",
"tensor([2.1711, 1.2629, 0.7444, 1.0246])\n"
]
}
],
"source": [
"print(r2[:,1])\n",
"print(r2[:,:2])\n",
"print(r2[:3,:])\n",
"num_ten = r2[2,3]\n",
"print(num_ten)\n",
"print(num_ten.item())\n",
"print(r2[2,:])"
]
},
{
"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
}