{"id":47519,"date":"2026-11-24T22:04:50","date_gmt":"2026-11-24T22:04:50","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=47519"},"modified":"2026-11-24T22:04:50","modified_gmt":"2026-11-24T22:04:50","slug":"tensorsflow-lab1-tensorsflow-and-variables","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/tensorsflow-lab1-tensorsflow-and-variables\/","title":{"rendered":"Tensorsflow Lab1 &#8211; Tensorsflow And Variables"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This lab covers the two core data structures in TensorFlow 2: immutable tensors created with <code>tf.constant<\/code>, and mutable <code>tf.Variable<\/code> objects that hold state such as model weights. The notebook embedded above creates tensors, converts them to and from NumPy, reshapes them, and updates variables in place. The sections below explain what each step does, give a runnable script version with expected output, and cover the dtype and shape errors beginners run into most often.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/79d3666a785ad5738d1c2154f4797e8a.js\"><\/script>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Tensors vs variables at a glance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <strong>tensor<\/strong> (<code>tf.Tensor<\/code>) is a typed, multi-dimensional array. Once created it cannot be changed. Every operation, such as <code>x0 + 5<\/code> or <code>tf.reshape<\/code>, returns a new tensor. A <strong>variable<\/strong> (<code>tf.Variable<\/code>) wraps a tensor whose value can be updated in place with <code>assign<\/code>, <code>assign_add<\/code> and <code>assign_sub<\/code>. Keras layers store their weights and biases as variables, and optimizers update them in place on every training step.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Property<\/th><th>tf.Tensor (tf.constant)<\/th><th>tf.Variable<\/th><\/tr><\/thead><tbody><tr><td>Mutable<\/td><td>No, operations return new tensors<\/td><td>Yes, through assign, assign_add and assign_sub<\/td><\/tr><tr><td>Typical use<\/td><td>Inputs, intermediate results, constants<\/td><td>Model parameters, counters, persistent state<\/td><\/tr><tr><td>Tracked by GradientTape automatically<\/td><td>No, needs tape.watch()<\/td><td>Yes, if trainable=True (the default)<\/td><\/tr><tr><td>Saved in checkpoints<\/td><td>No<\/td><td>Yes, when attached to a tracked object such as a Keras model or tf.train.Checkpoint<\/td><\/tr><tr><td>Result of math on it<\/td><td>tf.Tensor<\/td><td>tf.Tensor, not a variable<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The last row explains the notebook heading &quot;Operations on Variables result in a Tensor&quot;: <code>tf.add(v1, v2)<\/code> reads both variables and returns an ordinary tensor. <code>v1<\/code> itself is unchanged.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites and environment setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The notebook metadata shows Python 3.7, which current TensorFlow releases no longer support. Use a recent Python 3 version that your chosen TensorFlow release supports, and work inside a virtual environment.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">python3 -m venv tf-lab\nsource tf-lab\/bin\/activate        <span class=\"hljs-comment\"># Windows: tf-lab\\Scripts\\activate<\/span>\npip install --upgrade pip\npip install tensorflow numpy jupyter\n\n<span class=\"hljs-comment\"># Verify the install and see which devices TensorFlow can use<\/span>\npython -c <span class=\"hljs-string\">\"import tensorflow as tf; print(tf.__version__); print(tf.config.list_physical_devices())\"<\/span>\n\njupyter notebook<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> On Linux machines with an NVIDIA GPU, recent TensorFlow releases can install the CUDA libraries for you with <code>pip install &quot;tensorflow[and-cuda]&quot;<\/code>. The lab itself runs fine on CPU, and the device placement log above shows every op running on <code>CPU:0<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Runnable script version of the lab<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The notebook cells were run out of order (the execution counts jump from 2 to 26 to 51). The script below runs the same steps top to bottom, so you can save it as <code>tensors_lab.py<\/code> and run it with <code>python tensors_lab.py<\/code>. Device placement logging is left out to keep the output readable.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">import numpy <span class=\"hljs-keyword\">as<\/span> np\nimport tensorflow <span class=\"hljs-keyword\">as<\/span> tf\n\n<span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">\"Eager:\"<\/span>, tf.executing_eagerly())\n\n<span class=\"hljs-comment\"># Constant tensors<\/span>\nx0 = tf.constant(<span class=\"hljs-number\">3<\/span>)                       <span class=\"hljs-comment\"># scalar, int32<\/span>\nx1 = tf.constant(&#91;<span class=\"hljs-number\">1.1<\/span>, <span class=\"hljs-number\">2.2<\/span>, <span class=\"hljs-number\">3.3<\/span>, <span class=\"hljs-number\">4.4<\/span>])    <span class=\"hljs-comment\"># shape (4,), float32<\/span>\nx2 = tf.constant(&#91;&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>], &#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>]])\nx2 = tf.cast(x2, tf.float32)              <span class=\"hljs-comment\"># int32 -&gt; float32<\/span>\n\n<span class=\"hljs-keyword\">print<\/span>(x0.shape, x0.dtype, x0.numpy())\n<span class=\"hljs-keyword\">print<\/span>((x0 + <span class=\"hljs-number\">5<\/span>).numpy())\n<span class=\"hljs-keyword\">print<\/span>(tf.add(x1, <span class=\"hljs-number\">5.0<\/span>).numpy())\n<span class=\"hljs-keyword\">print<\/span>(tf.multiply(x1, x2).numpy())        <span class=\"hljs-comment\"># (4,) broadcast against (2, 4)<\/span>\n\n<span class=\"hljs-comment\"># NumPy interop<\/span>\narr = np.<span class=\"hljs-keyword\">array<\/span>(&#91;&#91;<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>], &#91;<span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">40<\/span>], &#91;<span class=\"hljs-number\">50<\/span>, <span class=\"hljs-number\">60<\/span>]])\nx4 = tf.convert_to_tensor(arr)\n<span class=\"hljs-keyword\">print<\/span>(tf.is_tensor(arr), tf.is_tensor(x4), x4.dtype)\n<span class=\"hljs-keyword\">print<\/span>(np.sqrt(x2))                        <span class=\"hljs-comment\"># returns a NumPy array<\/span>\n\n<span class=\"hljs-comment\"># Creation and reshape<\/span>\nt0 = tf.zeros(&#91;<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">5<\/span>], tf.int32)\n<span class=\"hljs-keyword\">print<\/span>(tf.reshape(t0, (<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">3<\/span>)).shape)\n\n<span class=\"hljs-comment\"># Variables<\/span>\nv1 = tf.Variable(&#91;&#91;<span class=\"hljs-number\">1.5<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">5<\/span>], &#91;<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">8<\/span>]])\nv1.assign(&#91;&#91;<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>], &#91;<span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">50<\/span>, <span class=\"hljs-number\">60<\/span>]])\nv1&#91;<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>].assign(<span class=\"hljs-number\">100<\/span>)\nv1.assign_add(tf.ones_like(v1))\nv1.assign_sub(<span class=\"hljs-number\">2<\/span> * tf.ones_like(v1))\n<span class=\"hljs-keyword\">print<\/span>(v1.numpy())\n\n<span class=\"hljs-comment\"># Copying a variable copies its value<\/span>\nvar_a = tf.Variable(&#91;<span class=\"hljs-number\">2.0<\/span>, <span class=\"hljs-number\">3.0<\/span>])\nvar_b = tf.Variable(var_a)\nvar_b.assign(&#91;<span class=\"hljs-number\">200<\/span>, <span class=\"hljs-number\">300<\/span>])\n<span class=\"hljs-keyword\">print<\/span>(var_a.numpy(), var_b.numpy())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Expected output (TensorFlow may print some startup log lines first):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">Eager: <span class=\"hljs-keyword\">True<\/span>\n() &lt;dtype: <span class=\"hljs-string\">'int32'<\/span>&gt; <span class=\"hljs-number\">3<\/span>\n<span class=\"hljs-number\">8<\/span>\n&#91;<span class=\"hljs-number\">6.1<\/span> <span class=\"hljs-number\">7.2<\/span> <span class=\"hljs-number\">8.3<\/span> <span class=\"hljs-number\">9.4<\/span>]\n&#91;&#91; <span class=\"hljs-number\">1.1<\/span>       <span class=\"hljs-number\">4.4<\/span>       <span class=\"hljs-number\">9.9<\/span>      <span class=\"hljs-number\">17.6<\/span>     ]\n &#91; <span class=\"hljs-number\">5.5<\/span>      <span class=\"hljs-number\">13.200001<\/span> <span class=\"hljs-number\">23.1<\/span>      <span class=\"hljs-number\">35.2<\/span>     ]]\n<span class=\"hljs-keyword\">False<\/span> <span class=\"hljs-keyword\">True<\/span> &lt;dtype: <span class=\"hljs-string\">'int64'<\/span>&gt;\n&#91;&#91;<span class=\"hljs-number\">1.<\/span>        <span class=\"hljs-number\">1.4142135<\/span> <span class=\"hljs-number\">1.7320508<\/span> <span class=\"hljs-number\">2.<\/span>       ]\n &#91;<span class=\"hljs-number\">2.236068<\/span>  <span class=\"hljs-number\">2.4494898<\/span> <span class=\"hljs-number\">2.6457512<\/span> <span class=\"hljs-number\">2.828427<\/span> ]]\n(<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">3<\/span>)\n&#91;&#91;<span class=\"hljs-number\">99.<\/span> <span class=\"hljs-number\">19.<\/span> <span class=\"hljs-number\">29.<\/span>]\n &#91;<span class=\"hljs-number\">39.<\/span> <span class=\"hljs-number\">49.<\/span> <span class=\"hljs-number\">59.<\/span>]]\n&#91;<span class=\"hljs-number\">2.<\/span> <span class=\"hljs-number\">3.<\/span>] &#91;<span class=\"hljs-number\">200.<\/span> <span class=\"hljs-number\">300.<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> The <code>13.200001<\/code> value is normal float32 rounding, not a bug. Compare floating-point results with <code>np.allclose<\/code> instead of <code>==<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How dtypes, casting and broadcasting behave<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Default dtypes:<\/strong> Python ints become <code>int32<\/code> and Python floats become <code>float32<\/code> in <code>tf.constant<\/code>. NumPy arrays keep their own dtype, which is why <code>x4<\/code> is <code>int64<\/code>. NumPy 1.x on Windows defaults to <code>int32<\/code>, so older Windows setups may show <code>int32<\/code> there.<\/li>\n\n\n<li><strong>No implicit promotion between tensors:<\/strong> TensorFlow does not silently mix <code>int32<\/code> and <code>float32<\/code> tensors. That is why the lab calls <code>tf.cast(x2, tf.float32)<\/code> before <code>tf.multiply(x1, x2)<\/code>.<\/li>\n\n\n<li><strong>Python scalars adapt:<\/strong> in <code>x0 + 5<\/code> the literal <code>5<\/code> is converted to the dtype of <code>x0<\/code>. <code>x0 + 5.5<\/code> fails because 5.5 cannot be represented as <code>int32<\/code>.<\/li>\n\n\n<li><strong>Broadcasting:<\/strong> <code>x1<\/code> has shape <code>(4,)<\/code> and <code>x2<\/code> has shape <code>(2, 4)<\/code>. The trailing dimensions match, so <code>x1<\/code> is applied to each row. This follows the same rules as NumPy.<\/li>\n\n\n<li><strong>Reshape keeps the element count:<\/strong> <code>(3, 5)<\/code> to <code>(5, 3)<\/code> works because both hold 15 elements. Pass <code>-1<\/code> for one dimension to let TensorFlow infer it, for example <code>tf.reshape(t0, (-1, 3))<\/code>.<\/li>\n\n\n<li><strong>NumPy functions on tensors:<\/strong> <code>np.square(x2)<\/code> and <code>np.sqrt(x2)<\/code> convert the tensor to a NumPy array and return a NumPy array, not a tensor. Use <code>tf.square<\/code> and <code>tf.sqrt<\/code> to stay inside TensorFlow, which matters for GPU execution, <code>tf.function<\/code> and gradients.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why variables matter: a gradient step<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The lab shows how to mutate variables but not why you would. The main reason is training. <code>tf.GradientTape<\/code> automatically watches trainable variables, computes gradients with respect to them, and the update is then applied in place. This is a hand-written version of what a Keras optimizer does.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">import tensorflow <span class=\"hljs-keyword\">as<\/span> tf\n\nw = tf.Variable(<span class=\"hljs-number\">3.0<\/span>)                      <span class=\"hljs-comment\"># trainable by default<\/span>\nstep = tf.Variable(<span class=\"hljs-number\">0<\/span>, trainable=<span class=\"hljs-keyword\">False<\/span>)    <span class=\"hljs-comment\"># state that should not get gradients<\/span>\n\nwith tf.GradientTape() <span class=\"hljs-keyword\">as<\/span> tape:\n    loss = w * w                          <span class=\"hljs-comment\"># d(loss)\/dw = 2w = 6<\/span>\n\ngrad = tape.gradient(loss, w)\nw.assign_sub(<span class=\"hljs-number\">0.1<\/span> * grad)                  <span class=\"hljs-comment\"># gradient descent: 3.0 - 0.6<\/span>\nstep.assign_add(<span class=\"hljs-number\">1<\/span>)\n\n<span class=\"hljs-keyword\">print<\/span>(grad.numpy(), w.numpy(), step.numpy())\n<span class=\"hljs-comment\"># 6.0 2.4 1<\/span>\n\n<span class=\"hljs-comment\"># A constant is not watched automatically<\/span>\nc = tf.constant(<span class=\"hljs-number\">3.0<\/span>)\nwith tf.GradientTape() <span class=\"hljs-keyword\">as<\/span> tape:\n    y = c * c\n<span class=\"hljs-keyword\">print<\/span>(tape.gradient(y, c))                <span class=\"hljs-comment\"># None<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">To get gradients for a constant, call <code>tape.watch(c)<\/code> inside the <code>with<\/code> block. In real code, pass <code>name=<\/code> when creating variables, for example <code>tf.Variable(0.0, name=&quot;bias&quot;)<\/code>. Every variable in the lab shows up as <code>Variable:0<\/code>, and named variables are much easier to find in checkpoints and debugging output.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common errors and how to fix them<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Error<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td>InvalidArgumentError: cannot compute AddV2 as input #1(zero-based) was expected to be a int32 tensor but is a float tensor<\/td><td>Mixing int32 and float32 tensors in one op<\/td><td>Cast one side with <code>tf.cast(t, tf.float32)<\/code> or create both with the same <code>dtype=<\/code><\/td><\/tr><tr><td>TypeError: Cannot convert 5.5 to EagerTensor of dtype int32<\/td><td>Adding a Python float to an integer tensor<\/td><td>Create the tensor as float, e.g. <code>tf.constant(3.0)<\/code><\/td><\/tr><tr><td>TypeError: &#039;EagerTensor&#039; object does not support item assignment<\/td><td>Trying <code>x0[0] = 1<\/code> on a tf.Tensor<\/td><td>Use a <code>tf.Variable<\/code> and <code>v[0].assign(1)<\/code>, or build a new tensor with <code>tf.tensor_scatter_nd_update<\/code><\/td><\/tr><tr><td>InvalidArgumentError: Input to reshape is a tensor with 15 values, but the requested shape has 16<\/td><td>Target shape has a different element count<\/td><td>Make the product of the dimensions match, or use <code>-1<\/code> for one dimension<\/td><\/tr><tr><td>ValueError about incompatible shapes on assign<\/td><td><code>v.assign()<\/code> with a value of a different shape<\/td><td>Assign a value of the same shape, or create the variable with <code>shape=tf.TensorShape(None)<\/code> if its shape must change<\/td><\/tr><tr><td>Device placement log lines missing in Jupyter<\/td><td>The log goes to the process log stream, not always to the cell output<\/td><td>Check the terminal running Jupyter, and call <code>set_log_device_placement(True)<\/code> before any ops run<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Notes and corrections for the notebook<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The post title spells the library as &quot;Tensorsflow&quot;. The correct name is <strong>TensorFlow<\/strong>.<\/li>\n\n\n<li>The notebook kernel is Python 3.7.4, which current TensorFlow releases do not support. Recreate the environment with a supported Python 3 version as shown above.<\/li>\n\n\n<li>The cells were executed out of order. Use <strong>Kernel &gt; Restart &amp; Run All<\/strong> to confirm the notebook works top to bottom before relying on its outputs.<\/li>\n\n\n<li>The markdown typo &quot;perfomed&quot; should read &quot;performed&quot;.<\/li>\n\n\n<li><code>v1.assign_add([[1, 1, 1], [1, 1, 1]])<\/code> works, but <code>tf.ones_like(v1)<\/code> keeps the shape and dtype tied to the variable and avoids hard-coding the shape.<\/li>\n\n\n<li><code>np.square(x2)<\/code> and <code>np.sqrt(x2)<\/code> return NumPy arrays, not tensors. Prefer <code>tf.square<\/code> and <code>tf.sqrt<\/code> inside models.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Set dtypes explicitly (<code>dtype=tf.float32<\/code>) for anything that feeds a model. Most layers and optimizers expect float32.<\/li>\n\n\n<li>Use variables only for state that must persist or be trained. Everything else should be a plain tensor.<\/li>\n\n\n<li>Mark counters and other non-learned state with <code>trainable=False<\/code> so optimizers and tapes skip them.<\/li>\n\n\n<li>Avoid calling <code>.numpy()<\/code> inside training loops or <code>tf.function<\/code> code. It forces a copy back to host memory and breaks graph tracing.<\/li>\n\n\n<li>Turn off <code>tf.debugging.set_log_device_placement<\/code> outside of debugging. It logs every single op.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between tf.constant and tf.Variable?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>tf.constant<\/code> creates an immutable tensor, and every operation on it returns a new tensor. <code>tf.Variable<\/code> holds a value you can change in place with <code>assign<\/code>, <code>assign_add<\/code> and <code>assign_sub<\/code>. It is also tracked automatically for gradients and checkpoints, which is why it is used for model weights.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I convert a TensorFlow tensor to a NumPy array?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In eager mode, call <code>.numpy()<\/code> on the tensor or variable. To go the other way, use <code>tf.convert_to_tensor(array)<\/code> or <code>tf.constant(array)<\/code>. Both keep the NumPy dtype unless you pass <code>dtype=<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why does adding an int tensor and a float tensor fail in TensorFlow?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike NumPy, TensorFlow does not automatically promote dtypes between tensors, so an int32 + float32 op raises InvalidArgumentError. Cast one operand with <code>tf.cast<\/code> so both have the same dtype.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does tf.Variable(other_variable) share memory with the original?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. It creates a new variable initialized with a copy of the current value, so later assignments to one do not affect the other. The final cells of the lab demonstrate this.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Is the result of an operation on a variable also a variable?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. Operations such as <code>tf.add(v1, v2)<\/code> read the variables and return a regular <code>tf.Tensor<\/code>. Only the <code>assign<\/code> family of methods changes a variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/artificial-intelligence\/machine-learning\/\">More Machine Learning tutorials<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/staff-mlops-engineer-role-blueprint-responsibilities-skills-kpis-and-career-path\/\">Staff MLOps Engineer: Role Blueprint, Responsibilities, Skills, KPIs, and Career Path<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/lead-machine-learning-engineer-role-blueprint-responsibilities-skills-kpis-and-career-path\/\">Lead Machine Learning Engineer: Role Blueprint, Responsibilities, Skills, KPIs, and Career Path<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This lab covers the two core data structures in TensorFlow 2: immutable tensors created with tf.constant, and mutable tf.Variable objects that hold state such as model weights&#8230;. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[6512],"tags":[7380],"class_list":["post-47519","post","type-post","status-publish","format-standard","hentry","category-machine-learning","tag-tensorflow"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=47519"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47519\/revisions"}],"predecessor-version":[{"id":80199,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47519\/revisions\/80199"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=47519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=47519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=47519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}