w3resource

TensorFlow Basics: Exercises and Solutions for Beginners

Python TensorFlow basic [16 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

These exercises provide a good starting point for users to learn the basics of TensorFlow. These include tensors, operations, sessions, eager execution, variables, and data types. Before moving on to more advanced TensorFlow topics, users can practice these exercises

TensorFlow Operations:

1. Write a Python program that creates a TensorFlow constant with the values [100, 200, 300]. Print its shape and data type.
Click me to see the sample solution

2. Write a Python program that implements element-wise addition on two TensorFlow tensors of shape (2, 3) filled with random values.
Click me to see the sample solution

3. Write a Python program to create a TensorFlow tensor of shape (3, 3) filled with zeros and then change its value to nine.
Click me to see the sample solution

4. Write a Python program that multiplies a TensorFlow tensor by a scalar value and prints the result.
Click me to see the sample solution

5. Write a Python program that uses TensorFlow to compute the dot product of two vectors (1-D tensors).
Click me to see the sample solution

Sessions and Eager Execution:

6. Write a Python program that enables eager execution in TensorFlow 2.x and prints the result of a simple operation without a session.
Click me to see the sample solution

7. Write a Python program that creates a TensorFlow graph and run it inside a session to compute the result of a basic mathematical operation.
Click me to see the sample solution

Variables and Constants:

8. Write a Python program that defines a TensorFlow variable to store a matrix of random values. Initialize and print the variable.
Click me to see the sample solution

9. Write a Python program that explains the difference between TensorFlow variables and constants.
Click me to see the sample solution

10. Write a Python program that creates a TensorFlow constant and a TensorFlow variable. Add, subtract, multiply and divide them and print the result.
Click me to see the sample solution

11. Write a Python program that demonstrates how to update a TensorFlow variable within a session.
Click me to see the sample solution

12. Write a Python program that initializes a TensorFlow variable with an initial value and then updates its value within a loop. Print the variable's value after each update.
Click me to see the sample solution

Data Types in TensorFlow:

13. Write a Python code to list some common TensorFlow data types available.
Click me to see the sample solution

14. Write a Python program that creates a TensorFlow tensor with a specified data type (e.g., float32) and prints its data type.
Click me to see the sample solution

15. Write a Python program that converts a TensorFlow tensor from one data type to another (e.g., from int32 to float64).
Click me to see the sample solution

16. Write a Python program that creates a TensorFlow tensor with a string data type and print its value.
Click me to see the sample solution

Python Code Editor:


[ Want to contribute to Python exercises? Send your code (attached with a .zip file) to us at w3resource[at]yahoo[dot]com. Please avoid copyrighted materials.]



Follow us on Facebook and Twitter for latest update.