Tensorflow共通機能モジュール

tensorflow バージョンの更新により、一部のモジュールが 2.0 で削除または変更されたことを記録します。エラーを解決する方法を試し、tensorflow 1.0 関数を通常どおり使用してください。

呼び出し関連パッケージ
  • 1)RNN

tensorflowのライブラリとパッケージの呼び出し方を変えるだけでOKです

# 原1.0写法:
import tensorflow as tf
import tensorflow.contrib.rnn as rnn

# 现2.0需改成
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from tensorflow.python.ops import rnn
#rnn_cell包同样
#from tensorflow.python.ops import rnn_cell
  • 2)CRF

次のエラーが発生します。

from tensorflow.contrib.crf import crf_log_likelihood
(或者是调用此库"from tensorflow.contrib.crf import viterbi_decode"[解决方法同理])
ModuleNotFoundError: No module named 'tensorflow.contrib'

Tensorflow2.0 は tf.contrib を削除し、サードパーティのライブラリに転送します

# 原1.0写法:
from tensorflow.contrib.crf import crf_log_likelihood

# 现2.0需改成
from tensorflow_addons.text.crf import crf_log_likelihood
"""
对于tensorflow_addons的下载:pip install tensorflow-addons
ps:注意:tfa版本需要安装对应Tensorflow版本,若tf为最新,则直接上面那样pip ↑ 就可以啦
"""

CRP機能の理解、参考リンク

機能モジュール
  • 1) セッション機能

tensorflow ライブラリの呼び出し方法を変更するだけです

# 原1.0写法:
import tensorflow as tf
with tf.Session() as sess:
    print(sess.run(result))

# 现2.0需改成
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
with tf.Session() as sess:
    print(sess.run(result))

おすすめ

転載: blog.csdn.net/Fuziqp/article/details/119815749