Tensorflow 2.14.x code replacement

Hello All,

I’m new to ML/tensorflow. Recently, the TensorFlow released the new version of the pipy package ```
pip install tensorflow[and-cuda]. The below codes are not working on the new version of TensorFlow.
Can you share a replacement for the below codes?

tf.Session(cf=tf.ConfigProto(gpu_options=gpuops, log_device_placement=False))

Hi @Sadheesh_S & Welcome to the Tensorflow forum.

The below codes are not working on the new version of TensorFlow.

Can you please show the error message your are getting?

Thank you.

Hi @Sadheesh_S ,

You can use this code:

# Get the list of available GPU device IDs.
gpu_devices = tf.config.experimental.list_physical_devices('GPU')

# Enable GPU support for the first GPU device.
tf.config.experimental.set_visible_devices([gpu_devices[0]], 'GPU')

And for more detailed follow this github issue.

I hope it helps!


1 Like

Hi Tagoma,

Thanks for your reply.

The below exception has mentioned for your reference:
AttributeErrorr: module ‘tensorflow’ has no attribute ‘GPUOptions’

Moreoever, the facial recognition code using the session.run() from tf. session

I’m not sure how to refactor the legacy code with new version of tensorflow

Thanks again for your valuable support.


That you are migrating from TF1.x to TF2.x is a crucial piece of information indeed.
To migrate your code accordingly, please have a look at this Tensorflow guide.
Alternatively, the tf.compat.v1 classes allow you to run your TF1.x code using TF2.x, e.g. tf.compat.v1.Session.