【问题标题】:TypeError: Tensors in list passed to 'values' of 'ConcatV2' Op have types [bool, float32] that don't all matchTypeError:传递给“ConcatV2”操作的“值”的列表中的张量具有不完全匹配的类型 [bool,float32]
【发布时间】:2020-03-30 07:14:11
【问题描述】:

我正在尝试使用我在此链接上找到的 LSTM 复制用于实体识别的笔记本: https://medium.com/@rohit.sharma_7010/a-complete-tutorial-for-named-entity-recognition-and-extraction-in-natural-language-processing-71322b6fb090

当我尝试训练模型时,我得到一个我无法理解的错误(我对 tensorflow 很陌生)。特别是有错误的代码部分是这个:

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

# Model definition
input = Input(shape=(MAX_LEN,))
model = Embedding(input_dim=n_words+2, output_dim=EMBEDDING, # n_words + 2 (PAD & UNK)
                  input_length=MAX_LEN, mask_zero=True)(input)  # default: 20-dim embedding
model = Bidirectional(LSTM(units=50, return_sequences=True,
                           recurrent_dropout=0.1))(model)  # variational biLSTM
model = TimeDistributed(Dense(50, activation="relu"))(model)  # a dense layer as suggested by neuralNer
crf = CRF(n_tags+1)  # CRF layer, n_tags+1(PAD)
print(model)
out = crf(model)  # output

model = Model(input, out)
model.compile(optimizer="rmsprop", loss=crf.loss_function, metrics=[crf.accuracy])

model.summary()

错误就行了

out = crf(model)

我得到的错误是这样的:

TypeError: Tensors in list passed to 'values' of 'ConcatV2' Op have types [bool, float32] that don't all match.

谁能给我解释一下?

【问题讨论】:

    标签: python tensorflow keras lstm named-entity-recognition


    【解决方案1】:

    我今天也遇到了这个问题。对我有用的是从嵌入层中删除mask_zero=True。不幸的是,我不知道这有什么帮助。

    【讨论】:

    • 我发现这是新版keras的问题。我安装的是2.2.4版本,但是最新版本不知道怎么解决
    • 我使用的是 keras 版本 2.3.1。删除掩码标志后,您的代码对我有用
    • 这对我也有用,但如果你现在已经发现它,我真的很想知道它为什么有效。
    • 这里有同样的问题,通过删除 mask_zero 解决了。但不确定为什么。谁能帮忙解答一下?
    【解决方案2】:

    当您在 keras_contrib crf 层的嵌入层中使用掩码时会出现问题: https://github.com/keras-team/keras-contrib/issues/498

    修复方法是在 K.zeros_like 中强制使用 dtype 以用于 keras_contrib/layers/crf.py 中的掩码,就像在此拉取请求中所做的那样: https://github.com/ashutoshsingh0223/keras-contrib/pull/1

    或直接从 keras_contrib 主分支安装。

    【讨论】:

      【解决方案3】:

      因为keras_contrib CRF不支持masking,所以需要关闭mask_zero=False

      【讨论】:

      • 欢迎来到 SO。请提供一个实际的例子。
      猜你喜欢
      • 2020-04-01
      • 2022-11-30
      • 2020-11-21
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      • 2021-11-19
      相关资源
      最近更新 更多