MaxPooling层的尺寸合适吗?

时间:2018-08-24 06:20:57

标签: python tensorflow machine-learning keras mnist

我正试图在tensorflow中创建一个模型来训练fashion_mnist数据集。我创建的第一层是conv2D层:

keras.layers.Conv2D(
    filters=6, 
    kernel_size=3, 
    strides=1, 
    padding='SAME', 
    input_shape=(28, 28, 1))

现在,我想在此之后添加一个MaxPooling层。由于我在conv层中使用了6个滤镜,因此conv层难道不是输出3D(2D图像和层数的额外维度)吗?在这种情况下我应该使用MaxPooling2D还是MaxPooling3D?

编辑:这是我编写的代码,它给出一个错误:Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (60000, 28, 28)

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

fashion_mnist = keras.datasets.fashion_mnist
(trainImg, trainLbl), (testImg, testLbl) = fashion_mnist.load_data()

classNames = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

trainImg = trainImg/255.0
trainLbl = trainLbl/255.0

model = keras.Sequential([
    keras.layers.Conv2D(activation=tf.nn.relu, filters=6, kernel_size=3, strides=1, padding='SAME', input_shape=(28, 28, 1)),
    keras.layers.MaxPooling2D(pool_size=2, strides=2, padding='SAME'),
    keras.layers.Conv2D(activation=tf.nn.relu, filters=12, kernel_size=9, strides=1, padding='SAME'),
    keras.layers.MaxPooling2D(pool_size=2, strides=2, padding='SAME'),
    keras.layers.Flatten(),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(10, activation=tf.nn.softmax),
])

model.compile(optimizer=tf.train.AdamOptimizer(),
             loss='sparse_categorical_crossentropy',
             metrics=['accuracy'])

model.fit(trainImg, trainLbl, epochs=10, batch_size=64)

1 个答案:

答案 0 :(得分:0)

根据我对您问题的理解。您可以使用MaxPooling2D。您可以使用类似的东西

model.add(keras.layers.Conv2D(filters=6, kernel_size=3, strides=1, padding='SAME', input_shape=(28, 28, 1)))
model.add(MaxPooling2D(pool_size=(2, 2)))

您可以根据自己的喜好更改pool_size。希望这会有所帮助