TypeError:传递给“ ConcatV2” Op的“值”的列表中的张量具有类型[bool,float32]均不匹配

时间:2019-12-05 11:03:06

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

我正在尝试使用我在此链接上找到的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.

有人可以给我一个解释吗?

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

此修复程序是在K.zeros_like中对掩码执行keras_contrib/layers/crf.py中的dtype,这是在此拉取请求中完成的: https://github.com/ashutoshsingh0223/keras-contrib/pull/1

或直接从 keras_contrib 主分支安装。

答案 2 :(得分:-1)

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