在Keras模型中使用新数据进行预测时出现错误

我有一个数据集,由432个批次组成,每个批次包含24个点。整个数据集的形状为:(432, 24)

举个例子,这是一个批次:

array([917,  15, 829,  87, 693,  71, 627, 359, 770, 303, 667, 367, 754,       359, 532,  39, 683, 407, 333, 551, 516,  31, 675,  39])

形状为 (24,)

我用这些信息来训练Keras模型,没有问题。当我尝试用相同形状(24,)的新数据进行预测时:

array([176,  71, 152,  63, 200,  71, 120,  87, 128,  87, 216, 103, 248,       126, 144, 150, 128, 206, 192, 206, 112, 277, 216, 269])

我的模型如下:

  model = keras.Sequential([        keras.layers.Flatten(batch_input_shape=(None,24)),        keras.layers.Dense(64, activation=tf.nn.relu),        keras.layers.Dense(2, activation=tf.nn.sigmoid),    ])  model.compile(optimizer='adam',                loss=tf.losses.categorical_crossentropy,                metrics=['accuracy'])

引发的错误是:

ValueError: Input 0 of layer dense_24 is incompatible with the layer: expected axis -1 of input shape to have value 24 but received input with shape (None, 1)

enter image description here


回答:

可以尝试为你的数据样本增加一个维度,然后将你的new_data输入模型进行预测:

import numpy as npnew_data= np.array([176,  71, 152,  63, 200,  71, 120,  87, 128,  87, 216, 103, 248,       126, 144, 150, 128, 206, 192, 206, 112, 277, 216, 269])new_data= np.expand_dims(new_data, axis=0)prediction = model.predict(new_data)print(prediction)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注