Understanding TensorFlow Basics

By Bill Sharlow

TensorFlow Deep Learning Framework

Welcome back to Day 2 of our 10-Day DIY TensorFlow Deep Learning Framework Setup series! Today, we’re diving into the foundational concepts of TensorFlow, exploring tensors, operations, and computational graphs.

TensorFlow Basics: A Quick Recap

TensorFlow revolves around the concept of tensors, which are multi-dimensional arrays representing data. Before we proceed, let’s reinforce our understanding of these key terms:

  • Tensor: The fundamental unit in TensorFlow, representing data in the form of multi-dimensional arrays
  • Operation (Op): A computational operation that manipulates tensors, such as matrix multiplication or element-wise addition
  • Graph: A computational graph represents a series of TensorFlow operations, showcasing the flow of data

Creating and Manipulating Tensors

In TensorFlow, tensors can be constants or variables. Let’s create a simple TensorFlow script to work with tensors:

import tensorflow as tf

# Create tensors
tensor_a = tf.constant([1, 2, 3], name='tensor_a')
tensor_b = tf.constant([4, 5, 6], name='tensor_b')

# Perform an operation
result_tensor = tf.add(tensor_a, tensor_b, name='result_tensor')

# Print the result
tf.print('Result Tensor:', result_tensor)

In this script:

  • We create two constant tensors, tensor_a and tensor_b
  • The tf.add operation is used to add the two tensors, resulting in result_tensor
  • The tf.print statement displays the result

Execute this script in your Python environment to see TensorFlow in action. Experiment by modifying tensors and operations to solidify your grasp of the basics.

Understanding Computational Graphs

TensorFlow operations are organized into computational graphs. Let’s visualize a simple graph for our example:

import tensorflow as tf

# Define nodes (tensors and operations)
tensor_a = tf.constant([1, 2, 3], name='tensor_a')
tensor_b = tf.constant([4, 5, 6], name='tensor_b')
result_tensor = tf.add(tensor_a, tensor_b, name='result_tensor')

# Visualize the graph
tf.summary.FileWriter('logs', tf.compat.v1.get_default_graph()).close()

Here, we’ve used tf.summary.FileWriter to create a log directory. You can visualize the graph using TensorBoard:

tensorboard --logdir=logs

Open your browser and navigate to http://localhost:6006/ to see the graph.

What’s Next?

You’ve now explored the basics of TensorFlow, creating tensors, performing operations, and visualizing computational graphs. In the upcoming days, we’ll delve deeper into building neural networks and deploying models.

Stay tuned for Day 3: Building Your First Neural Network, where we’ll take our first steps into the exciting world of deep learning. Happy coding!

Leave a Comment