398 lines
7.8 KiB
Plaintext
398 lines
7.8 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# 201 Torch and Numpy\n",
|
|
"\n",
|
|
"View more, visit my tutorial page: https://morvanzhou.github.io/tutorials/\n",
|
|
"My Youtube Channel: https://www.youtube.com/user/MorvanZhou\n",
|
|
"\n",
|
|
"Dependencies:\n",
|
|
"* torch: 0.1.11\n",
|
|
"* numpy\n",
|
|
"\n",
|
|
"Details about math operation in torch can be found in: http://pytorch.org/docs/torch.html#math-operations\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {
|
|
"collapsed": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import torch\n",
|
|
"import numpy as np"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"numpy array: [[0 1 2]\n",
|
|
" [3 4 5]] \n",
|
|
"torch tensor: \n",
|
|
" 0 1 2\n",
|
|
" 3 4 5\n",
|
|
"[torch.LongTensor of size 2x3]\n",
|
|
" \n",
|
|
"tensor to array: [[0 1 2]\n",
|
|
" [3 4 5]]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# convert numpy to tensor or vise versa\n",
|
|
"np_data = np.arange(6).reshape((2, 3))\n",
|
|
"torch_data = torch.from_numpy(np_data)\n",
|
|
"tensor2array = torch_data.numpy()\n",
|
|
"print(\n",
|
|
" '\\nnumpy array:', np_data, # [[0 1 2], [3 4 5]]\n",
|
|
" '\\ntorch tensor:', torch_data, # 0 1 2 \\n 3 4 5 [torch.LongTensor of size 2x3]\n",
|
|
" '\\ntensor to array:', tensor2array, # [[0 1 2], [3 4 5]]\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"abs \n",
|
|
"numpy: [1 2 1 2] \n",
|
|
"torch: \n",
|
|
" 1\n",
|
|
" 2\n",
|
|
" 1\n",
|
|
" 2\n",
|
|
"[torch.FloatTensor of size 4]\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# abs\n",
|
|
"data = [-1, -2, 1, 2]\n",
|
|
"tensor = torch.FloatTensor(data) # 32-bit floating point\n",
|
|
"print(\n",
|
|
" '\\nabs',\n",
|
|
" '\\nnumpy: ', np.abs(data), # [1 2 1 2]\n",
|
|
" '\\ntorch: ', torch.abs(tensor) # [1 2 1 2]\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"\n",
|
|
" 1\n",
|
|
" 2\n",
|
|
" 1\n",
|
|
" 2\n",
|
|
"[torch.FloatTensor of size 4]"
|
|
]
|
|
},
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"tensor.abs()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"sin \n",
|
|
"numpy: [-0.84147098 -0.90929743 0.84147098 0.90929743] \n",
|
|
"torch: \n",
|
|
"-0.8415\n",
|
|
"-0.9093\n",
|
|
" 0.8415\n",
|
|
" 0.9093\n",
|
|
"[torch.FloatTensor of size 4]\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# sin\n",
|
|
"print(\n",
|
|
" '\\nsin',\n",
|
|
" '\\nnumpy: ', np.sin(data), # [-0.84147098 -0.90929743 0.84147098 0.90929743]\n",
|
|
" '\\ntorch: ', torch.sin(tensor) # [-0.8415 -0.9093 0.8415 0.9093]\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"\n",
|
|
" 0.2689\n",
|
|
" 0.1192\n",
|
|
" 0.7311\n",
|
|
" 0.8808\n",
|
|
"[torch.FloatTensor of size 4]"
|
|
]
|
|
},
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"tensor.sigmoid()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"\n",
|
|
" 0.3679\n",
|
|
" 0.1353\n",
|
|
" 2.7183\n",
|
|
" 7.3891\n",
|
|
"[torch.FloatTensor of size 4]"
|
|
]
|
|
},
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"tensor.exp()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"mean \n",
|
|
"numpy: 0.0 \n",
|
|
"torch: 0.0\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# mean\n",
|
|
"print(\n",
|
|
" '\\nmean',\n",
|
|
" '\\nnumpy: ', np.mean(data), # 0.0\n",
|
|
" '\\ntorch: ', torch.mean(tensor) # 0.0\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"matrix multiplication (matmul) \n",
|
|
"numpy: [[ 7 10]\n",
|
|
" [15 22]] \n",
|
|
"torch: \n",
|
|
" 7 10\n",
|
|
" 15 22\n",
|
|
"[torch.FloatTensor of size 2x2]\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# matrix multiplication\n",
|
|
"data = [[1,2], [3,4]]\n",
|
|
"tensor = torch.FloatTensor(data) # 32-bit floating point\n",
|
|
"# correct method\n",
|
|
"print(\n",
|
|
" '\\nmatrix multiplication (matmul)',\n",
|
|
" '\\nnumpy: ', np.matmul(data, data), # [[7, 10], [15, 22]]\n",
|
|
" '\\ntorch: ', torch.mm(tensor, tensor) # [[7, 10], [15, 22]]\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 14,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"matrix multiplication (dot) \n",
|
|
"numpy: [[ 7 10]\n",
|
|
" [15 22]] \n",
|
|
"torch: 30.0\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# incorrect method\n",
|
|
"data = np.array(data)\n",
|
|
"print(\n",
|
|
" '\\nmatrix multiplication (dot)',\n",
|
|
" '\\nnumpy: ', data.dot(data), # [[7, 10], [15, 22]]\n",
|
|
" '\\ntorch: ', tensor.dot(tensor) # this will convert tensor to [1,2,3,4], you'll get 30.0\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Note that:\n",
|
|
"\n",
|
|
"torch.dot(tensor1, tensor2) → float\n",
|
|
"\n",
|
|
"Computes the dot product (inner product) of two tensors. Both tensors are treated as 1-D vectors."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"\n",
|
|
" 7 10\n",
|
|
" 15 22\n",
|
|
"[torch.FloatTensor of size 2x2]"
|
|
]
|
|
},
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"tensor.mm(tensor)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"\n",
|
|
" 1 4\n",
|
|
" 9 16\n",
|
|
"[torch.FloatTensor of size 2x2]"
|
|
]
|
|
},
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"tensor * tensor"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"30.0"
|
|
]
|
|
},
|
|
"execution_count": 13,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"tensor.dot(tensor)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true
|
|
},
|
|
"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.5.2"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|