首页 > 解决方案 > tensorflow.python.framework.errors_impl.InvalidArgumentError:使用输入昏暗2的索引超出范围;输入只有 2 个暗淡

问题描述

我使用 BiLSTM+CRF 在 Keras 中编写代码来进行命名实体识别。这是我的模型:

model = Sequential()
model.add(Embedding(1000, output_dim=128, 
input_length=100))
model.add(Bidirectional(LSTM(200, return_sequences=True)))
model.add(Dropout(0.3))
model.add(TimeDistributed(Dense(2)))
crf_layer = CRF(2)
model.add(crf_layer)
model.compile('rmsprop', loss=crf_layer.loss_function, metrics=[crf_layer.accuracy])
return model

和测试数据&标签

docs = ['Well done!',
        'Good work',
        'Great effort',
        'nice work',
        'Excellent!',
        'Weak',
        'Poor effort!',
        'not good',
        'poor work',
        'Could have done better.']
labels = [1,1,1,1,1,0,0,0,0,0]
#embedding
vocab_size = 2500
encoded_docs = [one_hot(d, vocab_size) for d in docs]
print(encoded_docs)

max_length = 100
padded_docs = pad_sequences(encoded_docs, maxlen=max_length, padding='post')
print(padded_docs)

model = build_embedding_bilstm2_crf_model()

print(model.summary())

model.fit(padded_docs, labels, epochs=50, verbose=0)

我收到以下错误。

tensorflow.python.framework.errors_impl.InvalidArgumentError: Index out of range using input dim 2; input has only 2 dims
     [[Node: loss/crf_1_loss/strided_slice_4 = StridedSlice[Index=DT_INT32, T=DT_FLOAT, begin_mask=7, ellipsis_mask=0, end_mask=5, new_axis_mask=0, shrink_axis_mask=0, _device="/job:localhost/replica:0/task:0/device:GPU:0"](_arg_crf_1_target_0_1/_81, metrics/acc/strided_slice_9/stack, metrics/acc/strided_slice_2/stack_1, metrics/acc/strided_slice_9/stack_2)]]
     [[Node: metrics/acc/while_1/Switch_2/_181 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_1763_metrics/acc/while_1/Switch_2", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](^_cloopmetrics/acc/while_1/ExpandDims/dim/_65)]]

使用 One_hot 将单词转换为向量。使用 * 生成新序列。我猜是神经网络的结构有问题。为什么会这样?

标签: pythontensorflowkeras

解决方案


推荐阅读