我有一个数据集,由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)
回答:
可以尝试为你的数据样本增加一个维度,然后将你的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)