如果没有完整的代码,很难说哪里出了问题。正如警告消息所暗示的那样,模型是在形状的输入(无,256)上训练的,但是在预测您使用不同的输入时。请提供与用于训练模型的输入数据具有相同特征和维度的预测函数的数据。
我在下面提供了一个示例来模仿您所面临的错误。在这里,我在 8 列上训练了模型,但在预测期间我只使用了 1 列。
代码 -
%tensorflow_version 1.x
import tensorflow as tf
print(tf.__version__)
# MLP for Pima Indians Dataset saved to single file
import numpy as np
from numpy import loadtxt
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Input, Concatenate
# load pima indians dataset
dataset = np.loadtxt("/content/pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
input1 = Input(shape=(8,))
# define model
x1 = Dense(12, input_shape = (2,), activation='relu')(input1)
x = Dense(8, activation='relu')(x1)
x = Dense(1, activation='sigmoid')(x)
model = Model(inputs=input1, outputs=x)
# compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Model Summary
model.summary()
# Fit the model
model.fit(x=X, y=Y, epochs=150, batch_size=10, verbose=0)
# evaluate the model
scores = model.predict(dataset[:,0])
输出-
1.15.2
Model: "model_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_3 (InputLayer) [(None, 8)] 0
_________________________________________________________________
dense_6 (Dense) (None, 12) 108
_________________________________________________________________
dense_7 (Dense) (None, 8) 104
_________________________________________________________________
dense_8 (Dense) (None, 1) 9
=================================================================
Total params: 221
Trainable params: 221
Non-trainable params: 0
_________________________________________________________________
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-32d25cb68448> in <module>()
42
43 # evaluate the model
---> 44 scores = model.predict(dataset[:,0])
3 frames
/tensorflow-1.15.2/python3.6/tensorflow_core/python/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
570 ': expected ' + names[i] + ' to have shape ' +
571 str(shape) + ' but got array with shape ' +
--> 572 str(data_shape))
573 return data
574
ValueError: Error when checking input: expected input_3 to have shape (8,) but got array with shape (1,)