MachineLP的Github(欢迎follow):https://github.com/MachineLP
CRF的应用还是挺多的,像前期deeplab的语义分割、bilstm+crf做词性标注。
CRF简单的例子:
# coding=utf-8
import numpy as np
import tensorflow as tf
# 参数设置
num_examples = 10
num_words = 20
num_features = 100
num_tags = 5
# 构建随机特征
x = np.random.rand(num_examples, num_words, num_features).astype(np.float32)
# 构建随机tag
y = np.random.randint(
num_tags, size=[num_examples, num_words]).astype(np.int32)
# 获取样本句长向量(因为每一个样本可能包含不一样多的词),在这里统一设为 num_words - 1,真实情况下根据需要设置
sequence_lengths = np.full(num_examples, num_words - 1, dtype=np.int32)
# 训练,评估模型
with tf.Graph().as_default():
with tf.Session() as session:
x_t = tf.constant(x)
y_t = tf.constant(y)
sequence_lengths_t = tf.constant(sequence_lengths)
#