0% found this document useful (0 votes)
13 views3 pages

Exp 1 Mam

The document describes implementing simple vector addition in TensorFlow. It defines a vector_addition function that takes two input vectors, converts them to TensorFlow tensors, performs element-wise addition using tf.add(), and returns the result tensor. Sample input vectors are defined and passed to the function, and the result is printed.

Uploaded by

Sur Esh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Exp 1 Mam

The document describes implementing simple vector addition in TensorFlow. It defines a vector_addition function that takes two input vectors, converts them to TensorFlow tensors, performs element-wise addition using tf.add(), and returns the result tensor. Sample input vectors are defined and passed to the function, and the result is printed.

Uploaded by

Sur Esh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Implement simple vector addition in Tensorflow

AIM :
To write a python program to implement a simple vector addition in
tensorflow.

Algorithm:

1. Import TensorFlow: The program starts by importing the TensorFlow library


(import tensorflow as tf), which is a popular open-source machine learning
framework.
2. Define the Vector Addition Function: The vector_addition function takes two
input vectors ( a and b) as parameters.
3. Convert Input Lists to TensorFlow Tensors: Inside the function, the input lists ( a
and b) are converted into TensorFlow constant tensors using tf.constant().
4. Perform Vector Addition: The tf.add() function is used to perform element-wise
addition of the two tensors ( tensor_a and tensor_b), resulting in a new TensorFlow
tensor (result).
5. Return the Result Tensor: The function returns the TensorFlow tensor representing
the result of the vector addition.
6. Main Program Execution: The if __name__ == "__main__": block ensures that
the following code is executed only if the script is run directly, not if it's imported as
a module.
7. Define Sample Input Vectors: Two sample input vectors ( vector1 and vector2) are
defined as Python lists.
8. Perform Vector Addition: The vector_addition function is called with the sample
input vectors, and the result is stored in the variable result_vector.
9. Convert Result Tensor to NumPy Array: The result tensor ( result_vector) is
converted to a NumPy array using the .numpy() method.
10. Print the Results: The program prints the original vectors ( vector1 and vector2)
and the result of the vector addition ( result).
Program :
import tensorflow as tf

def vector_addition(a, b):


# Convert the input lists to TensorFlow tensors
tensor_a = tf.constant(a)
tensor_b = tf.constant(b)

# Perform the vector addition


result = tf.add(tensor_a, tensor_b)

return result

if __name__ == "__main__":
# Sample input vectors
vector1 = [1, 2, 3]
vector2 = [4, 5, 6]

# Perform vector addition using the function


result_vector = vector_addition(vector1, vector2)

# Open a TensorFlow session to execute the computation (not required in TF


2.x)
# with tf.compat.v1.Session() as sess:
# result = sess.run(result_vector)
# Instead, in TF 2.x, we can directly access the result as a NumPy array
result = result_vector.numpy()

print("Vector 1:", vector1)


print("Vector 2:", vector2)
print("Vector Addition Result:", result)

output:
Vector 1: [1, 2, 3]
Vector 2: [4, 5, 6]
Vector Addition Result: [5 7 9]

Result:
Thus the implementation of simple vector addition in tensorflow has
been executed successfully.

You might also like