340 lines
8.5 KiB
Plaintext
340 lines
8.5 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# 201 Torch and Numpy\n",
|
|
"\n",
|
|
"View more, visit my tutorial page: https://mofanpy.com/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": [
|
|
"\nnumpy array: [[0 1 2]\n [3 4 5]] \ntorch tensor: tensor([[ 0, 1, 2],\n [ 3, 4, 5]], dtype=torch.int32) \ntensor 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": [
|
|
"\nabs \nnumpy: [1 2 1 2] \ntorch: tensor([ 1., 2., 1., 2.])\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": [
|
|
"tensor([ 1., 2., 1., 2.])"
|
|
]
|
|
},
|
|
"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": [
|
|
"\nsin \nnumpy: [-0.84147098 -0.90929743 0.84147098 0.90929743] \ntorch: tensor([-0.8415, -0.9093, 0.8415, 0.9093])\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": [
|
|
"tensor([ 0.2689, 0.1192, 0.7311, 0.8808])"
|
|
]
|
|
},
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"tensor.sigmoid()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"tensor([ 0.3679, 0.1353, 2.7183, 7.3891])"
|
|
]
|
|
},
|
|
"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": [
|
|
"\nmean \nnumpy: 0.0 \ntorch: tensor(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": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\nmatrix multiplication (matmul) \nnumpy: [[ 7 10]\n [15 22]] \ntorch: tensor([[ 7., 10.],\n [15., 22.]])\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": 3,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"ename": "RuntimeError",
|
|
"evalue": "dot: Expected 1-D argument self, but got 2-D",
|
|
"traceback": [
|
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
|
"\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
|
|
"\u001b[0;32m<ipython-input-3-a29f9258176b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m'\\nmatrix multiplication (dot)'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m'\\nnumpy: '\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;31m# [[7, 10], [15, 22]]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0;34m'\\ntorch: '\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtensor\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtensor\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# 30.0. Beware that torch.dot does not broadcast, only works for 1-dimensional tensor\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m )\n",
|
|
"\u001b[0;31mRuntimeError\u001b[0m: dot: Expected 1-D argument self, but got 2-D"
|
|
],
|
|
"output_type": "error"
|
|
}
|
|
],
|
|
"source": [
|
|
"# incorrect method\n",
|
|
"data = np.array(data)\n",
|
|
"tensor = torch.Tensor(data)\n",
|
|
"print(\n",
|
|
" '\\nmatrix multiplication (dot)',\n",
|
|
" '\\nnumpy: ', data.dot(data), # [[7, 10], [15, 22]]\n",
|
|
" '\\ntorch: ', torch.dot(tensor.dot(tensor)) # NOT WORKING! Beware that torch.dot does not broadcast, only works for 1-dimensional tensor\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": 10,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"tensor([[ 7., 10.],\n [ 15., 22.]])"
|
|
]
|
|
},
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"tensor.mm(tensor)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"tensor([[ 1., 4.],\n [ 9., 16.]])"
|
|
]
|
|
},
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"tensor * tensor"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"tensor(7.)"
|
|
]
|
|
},
|
|
"execution_count": 13,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"torch.dot(torch.Tensor([2, 3]), torch.Tensor([2, 1]))"
|
|
]
|
|
},
|
|
{
|
|
"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
|
|
}
|