You can use ‘with’ statement to close a TensorFlow session automatically in your code. Here is an example of how you can implement this:
with tf.session() as sess1:
output = sess1.run(result)
print(output)
If you run the above code, the TensorFlow session will automatically be closed after printing the output.
If you want to learn Tensorflow, you can enroll in this Artificial Intelligence course by Intellipaat.
You can watch this video on Tensorflow in 10 minutes to know tricks with TensorFlow: