我收到了 InvalidArgumentError
同时尝试训练在 Keras 中实现的深度学习模型。我在 Keras 和 TensorFlow 中搜索过类似的问题,但是由于找不到索引,我的错误消息似乎不寻常。下面是错误信息。
tensorflow.python.framework.errors_impl.InvalidArgumentError: indices[427,9] = -2147483648 is not in [0, 38545) [[Node: time_distributed_1/Gather = Gather[Tindices=DT_INT32, Tparams=DT_FLOAT, validate_indices=true, _device="/job:localhost/replica:0/task:0/device:CPU:0"](embeddings/read, time_distributed_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
组成。正指数。 (即词汇表中相关词的索引)我什至使用简单的 for 循环检查了整个数据集(在将其转换为索引之后),但在
[0, 38545)
范围之外找不到任何索引值。 .为什么这样的索引循环为 -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
关于python-3.x - 由于负索引查找,Keras 嵌入层中的 InvalidArgumentError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50100729/