numpy 我打算2维内核,但层3维内核

5w9g7ksd  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(102)

我尝试使用mnist模型进行分类,因此更改数字图像的形状,如(pic_num,size,size,?(我不知道此维度的含义),并使用“categorical_entropy”的损失函数,将一维标签数据更改为单热标签数据

model=Sequential()
model.add(Conv2D(32,kernel_size=(3,3),input_shape=(28,28,1),activation='relu'))
model.add(Conv2D(64,(3,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10,activation='softmax'))

model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

字符串
这是我的模型和编译器。
当我写这段代码时:
'''

history=model.fit(x_train,y_train,validation_split=0.25,epochs=30,
batch_size=200,verbose=0,callbacks=[early_stopping_callback,checkpointer])


错误代码:

~ in categorical_crossentropy
        target.shape.assert_is_compatible_with(output.shape)
 Shapes (200, 10, 2) and (200, 10) are incompatible


我试图改变flatten层的位置,改变训练数据和标签的形状,但这一切都不起作用。

9avjhtql

9avjhtql1#

您的代码中有几处更正。您可以将此代码用于MNIST:

model = keras.Sequential(
    [
        keras.Input(shape=input_shape),
        layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),
        layers.MaxPooling2D(pool_size=(2, 2)),
        layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
        layers.MaxPooling2D(pool_size=(2, 2)),
        layers.Flatten(),
        layers.Dropout(0.5),
        layers.Dense(num_classes, activation="softmax"),
    ]
)

model.summary()

字符串
输出量:

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 26, 26, 32)        320       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 13, 13, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 11, 11, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 5, 5, 64)          0         
_________________________________________________________________
flatten (Flatten)            (None, 1600)              0         
_________________________________________________________________
dropout (Dropout)            (None, 1600)              0         
_________________________________________________________________
dense (Dense)                (None, 10)                16010     
=================================================================
Total params: 34,826
Trainable params: 34,826
Non-trainable params: 0
____________________________________
batch_size = 128
epochs = 15

model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])

model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)

score = model.evaluate(x_test, y_test, verbose=0)
print("Test loss:", score[0])
print("Test accuracy:", score[1])

的数据
最终输出:
测试损耗:0.023950600996613503
测试精度:0.9922000169754028

相关问题