[PYTHON] How to run CNN in 1 system notation in Tensorflow 2

Assumed reader

--People who are trying to run CNN (convolutional NN) written in 1 system code in Tensorflow 2 environment

Premise

Execution environment

About CNN code

I will not post the code due to various reasons,

And here's the code running CNN.

Main changes from 1 system to 2 system

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:

How to move the code of 1 system notation with 2 system

There seem to be the following three ways to move CNN in 1-series notation in 2-series (change the code to do so).

  1. Rewrite the changed API steadily
  2. Utilize the update script 3.2 Disable 2 system behavior

1. Rewrite the changed API steadily

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:

(1) Disabling Eager Execution

ʻImport tensorflow as tf` Immediately after, add the following to disable Eager Execution.

tf.compat.v1.disable_eager_execution()

(2) Some API notation changes

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.

2. Utilize the update script

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.

3.2 Disable 2 system behavior

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)

At the end

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

How to run CNN in 1 system notation in Tensorflow 2
How to run TensorFlow 1.0 code in 2.0
[TensorFlow 2 / Keras] How to run learning with CTC Loss in Keras
How to run some script regularly in Django
How to create data to put in CNN (Chainer)
How to run Leap Motion in non-Apple Python
[TF] How to build Tensorflow in Proxy environment
How to run the Ansible module added in Ansible Tower
How to run AutoGluon in Google Colab GPU environment
How to run tests in bulk with Python unittest
How to run setUp only once in python unittest
How to run Notepad ++ Python
How to develop in Python
[Python] How to do PCA in Python
How to handle session in SQLAlchemy
How to use classes in Theano
How to write soberly in pandas
How to collect images in Python
How to update Spyder in Anaconda
How to use SQLite in Python
[EC2] How to run selenium webdriver
How to run matplotlib on heroku
How to convert 0.5 to 1056964608 in one shot
Misunderstanding on how to connect cnn
How to install TensorFlow on CentOS 7
How to reflect CSS in Django
How to kill processes in bulk
How to use Mysql in python
How to convert Tensorflow model to Lite
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
How to log in to Docker + NGINX
How to call PyTorch in Julia
[TensorFlow 2] How to check the contents of Tensor in graph mode
[Introduction to Python] How to use class in Python?
How to suppress display error in matplotlib
How to access environment variables in Python
How to dynamically define variables in Python
How to convert csv to tsv in CLI
How to delete expired sessions in Django
[Itertools.permutations] How to put permutations in Python
How to use Google Test in C
How to implement nested serializer in drf-flex-fields
How to work with BigQuery in Python
How to execute commands in jupyter notebook
How to do'git fetch --tags' in GitPython
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to reassign index in pandas dataframe
How to check opencv version in python
How to enable SSL (TLS) in Apache
How to use Anaconda interpreter in PyCharm
How to specify non-check target in Flake8
How to handle consecutive values in MySQL
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to use tensorflow under docker environment