问题:AttributeError: module ‘tensorflow‘ has no attribute ‘reset_default_graph‘,no attribute ‘placehol

Two problems are encountered when using TensorFlow2.x or above:

1. reset_default_graph problem

AttributeError: module ‘tensorflow’ has no attribute ‘reset_default_graph’
Insert image description here

1.1 Problem analysis

This error is because the reset_default_graph function has been removed in TensorFlow 2.0 and higher. In TensorFlow 1.x versions, the reset_default_graph function is used to clear the default graph stack and reset the global default graph. However, in TensorFlow 2.0 and higher, due to the introduction of eager execution (Eager Execution) mode, this function is no longer needed.

1.2 Problem solving

If you need to use functionality similar to reset_default_graph in TensorFlow 2.0, you can create a new tf.Graph instance and use the tf.function decorator to encapsulate your calculations in this new graph. This way, every time you create a new tf.Graph instance, you reset the graph.

My own specific example solution is as follows:
Insert image description here

2. Placeholder problem

AttributeError: module ‘tensorflow’ has no attribute ‘placeholder’
Insert image description here

2.1 Problem analysis

This error is because the placeholder function has been removed in TensorFlow 2.0 and higher. In TensorFlow 1.x version, the placeholder function is used to define a placeholder, which will be filled with actual data at runtime. However, in TensorFlow 2.0 and higher, due to the introduction of eager execution (Eager Execution) mode, this function is no longer needed.

2.2 Problem solving

In TensorFlow 2.0 and higher, you can use variables directly instead of placeholders. Modify tf.placeholder() to tf.Variable()

My own specific example is as follows:
Insert image description here

3. Summary

The above is the detailed method to solve the problem in TensorFlow2. your turn!

It’s not easy to summarize. Thank you for your support!

Guess you like

Origin blog.csdn.net/qq_40280673/article/details/134827187