pytorch-stuff/Numpy_Bridge.ipynb

247 lines
4.7 KiB
Plaintext
Raw Normal View History

2020-04-16 19:29:41 +00:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import torch\n",
"import torchvision\n",
"torch.cuda.is_available()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([1., 1., 1., 1., 1.])\n",
"[1. 1. 1. 1. 1.]\n",
"tensor([2., 2., 2., 2., 2.])\n",
"[2. 2. 2. 2. 2.]\n"
]
}
],
"source": [
"# the numpy array is just a pointer to the torch tensor\n",
"a = torch.ones(5)\n",
"print(a)\n",
"b = a.numpy()\n",
"print(b)\n",
"a.add_(1)\n",
"print(a)\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2. 2. 2. 2. 2.]\n",
"tensor([2., 2., 2., 2., 2.], dtype=torch.float64)\n"
]
}
],
"source": [
"a = np.ones(5)\n",
"b = torch.from_numpy(a)\n",
"np.add(a,1,out=a)\n",
"print(a)\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"r2 = torch.randn(4,4)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"r = torch.rand(4,4)\n",
"\n",
"add_result = torch.add(r,r2)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[-0.5124, -0.0635, -1.0141, 0.2371],\n",
" [ 0.7809, -1.0184, 1.1734, -0.4778],\n",
" [ 1.6340, 0.0201, 0.4821, -0.1986],\n",
" [ 1.7512, -0.1389, 1.4546, -0.2230]], device='cuda:0')\n"
]
}
],
"source": [
"# Move tensor to GPU\n",
"r2 = r2.cuda()\n",
"print(r2)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"tensor([[ 0.0573, 0.7549, -0.1884, 0.6424],\n",
" [ 0.9001, -0.9460, 2.1332, 0.0502],\n",
" [ 1.7473, 0.6463, 0.5804, 0.2859],\n",
" [ 2.0273, 0.6695, 2.4453, 0.4559]], device='cuda:0')\n"
]
}
],
"source": [
"CUDA = torch.cuda.is_available()\n",
"print(CUDA)\n",
"\n",
"if CUDA:\n",
" add_result = add_result.cuda()\n",
" print(add_result)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[-0.4551, 0.6915, -1.2024, 0.8795],\n",
" [ 1.6810, -1.9643, 3.3066, -0.4276],\n",
" [ 3.3813, 0.6664, 1.0625, 0.0872],\n",
" [ 3.7784, 0.5306, 3.8999, 0.2329]], device='cuda:0')"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add_result.add(r2)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 3, 4, 1]\n",
"tensor([2, 3, 4, 1]) torch.int64\n"
]
}
],
"source": [
"a = [2,3,4,1]\n",
"print(a)\n",
"to_list = torch.tensor(a)\n",
"print(to_list, to_list.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[1, 2],\n",
" [3, 4],\n",
" [5, 6],\n",
" [7, 8]]) torch.int64\n"
]
}
],
"source": [
"data = [[1,2],[3,4],[5,6],[7,8]]\n",
"T = torch.tensor(data)\n",
"print(T,T.dtype)"
]
},
{
"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
}