错误:valueError:输入数组的样本数应与目标数组相同。找到1个输入样本和0个目标样本

时间:2019-02-21 12:03:03

标签: python keras neural-network nlp

我正在尝试进行系统调用分类的任务。下面的代码是受文本分类项目启发的。我的系统调用表示为1到340之间的整数序列。我得到的错误是:

**valueError: input arrays should have the same number of samples as target arrays. Find 1 input samples and 0 target samples**. 我不知道该怎么办,因为这是我第一次 预先谢谢你

`

       df = pd.read_csv("data.txt") 
       df_test = pd.read_csv("validation.txt")
      #split arrays into train and test data (cross validation)
        train_text, test_text, train_y, test_y = train_test_split(df,df,test_size = 0.2)
    MAX_NB_WORDS = 5700
   # get the raw text data
    texts_train = train_text.astype(str)
    texts_test = test_text.astype(str)
    # finally, vectorize the text samples into a 2D integer tensor
   tokenizer = Tokenizer(nb_words=MAX_NB_WORDS, char_level=False)
   tokenizer.fit_on_texts(texts_train)
   sequences = tokenizer.texts_to_sequences(texts_train)
   sequences_test = tokenizer.texts_to_sequences(texts_test)

  word_index = tokenizer.word_index
  type(tokenizer.word_index), len(tokenizer.word_index)
  index_to_word = dict((i, w) for w, i in tokenizer.word_index.items()) 
 " ".join([index_to_word[i] for i in sequences[0]])
  seq_lens = [len(s) for s in sequences]

  MAX_SEQUENCE_LENGTH = 100
 # pad sequences with 0s
 x_train = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH) 
 x_test = pad_sequences(sequences_test, maxlen=MAX_SEQUENCE_LENGTH)
 #print('Shape of data train:', x_train.shape)  #cela a donnée (1,100)
 #print('Shape of data test tensor:', x_test.shape)
 y_train = train_y
 y_test = test_y
 print('Shape of label tensor:', y_train.shape)
 EMBEDDING_DIM = 32
 N_CLASSES = 2

y_train = keras.utils.to_categorical( y_train , N_CLASSES )
sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='float32')

embedding_layer = Embedding(MAX_NB_WORDS, EMBEDDING_DIM,
                        input_length=MAX_SEQUENCE_LENGTH,
                        trainable=True)
embedded_sequences = embedding_layer(sequence_input)

average = GlobalAveragePooling1D()(embedded_sequences)
 predictions = Dense(N_CLASSES, activation='softmax')(average)

 model = Model(sequence_input, predictions)
 model.compile(loss='categorical_crossentropy',
          optimizer='adam', metrics=['acc'])
 model.fit(x_train, y_train, validation_split=0.1,
      nb_epoch=10, batch_size=1)
  output_test = model.predict(x_test)
  print("test auc:", roc_auc_score(y_test,output_test[:,1]))

`

1 个答案:

答案 0 :(得分:1)

该错误表明:

x_train.shape[0] != y_train.shape[0]

您需要检查数据准备过程,并确保要传递给fit函数的数据数组的第一维相同。换句话说,输入数组应具有与目标数组相同数量的样本。