--People who are trying to run CNN (convolutional NN) written in 1 system code in Tensorflow 2 environment
I will not post the code due to various reasons,
tf.keras. *
To get MNIST datatf.nn. *
, Tf.train. *
, Tf. *
And here's the code running CNN.
For the main changes from Tensorflow 1 to 2, [here](https://data-analysis-stats.jp/%E6%B7%B1%E5%B1%9E%E5%AD%A6%E7% I saw various articles such as BF% 92 / tensorflow-2-0-% E4% B8% BB% E3% 81% AA% E5% A4% 89% E6% 9B% B4% E7% 82% B9 /) However, the ones that had a direct impact on running CNN were:
Eager Execution default
Abolition of Session and Placeholder
There may be other things depending on how CNN is implemented, but please forgive that point.
There seem to be the following three ways to move CNN in 1-series notation in 2-series (change the code to do so).
It's not a wise method, but if there are few changes, I think it's possible to steadily rewrite the relevant parts. Here's the fix I needed to get CNN working:
ʻImport tensorflow as tf` Immediately after, add the following to disable Eager Execution.
tf.compat.v1.disable_eager_execution()
Change the API corresponding to the ** '1 series' ** column in the code as described in ** '2 series' **.
# | 1 system | 2 system |
---|---|---|
1 | tf.placeholder | tf **.compat.v1.**placeholder |
2 | tf.random_uniform | tf.random.uniform |
3 | tf.add_to_collection | tf **.compat.v1.**add_to_collection |
4 | tf.get_collection('costs') | tf **.compat.v1.**get_collection |
5 | tf.train.AdamOptimizer | tf **.compat.v1.**train.AdamOptimizer |
6 | tf.Session() | tf **.compat.v1.**Session() |
Some, like ** # 2 **, did not simply add compat.v1
.
For APIs other than the above that require the addition of compat.v1
, see Tensorflow Module: tf.compat.v1. Please refer.
There seems to be a way to convert the code to 2 system notation using the update script, referring to Automatically upgrade the code to TensorFlow 2. .. I think that it is safer than the manual conversion of 1, but there is a description that some parts require manual conversion, so it seems that it is not fully automatic.
According to Migrate TensorFlow1 code to TensorFlow2 Tensorflow import statement
import tensorflow as tf
To
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
It seems that the code of 1 system notation can be executed by changing like this to invalidate the 2 system behavior. (I was able to confirm the execution in my code)
I would be most happy if it works in 3 ways. I found method 3 after doing 1 so I wrote this so that no one would come out. I want to get used to Tensorflow more and more.
Recommended Posts