检查输入时出错:预期density_85_input具有形状(6,),但数组的形状为(1,)

时间:2020-04-28 09:41:58

标签: python tensorflow keras

我有keras网络训练模型。不幸的是,我无法对它们使用预测功能。模型的创建是:

def create_model(self, item_id):
    self.model = Sequential()

    n_cols = self.train_X.shape[1]

    self.model.add(Dense(64, activation='relu', input_shape=(n_cols,)))
    self.model.add(Dense(32, activation='relu'))
    self.model.add(Dense(32, activation='sigmoid'))
    self.model.add(Dense(64, activation='sigmoid'))
    self.model.add(Dense(64, activation='sigmoid'))
    self.model.add(Dense(2, activation='softmax'))

    self.model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

    early_stopping_monitor = EarlyStopping(patience=3)
    self.model.fit(self.train_X, self.train_Y, validation_split=0.2, epochs=30, callbacks=[early_stopping_monitor])

    self.save_model(item_id)

预测函数如下所示

def valuate_criteria(self, item_id):
    if self.models[item_id] is not None:
        test_y_predictions = self.models[item_id].predict(self.test_X.to_numpy().shape[1])
        if test_y_predictions[0][1] >= 0.75:
            return True
        return False

我正在输入的数据是dataframe thal

     age  destination  sex  transport  criterion  weather
0    2    23           1    29         1          1

我在数据上使用相同的格式来训练和预测仍然出现此错误:

Error when checking input: expected dense_43_input to have shape (6,) but got array with shape (1,)

预先感谢您,我已经迷失了三天。

train_df这个项目是0还是1,这就是我要预测的

     age  destination  sex  transport  criterion  weather  item
0    2    23           1    29         1          1         0

这就是我从以前的数据框中获取训练数据的方式

self.train_X = self.train_df.drop(columns=['item'])
self.train_Y = to_categorical(self.train_df[['item']])

创建和教导模型正在运行的问题是当我对self.test_X中的数据使用预测函数时:

     age  destination  sex  transport  criterion  weather
0    2    23           1    29         1          1

更新: 我使用此问题Tensor is not an element of this graph; deploying Keras model

找到了解决方案

0 个答案:

没有答案