Skip to content

Instantly share code, notes, and snippets.

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 mcne65/5f989b180ce80311d5b46455fe8b80a1 to your computer and use it in GitHub Desktop.
Save mcne65/5f989b180ce80311d5b46455fe8b80a1 to your computer and use it in GitHub Desktop.
Pytorch Numpy Array Observations
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Pytorch Numpy Array Observations.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyOn+Xm2YMMuyKnkTydwQFpr"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/kalmufti/b5609b6930eb99befeba663b06a9aa89/pytorch-numpy-array-observations.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Q6mbBhmUxzBY",
"colab_type": "text"
},
"source": [
"Observation/differences between Numpy and Pytorch arrays and data types."
]
},
{
"cell_type": "code",
"metadata": {
"id": "5EvwykMEq0g5",
"colab_type": "code",
"colab": {}
},
"source": [
"import torch\n",
"import numpy as np"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Vb1IS_OdrJy3",
"colab_type": "code",
"colab": {}
},
"source": [
"n = np.zeros(3)\n",
"t = torch.zeros(3)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "NHQBBouZslEI",
"colab_type": "text"
},
"source": [
"Numpy array is a Python like list of type numpy.ndarrays (number of dimensions arrays). While Pytorch array is of type tensor that can be used on a GPU. Both are multidimensional homogeneous data type arrays."
]
},
{
"cell_type": "code",
"metadata": {
"id": "SWs1XLbnr1YP",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 85
},
"outputId": "9d4db3ea-a90d-4d33-d891-bbf1a60f1c71",
"executionInfo": {
"status": "ok",
"timestamp": 1580849138140,
"user_tz": 360,
"elapsed": 624,
"user": {
"displayName": "Khalid Almufti",
"photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mDKIcxSgguIJDPHq2fm_5baTlkB8BZ3_JqIjgAi=s64",
"userId": "04536491061392437351"
}
}
},
"source": [
"print(n)\n",
"print(t)\n",
"print(type(n))\n",
"print(type(t))"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"[0. 0. 0.]\n",
"tensor([0., 0., 0.])\n",
"<class 'numpy.ndarray'>\n",
"<class 'torch.Tensor'>\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "z3QIh2nxuGfd",
"colab_type": "text"
},
"source": [
"Numpy shape attribute return a tuple, while Pytorch return the same, shape in Pytorch is an alias for .size() see next."
]
},
{
"cell_type": "code",
"metadata": {
"id": "j1gHf8vVrOTL",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 85
},
"outputId": "859c979b-75b4-4823-b2a3-e2e06f2320f4",
"executionInfo": {
"status": "ok",
"timestamp": 1580849138141,
"user_tz": 360,
"elapsed": 617,
"user": {
"displayName": "Khalid Almufti",
"photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mDKIcxSgguIJDPHq2fm_5baTlkB8BZ3_JqIjgAi=s64",
"userId": "04536491061392437351"
}
}
},
"source": [
"print(n.shape)\n",
"print(t.shape)\n",
"print(type(n.shape))\n",
"print(type(t.shape))"
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": [
"(3,)\n",
"torch.Size([3])\n",
"<class 'tuple'>\n",
"<class 'torch.Size'>\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "kG1syYKr0MU4",
"colab_type": "text"
},
"source": [
"Numpy size is an attributes which return an integer. While Pytorch is a method that returns a tuple."
]
},
{
"cell_type": "code",
"metadata": {
"id": "iTbDXonmzR5U",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
},
"outputId": "a0a3796b-ce2b-4616-d9d3-4b1955d68a65",
"executionInfo": {
"status": "ok",
"timestamp": 1580849138142,
"user_tz": 360,
"elapsed": 611,
"user": {
"displayName": "Khalid Almufti",
"photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mDKIcxSgguIJDPHq2fm_5baTlkB8BZ3_JqIjgAi=s64",
"userId": "04536491061392437351"
}
}
},
"source": [
"print(n.size)\n",
"print(t.size)\n",
"print(t.size())"
],
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": [
"3\n",
"<built-in method size of Tensor object at 0x7f22ff86daf8>\n",
"torch.Size([3])\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "1DYDa-oj0JVA",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 255
},
"outputId": "411c8086-823e-4483-a6cb-127d1bc1011c",
"executionInfo": {
"status": "ok",
"timestamp": 1580849138142,
"user_tz": 360,
"elapsed": 603,
"user": {
"displayName": "Khalid Almufti",
"photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mDKIcxSgguIJDPHq2fm_5baTlkB8BZ3_JqIjgAi=s64",
"userId": "04536491061392437351"
}
}
},
"source": [
"print(help(t.size))"
],
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": [
"Help on built-in function size:\n",
"\n",
"size(...) method of torch.Tensor instance\n",
" size() -> torch.Size\n",
" \n",
" Returns the size of the :attr:`self` tensor. The returned value is a subclass of\n",
" :class:`tuple`.\n",
" \n",
" Example::\n",
" \n",
" >>> torch.empty(3, 4, 5).size()\n",
" torch.Size([3, 4, 5])\n",
"\n",
"None\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wWWUwiMqwkxG",
"colab_type": "text"
},
"source": [
" Numpy data type defaults to float64. While torch tensor is float32 data type."
]
},
{
"cell_type": "code",
"metadata": {
"id": "opzq2tziwHiL",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 85
},
"outputId": "6504efc2-0095-4042-9c16-1dc340ded79b",
"executionInfo": {
"status": "ok",
"timestamp": 1580849138143,
"user_tz": 360,
"elapsed": 594,
"user": {
"displayName": "Khalid Almufti",
"photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mDKIcxSgguIJDPHq2fm_5baTlkB8BZ3_JqIjgAi=s64",
"userId": "04536491061392437351"
}
}
},
"source": [
"print(type(n[0]))\n",
"print(type(t[0]))\n",
"\n",
"print(n.dtype)\n",
"print(t.dtype)"
],
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": [
"<class 'numpy.float64'>\n",
"<class 'torch.Tensor'>\n",
"float64\n",
"torch.float32\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment