【问题标题】:Replacing MLP code with CNN in python using tensorflow and keras使用tensorflow和keras在python中用CNN替换MLP代码
【发布时间】:2020-11-06 05:51:05
【问题描述】:

我用 MLP 尝试了下面的代码,现在我需要用具有以下结构的 CNN 8 层替换这个 MLP 代码:3×3×32 Convolutional → 3×3×64 Convolutional → 2×2 MaxPool → Dropout → Flatten → 1×128全连接→Dropout→128×10全连接→Softmax。

//declear path to your mnist data folder
img_path = 'c:/kaggleMNISTdata/trainingSet/trainingSet'
 //get the path list using the path object
image_paths = list(paths.list_images(img_path))
//apply our function
image_list, label_list = load(image_paths, verbose=10000)
// binarize the labels
lb = LabelBinarizer()
label_list = lb.fit_transform(label_list)
// split data into training and test set
X_train, X_test, y_train, y_test = train_test_split(image_list,
                                                    label_list,
                                                    test_size=0.1,
                                                    random_state=42)
data = list(zip(image_list, label_list))
random.shuffle(data)
class SimpleMLP:
    @staticmethod
    def build(shape, classes):
        model = Sequential()
        model.add(Dense(200, input_shape=(shape,)))
        model.add(Activation("relu"))
        model.add(Dense(200))
        model.add(Activation("relu"))
        model.add(Dense(classes))
        model.add(Activation("softmax"))
        return model

我试过了

def build(shape, classes):
        model = Sequential()
        model.add(Conv2D(200, input_shape=(shape,)))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Flatten(1))
        model.add(Dropout(1))
        model.add(Dense(200))
        model.add(Activation("softmax"))
        return model

对吗?

【问题讨论】:

  • 关于你的结构:你确定你必须在网络的开头使用两个没有中间 Max-Pooling 层的卷积层吗?您想在卷积层和全连接层中使用哪些激活函数?
  • 是的,结构应该是这样的,我将使用 softmax 激活函数。你能帮忙
  • 看我的回答..

标签: tensorflow keras neural-network conv-neural-network mlp


【解决方案1】:

用你给定的架构试试这个:

cnn_model = Sequential()
cnn_model.add(Conv2D(32, (3, 3), input_shape=(28, 28, 1)))
cnn_model.add(Conv2D(64, (3, 3)))
cnn_model.add(MaxPooling2D((2, 2)))
cnn_model.add(Dropout(0.5))
cnn_model.add(Flatten())
cnn_model.add(Dense(128))
cnn_model.add(Dropout(0.5))
cnn_model.add(Dense(classes))
cnn_model.add(Activation("softmax"))

【讨论】:

  • 我正在关注本教程ichi.pro/fr/… 它给出了错误
  • FFile ".\Updatedcnn.py", line 894, in build global_model = smlp_global.build(784, 10) File ".\Updatedcnn.py", line 894, in build model.add( Conv2D(32, (3, 3), input_shape=(shape,))) 文件“C:\Users\Anaconda3\lib\site-packages\tensorflow\python\training\tracking\base.py”,第 457 行,在_method_wrapper 结果 = 方法(self, *args, **kwargs)
  • 文件 "C:\Users\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\input_spec.py",第 191 行,在 assert_input_compatibility raise ValueError('Input ' + str( input_index) + ' of layer ' + ValueError: 层 conv2d 的输入 0 与层不兼容: : 预期 min_ndim=4, 发现 ndim=2. 收到完整形状: [None, 784]
  • 你怎么调用build-方法,尤其是你的shape参数是什么?
  • 好的,所以我们有两个模型:globallocal。对于您的全局模型(第一层是 Dense),您可以使用带有 input_shape=(784,) 的静态构建方法。对于您当地的人,请参阅我编辑的答案。
猜你喜欢
  • 2020-05-12
  • 2019-07-02
  • 2018-04-20
  • 2017-01-30
  • 1970-01-01
  • 2019-03-02
  • 1970-01-01
  • 2022-01-02
  • 1970-01-01
相关资源
最近更新 更多