Nlp InvalidArgumentError:尝试访问包含24个元素的列表中的24个元素

Nlp InvalidArgumentError:尝试访问包含24个元素的列表中的24个元素,nlp,lstm,stanford-nlp,recurrent-neural-network,Nlp,Lstm,Stanford Nlp,Recurrent Neural Network,我在斯坦福情绪树银行尝试情绪分析,并自己制作了一个模型: modelRNN = Sequential() modelRNN.add(Embedding(len(weight_matrix), EMBEDDING_DIM, weights=[weight_matrix], input_length=max_words, trainable=False)) modelRNN.add(Bidirectional(LSTM(128, dropout=0.2, recurrent_dropout=0.2)

我在斯坦福情绪树银行尝试情绪分析,并自己制作了一个模型:

modelRNN = Sequential()
modelRNN.add(Embedding(len(weight_matrix), EMBEDDING_DIM, weights=[weight_matrix], input_length=max_words, trainable=False))
modelRNN.add(Bidirectional(LSTM(128, dropout=0.2, recurrent_dropout=0.2)))
modelRNN.add(Dense(512, activation='relu'))
modelRNN.add(Dropout(0.4))
modelRNN.add(Dense(10, activation='softmax'))
# try using different optimizers and different optimizer configs
modelRNN.compile(loss='categorical_crossentropy',optimizer='adam', metrics=['accuracy'])
print(modelRNN.summary())
我正在对我从amazon.in放弃的评论进行预测,并得到了一些错误,但在我们通过错误代码之前,让我们看看预测代码:

    weight_path = 'stanfordSentimentTreebank/best_modelRNN.hdf5'
    modelRNN.load_weights(weight_path)

    result = []

    for review in df_amazon['Reviews']:

    data_sample = review

    live_list = []
    live_list_np = np.zeros((len(review),1))
    # split the sentence into its words and remove any punctuations.
    tokenizer = RegexpTokenizer(r'\w+')
    data_sample_list = tokenizer.tokenize(data_sample)

    labels = np.array(['1','2','3','4','5','6','7','8','9','10'], dtype = "int")
    #word_idx['I']
    # get index for the live stage
    data_index = np.array([word_idx[word.lower()] if word.lower() in word_idx else 0 for word in data_sample_list])
    data_index_np = np.array(data_index)

    # padded with zeros of length 56 i.e maximum length
    padded_array = np.zeros(len(review)) # use the def maxSeqLen(training_data) function to detemine the padding length for your data
    padded_array[:data_index_np.shape[0]] = data_index_np
    data_index_np_pad = padded_array.astype(int)
    live_list.append(data_index_np_pad)
    live_list_np = np.asarray(live_list)
    type(live_list_np)

    # get score from the model
    score = modelRNN.predict(live_list_np, batch_size=1, verbose=0)
    #print (score)

    single_score = np.round(np.argmax(score)/10, decimals=2) # maximum of the array i.e single band
现在我有一个错误:

    InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-45-8e67d104a99c> in <module>()
     29 
     30     # get score from the model
---> 31     score = modelRNN.predict(live_list_np, batch_size=1, verbose=0)
     32     #print (score)


/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     58     ctx.ensure_initialized()
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:
     62     if name is not None:

InvalidArgumentError:  Trying to access element 24 in a list with 24 elements.
     [[{{node sequential_1/bidirectional_1/forward_lstm_1/while/body/_1/sequential_1/bidirectional_1/forward_lstm_1/while/TensorArrayV2Read/TensorListGetItem}}]] [Op:__inference_predict_function_4534]

Function call stack:
predict_function
InvalidArgumentError回溯(最近一次调用上次)
在()
29
30#从模型中获得分数
--->31分=modelRNN.predict(实时列表,批量大小=1,详细度=0)
32#打印(分数)
/快速执行中的usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py(op_name、num_output、input、attrs、ctx、name)
58 ctx.确保_已初始化()
59张量=pywrap\u tfe.tfe\u Py\u Execute(ctx.\u句柄、设备名称、操作名称、,
--->60个输入、属性、数量输出)
61除堆芯外,其他状态除外,如e:
62如果名称不是无:
InvalidArgumentError:尝试访问包含24个元素的列表中的24个元素。
[{{node sequential_1/双向_1/前向_lstm_1/while/body/_1/顺序_1/双向_1/前向_lstm_1/while/TensorArrayV2Read/TensorListGetItem}][Op:_推理_预测_4534]
函数调用堆栈:
预测函数

我得到了一个影响这次跑步的答案。我在modelRNN.predict()中做了一个更改,其中它有batch\u size属性。我更改了batch\u size=max\u len\u word,而不是batch\u size=1。