tensorflow中 bilstm+crf实现代码总结

正看这方面的资料,网上资源多多,而且质量很高,总结放在此处:

Implementing Bi-directional LSTM-CRF Network
http://stackoverflow.com/questions/33078423/implementing-bi-directional-lstm-crf-network
https://www.reddit.com/r/MachineLearning/comments/3oovqh/bidirectional_lstm_with_crf/

CRF和LSTM 模型在序列标注上的优劣?
https://www.zhihu.com/question/46688107?sort=created

TensorFlow中.crf_log_likelihood()怎么用,越具体越好?
https://www.zhihu.com/question/57666556

TensorFlow (RNN)深度学习 双向LSTM(BiLSTM)+CRF 实现 sequence labeling 序列标注问题 源码下载
http://blog.csdn.net/scotfield_msn/article/details/60339415

  • 4
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import tensorflow as tf from tensorflow.keras import layers, Sequential from tensorflow.keras.preprocessing.sequence import pad_sequences # 构建BiLSTM-CRF模型 class BiLSTMCRF(tf.keras.Model): def __init__(self, vocab_size, tag_size, embedding_dim, units): super(BiLSTMCRF, self).__init__() self.embedding = layers.Embedding(vocab_size, embedding_dim, mask_zero=True) self.lstm = layers.Bidirectional(layers.LSTM(units, return_sequences=True)) self.dense = layers.Dense(tag_size) self.crf = CRF(tag_size) def call(self, inputs, training=False): x = self.embedding(inputs) x = self.lstm(x) x = self.dense(x) outputs = self.crf(x) return outputs # 定义CRF层 class CRF(layers.Layer): def __init__(self, units): super(CRF, self).__init__() self.units = units def build(self, input_shape): self.transition_params = self.add_weight("transition_params", shape=[self.units, self.units]) def call(self, inputs, sequence_lengths=None, training=None): if training is None: training = self.trainable if training: log_likelihood, self.transition_params = tf.contrib.crf.crf_log_likelihood(inputs, tag_indices, sequence_lengths) else: log_likelihood, _ = tf.contrib.crf.crf_log_likelihood(inputs, tag_indices, sequence_lengths, self.transition_params) return log_likelihood # 构建数据 vocab = {'apple': 0, 'orange': 1, 'banana': 2} tag = {'B': 0, 'I': 1, 'O': 2} x = [[vocab['apple']], [vocab['orange']], [vocab['banana'], vocab['orange']]] y = [[tag['B']], [tag['I']], [tag['B'], tag['I']]] x = pad_sequences(x, padding='post') y = pad_sequences(y, padding='post') # 编码标签 decoded_y = tf.keras.utils.to_categorical(y, num_classes=len(tag)) # 定义模型 model = BiLSTMCRF(vocab_size=len(vocab), tag_size=len(tag), embedding_dim=64, units=100) # 编译模型 model.compile(optimizer='adam', loss=model.crf, metrics=[model.crf]) # 训练模型 model.fit(x, decoded_y, batch_size=32, epochs=10, validation_split=0.2)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值