keras 层“sequential_23”的输入0与层不兼容:预期形状=(无,1797,8,8),找到的形状=(无,8,8)

jhiyze9q  于 2022-12-13  发布在  其他
关注(0)|答案(2)|浏览(142)

当我拟合我的模型时,有一个valueError:“层“sequential_41”的输入0与层不兼容:下面是我的代码。

from sklearn.datasets import load_digits
digits=load_digits()
digits.keys()
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(digits.images,digits.target)

model1=keras.Sequential([
    keras.layers.Conv2D(filters=32,kernel_size=(3,3),input_shape=(1347,8,8),activation='relu'),
    keras.layers.MaxPooling2D(2,2),

    keras.layers.Flatten(),
    keras.layers.Dense(50,activation='relu'),
    keras.layers.Dense(10,activation='sigmoid')

])
model1.compile(optimizer='SGD',
    loss='sparse_categorical_crossentropy',
             metrics=['accuracy'])

当我试图拟合我的模型时,我得到了一个错误

model1.fit(x_train,y_train,epochs=10)
7gcisfzg

7gcisfzg1#

使用输入形状(8, 8, 1)和softmax作为输出图层的激活函数。

from sklearn.datasets import load_digits
import tensorflow as tf

digits=load_digits()
digits.keys()
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(digits.images,digits.target)

model1=tf.keras.Sequential([
    tf.keras.layers.Conv2D(filters=32,kernel_size=(3,3),input_shape=(8, 8, 1),activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(50,activation='relu'),
    tf.keras.layers.Dense(10,activation='softmax')

])
model1.compile(optimizer='SGD',
    loss='sparse_categorical_crossentropy',
             metrics=['accuracy'])
model1.fit(x_train,y_train,epochs=10)
jbose2ul

jbose2ul2#

调整图像大小以匹配模型的预期大小:

img = cv2.resize(img,(240,240))

返回具有TF所需形状的图像:

img = img.reshape(1,240,240,3)

相关问题