{
"cells": [
{
"cell_type": "markdown",
"id": "7895f4e2",
"metadata": {},
"source": [
"# More on Tensor \n",
"\n",
"## Prepared by Sanasam Ranbir Singh"
]
},
{
"cell_type": "markdown",
"id": "568b6fc9",
"metadata": {},
"source": [
"## Variable tensor\n",
"\n",
"Unlike constant tensor, you can change the value of a tensor using tf.assign() method. However, variable tensor should be initialized while creating it."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "270541d7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"\n",
"x = tf.Variable([1,2,3,4]) # initialize with [1,2,3,4]\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 109,
"id": "1cf0ce1c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"x = tf.Variable([[1,2,3,4],[5,6,7,8]])\n",
"print(x)\n"
]
},
{
"cell_type": "code",
"execution_count": 111,
"id": "bf5bbdc3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"x = tf.Variable([[1,2,3,4],[5,6,7,8]], dtype=tf.float32)\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "2b027683",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Variable:0\n",
"(4,)\n",
"\n",
"[1 2 3 4]\n"
]
}
],
"source": [
"x = tf.Variable([1,2,3,4])\n",
"print(x.name)\n",
" \n",
"print(x.shape)\n",
" \n",
"print(x.dtype)\n",
" \n",
"print(x.numpy())\n",
" "
]
},
{
"cell_type": "markdown",
"id": "74d37de5",
"metadata": {},
"source": [
"## Show attributes of a tensor"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "46961628",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(4,)\n",
"\n",
"[1 2 3 4]\n"
]
}
],
"source": [
"x = tf.constant([1,2,3,4])\n",
"#print(x.name) #possible, when eager execution is disabled\n",
" \n",
"print(x.shape)\n",
" \n",
"print(x.dtype)\n",
" \n",
"print(x.numpy())"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "46032d04",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Variable:0\n",
"(2, 4)\n",
"\n",
"[[1 2 3 4]\n",
" [5 6 7 8]]\n"
]
}
],
"source": [
"x = tf.Variable([[1,2,3,4],[5,6,7,8]])\n",
"print(x.name) \n",
" \n",
"print(x.shape)\n",
" \n",
"print(x.dtype)\n",
" \n",
"print(x.numpy())"
]
},
{
"cell_type": "markdown",
"id": "78ecc3ec",
"metadata": {},
"source": [
"## Convert a content tensor to a variable tensor and vice versa"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "42e54688",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"x_con = tf.constant([1,2,3,4])\n",
" \n",
"x_var = tf.Variable(t_con)\n",
"print(x_var)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "7866306f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor([1 2 3 4], shape=(4,), dtype=int32)\n"
]
}
],
"source": [
"x_var = tf.Variable([1,2,3,4])\n",
"x_con = tf.constant(x_var)\n",
"print(x_con)"
]
},
{
"cell_type": "markdown",
"id": "9d424cdc",
"metadata": {},
"source": [
"## Reshape a tensor\n",
"\n",
"You can chage the sape of the tensor after creating it. but, the number of element of the source tensor and target sensor should be same."
]
},
{
"cell_type": "code",
"execution_count": 71,
"id": "789b791e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor(\n",
"[[1 2 3 4]\n",
" [5 6 7 8]], shape=(2, 4), dtype=int32)\n"
]
}
],
"source": [
"x = tf.constant([1,2,3,4,5,6,7,8], shape=(2,4))\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 112,
"id": "f57d9c39",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 112,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = tf.constant([1,2,3,4,5,6,7,8], shape=(2,4))\n",
"tf.reshape(x, (4,2))"
]
},
{
"cell_type": "code",
"execution_count": 87,
"id": "14bb8dcd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = tf.constant([[1,2,3,4],[5,6,7,8]])\n",
"tf.reshape(x, (8))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d39d4c35",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = tf.constant([[1,2,3,4],[5,6,7,8]], shape=(2,4))\n",
"tf.reshape(x, (-1)) # flatten the tensor in 1D"
]
},
{
"cell_type": "markdown",
"id": "5e0f42f7",
"metadata": {},
"source": [
"## Use of -1\n",
"The -1 is like a don't care. When you reshape with (x,-1), it generate a new 2D tensor with 4 number of 1D sensors of equal shape. The number of 0D tensor in each 1D tensor depends on the number of elements in the original tensor. Note that the number of elements in the original tensor and new tensor should be same."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "3cfb34b7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tf.reshape(x, (4,-1))"
]
},
{
"cell_type": "code",
"execution_count": 115,
"id": "5970945a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 115,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = tf.constant([1,2,3,4,5,6,7,8])\n",
"tf.reshape(x, (2,-1,2))"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "47b877e8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = tf.constant([[1,2,3,4],[5,6,7,8]])\n",
"tf.reshape(x, (2,2,-1))"
]
},
{
"cell_type": "code",
"execution_count": 76,
"id": "d4d2ee1b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = tf.constant([[1,2,3,4],[5,6,7,8]])\n",
"tf.reshape(x, (-1,2,2))"
]
},
{
"cell_type": "markdown",
"id": "68863e7e",
"metadata": {},
"source": [
"# Access the elements of a tensor"
]
},
{
"cell_type": "markdown",
"id": "a8742c12",
"metadata": {},
"source": [
"## argmax()\n",
"Return the index of the maximum element in kD tendor and return a (k-1)D index tensor"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "09fe1e04",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = tf.constant([[1,2,3,4], [5,6,7,8]])\n",
"tf.argmax(x)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "8c81dbf8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = tf.constant([[1,2,3,4], [5,6,7,8]])\n",
"tf.argmin(x)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "7918bd3e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor([0 1 0 1], shape=(4,), dtype=int64)\n"
]
}
],
"source": [
"x = tf.constant([[9,2,10,4],[5,6,7,8]])\n",
"print(tf.math.argmax(x))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "8a9a6a9e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor(\n",
"[[ 2 20 30 3 6]\n",
" [ 3 11 16 1 8]\n",
" [14 45 23 5 27]], shape=(3, 5), dtype=int32)\n",
"tf.Tensor([2 2 0 2 2], shape=(5,), dtype=int64)\n"
]
}
],
"source": [
"x = tf.constant([[2, 20, 30, 3, 6], [3, 11, 16, 1, 8],\n",
" [14, 45, 23, 5, 27]])\n",
"print(x)\n",
"print(tf.math.argmax(x))"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "e6e920b7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor(\n",
"[[[ 2 20 30 3 6]\n",
" [ 3 11 16 1 8]]\n",
"\n",
" [[ 1 1 1 1 1]\n",
" [14 45 23 5 27]]], shape=(2, 2, 5), dtype=int32)\n",
"tf.Tensor(\n",
"[[0 0 0 0 0]\n",
" [1 1 1 1 1]], shape=(2, 5), dtype=int64)\n"
]
}
],
"source": [
"x = tf.constant([[[2, 20, 30, 3, 6], [3, 11, 16, 1, 8]],[[1,1,1,1,1],\n",
" [14, 45, 23, 5, 27]]])\n",
"print(x)\n",
"print(tf.math.argmax(x))"
]
},
{
"cell_type": "markdown",
"id": "ec293733",
"metadata": {},
"source": [
"## Define the axis of the application."
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "4f5a4f0d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor(\n",
"[[ 2 20 30 3 6]\n",
" [ 3 11 16 1 8]\n",
" [14 45 23 5 27]], shape=(3, 5), dtype=int32)\n",
"tf.Tensor([2 2 0 2 2], shape=(5,), dtype=int64)\n"
]
}
],
"source": [
"x = tf.constant([[2, 20, 30, 3, 6], [3, 11, 16, 1, 8],\n",
" [14, 45, 23, 5, 27]])\n",
"print(x)\n",
"print(tf.math.argmax(x,0)) # fine across the 0-axis which is the default"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "de205cd1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor(\n",
"[[ 2 20 30 3 6]\n",
" [ 3 11 16 1 8]\n",
" [14 45 23 5 27]], shape=(3, 5), dtype=int32)\n",
"tf.Tensor([2 2 1], shape=(3,), dtype=int64)\n"
]
}
],
"source": [
"x = tf.constant([[2, 20, 30, 3, 6], [3, 11, 16, 1, 8],\n",
" [14, 45, 23, 5, 27]])\n",
"print(x)\n",
"print(tf.math.argmax(x,1)) # fine across the 1-axis i.e., across each 1D tensor"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "d352db11",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor(\n",
"[[[ 2 20 30 3 6]\n",
" [ 3 11 16 1 8]]\n",
"\n",
" [[ 1 1 1 1 1]\n",
" [14 45 23 5 27]]], shape=(2, 2, 5), dtype=int32)\n",
"tf.Tensor(\n",
"[[1 0 0 0 1]\n",
" [1 1 1 1 1]], shape=(2, 5), dtype=int64)\n"
]
}
],
"source": [
"x = tf.constant([[[2, 20, 30, 3, 6], [3, 11, 16, 1, 8]],[[1,1,1,1,1],\n",
" [14, 45, 23, 5, 27]]])\n",
"print(x)\n",
"print(tf.math.argmax(x,1)) # across the 2D tensors"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "feb3d8cd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor(\n",
"[[[ 2 20 30 3 6]\n",
" [ 3 11 16 1 8]]\n",
"\n",
" [[ 1 1 1 1 1]\n",
" [14 45 23 5 27]]], shape=(2, 2, 5), dtype=int32)\n",
"tf.Tensor(\n",
"[[2 2]\n",
" [0 1]], shape=(2, 2), dtype=int64)\n"
]
}
],
"source": [
"x = tf.constant([[[2, 20, 30, 3, 6], [3, 11, 16, 1, 8]],[[1,1,1,1,1],\n",
" [14, 45, 23, 5, 27]]])\n",
"print(x)\n",
"print(tf.math.argmax(x,2)) # across the 1D tensors"
]
},
{
"cell_type": "markdown",
"id": "5b365816",
"metadata": {},
"source": [
"## return the maximum element"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "67b4689d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tf.Tensor(50, shape=(), dtype=int32)\n"
]
}
],
"source": [
"x = tf.constant([[9,2,10,4],[5,6,7,50]])\n",
"print(tf.reduce_max(x))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a457a51e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}