3.1: Tensors

Tensors are the central unit of data in TensorFlow. They are similar to arrays or matrices in other programming languages. A tensor has a data type and a shape. For example, a tensor can be one-dimensional (like a vector), two-dimensional (like a matrix), or even higher-dimensional.

3.2: Operations on Tensors

TensorFlow provides a variety of operations that you can perform on tensors. These include arithmetic operations like addition, subtraction, multiplication, and division, as well as more complex operations like matrix multiplication, broadcasting, reshaping, and slicing.

3.3: Variables and Constants

In TensorFlow, variables are mutable tensor values that survive across multiple calls to run(); they are used to hold and update parameters of a machine learning model. Constants, on the other hand, are tensor values that are immutable, meaning their values do not change. They are often used to store fixed values that do not require updating during training.

3.1: Tensors

In TensorFlow, a tensor represents a n-dimensional array of data. Here’s a simple example of how to create tensors:

				
					import tensorflow as tf

# Creating a 1-D tensor (a vector)
tensor_1d = tf.constant([1, 2, 3, 4])

# Creating a 2-D tensor (a matrix)
tensor_2d = tf.constant([[1, 2, 3], [4, 5, 6]])

# Print the tensors
print(tensor_1d)
print(tensor_2d)

				
			

3.2: Operations on Tensors

You can perform a variety of operations on tensors. Here are a few examples:

				
					# Addition
tensor_add = tf.add(tensor_1d, 5)

# Element-wise multiplication
tensor_mul = tf.multiply(tensor_1d, tensor_1d)

# Matrix multiplication
result = tf.matmul(tensor_2d, [[1], [2], [3]])

# Print the results
print(tensor_add)
print(tensor_mul)
print(result)

				
			

3.3: Variables and Constants

Variables are mutable and can be used to store model parameters, while constants are immutable and are typically used for fixed values.

				
					# Creating a variable
var = tf.Variable([0.0, 0.0, 0.0, 0.0])

# Creating a constant
const = tf.constant([1.0, 1.0, 1.0, 1.0])

# Updating the value of the variable
var.assign_add(const)

# Print the variable and constant
print(var)
print(const)

				
			
Bytes of Intelligence
Bytes of Intelligence
Bytes Of Intelligence

Exploring AI's mysteries in 'Bytes of Intelligence': Your Gateway to Understanding and Harnessing the Power of Artificial Intelligence.

Would you like to share your thoughts?