如何解决在pyhton中运行keras模型时出现ValueError
我正在尝试在 python 中运行下面提到的 Keras tutorial:
#Import Libraries
from keras.models import Sequential
from keras.layers import Dense,Conv2D,MaxPool2D,Flatten
from keras.optimizers import SGD
#model details
vgg19 = Sequential()
vgg19.add(Conv2D(input_shape=(224,224,3),filters=64,kernel_size=(3,padding="same",activation="relu"))
vgg19.add(Conv2D(filters=64,activation="relu"))
vgg19.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))
vgg19.add(Conv2D(filters=128,activation="relu"))
vgg19.add(Conv2D(filters=128,2)))
vgg19.add(Conv2D(filters=256,activation="relu"))
vgg19.add(Conv2D(filters=256,2)))
vgg19.add(Conv2D(filters=512,activation="relu"))
vgg19.add(Conv2D(filters=512,2)))
vgg19.add(Flatten())
vgg19.add(Dense(units=4096,activation="relu"))
vgg19.add(Dense(units=4096,activation="relu"))
vgg19.add(Dense(units=10,activation="softmax"))
#Preparing Dataset
from keras.datasets import cifar10
from keras.utils import to_categorical
(X,Y),(tsX,tsY) = cifar10.load_data()
# Use a one-hot-encoding
Y = to_categorical(Y)
tsY = to_categorical(tsY)
# Change datatype to float
X = X.astype('float32')
tsX = tsX.astype('float32')
# Scale X and tsX so each entry is between 0 and 1
X = X / 255.0
tsX = tsX / 255.0
#training
optimizer = SGD(lr=0.001,momentum=0.9)
vgg19.compile(optimizer=optimizer,loss='categorical_crossentropy',metrics=['accuracy'])
history = vgg19.fit(X,Y,epochs=100,batch_size=64,validation_data=(tsX,tsY),verbose=0)
在训练模型后,我得到了下面提到的 value error
:
ValueError: Input 0 of layer dense_9 is incompatible with the layer: expected axis -1 of input shape to have value 25088 but received input with shape (None,512)
请提出建议,如何修复输入形状,如果有人能提供对该问题的简要说明,那就更好了。 提前致谢!
解决方法
您可以使用 LitElement
检查 X 的形状。
它清楚地表明 X 的形状是 (50000,32,3)
所以你的第一层应该是这样的:
X.shape
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。