1"use strict";(globalThis.webpackChunkmikes_dev_notebook=globalThis.webpackChunkmikes_dev_notebook||[]).push([[10568],{433170(n,e,s){s.r(e),s.d(e,{assets:()=>l,contentTitle:()=>o,default:()=>h,frontMatter:()=>a,metadata:()=>t,toc:()=>d});const t=JSON.parse('{"id":"IoT-and-Machine-Learning/ML/2023-02-21-tensorflow-tensors-2/index","title":"Tensorflow 2 - An (Re)Introduction 2023 (2)","description":"Tensor Indexing, Expanding and Manipulations","source":"@site/docs/IoT-and-Machine-Learning/ML/2023-02-21-tensorflow-tensors-2/index.md","sourceDirName":"IoT-and-Machine-Learning/ML/2023-02-21-tensorflow-tensors-2","slug":"/IoT-and-Machine-Learning/ML/2023-02-21-tensorflow-tensors-2/2023-02-21","permalink":"/docs/IoT-and-Machine-Learning/ML/2023-02-21-tensorflow-tensors-2/2023-02-21","draft":false,"unlisted":false,"editUrl":"https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/docs/IoT-and-Machine-Learning/ML/2023-02-21-tensorflow-tensors-2/index.md","tags":[{"inline":true,"label":"Python","permalink":"/docs/tags/python"},{"inline":true,"label":"Machine Learning","permalink":"/docs/tags/machine-learning"},{"inline":true,"label":"Tensorflow","permalink":"/docs/tags/tensorflow"}],"version":"current","sidebarPosition":4670,"frontMatter":{"sidebar_position":4670,"slug":"2023-02-21","title":"Tensorflow 2 - An (Re)Introduction 2023 (2)","authors":"mpolinowski","tags":["Python","Machine Learning","Tensorflow"],"description":"Tensor Indexing, Expanding and Manipulations"},"sidebar":"tutorialSidebar","previous":{"title":"Tensorflow 2 - An (Re)Introduction 2023 (3)","permalink":"/docs/IoT-and-Machine-Learning/ML/2023-02-22-tensorflow-tensors-3/2023-02-22"},"next":{"title":"Tensorflow 2 - An (Re)Introduction 2023","permalink":"/docs/IoT-and-Machine-Learning/ML/2023-02-19-tensorflow-introduction/2023-02-19"}}');var i=s(474848),r=s(28453);const a={sidebar_position:4670,slug:"2023-02-21",title:"Tensorflow 2 - An (Re)Introduction 2023 (2)",authors:"mpolinowski",tags:["Python","Machine Learning","Tensorflow"],description:"Tensor Indexing, Expanding and Manipulations"},o=void 0,l={},d=[{value:"Tensorflow Fundamentals",id:"tensorflow-fundamentals",level:2},{value:"Indexing Tensors",id:"indexing-tensors",level:3},{value:"Expanding Tensors",id:"expanding-tensors",level:3},{value:"Manipulating Tensors",id:"manipulating-tensors",level:3},{value:"Changing the Datatype",id:"changing-the-datatype",level:3},{value:"Aggregation",id:"aggregation",level:3},{value:"Variance and Standard Deviation",id:"variance-and-standard-deviation",level:3},{value:"Positional Maximum and Minimum",id:"positional-maximum-and-minimum",level:3}];function c(n){const e={a:"a",code:"code",em:"em",h2:"h2",h3:"h3",img:"img",li:"li",p:"p",pre:"pre",strong:"strong",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",ul:"ul",...(0,r.R)(),...n.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(e.p,{children:(0,i.jsx)(e.img,{alt:"Mong Kok, Hong Kong",src:s(695444).A+"",width:"2830",height:"1272"})}),"\n",(0,i.jsxs)(e.ul,{children:["\n",(0,i.jsxs)(e.li,{children:[(0,i.jsx)(e.a,{href:"#tensorflow-fundamentals",children:"Tensorflow Fundamentals"}),"\n",(0,i.jsxs)(e.ul,{children:["\n",(0,i.jsx)(e.li,{children:(0,i.jsx)(e.a,{href:"#indexing-tensors",children:"Indexing Tensors"})}),"\n",(0,i.jsx)(e.li,{children:(0,i.jsx)(e.a,{href:"#expanding-tensors",children:"Expanding Tensors"})}),"\n",(0,i.jsx)(e.li,{children:(0,i.jsx)(e.a,{href:"#manipulating-tensors",children:"Manipulating Tensors"})}),"\n",(0,i.jsx)(e.li,{children:(0,i.jsx)(e.a,{href:"#changing-the-datatype",children:"Changing the Datatype"})}),"\n",(0,i.jsx)(e.li,{children:(0,i.jsx)(e.a,{href:"#aggregation",children:"Aggregation"})}),"\n",(0,i.jsx)(e.li,{children:(0,i.jsx)(e.a,{href:"#variance-and-standard-deviation",children:"Variance and Standard Deviation"})}),"\n",(0,i.jsx)(e.li,{children:(0,i.jsx)(e.a,{href:"#positional-maximum-and-minimum",children:"Positional Maximum and Minimum"})}),"\n"]}),"\n"]}),"\n"]}),"\n",(0,i.jsx)(e.p,{children:(0,i.jsx)(e.a,{href:"https://github.com/mpolinowski/tf-2023",children:"Github Repository"})}),"\n",(0,i.jsx)(e.p,{children:(0,i.jsx)(e.em,{children:"See also:"})}),"\n",(0,i.jsxs)(e.ul,{children:["\n",(0,i.jsxs)(e.li,{children:["Fun, fun, tensors: ",(0,i.jsx)(e.a,{href:"/docs/IoT-and-Machine-Learning/ML/2023-02-19-tensorflow-introduction/2023-02-19",children:"Tensor Constants, Variables and Attributes"}),", ",(0,i.jsx)(e.a,{href:"/docs/IoT-and-Machine-Learning/ML/2023-02-21-tensorflow-tensors-2/2023-02-21",children:"Tensor Indexing, Expanding and Manipulations"}),", ",(0,i.jsx)(e.a,{href:"/docs/IoT-and-Machine-Learning/ML/2023-02-22-tensorflow-tensors-3/2023-02-22",children:"Matrix multiplications, Squeeze, One-hot and Numpy"})]}),"\n",(0,i.jsxs)(e.li,{children:["Tensorflow 2 - Neural Network Regression: ",(0,i.jsx)(e.a,{href:"/docs/IoT-and-Machine-Learning/ML/2023-02-23-tensorflow-neural-network-regression/2023-02-23",children:"Building a Regression Model"}),", ",(0,i.jsx)(e.a,{href:"/docs/IoT-and-Machine-Learning/ML/2023-02-24-tensorflow-neural-network-regression-evaluation/2023-02-24",children:"Model Evaluation"}),", ",(0,i.jsx)(e.a,{href:"/docs/IoT-and-Machine-Learning/ML/2023-02-25-tensorflow-neural-network-regression-experiments/2023-02-25",children:"Model Optimization"}),", ",(0,i.jsx)(e.a,{href:"/docs/IoT-and-Machine-Learning/ML/2023-02-26-tensorflow-neural-network-regression-real-dataset/2023-02-26",children:'Working with a "Real" Dataset'}),", ",(0,i.jsx)(e.a,{href:"/docs/IoT-and-Machine-Learning/ML/2023-02-26-tensorflow-neural-network-regression-data-preprocessing/2023-02-26",children:"Feature Scaling"})]}),"\n",(0,i.jsxs)(e.li,{children:["Tensorflow 2 - Neural Network Classification: ",(0,i.jsx)(e.a,{href:"/docs/IoT-and-Machine-Learning/ML/2023-02-27-tensorflow-neural-network-classification/2023-02-27",children:"Non-linear Data and Activation Functions"}),", ",(0,i.jsx)(e.a,{href:"/docs/IoT-and-Machine-Learning/ML/2023-02-28-tensorflow-neural-network-classification-model-evaluation/2023-02-28",children:"Model Evaluation and Performance Improvement"}),", ",(0,i.jsx)(e.a,{href:"/docs/IoT-and-Machine-Learning/ML/2023-03-02-tensorflow-neural-network-multi-classification/2023-03-02",children:"Multiclass Classification Problems"})]}),"\n",(0,i.jsxs)(e.li,{children:["Tensorflow 2 - Convolutional Neural Networks: ",(0,i.jsx)(e.a,{href:"/docs/IoT-and-Machine-Learning/ML/2023-03-03-tensorflow-convolutional-neural-network-binary-classifications/2023-03-03",children:"Binary Image Classification"}),", ",(0,i.jsx)(e.a,{href:"/docs/IoT-and-Machine-Learning/ML/2023-03-05-tensorflow-convolutional-neural-network-multiclass-classifications/2023-03-05",children:"Multiclass Image Classification"})]}),"\n",(0,i.jsxs)(e.li,{children:["Tensorflow 2 - Transfer Learning: ",(0,i.jsx)(e.a,{href:"/docs/IoT-and-Machine-Learning/ML/2023-03-06-tensorflow-transfer-learning-feature-extraction/2023-03-06",children:"Feature Extraction"}),", ",(0,i.jsx)(e.a,{href:"/docs/IoT-and-Machine-Learning/ML/2023-03-11-tensorflow-transfer-learning-fine-tuning/2023-03-11",children:"Fine-Tuning"}),", ",(0,i.jsx)(e.a,{href:"/docs/IoT-and-Machine-Learning/ML/2023-03-16-tensorflow-transfer-learning-scaling/2023-03-16",children:"Scaling"})]}),"\n",(0,i.jsxs)(e.li,{children:["Tensorflow 2 - Unsupervised Learning: ",(0,i.jsx)(e.a,{href:"/docs/IoT-and-Machine-Learning/ML/2023-03-24-tensorflow-unsupervised-learning-autoencoders/2023-03-24",children:"Autoencoder Feature Detection"}),", ",(0,i.jsx)(e.a,{href:"/docs/IoT-and-Machine-Learning/ML/2023-03-26-tensorflow-unsupervised-learning-autoencoders-super-resolution/2023-03-26",children:"Autoencoder Super-Resolution"}),", ",(0,i.jsx)(e.a,{href:"/docs/IoT-and-Machine-Learning/ML/2023-03-26-tensorflow-unsupervised-learning-generative-adversial-networks/2023-03-26",children:"Generative Adverserial Networks"})]}),"\n"]}),"\n",(0,i.jsx)(e.h2,{id:"tensorflow-fundamentals",children:"Tensorflow Fu
1ndamentals"}),"\n",(0,i.jsx)(e.h3,{id:"indexing-tensors",children:"Indexing Tensors"}),"\n",(0,i.jsx)(e.p,{children:"To get an insight into what kind data a tensor contains we can visualize parts of it. For example get the first 2 elements of each dimension:"}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-py",children:"# tensor with 4 dimensions\nrank_4_tensor = tf.zeros(shape=[2, 3, 4, 5])\n# get 2 elements of each dimension\nprint(rank_4_tensor[:2, :2, :2, :2])\n# tf.Tensor(\n# [[[[0. 0.]\n# [0. 0.]]\n\n# [[0. 0.]\n# [0. 0.]]]\n\n\n# [[[0. 0.]\n# [0. 0.]]\n\n# [[0. 0.]\n# [0. 0.]]]], shape=(2, 2, 2, 2), dtype=float32)\n"})}),"\n",(0,i.jsx)(e.p,{children:'Or, only retrieve the first element of all but the "x" dimension:'}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-py",children:"# get first element of all but the 4th dimension\nprint(rank_4_tensor[:1, :1, :1, :])\n# tf.Tensor([[[[0. 0. 0. 0. 0.]]]], shape=(1, 1, 1, 5), dtype=float32)\n\n# get first element of all but the 3rd dimension\nprint(rank_4_tensor[:1, :1, :, :1])\n# tf.Tensor(\n# [[[[0.]\n# [0.]\n# [0.]\n# [0.]]]], shape=(1, 1, 4, 1), dtype=float32)\n\n# get first element of all but the 2nd dimension\nprint(rank_4_tensor[:1, :, :1, :1])\n# tf.Tensor(\n# [[[[0.]]\n\n# [[0.]]\n\n# [[0.]]]], shape=(1, 3, 1, 1), dtype=float32)\n\n\n# get first element of all but the 1st dimension\nprint(rank_4_tensor[:, :1, :1, :1])\n# tf.Tensor(\n# [[[[0.]]]\n\n\n# [[[0.]]]], shape=(2, 1, 1, 1), dtype=float32)\n"})}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-py",children:"# tensor with 2 dimensions\nrank_2_tensor = tf.constant([[44, 88], [22, 77]])\nprint(rank_2_tensor.ndim)\n# 2\n\n# show last element of each row\nprint(rank_2_tensor[:, -1])\n# tf.Tensor([88 77], shape=(2,), dtype=int32)\n"})}),"\n",(0,i.jsx)(e.h3,{id:"expanding-tensors",children:"Expanding Tensors"}),"\n",(0,i.jsx)(e.p,{children:(0,i.jsx)(e.strong,{children:"tf.newaxis"})}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-py",children:"# add dimension to tensor\nrank_3_tensor = rank_2_tensor[..., tf.newaxis]\n# dot notation equals: rank_2_tensor[:, :, tf.newaxis]\n\nprint(rank_2_tensor)\n# tf.Tensor(\n# [[44 88]\n# [22 77]], shape=(2, 2), dtype=int32)\n\nprint(rank_3_tensor)\n# tf.Tensor(\n# [[[44]\n# [88]]\n\n# [[22]\n# [77]]], shape=(2, 2, 1), dtype=int32)\n"})}),"\n",(0,i.jsx)(e.p,{children:(0,i.jsx)(e.strong,{children:"tf.expand_dims()"})}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-py",children:"# Expand the final axis (-1)\nrank_5_tensor = tf.expand_dims(rank_4_tensor, axis=-1)\nprint(rank_5_tensor.shape)\n# (2, 3, 4, 5, 1)\n\n# Expand the 0 axis\nrank_5_tensor = tf.expand_dims(rank_4_tensor, axis=0)\nprint(rank_5_tensor.shape)\n# (2, 3, 4, 5, 1)\n"})}),"\n",(0,i.jsx)(e.h3,{id:"manipulating-tensors",children:"Manipulating Tensors"}),"\n",(0,i.jsxs)(e.p,{children:["Basic tensor operations ",(0,i.jsx)(e.code,{children:"+"}),", ",(0,i.jsx)(e.code,{children:"-"}),", ",(0,i.jsx)(e.code,{children:"*"}),", ",(0,i.jsx)(e.code,{children:"/"}),":"]}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-bash",children:"# tensor manipulation\noriginal_tensor = tf.constant([[44, 66], [33, 77]])\n\ntensor_add = original_tensor + 4\nprint(tensor_add)\n# tf.Tensor(\n# [[48 70]\n# [37 81]], shape=(2, 2), dtype=int32)\n\ntensor_subtract = original_tensor - 4\nprint(tensor_subtract)\n# tf.Tensor(\n# [[40 62]\n# [29 73]], shape=(2, 2), dtype=int32)\n\ntensor_multiply = original_tensor * 99\nprint(tensor_multiply)\n# tf.Tensor(\n# [[4356 6534]\n# [3267 7623]], shape=(2, 2), dtype=int32)\n"})}),"\n",(0,i.jsxs)(e.p,{children:["To be able to use GPU acceleration we should use the ",(0,i.jsx)(e.code,{children:"tf.math()"})," functions instead:"]}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-py",children:"print(tf.multiply(original_tensor, 99))\n# tf.Tensor(\n# [[4356 6534]\n# [3267 7623]], shape=(2, 2), dtype=int32)\n"})}),"\n",(0,i.jsx)(e.h3,{id:"changing-the-datatype",children:"Changing the Datatype"}),"\n",(0,i.jsx)(e.p,{children:"Reducing the precision of your data to speed up a training:"}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-py",children:"# reducing precision 32 -> 16\ntensor_float_16 = tf.cast(tensor_float, dtype=tf.float16)\nprint(tensor_float_16.dtype)\n# <dtype: 'float16'>\n"})}),"\n",(0,i.jsx)(e.p,{children:"Increasing precision to minimize rounding error:"}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-py",children:"# increasing precision\ntensor_int = tf.constant([9, 14])\nprint(tensor_int)\n# tf.Tensor([ 9 14], shape=(2,), dtype=int32)\n\ntensor_int_to_float_16 = tf.cast(tensor_int, dtype=tf.float16)\nprint(tensor_int_to_float_16)\n# tf.Tensor([ 9. 14.], shape=(2,), dtype=float16)\n\ntensor_division = tf.divi
1de(tensor_int_to_float_16, tensor_float_16)\nprint(tensor_division)\n# tf.Tensor([0.9854 1.007 ], shape=(2,), dtype=float16)\n"})}),"\n",(0,i.jsx)(e.h3,{id:"aggregation",children:"Aggregation"}),"\n",(0,i.jsxs)(e.p,{children:["Get the ",(0,i.jsx)(e.strong,{children:"absolute values"})," of all elements inside a tensor:"]}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-py",children:"# aggregation\ntensor_random = tf.constant(np.random.randint(-100, 100, size=42))\nprint(tensor_random)\n# tf.Tensor(\n# [-32 -45 41 -23 -6 -66 90 63 1 -74 -66 -92 99 19 -97 94 50 51\n# -69 -60 -15 24 33 10 -86 74 92 55 95 30 91 7 44 55 35 -82\n# 38 99 93 -92 -39 21], shape=(42,), dtype=int64)\n\n## show absolute value\nprint(tf.abs(tensor_random))\n# tf.Tensor(\n# [32 45 41 23 6 66 90 63 1 74 66 92 99 19 97 94 50 51 69 60 15 24 33 10\n# 86 74 92 55 95 30 91 7 44 55 35 82 38 99 93 92 39 21], shape=(42,), dtype=int64)\n"})}),"\n",(0,i.jsxs)(e.p,{children:["Get the ",(0,i.jsx)(e.strong,{children:"minimum value"}),":"]}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-py",children:"## show minimum value\nprint(tf.reduce_min(tensor_random))\n# tf.Tensor(-97, shape=(), dtype=int64)\n"})}),"\n",(0,i.jsxs)(e.p,{children:["Get the ",(0,i.jsx)(e.strong,{children:"maximum value"}),":"]}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-py",children:"## show maximum value\nprint(tf.reduce_max(tensor_random))\n# tf.Tensor(99, shape=(), dtype=int64)\n"})}),"\n",(0,i.jsxs)(e.p,{children:["Get the ",(0,i.jsx)(e.strong,{children:"mean of all elements"})," of a tensor:"]}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-py",children:"## show mean value\nprint(tf.reduce_mean(tensor_random))\n# tf.Tensor(10, shape=(), dtype=int64)\n"})}),"\n",(0,i.jsxs)(e.p,{children:["Get the ",(0,i.jsx)(e.strong,{children:"sum of all elements"})," of a tensor:"]}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-py",children:"## show sum\nprint(tf.reduce_sum(tensor_random))\n# tf.Tensor(460, shape=(), dtype=int64)\n"})}),"\n",(0,i.jsx)(e.h3,{id:"variance-and-standard-deviation",children:"Variance and Standard Deviation"}),"\n",(0,i.jsxs)(e.p,{children:["Calculating the ",(0,i.jsx)(e.strong,{children:"Standard Deviation"})," and ",(0,i.jsx)(e.strong,{children:"Variance"})," of a tensor:"]}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-py",children:"import tensorflow_probability as tfp\n\n## calculate variance\nprint(tf.math.reduce_variance(tf.cast(tensor_random, dtype=tf.float16))) # needs type float\nprint(tfp.stats.variance(tensor_random))\n# tf.Tensor(3921.0, shape=(), dtype=float16)\n# tf.Tensor(3921, shape=(), dtype=int64)\n\n## calculate standard deviation\nprint(tf.math.reduce_std(tf.cast(tensor_random, dtype=tf.float16))) # needs type float\nprint(tfp.stats.stddev(tf.cast(tensor_random, dtype=tf.float16))) # needs type float\n# tf.Tensor(62.62, shape=(), dtype=float16)\n# tf.Tensor(62.62, shape=(), dtype=float16)\n"})}),"\n",(0,i.jsx)(e.h3,{id:"positional-maximum-and-minimum",children:"Positional Maximum and Minimum"}),"\n",(0,i.jsx)(e.p,{children:"Before we determined the highest value within a tensor. But we often also need to determine the position as well, e.g."}),"\n",(0,i.jsxs)(e.table,{children:[(0,i.jsx)(e.thead,{children:(0,i.jsxs)(e.tr,{children:[(0,i.jsx)(e.th,{children:"Probabilities"}),(0,i.jsx)(e.th,{children:"class A"}),(0,i.jsx)(e.th,{children:"class B"}),(0,i.jsx)(e.th,{children:"class C"})]})}),(0,i.jsxs)(e.tbody,{children:[(0,i.jsxs)(e.tr,{children:[(0,i.jsx)(e.td,{children:"Input A"}),(0,i.jsx)(e.td,{children:"0.9982"}),(0,i.jsx)(e.td,{children:"0.0011"}),(0,i.jsx)(e.td,{children:"0.3456"})]}),(0,i.jsxs)(e.tr,{children:[(0,i.jsx)(e.td,{children:"Input B"}),(0,i.jsx)(e.td,{children:"0.0011"}),(0,i.jsx)(e.td,{children:"0.9982"}),(0,i.jsx)(e.td,{children:"0.3456"})]}),(0,i.jsxs)(e.tr,{children:[(0,i.jsx)(e.td,{children:"Input C"}),(0,i.jsx)(e.td,{children:"0.3456"}),(0,i.jsx)(e.td,{children:"0.0011"}),(0,i.jsx)(e.td,{children:"0.9982"})]})]})]}),"\n",(0,i.jsx)(e.p,{children:"This table assigns probabilities to inputs how likely it is that they belong to a label (class). Here we need to extract the highest probability and also it's position within the matrix to make the assignment."}),"\n",(0,i.jsx)(e.p,{children:"Let's create a tensor with a fixed seed:"}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-py",children:"# positional maximum & minimum\ntf.random.set_seed(42)\ntensor_const_seed = tf.random.uniform(shape=[42])\nprint(tensor_const_seed)\n# tf.Tensor(\n# [0.6645621 0.44100678 0.3528825 0.46448255 0.03366041 0.68467236\n# 0.74011743 0.8724445 0.22632635 0.22319686 0.3103881 0.7223358\n# 0.13318717 0.5480639 0.5746088 0.8996835 0.00946367 0.5212307\n# 0.6345445 0.1993283 0.72942245 0.54583454 0.10756552 0.6767061\n# 0.6602763 0.33695042 0.60141766 0.21062577 0.8527372 0.44062173\n# 0.9485276 0.23752594 0.81179297 0.5263394 0.494308 0.21612847\n# 0.8457197 0.8718841 0.3083862 0.6868038 0.23764038 0.7817228 ], shape=(42,), dtype=float32)\n"})}),"\n",(0,i.jsxs)(e.p,{children:["The element with the maximum value ",(0,i.jsx)(e.strong,{children:"0.9485276"}
1)," is in position ",(0,i.jsx)(e.strong,{children:"30"}),"."]}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-py",children:"## find positional maximum\nprint(tf.reduce_max(tensor_const_seed))\nprint(tf.argmax(tensor_const_seed))\n# tf.Tensor(0.9485276, shape=(), dtype=float32)\n# tf.Tensor(30, shape=(), dtype=int64)\n"})}),"\n",(0,i.jsxs)(e.p,{children:["The element with the minimum value ",(0,i.jsx)(e.strong,{children:"0.009463668"})," is in position ",(0,i.jsx)(e.strong,{children:"16"}),"."]}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-py",children:"## find positional minimum\nprint(tf.reduce_min(tensor_const_seed))\nprint(tf.argmin(tensor_const_seed))\n# tf.Tensor(0.009463668, shape=(), dtype=float32)\n# tf.Tensor(16, shape=(), dtype=int64)\n"})}),"\n",(0,i.jsx)(e.p,{children:"We can also use the positional index to find the corresponding value:"}),"\n",(0,i.jsx)(e.pre,{children:(0,i.jsx)(e.code,{className:"language-py",children:"## find value by position index\nprint(tensor_const_seed[tf.argmax(tensor_const_seed)])\nprint(tensor_const_seed[tf.argmin(tensor_const_seed)])\n# tf.Tensor(0.9485276, shape=(), dtype=float32)\n# tf.Tensor(0.009463668, shape=(), dtype=float32\n"})})]})}function h(n={}){const{wrapper:e}={...(0,r.R)(),...n.components};return e?(0,i.jsx)(e,{...n,children:(0,i.jsx)(c,{...n})}):c(n)}},695444(n,e,s){const t=s.p+"assets/images/photo-kt443t6d_64hdh43hfh6dgjdfhg4_d-f80e63ee872dae25129198058ac93b4e.jpg";s.d(e,["A",0,t])},28453(n,e,s){s.d(e,{R:()=>a,x:()=>o});var t=s(296540);const i={},r=t.createContext(i);function a(n){const e=t.useContext(r);return t.useMemo(function(){return"function"==typeof n?n(e):{...e,...n}},[e,n])}function o(n){let e;return e=n.disableParentContext?"function"==typeof n.components?n.components(i):n.components||i:a(n.components),t.createElement(r.Provider,{value:e},n.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.