{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "a = [1.0, 2.0, 1.0]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1.0, 2.0, 3.0]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[2] = 3.0\n", "a" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([ 1., 1., 1.])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import torch\n", "a = torch.ones(3)\n", "a" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(1.)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(a[1])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([ 1., 1., 2.])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[2] = 2.0\n", "a" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "points = torch.zeros(6) # <1>\n", "points[0] = 1.0 # <2>\n", "points[1] = 4.0\n", "points[2] = 2.0\n", "points[3] = 1.0\n", "points[4] = 3.0\n", "points[5] = 5.0" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([ 1., 4., 2., 1., 3., 5.])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points = torch.tensor([1.0, 4.0, 2.0, 1.0, 3.0, 5.0])\n", "points" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1.0, 4.0)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(points[0]), float(points[1])" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 1., 4.],\n", " [ 2., 1.],\n", " [ 3., 5.]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points = torch.tensor([[1.0, 4.0], [2.0, 1.0], [3.0, 5.0]])\n", "points" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "torch.Size([3, 2])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points.shape" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 0., 0.],\n", " [ 0., 0.],\n", " [ 0., 0.]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points = torch.zeros(3, 2)\n", "points" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 1., 4.],\n", " [ 2., 1.],\n", " [ 3., 5.]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points = torch.FloatTensor([[1.0, 4.0], [2.0, 1.0], [3.0, 5.0]])\n", "points" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(4.)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points[0, 1]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([ 1., 4.])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points[0]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " 1.0\n", " 4.0\n", " 2.0\n", " 1.0\n", " 3.0\n", " 5.0\n", "[torch.FloatStorage of size 6]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points = torch.tensor([[1.0, 4.0], [2.0, 1.0], [3.0, 5.0]])\n", "points.storage()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points_storage = points.storage()\n", "points_storage[0]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points.storage()[1]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 2., 4.],\n", " [ 2., 1.],\n", " [ 3., 5.]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points = torch.tensor([[1.0, 4.0], [2.0, 1.0], [3.0, 5.0]])\n", "points_storage = points.storage()\n", "points_storage[0] = 2.0\n", "points" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points = torch.tensor([[1.0, 4.0], [2.0, 1.0], [3.0, 5.0]])\n", "second_point = points[1]\n", "second_point.storage_offset()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "torch.Size([2])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "second_point.size()" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "torch.Size([2])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "second_point.shape" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 1)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points.stride()" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "torch.Size([2])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points = torch.tensor([[1.0, 4.0], [2.0, 1.0], [3.0, 5.0]])\n", "second_point = points[1]\n", "second_point.size()" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "second_point.storage_offset()" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1,)" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "second_point.stride()" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 1., 4.],\n", " [ 10., 1.],\n", " [ 3., 5.]])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points = torch.tensor([[1.0, 4.0], [2.0, 1.0], [3.0, 5.0]])\n", "second_point = points[1]\n", "second_point[0] = 10.0\n", "points" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 1., 4.],\n", " [ 2., 1.],\n", " [ 3., 5.]])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points = torch.tensor([[1.0, 4.0], [2.0, 1.0], [3.0, 5.0]])\n", "second_point = points[1].clone()\n", "second_point[0] = 10.0\n", "points" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 1., 4.],\n", " [ 2., 1.],\n", " [ 3., 5.]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points = torch.tensor([[1.0, 4.0], [2.0, 1.0], [3.0, 5.0]])\n", "points" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 1., 2., 3.],\n", " [ 4., 1., 5.]])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points_t = points.t()\n", "points_t" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(points.storage()) == id(points_t.storage())" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 1)" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points.stride()" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points_t.stride()" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "torch.Size([3, 4, 5])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_tensor = torch.ones(3, 4, 5)\n", "some_tensor_t = some_tensor.transpose(0, 2)\n", "some_tensor.shape" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "torch.Size([5, 4, 3])" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_tensor_t.shape" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(20, 5, 1)" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_tensor.stride()" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 5, 20)" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_tensor_t.stride()" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points.is_contiguous()" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points_t.is_contiguous()" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 1., 2., 3.],\n", " [ 4., 1., 5.]])" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points = torch.tensor([[1.0, 4.0], [2.0, 1.0], [3.0, 5.0]])\n", "points_t = points.t()\n", "points_t" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " 1.0\n", " 4.0\n", " 2.0\n", " 1.0\n", " 3.0\n", " 5.0\n", "[torch.FloatStorage of size 6]" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points_t.storage()" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points_t.stride()" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 1., 2., 3.],\n", " [ 4., 1., 5.]])" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points_t_cont = points_t.contiguous()\n", "points_t_cont" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 1)" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points_t_cont.stride()" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " 1.0\n", " 2.0\n", " 3.0\n", " 4.0\n", " 1.0\n", " 5.0\n", "[torch.FloatStorage of size 6]" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points_t_cont.storage()" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "double_points = torch.ones(10, 2, dtype=torch.double)\n", "short_points = torch.tensor([[1, 2], [3, 4]], dtype=torch.short)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "torch.int16" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "short_points.dtype" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "double_points = torch.zeros(10, 2).double()\n", "short_points = torch.ones(10, 2).short()" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "double_points = torch.zeros(10, 2).to(torch.double)\n", "short_points = torch.ones(10, 2).to(dtype=torch.short)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "points = torch.randn(10, 2)\n", "short_points = points.type(torch.short)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "points = torch.tensor([[1.0, 4.0], [2.0, 1.0], [3.0, 4.0]])" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 3]" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "some_list = list(range(6))\n", "some_list[:] # all elements in the list\n", "some_list[1:4] # from element 1 inclusive to element 4 exclusive\n", "some_list[1:] # from element 1 inclusive to the end of the list\n", "some_list[:4] # from the start of the list to element 4 exclusive\n", "some_list[:-1] # from the start of the list to one before the last element\n", "some_list[1:4:2] # from element 1 inclusive to element 4 exclusive in steps of 2" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([ 2., 3.])" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points[1:] # all rows but first, implicitly all columns\n", "points[1:, :] # all rows but first, all columns\n", "points[1:, 0] # all rows but first, first column" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.]], dtype=float32)" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "points = torch.ones(3, 4)\n", "points_np = points.numpy()\n", "points_np" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "points = torch.from_numpy(points_np)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "torch.save(points, 'ourpoints.t')" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "with open('ourpoints.t','wb') as f:\n", " torch.save(points, f)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [], "source": [ "points = torch.load('ourpoints.t')" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [], "source": [ "with open('ourpoints.t','rb') as f:\n", " points = torch.load(f)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [], "source": [ "import h5py\n", "\n", "f = h5py.File('ourpoints.hdf5', 'w')\n", "dset = f.create_dataset('coords', data=points.numpy())\n", "f.close()" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "f = h5py.File('ourpoints.hdf5', 'r')\n", "dset = f['coords']\n", "last_points = dset[1:]" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [], "source": [ "last_points = torch.from_numpy(dset[1:])\n", "f.close()" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [], "source": [ "points_gpu = torch.tensor([[1.0, 4.0], [2.0, 1.0], [3.0, 4.0]], device='cuda')" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [], "source": [ "points_gpu = points.to(device='cuda')" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [], "source": [ "points_gpu = points.to(device='cuda:0')" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [], "source": [ "points = 2 * points # <1>\n", "points_gpu = 2 * points.to(device='cuda') # <2>" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [], "source": [ "points_gpu = points_gpu + 4" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [], "source": [ "points_cpu = points_gpu.to(device='cpu')" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [], "source": [ "points_gpu = points.cuda() # <1>\n", "points_gpu = points.cuda(0)\n", "points_cpu = points_gpu.cpu()" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [], "source": [ "a = torch.ones(3, 2)\n", "a_t = torch.transpose(a, 0, 1)" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [], "source": [ "a = torch.ones(3, 2)\n", "a_t = a.transpose(0, 1)" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [], "source": [ "a = torch.ones(3, 2)" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 0., 0.],\n", " [ 0., 0.],\n", " [ 0., 0.]])" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.zero_()\n", "a" ] } ], "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }