Overview of TensorFlow Fundamental Ideas
TensorFlow 2.0 has simplified lots of the complexities of earlier variations, offering an intuitive and extra user-friendly API. TensorFlow 2.0 gives a a lot simpler and extra Pythonic expertise in comparison with TensorFlow 1.0 whereas retaining the core ideas of deep studying computations. Nevertheless, understanding key ideas like computational graphs, classes, variables, placeholders, constants, and operations continues to be elementary to utilizing TensorFlow successfully.
It will make it easier to with understanding and differentiating between TensorFlow 1 and TensorFlow 2.
Inside this content material, we cowl an summary of the above fundamental ideas of TensorFlow.
What are Computational Graphs?
In TensorFlow, a computational graph is a method to characterize mathematical computations. Nodes within the graph characterize operations (e.g., addition, multiplication), and edges characterize tensors, that are multi-dimensional arrays used to move knowledge between nodes.
In TensorFlow 1.0, customers would explicitly construct and manipulate this graph. However in TensorFlow 2.0, you work together with the graph extra implicitly and naturally, due to keen execution.
Static Graph (TF 1.0 type)
You possibly can nonetheless create static graphs utilizing the tf.perform
decorator, which transforms a Python perform right into a TensorFlow graph.
Under, the output returns the consequence with graph execution.
import tensorflow as tf @tf.perform def my_function(x, y): return x * y + y consequence = my_function(3, 4) print(consequence) # Consequence: tf.Tensor(16, form=(), dtype=int32)
Keen Execution
In TensorFlow 2.0, keen execution is enabled by default, which implies operations are evaluated instantly. You don’t have to explicitly create or execute a graph, which makes the code simpler to debug and extra Pythonic.
Keen execution (default in TensorFlow 2.0). The next will instantly return the consequence (5.0).
import tensorflow as tf a = tf.fixed(2.0) b = tf.fixed(3.0) c = a + b print(c)
Classes in TensorFlow
In TensorFlow 1.0, a session was used to execute operations inside a computational graph. You first needed to construct a graph after which run it in a session to get the outcomes. Nevertheless, with TensorFlow 2.0, classes have been changed with keen execution, so you possibly can merely run operations as Python code.
TensorFlow 1.0 Session
Please remember that the next produces an error “module ‘tensorflow’ has no attribute ‘Session’” in TF2. It must run in model 1 so as to work. We solely exhibit with the under code.
import tensorflow as tf a = tf.fixed(3.0) b = tf.fixed(4.0) c = a + b with tf.Session() as sess: consequence = sess.run(c) print(consequence) # error in TF2 - module 'tensorflow' has no attribute 'Session'
TensorFlow 2.0 Keen Execution (no session required)
import tensorflow as tf a = tf.fixed(3.0) b = tf.fixed(4.0) c = a + b print(c.numpy()) # Output: 7.0
Variables in TensorFlow
A variable in TensorFlow is a particular kind of tensor that may change its worth throughout coaching. It’s sometimes used for storing weights and biases in a machine studying mannequin.
Within the first occasion, we outline a variable my_var. Then, we replace the worth of the variable by way of the .assign methodology. Values change from (1.0, 2.0, 3.0) to (3.0, 2.0, 1.0).
Create a Variable in TensorFlow
import tensorflow as tf my_var = tf.Variable([1.0, 2.0, 3.0], dtype=tf.float32) print(my_var)
Replace a Variable in TensorFlow
my_var.assign([3.0, 2.0, 1.0]) print(my_var)
Placeholders in TensorFlow
In TensorFlow 1.0, placeholders had been used to feed knowledge right into a TensorFlow graph at runtime. They had been placeholders for tensors that might be offered throughout the execution. In TensorFlow 2.0, placeholders are not essential as a result of you possibly can straight move knowledge into the operations throughout keen execution.
Equally to classes, in TF2 the code for placeholders produces an error “module ‘tensorflow’ has no attribute ‘placeholder’”. Instance just for demonstration.
Placeholder (TensorFlow 1.0)
import tensorflow as tf x = tf.placeholder(tf.float32, form=[None, 3]) y = x * 2 with tf.Session() as sess: consequence = sess.run(y, feed_dict={x: [[1, 2, 3], [4, 5, 6]]}) print(consequence)
Placeholder (TensorFlow 2.0 – no placeholders required)
import tensorflow as tf def multiply_by_two(x): return x * 2 consequence = multiply_by_two(tf.fixed([[1, 2, 3], [4, 5, 6]])) print(consequence)
Constants in TensorFlow
A fixed in TensorFlow is a set worth tensor that doesn’t change throughout execution. It defines values that can stay unchanged all through the computation.
Create Constants
First, we create 2 constants (with values 5 and 6). Then, we carry out an addition operation.
import tensorflow as tf const_a = tf.fixed(5) const_b = tf.fixed(6) consequence = const_a + const_b print(consequence.numpy()) # Output: 11
Operations in TensorFlow
Operations in TensorFlow outline the computational duties you need to carry out. Examples embody addition, multiplication, and different mathematical operations.
TensorFlow additionally helps extra complicated operations like matrix multiplication.
Fundamental Operations TensorFlow
import tensorflow as tf # Outline constants a = tf.fixed(10) b = tf.fixed(5) # Operations add = tf.add(a, b) # Addition subtract = tf.subtract(a, b) # Subtraction multiply = tf.multiply(a, b) # Multiplication divide = tf.divide(a, b) # Division print("Addition:", add.numpy()) print("Subtraction:", subtract.numpy()) print("Multiplication:", multiply.numpy()) print("Division:", divide.numpy())
Matrix Operations
import tensorflow as tf # Outline two matrices matrix1 = tf.fixed([[1, 2], [3, 4]]) matrix2 = tf.fixed([[5, 6], [7, 8]]) # Carry out matrix multiplication consequence = tf.matmul(matrix1, matrix2) print(consequence.numpy())
Earlier: Introduction to TensorFlow