如何在 tensorflow keras 中使用 CRF? [英] how to use CRF in tensorflow keras?

查看:22
本文介绍了如何在 tensorflow keras 中使用 CRF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码是这样的:

import tensorflow as tf
from keras_contrib.layers import CRF
from tensorflow import keras

def create_model(max_seq_len, adapter_size=64):
    """Creates a classification model."""

    # adapter_size = 64  # see - arXiv:1902.00751

    # create the bert layer
    with tf.io.gfile.GFile(bert_config_file, "r") as reader:
        bc = StockBertConfig.from_json_string(reader.read())
        bert_params = map_stock_config_to_params(bc)
        bert_params.adapter_size = adapter_size
        bert = BertModelLayer.from_params(bert_params, name="bert")

    input_ids = keras.layers.Input(shape=(max_seq_len,), dtype='int32', name="input_ids")
    # token_type_ids = keras.layers.Input(shape=(max_seq_len,), dtype='int32', name="token_type_ids")
    # output         = bert([input_ids, token_type_ids])
    bert_output = bert(input_ids)
    print("bert_output.shape: {}".format(bert_output.shape))  # (?, 100, 768)

    crf = CRF(len(tag2idx))
    logits = crf(bert_output)
    model = keras.Model(inputs=input_ids, outputs=logits)
    model.build(input_shape=(None, max_seq_len))

    # load the pre-trained model weights
    load_stock_weights(bert, bert_ckpt_file)

    # freeze weights if adapter-BERT is used
    if adapter_size is not None:
        freeze_bert_layers(bert)

    model.compile('adam', loss=crf.loss_function, metrics=[crf.accuracy])

    model.summary()

    return model

我正在使用 tensorflow keras 并使用 keras_contrib 包来做 NER.tensorflow keras 包似乎不适用于 keras_contrib 包.

I am using tensorflow keras and also use keras_contrib package, to do NER. it seems the tensorflow keras package does not work well with keras_contrib package.

Traceback 信息如下:

The Traceback information is listed below:

Traceback (most recent call last):
  File "F:/_gitclone3/bert_examples/bert_ner_example_eval.py", line 120, in <module>
    model = create_model(max_seq_len, adapter_size=adapter_size)
  File "F:/_gitclone3/bert_examples/bert_ner_example_eval.py", line 101, in create_model
    logits = crf(bert_output)
  File "C:\Users\yuexiang\Anaconda3\lib\site-packages\keras\engine\base_layer.py", line 443, in __call__
    previous_mask = _collect_previous_mask(inputs)
  File "C:\Users\yuexiang\Anaconda3\lib\site-packages\keras\engine\base_layer.py", line 1311, in _collect_previous_mask
    mask = node.output_masks[tensor_index]
AttributeError: 'Node' object has no attribute 'output_masks'

如何将 CRF 与 tensorflow keras 结合使用?

How do I use CRF with tensorflow keras?

推荐答案

我遇到了类似的问题,并花了很多时间试图让事情发挥作用.这是使用 python 3.6.5 对我有用的方法:

I run into a similar problem and spent a lot of time trying to get things to work. Here's what worked for me using python 3.6.5:

顺序:

pip install seqeval==0.0.5

Keras:

pip install keras==2.2.4

Keras-contrib (2.0.8):

Keras-contrib (2.0.8):

git clone https://www.github.com/keras-team/keras-contrib.git

cd keras-contrib

python setup.py install

TensorFlow:

TensorFlow:

pip install tensorflow==1.14.0

执行pip list 以确保您确实安装了这些版本(例如pip seqeval 可能会自动更新您的keras)

Do pip list to make sure you have actually installed those versions (eg pip seqeval may automatically update your keras)

然后在你的代码中像这样导入:

Then in your code import like so:

from keras.models import *
from keras.layers import LSTM, Embedding, Dense, TimeDistributed, Dropout, Bidirectional, Input
from keras_contrib.layers import CRF
#etc.

希望这会有所帮助,祝你好运!

Hope this helps, good luck!

这篇关于如何在 tensorflow keras 中使用 CRF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆