Python 3.x 由于负索引查找,Keras嵌入层中的InvalidArgumentError无效
我在尝试培训在Keras中实施的深度学习模型时收到了一份Python 3.x 由于负索引查找,Keras嵌入层中的InvalidArgumentError无效,python-3.x,tensorflow,keras,Python 3.x,Tensorflow,Keras,我在尝试培训在Keras中实施的深度学习模型时收到了一份InvalidArgumeInterror。我在Keras和TensorFlow中搜索过类似的问题,但是由于找不到索引,我的错误消息似乎不寻常。下面是错误消息 tensorflow.python.framework.errors\u impl.无效arguminterror:索引[427,9]=-2147483648不在[038545]中 [[Node:time\u distributed\u 1/Gather=Gather[Tindice
InvalidArgumeInterror
。我在Keras和TensorFlow中搜索过类似的问题,但是由于找不到索引,我的错误消息似乎不寻常。下面是错误消息
tensorflow.python.framework.errors\u impl.无效arguminterror:索引[427,9]=-2147483648不在[038545]中
[[Node:time\u distributed\u 1/Gather=Gather[Tindices=DT\u INT32,Tparams=DT\u FLOAT,validate\u Indexes=true,\u device=“/job:localhost/replica:0/task:0/device:CPU:0”](嵌入/读取,time\u distributed\u 1/Cast)]]
我使用的是Python 3.5.2,TensorFlow版本是1.4.1,Keras版本是2.1.5
正如您所注意到的,不仅要查找的索引是负数,它实际上等于-2^31。(即,最低32位有符号整数值)
下面是我用来准备模型的代码
import numpy
from keras.layers import Embedding, Bidirectional, LSTM, TimeDistributed
from keras_contrib.layers import CRF
# Form embedding layer's weight matrix
V = len(word_to_index) + 1 # V = 38545
embedding_weights = numpy.zeros((V, N))
for word, index in word_to_index.items():
embedding_weights[index, :] = word_vec_dict[word]
embedding_layer = Embedding(V, N,
weights=[embedding_weights], mask_zero=True)
model = Sequential()
model.add(TimeDistributed(embedding_layer,
input_shape=(C, U)))
model.add(TimeDistributed(Bidirectional(LSTM(M // 2, return_sequences=True))))
model.add(TimeDistributed(GlobalMaxPooling1D()))
model.add(Bidirectional(LSTM(H // 2, return_sequences = True), merge_mode='concat'))
crf = CRF(num_tags, sparse_target=True)
model.add(crf)
model.compile('adam', loss = crf.loss_function, metrics=[crf.accuracy])
馈送到此模型的数据具有维度(C,U,N)
,类型为int
(即不包括批量维度B
)简单地说,一批样本中的每个样本都是长度C
的对话。每个对话由固定长度U
的话语组成。最后,每个话语都由N
正指数组成。(即词汇表中相关词的指数)
我甚至使用simple for循环检查了整个数据集(在转换为索引后),没有找到超出范围的任何索引值
[038545)
。为什么在培训期间会出现-2^31
的索引循环?我最终解决了这个问题。我在培训模型时使用了批生成,并且在批生成器函数中未初始化输入数组的一部分
我不清楚为什么要查找的索引是-2147483648,确切地说。但是,我认为,由于数组的未初始化部分包含的值大于词汇表大小,甚至大于32位整数的边界,因此导致了未定义的行为
在我正确地初始化了整个批输入之后,问题就解决了。下面是我使用的批生成器函数的简化版本。添加的初始化部分对此有一个注释,以突出上面所述的内容
def batch_generator(dataset_x, dataset_y, tag_indices, mini_batch_list, C, U,
num_tags, word_index_to_append, tag_index_to_append):
num_mini_batches = len(mini_batch_list)
index_list = [x for x in range(num_mini_batches)]
random.shuffle(index_list)
k = -1
while True:
k = (k + 1) % len(index_list)
index = index_list[k]
conversation_indices = mini_batch_list[index]
num_conversations = len(conversation_indices)
batch_features = numpy.empty(shape = (num_conversations, C, U),
dtype = int)
label_list = []
for i in range(num_conversations):
utterances = dataset_x[conversation_indices[i]]
labels = copy.deepcopy(dataset_y[conversation_indices[i]])
num_utterances = len(utterances)
num_labels_to_append = max(0, C - len(labels))
labels += [tag_index_to_append] * num_labels_to_append
tags = to_categorical(labels, num_tags)
del labels
for j in range(num_utterances):
utterance = copy.deepcopy(utterances[j])
num_to_append = max(0, U - len(utterance))
if num_to_append > 0:
appendage = [word_index_to_append] * num_to_append
utterance += appendage
batch_features[i][j] = utterance
# ADDING THE TWO LINES BELOW SOLVED THE ISSUE
remaining_space = (C - num_utterances, U)
batch_features[i][num_utterances:] = numpy.ones(remaining_space) *\
word_index_to_append
label_list.append(tags)
batch_labels = numpy.array(label_list)
yield batch_features, batch_labels
@DanielMöller如我的问题所述,我使用for循环对数据进行迭代,检查了整个数据集是否存在超出范围的值。没有任何值。此外,不仅如此,我还将数据打印到文本文件中,并使用文本编辑器搜索减号字符
-
。找不到任何值。38545大于最大值int 16值。你确定所有的模型层、数据数组等都是int 32吗?如果有东西溢出,Keras中的下限值通常会出现…@DanielMöller我通过填充大小为(B,C,U)
的初始空numpy
数组来形成训练验证和测试批,如下所示:numpy.empty(shape=(B,C,U),dtype=int)
。它的dtype
是int64
。至于模型的其余部分,我不确定如何执行int64
,因为没有任何参数。此外,错误语句似乎提到错误具体发生在模型的第一层。