InvalidArgumentError索引[i,0] = x不在keras的[0,x]中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了InvalidArgumentError索引[i,0] = x不在keras的[0,x]中相关的知识,希望对你有一定的参考价值。

我有使用keras 1.2tensorflow 1.1的代码。我已经运行但有错误

import numpy as np
import keras
from keras import backend as K
from keras import initializers
from keras.models import Sequential, Model, load_model, save_model
from keras.layers.core import Dense, Lambda, Activation
from keras.layers import Embedding, Input, Dense, Multiply, Reshape, Flatten
from keras.optimizers import Adagrad, Adam, SGD, RMSprop
from keras.regularizers import l2

from sklearn.metrics import average_precision_score
from sklearn.metrics import  auc

def init_normal(shape, name=None):

    return initializers.lecun_uniform(seed=None)

def get_model(num_a, num_b, num_c, dim, regs=[0,0,0]):

    a = Input(shape=(1,), dtype='int32', name = 'a')
    b = Input(shape=(1,), dtype='int32', name = 'b')
    c = Input(shape=(1,), dtype='int32', name = 'c')



    Embedding_a = Embedding(input_dim = num_a, output_dim = dim,
                              embeddings_initializer='uniform', W_regularizer = l2(regs[0]), input_length=1)
    Embedding_b = Embedding(input_dim = num_b, output_dim = dim,
                              embeddings_initializer='uniform', W_regularizer = l2(regs[1]), input_length=1)   
    Embedding_c = Embedding(input_dim = num_c, output_dim = dim,
                              embeddings_initializer='uniform', W_regularizer = l2(regs[2]), input_length=1)  


    a_latent = Flatten()(Embedding_a(a))
    b_latent = Flatten()(Embedding_b(b))
    c_latent = Flatten()(Embedding_c(c))


    predict_vector = Multiply()([a_latent, b_latent, b_latent])
    prediction = Dense(1, activation='sigmoid', init='lecun_uniform', name = 'prediction')(predict_vector)



    model = Model(input=[a, b, c], output=prediction)

    return model

def evaluate_model(model, test_pos, test_neg):

    global _model
    global _test_pos
    global _test_neg
    _model = model
    _test_pos = test_pos
    _test_neg = test_neg
    print(_test_neg)


    a, b, c, labels = [],[],[],[]
    for item in _test_pos:

        a.append(item[0])
        b.append(item[1])
        c.append(item[2])
        labels.append(1)

    for item in _test_neg:

        a.append(item[0])
        b.append(item[1])
        c.append(item[2])
        labels.append(0)

    a = np.array(a)
    b = np.array(b)
    c = np.array(c)


    predictions = _model.predict([a, b, c], 
                             batch_size=100, verbose=0)
    return average_precision_score(labels, predictions), auc(labels, predictions)

model = get_model(4, 8, 12, 2, [0,0,0])
model.compile(optimizer=Adam(lr=0.001), loss='binary_crossentropy')


pos_test = [[0, 0, 2], [4, 8, 8], [2, 5, 4], [0, 0, 0]]
neg_test = [[3, 3, 2], [2, 1, 8], [1, 4, 1], [3, 3, 12]]


aupr, auc = evaluate_model(model, pos_test, neg_test)
print(aupr, auc)

但是,它给我错误:任何方法来解决它?

InvalidArgumentError (see above for traceback): indices[1,0] = 4 is not in [0, 4)
     [[Node: embedding_4/embedding_lookup = Gather[Tindices=DT_INT32, Tparams=DT_FLOAT, _class=["loc:@embedding_4/embeddings"], validate_indices=true, _device="/job:localhost/replica:0/task:0/cpu:0"](embedding_4/embeddings/read, _recv_a_1_0)]]
答案

问题是,你定义嵌入input_dim为4,8和12,而它应该是5,9,13。因为嵌入的input_dim应该是max_index + 1。它在Keras docs中也有明确提及:

词汇量的大小,即最大整数索引+ 1。

如何解决这个问题?

get_model方法更改为:

model = get_model(5, 9, 13, 2, [0, 0, 0])

或者将数据索引更改为:

pos_test = [[0, 0, 2], [3, 7, 7], [2, 5, 4], [0, 0, 0]]
neg_test = [[3, 3, 2], [2, 1, 7], [1, 4, 1], [3, 3, 11]]

以上是关于InvalidArgumentError索引[i,0] = x不在keras的[0,x]中的主要内容,如果未能解决你的问题,请参考以下文章

使用 Keras 训练时的 Tensorflow InvalidArgumentError(索引)

InvalidArgumentError:没有注册 OpKernel 来支持 Op 'CudnnRNN'

InvalidArgumentError:拟合模型时出错

InvalidArgumentError:输入必须是向量,得到形状:[]

Tensorflow 中带有 model.fit 的 InvalidArgumentError

InvalidArgumentError: input_1:0 已输入和提取