如何调试“ ValueError:检查输入时出错”

时间:2019-04-30 00:41:06

标签: keras python-3.6

我使用keras训练了一个模型,现在我想从熊猫数据框中的每一行预测值。我不知道输入的形状在哪里发生变化,它一直一直到预测步骤。

我想比较训练后的权重前后神经网络的性能。我已经节省了体重

PS:我松散地遵循this tutorial

中的步骤

创建NN:

def create_model(sample_input): # numpy array so we can use .shape
    model = tf.keras.Sequential([
        layers.Dense(64, activation='relu', 
            input_shape=sample_input.shape),
        layers.Dense(64, activation='relu'),
        layers.Dense(9, activation='softmax')
    ])

    model.compile(optimizer=tf.train.RMSPropOptimizer(0.01),
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

    return model

预测:

right = 0
wrong = 0
for index, row in X_test.iterrows():
    print(row.values.shape)
    prediction = model.predict(row.values)
    if prediction == y_test.values[index]:
        right += 1
    else:
        wrong +=1
print("Correct prediction = ", right)
print("Wrong prediction = ", wrong)

但是,我得到以下输出(注意:,它在第一次迭代中中断,请注意,当我打印要预测的样本形状时,它与Keras抱怨的预期输入匹配):< / p>

(11,)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-30-7b432180b212> in <module>
      3 for index, row in X_test.iterrows():
      4     print(row.values.shape)
----> 5     prediction = model.predict(row.values)
      6     if prediction == y_test.values[index]:
      7         right += 1

~/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in predict(self, x, batch_size, verbose, steps, max_queue_size, workers, use_multiprocessing)
   1094       # batch size.
   1095       x, _, _ = self._standardize_user_data(
-> 1096           x, check_steps=True, steps_name='steps', steps=steps)
   1097 
   1098     if (self.run_eagerly or (isinstance(x, iterator_ops.EagerIterator) and

~/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split, shuffle)
   2380         feed_input_shapes,
   2381         check_batch_axis=False,  # Don't enforce the batch size.
-> 2382         exception_prefix='input')
   2383 
   2384     if y is not None:

~/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    360                 'Error when checking ' + exception_prefix + ': expected ' +
    361                 names[i] + ' to have shape ' + str(shape) +
--> 362                 ' but got array with shape ' + str(data_shape))
    363   return data
    364 

ValueError: Error when checking input: expected dense_9_input to have shape (11,) but got array with shape (1,)

我尝试像model.predict([row.values])那样包装它,以为在某些时候keras访问了内部元素,但没有运气,同样的问题。

我希望模型能够预测某些内容,即使它是错误的。

0 个答案:

没有答案