2021-05-12

Tensorflow2.+版本使用CRF

最近因为GPU原因从tf1.x转到tf2.x,许多方法都用不了了,其中就有CRF,特此记录一下
之前CRF一直都存在于tf1.x中的contrib中,不过tf2.x连contrib都没了,CRF也转到了tensorflow_addons下

调用方式

import tensorflow_addons或者pip list查看一下
没有的话就pip安装即可:
pip install tensorflow_addons

安装好了直接调用即可:

import tensorflow as tf
import tensorflow_addons as tfa

# log_likelikelihood,transition_params = tf.contrib.crf.crf_log_likehood(input,tags,seqlen)
# predict, viterbi_score = tf.contrib.crf.crf_decode(input, transition_params, seqlen)

log_likelikelihood,transition_params = tf.text.crf.crf_log_likehood(input,tags,seqlen)
predict, viterbi_score = tfa.text.crf.crf_decode(input,transition_params,seqlen)

现在应该可以成功调用了
此外,如果还有其他方法在tf2中没法用了,可以尝试tf.compat.v1的方式来调用该方法

例如:
tf2中没法调用tf.gfile.Exists()方法,但是可以通过:tf.compat.v1.gfile.Exists()来使用
tf.compat.v1模块是确保高版本的TF支持低版本的TF的,还是建议转换为TF2.0中的新方法:
tf.gfile.Exists()方法在TF2.0中为: tf.io.gfile.exists()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值