天天看點

5 分鐘将 TensorFlow 1 代碼轉換到 TensorFlow 2

5 分鐘将 TensorFlow 1 代碼轉換到 TensorFlow 2

跑代碼 戳這裡👇:

5 分鐘将 TensorFlow 1 代碼轉換到 TensorFlow 2

小夥伴問了:現在很多新的 GPU 對以前的 TensorFlow 1.x 的支援很不好,比如 30 系列的GPU,以前的代碼跑不起來咋辦?

5 分鐘将 TensorFlow 1 代碼轉換到 TensorFlow 2

TensorFLow v1 原本是為了運作效率讓使用者直接使用 Python 操作計算圖。

但是漸漸的使用者發現:

  • 調試困難
  • 開發低效
  • 各種 API 不統一
  • 以及來自外部的 Keras

都讓曾經的 TensorFlow v1 越來越複雜以及 PyTorch 在學術研究領域的緊逼等等各種原因,TensorFlow v2 就誕生了。這就帶來一個問題,曾經那些使用 TensorFlow v1 寫的代碼怎麼遷移到新版呢?

TensorFlow 當然想到了,他們建立了 tf_upgrade_v2 實用程式來幫助将舊代碼轉換為新 API,下面我們就來看看怎麼使用。

擷取 TensorFlow v1 實驗代碼

!git clone --branch r1.13.0 --depth 1 https://github.com/tensorflow/models
Cloning into 'models'...
remote: Enumerating objects: 2927, done.
remote: Counting objects: 100% (2927/2927), done.
remote: Compressing objects: 100% (2428/2428), done.
remote: Total 2927 (delta 504), reused 2113 (delta 424), pack-reused 0
Receiving objects: 100% (2927/2927), 369.04 MiB | 7.16 MiB/s, done.
Resolving deltas: 100% (504/504), done.
Checking out files: 100% (2768/2768), done.
           
!python -c "import tensorflow as tf; print('TensorFlow Version:', tf.__version__)"
2021-09-17 11:05:53.348159: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0
TensorFlow Version: 2.5.0
           
!cd models/samples/cookbook/regression && python custom_regression.py
2021-09-17 10:23:15.907129: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0
Traceback (most recent call last):
  File "custom_regression.py", line 162, in <module>
    tf.logging.set_verbosity(tf.logging.INFO)
AttributeError: module 'tensorflow' has no attribute 'logging'
           

結束

5 分鐘将 TensorFlow 1 代碼轉換到 TensorFlow 2

我們很好的複現了使用 TensorFlow v2 運作出錯,好了,今天的内容就到這了。

小夥伴:就這?

使用 tf_upgrade_v2 轉換代碼

接下來就可以使用 tf_upgrade_v2 來對不相容的代碼進行轉換。

  • –intree 需要轉換的目錄
  • –outtree 轉換後的目标目錄
  • –reportfile 轉換日志
!tf_upgrade_v2 \
    --intree models/samples/cookbook/regression/ \
    --outtree regression_v2/ \
    --reportfile tree_report.txt
           
2021-09-17 09:39:27.511795: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0
INFO line 38:8: Renamed 'tf.feature_column.input_layer' to 'tf.compat.v1.feature_column.input_layer'
INFO line 43:10: Renamed 'tf.layers.dense' to 'tf.compat.v1.layers.dense'
INFO line 46:17: Renamed 'tf.layers.dense' to 'tf.compat.v1.layers.dense'
INFO line 57:17: tf.losses.mean_squared_error requires manual check. tf.losses have been replaced with object oriented versions in TF 2.0 and after. The loss function calls have been converted to compat.v1 for backward compatibility. Please update these calls to the TF 2.0 versions.
INFO line 57:17: Renamed 'tf.losses.mean_squared_error' to 'tf.compat.v1.losses.mean_squared_error'
INFO line 61:15: Added keywords to args of function 'tf.shape'
INFO line 62:15: Changed tf.to_float call to tf.cast(..., dtype=tf.float32).
INFO line 65:40: Renamed 'tf.train.AdamOptimizer' to 'tf.compat.v1.train.AdamOptimizer'
INFO line 68:39: Renamed 'tf.train.get_global_step' to 'tf.compat.v1.train.get_global_step'
INFO line 83:9: tf.metrics.root_mean_squared_error requires manual check. tf.metrics have been replaced with object oriented versions in TF 2.0 and after. The metric function calls have been converted to compat.v1 for backward compatibility. Please update these calls to the TF 2.0 versions.
INFO line 83:9: Renamed 'tf.metrics.root_mean_squared_error' to 'tf.compat.v1.metrics.root_mean_squared_error'
INFO line 142:23: Renamed 'tf.train.AdamOptimizer' to 'tf.compat.v1.train.AdamOptimizer'
INFO line 162:2: Renamed 'tf.logging.set_verbosity' to 'tf.compat.v1.logging.set_verbosity'
INFO line 162:27: Renamed 'tf.logging.INFO' to 'tf.compat.v1.logging.INFO'
INFO line 163:2: Renamed 'tf.app.run' to 'tf.compat.v1.app.run'
INFO line 72:10: tf.estimator.DNNRegressor: Default value of loss_reduction has been changed to SUM_OVER_BATCH_SIZE; inserting old default value tf.keras.losses.Reduction.SUM.

INFO line 96:2: Renamed 'tf.logging.set_verbosity' to 'tf.compat.v1.logging.set_verbosity'
INFO line 96:27: Renamed 'tf.logging.INFO' to 'tf.compat.v1.logging.INFO'
INFO line 97:2: Renamed 'tf.app.run' to 'tf.compat.v1.app.run'
INFO line 58:10: tf.estimator.LinearRegressor: Default value of loss_reduction has been changed to SUM_OVER_BATCH_SIZE; inserting old default value tf.keras.losses.Reduction.SUM.

INFO line 101:2: Renamed 'tf.logging.set_verbosity' to 'tf.compat.v1.logging.set_verbosity'
INFO line 101:27: Renamed 'tf.logging.INFO' to 'tf.compat.v1.logging.INFO'
INFO line 102:2: Renamed 'tf.app.run' to 'tf.compat.v1.app.run'
INFO line 82:10: tf.estimator.LinearRegressor: Default value of loss_reduction has been changed to SUM_OVER_BATCH_SIZE; inserting old default value tf.keras.losses.Reduction.SUM.

INFO line 105:2: Renamed 'tf.logging.set_verbosity' to 'tf.compat.v1.logging.set_verbosity'
INFO line 105:27: Renamed 'tf.logging.INFO' to 'tf.compat.v1.logging.INFO'
INFO line 106:2: Renamed 'tf.app.run' to 'tf.compat.v1.app.run'
INFO line 40:7: Renamed 'tf.test.mock' to 'tf.compat.v1.test.mock'
WARNING line 125:15: Changing dataset.make_one_shot_iterator() to tf.compat.v1.data.make_one_shot_iterator(dataset). Please check this transformation.

TensorFlow 2.0 Upgrade Script
-----------------------------
Converted 7 files
Detected 1 issues that require attention
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
File: models/samples/cookbook/regression/automobile_data.py
--------------------------------------------------------------------------------
models/samples/cookbook/regression/automobile_data.py:125:15: WARNING: Changing dataset.make_one_shot_iterator() to tf.compat.v1.data.make_one_shot_iterator(dataset). Please check this transformation.



Make sure to read the detailed log 'tree_report.txt'

           

成功運作

轉換完成了,我們趕緊執行測試一下。

!python regression_v2/custom_regression.py
           
2021-09-17 10:38:48.058202: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0
INFO:tensorflow:Using default config.
I0917 10:38:49.609052 140619878479680 estimator.py:1825] Using default config.
WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmpz1q4nr2s
W0917 10:38:49.609834 140619878479680 estimator.py:1847] Using temporary folder as model directory: /tmp/tmpz1q4nr2s
INFO:tensorflow:Using config: {'_model_dir': '/tmp/tmpz1q4nr2s', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true
graph_options {
  rewrite_options {
    meta_optimizer_iterations: ONE
  }
}
, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_experimental_max_worker_delay_secs': None, '_session_creation_timeout_secs': 7200, '_checkpoint_save_graph_def': True, '_service': None, '_cluster_spec': ClusterSpec({}), '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}
I0917 10:38:49.610240 140619878479680 estimator.py:191] Using config: {'_model_dir': '/tmp/tmpz1q4nr2s', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true
graph_options {
  rewrite_options {
    meta_optimizer_iterations: ONE
  }
}
, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_experimental_max_worker_delay_secs': None, '_session_creation_timeout_secs': 7200, '_checkpoint_save_graph_def': True, '_service': None, '_cluster_spec': ClusterSpec({}), '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}
WARNING:tensorflow:From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/training/training_util.py:236: Variable.initialized_value (from tensorflow.python.ops.variables) is deprecated and will be removed in a future version.
Instructions for updating:
Use Variable.read_value. Variables in 2.X are initialized automatically both in eager and graph (inside tf.defun) contexts.
W0917 10:38:49.615717 140619878479680 deprecation.py:336] From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/training/training_util.py:236: Variable.initialized_value (from tensorflow.python.ops.variables) is deprecated and will be removed in a future version.
Instructions for updating:
Use Variable.read_value. Variables in 2.X are initialized automatically both in eager and graph (inside tf.defun) contexts.
2021-09-17 10:38:49.621989: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcuda.so.1
2021-09-17 10:38:49.628413: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-09-17 10:38:49.628976: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1733] Found device 0 with properties: 
pciBusID: 0000:06:00.0 name: Tesla V100-SXM2-16GB computeCapability: 7.0
coreClock: 1.53GHz coreCount: 80 deviceMemorySize: 15.78GiB deviceMemoryBandwidth: 836.37GiB/s
2021-09-17 10:38:49.629010: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0
2021-09-17 10:38:49.632552: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcublas.so.11
2021-09-17 10:38:49.632609: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcublasLt.so.11
2021-09-17 10:38:49.634175: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcufft.so.10
2021-09-17 10:38:49.634581: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcurand.so.10
2021-09-17 10:38:49.638331: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcusolver.so.11
2021-09-17 10:38:49.639126: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcusparse.so.11
2021-09-17 10:38:49.639326: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudnn.so.8
2021-09-17 10:38:49.639447: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-09-17 10:38:49.639964: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-09-17 10:38:49.640432: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1871] Adding visible gpu devices: 0
INFO:tensorflow:Calling model_fn.
I0917 10:38:49.709739 140619878479680 estimator.py:1162] Calling model_fn.
WARNING:tensorflow:From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column.py:205: IndicatorColumn._get_dense_tensor (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
W0917 10:38:49.710260 140619878479680 deprecation.py:336] From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column.py:205: IndicatorColumn._get_dense_tensor (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
WARNING:tensorflow:From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column.py:2192: IndicatorColumn._transform_feature (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
W0917 10:38:49.710400 140619878479680 deprecation.py:336] From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column.py:2192: IndicatorColumn._transform_feature (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
WARNING:tensorflow:From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column_v2.py:4221: VocabularyListCategoricalColumn._get_sparse_tensors (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
W0917 10:38:49.710472 140619878479680 deprecation.py:336] From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column_v2.py:4221: VocabularyListCategoricalColumn._get_sparse_tensors (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
WARNING:tensorflow:From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column.py:2192: VocabularyListCategoricalColumn._transform_feature (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
W0917 10:38:49.710545 140619878479680 deprecation.py:336] From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column.py:2192: VocabularyListCategoricalColumn._transform_feature (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
WARNING:tensorflow:From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column_v2.py:4223: IndicatorColumn._variable_shape (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
W0917 10:38:49.716857 140619878479680 deprecation.py:336] From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column_v2.py:4223: IndicatorColumn._variable_shape (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
WARNING:tensorflow:From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column_v2.py:4248: VocabularyListCategoricalColumn._num_buckets (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
W0917 10:38:49.716978 140619878479680 deprecation.py:336] From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column_v2.py:4248: VocabularyListCategoricalColumn._num_buckets (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
WARNING:tensorflow:From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column.py:205: NumericColumn._get_dense_tensor (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
W0917 10:38:49.724601 140619878479680 deprecation.py:336] From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column.py:205: NumericColumn._get_dense_tensor (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
WARNING:tensorflow:From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column.py:2192: NumericColumn._transform_feature (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
W0917 10:38:49.724731 140619878479680 deprecation.py:336] From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column.py:2192: NumericColumn._transform_feature (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
WARNING:tensorflow:From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column.py:206: NumericColumn._variable_shape (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
W0917 10:38:49.725898 140619878479680 deprecation.py:336] From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column.py:206: NumericColumn._variable_shape (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
WARNING:tensorflow:From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column.py:205: EmbeddingColumn._get_dense_tensor (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
W0917 10:38:49.732480 140619878479680 deprecation.py:336] From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column.py:205: EmbeddingColumn._get_dense_tensor (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
WARNING:tensorflow:From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column_v2.py:3092: HashedCategoricalColumn._get_sparse_tensors (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
W0917 10:38:49.732640 140619878479680 deprecation.py:336] From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column_v2.py:3092: HashedCategoricalColumn._get_sparse_tensors (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
WARNING:tensorflow:From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column.py:2192: HashedCategoricalColumn._transform_feature (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
W0917 10:38:49.732730 140619878479680 deprecation.py:336] From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column.py:2192: HashedCategoricalColumn._transform_feature (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
WARNING:tensorflow:From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column_v2.py:3032: HashedCategoricalColumn._num_buckets (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
W0917 10:38:49.735543 140619878479680 deprecation.py:336] From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column_v2.py:3032: HashedCategoricalColumn._num_buckets (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
WARNING:tensorflow:From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column.py:206: EmbeddingColumn._variable_shape (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
W0917 10:38:49.759805 140619878479680 deprecation.py:336] From /environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column.py:206: EmbeddingColumn._variable_shape (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.
Instructions for updating:
The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.
/environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/keras/legacy_tf_layers/core.py:171: UserWarning: `tf.layers.dense` is deprecated and will be removed in a future version. Please use `tf.keras.layers.Dense` instead.
  warnings.warn('`tf.layers.dense` is deprecated and '
/environment/python/versions/miniconda3-4.7.12/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer_v1.py:1692: UserWarning: `layer.apply` is deprecated and will be removed in a future version. Please use `layer.__call__` method instead.
  warnings.warn('`layer.apply` is deprecated and '
INFO:tensorflow:Done calling model_fn.
I0917 10:38:49.942236 140619878479680 estimator.py:1164] Done calling model_fn.
INFO:tensorflow:Create CheckpointSaverHook.
I0917 10:38:49.943118 140619878479680 basic_session_run_hooks.py:546] Create CheckpointSaverHook.
INFO:tensorflow:Graph was finalized.
I0917 10:38:50.051830 140619878479680 monitored_session.py:247] Graph was finalized.
2021-09-17 10:38:50.052147: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-09-17 10:38:50.053018: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-09-17 10:38:50.053590: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1733] Found device 0 with properties: 
pciBusID: 0000:06:00.0 name: Tesla V100-SXM2-16GB computeCapability: 7.0
coreClock: 1.53GHz coreCount: 80 deviceMemorySize: 15.78GiB deviceMemoryBandwidth: 836.37GiB/s
2021-09-17 10:38:50.053708: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-09-17 10:38:50.054184: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-09-17 10:38:50.054616: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1871] Adding visible gpu devices: 0
2021-09-17 10:38:50.054696: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0
2021-09-17 10:38:50.666505: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1258] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-09-17 10:38:50.666579: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1264]      0 
2021-09-17 10:38:50.666609: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1277] 0:   N 
2021-09-17 10:38:50.666845: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-09-17 10:38:50.667403: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-09-17 10:38:50.667912: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-09-17 10:38:50.668358: W tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:39] Overriding allow_growth setting because the TF_FORCE_GPU_ALLOW_GROWTH environment variable is set. Original config value was 0.
2021-09-17 10:38:50.668415: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1418] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 14065 MB memory) -> physical GPU (device: 0, name: Tesla V100-SXM2-16GB, pci bus id: 0000:06:00.0, compute capability: 7.0)
2021-09-17 10:38:50.684360: I tensorflow/core/platform/profile_utils/cpu_utils.cc:114] CPU Frequency: 2100095000 Hz
INFO:tensorflow:Running local_init_op.
I0917 10:38:50.741362 140619878479680 session_manager.py:531] Running local_init_op.
INFO:tensorflow:Done running local_init_op.
I0917 10:38:50.749526 140619878479680 session_manager.py:534] Done running local_init_op.
INFO:tensorflow:Calling checkpoint listeners before saving checkpoint 0...
I0917 10:38:50.989682 140619878479680 basic_session_run_hooks.py:614] Calling checkpoint listeners before saving checkpoint 0...
INFO:tensorflow:Saving checkpoints for 0 into /tmp/tmpz1q4nr2s/model.ckpt.
I0917 10:38:50.991084 140619878479680 basic_session_run_hooks.py:618] Saving checkpoints for 0 into /tmp/tmpz1q4nr2s/model.ckpt.
INFO:tensorflow:Calling checkpoint listeners after saving checkpoint 0...
I0917 10:38:51.049777 140619878479680 basic_session_run_hooks.py:626] Calling checkpoint listeners after saving checkpoint 0...
2021-09-17 10:38:51.187209: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcublas.so.11
2021-09-17 10:38:51.760481: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcublasLt.so.11
INFO:tensorflow:loss = 11881942.0, step = 0
I0917 10:38:51.769505 140619878479680 basic_session_run_hooks.py:262] loss = 11881942.0, step = 0
INFO:tensorflow:global_step/sec: 152.06
I0917 10:38:52.425096 140619878479680 basic_session_run_hooks.py:702] global_step/sec: 152.06
INFO:tensorflow:loss = 2670.899, step = 100 (0.657 sec)
I0917 10:38:52.426279 140619878479680 basic_session_run_hooks.py:260] loss = 2670.899, step = 100 (0.657 sec)
INFO:tensorflow:global_step/sec: 182.174
I0917 10:38:52.973980 140619878479680 basic_session_run_hooks.py:702] global_step/sec: 182.174
INFO:tensorflow:loss = 2834.4111, step = 200 (0.549 sec)
I0917 10:38:52.975166 140619878479680 basic_session_run_hooks.py:260] loss = 2834.4111, step = 200 (0.549 sec)
INFO:tensorflow:global_step/sec: 190.446
I0917 10:38:53.499054 140619878479680 basic_session_run_hooks.py:702] global_step/sec: 190.446
INFO:tensorflow:loss = 2626.0112, step = 300 (0.525 sec)
I0917 10:38:53.500172 140619878479680 basic_session_run_hooks.py:260] loss = 2626.0112, step = 300 (0.525 sec)
INFO:tensorflow:global_step/sec: 183.912
I0917 10:38:54.042769 140619878479680 basic_session_run_hooks.py:702] global_step/sec: 183.912
INFO:tensorflow:loss = 2637.4644, step = 400 (0.544 sec)
I0917 10:38:54.043846 140619878479680 basic_session_run_hooks.py:260] loss = 2637.4644, step = 400 (0.544 sec)
INFO:tensorflow:global_step/sec: 174.393
I0917 10:38:54.616265 140619878479680 basic_session_run_hooks.py:702] global_step/sec: 174.393
INFO:tensorflow:loss = 2772.9592, step = 500 (0.574 sec)
I0917 10:38:54.617521 140619878479680 basic_session_run_hooks.py:260] loss = 2772.9592, step = 500 (0.574 sec)
INFO:tensorflow:global_step/sec: 153.132
I0917 10:38:55.269284 140619878479680 basic_session_run_hooks.py:702] global_step/sec: 153.132
INFO:tensorflow:loss = 2566.7422, step = 600 (0.653 sec)
I0917 10:38:55.270813 140619878479680 basic_session_run_hooks.py:260] loss = 2566.7422, step = 600 (0.653 sec)
INFO:tensorflow:global_step/sec: 152.739
I0917 10:38:55.923968 140619878479680 basic_session_run_hooks.py:702] global_step/sec: 152.739
INFO:tensorflow:loss = 2065.108, step = 700 (0.654 sec)
I0917 10:38:55.925307 140619878479680 basic_session_run_hooks.py:260] loss = 2065.108, step = 700 (0.654 sec)
INFO:tensorflow:global_step/sec: 156.754
I0917 10:38:56.561982 140619878479680 basic_session_run_hooks.py:702] global_step/sec: 156.754
INFO:tensorflow:loss = 2342.2454, step = 800 (0.639 sec)
I0917 10:38:56.563928 140619878479680 basic_session_run_hooks.py:260] loss = 2342.2454, step = 800 (0.639 sec)
INFO:tensorflow:global_step/sec: 151.702
I0917 10:38:57.221118 140619878479680 basic_session_run_hooks.py:702] global_step/sec: 151.702
INFO:tensorflow:loss = 1715.0842, step = 900 (0.659 sec)
I0917 10:38:57.222758 140619878479680 basic_session_run_hooks.py:260] loss = 1715.0842, step = 900 (0.659 sec)
INFO:tensorflow:Calling checkpoint listeners before saving checkpoint 1000...
I0917 10:38:57.865512 140619878479680 basic_session_run_hooks.py:614] Calling checkpoint listeners before saving checkpoint 1000...
INFO:tensorflow:Saving checkpoints for 1000 into /tmp/tmpz1q4nr2s/model.ckpt.
I0917 10:38:57.865842 140619878479680 basic_session_run_hooks.py:618] Saving checkpoints for 1000 into /tmp/tmpz1q4nr2s/model.ckpt.
INFO:tensorflow:Calling checkpoint listeners after saving checkpoint 1000...
I0917 10:38:57.930022 140619878479680 basic_session_run_hooks.py:626] Calling checkpoint listeners after saving checkpoint 1000...
INFO:tensorflow:Loss for final step: 512.958.
I0917 10:39:01.411782 140619878479680 estimator.py:350] Loss for final step: 512.958.
INFO:tensorflow:Calling model_fn.
I0917 10:39:01.525945 140619878479680 estimator.py:1162] Calling model_fn.
Tensor("IteratorGetNext:25", shape=(None,), dtype=float64, device=/device:CPU:0)
Tensor("Squeeze:0", shape=(None,), dtype=float32)
INFO:tensorflow:Done calling model_fn.
I0917 10:39:01.663023 140619878479680 estimator.py:1164] Done calling model_fn.
INFO:tensorflow:Starting evaluation at 2021-09-17T10:39:01
I0917 10:39:01.677987 140619878479680 evaluation.py:255] Starting evaluation at 2021-09-17T10:39:01
INFO:tensorflow:Graph was finalized.
I0917 10:39:01.719496 140619878479680 monitored_session.py:247] Graph was finalized.
2021-09-17 10:39:01.720056: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-09-17 10:39:01.720654: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1733] Found device 0 with properties: 
pciBusID: 0000:06:00.0 name: Tesla V100-SXM2-16GB computeCapability: 7.0
coreClock: 1.53GHz coreCount: 80 deviceMemorySize: 15.78GiB deviceMemoryBandwidth: 836.37GiB/s
2021-09-17 10:39:01.720807: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-09-17 10:39:01.721299: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-09-17 10:39:01.721717: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1871] Adding visible gpu devices: 0
2021-09-17 10:39:01.721774: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1258] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-09-17 10:39:01.721809: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1264]      0 
2021-09-17 10:39:01.721817: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1277] 0:   N 
2021-09-17 10:39:01.721951: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-09-17 10:39:01.722441: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-09-17 10:39:01.722877: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1418] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 14065 MB memory) -> physical GPU (device: 0, name: Tesla V100-SXM2-16GB, pci bus id: 0000:06:00.0, compute capability: 7.0)
INFO:tensorflow:Restoring parameters from /tmp/tmpz1q4nr2s/model.ckpt-1000
I0917 10:39:01.723034 140619878479680 saver.py:1298] Restoring parameters from /tmp/tmpz1q4nr2s/model.ckpt-1000
INFO:tensorflow:Running local_init_op.
I0917 10:39:01.753009 140619878479680 session_manager.py:531] Running local_init_op.
INFO:tensorflow:Done running local_init_op.
I0917 10:39:01.761684 140619878479680 session_manager.py:534] Done running local_init_op.
INFO:tensorflow:Inference Time : 0.19149s
I0917 10:39:01.869647 140619878479680 evaluation.py:273] Inference Time : 0.19149s
INFO:tensorflow:Finished evaluation at 2021-09-17-10:39:01
I0917 10:39:01.870736 140619878479680 evaluation.py:276] Finished evaluation at 2021-09-17-10:39:01
INFO:tensorflow:Saving dict for global step 1000: global_step = 1000, loss = 853.329, rmse = 4.2163596
I0917 10:39:01.870981 140619878479680 estimator.py:2066] Saving dict for global step 1000: global_step = 1000, loss = 853.329, rmse = 4.2163596
INFO:tensorflow:Saving 'checkpoint_path' summary for global step 1000: /tmp/tmpz1q4nr2s/model.ckpt-1000
I0917 10:39:01.936871 140619878479680 estimator.py:2127] Saving 'checkpoint_path' summary for global step 1000: /tmp/tmpz1q4nr2s/model.ckpt-1000

********************************************************************************
RMS error for the test set: $4216
           
5 分鐘将 TensorFlow 1 代碼轉換到 TensorFlow 2

從日志我們可以看到已經成功運作完畢并且儲存了 /tmp/tmpz1q4nr2s/model.ckpt-1000

注意!

在 TensorFlow v1 代碼向 TensorFlow v2 遷移的過程中也會遇到其他問題,例如:Tensorpack 目前沒有計劃支援 TensorFlow v2,與 Tensorpack 相關的 API 自然也無法通過 tf_upgrade_v2 來進行更新。

5 分鐘将 TensorFlow 1 代碼轉換到 TensorFlow 2

繼續閱讀