如何为MNIST手写数据集修复lstm和cnn代码

时间:2019-08-13 05:23:32

标签: tensorflow keras lstm

我正在用一个代码为MNIST手写数据集编码LSTM + CNN,如何解决尺寸问题?

我分别为MNIST手写数据集编码LSTM和CNN,但是合并存在问题

import tensorflow as tf
from tensorflow.keras import datasets, layers, models
from tensorflow.keras.layers import Dense, Dropout, LSTM

################### Loading dataset ##########################
mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.reshape((60000, 28, 28, 1))
x_test = x_test.reshape((10000, 28, 28, 1))

################### Normalizing dataset ######################
x_train, x_test = x_train / 255.0, x_test / 255.0

################### Building a model #########################
ConvNN_model = models.Sequential()

ConvNN_model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
ConvNN_model.add(layers.MaxPooling2D((2, 2)))

ConvNN_model.add(layers.Conv2D(64, (3, 3), activation='relu'))

ConvNN_model.add(LSTM(128, activation='relu'))
ConvNN_model.add(Dropout(0.2))

ConvNN_model.add(layers.Dense(64, activation='relu'))
ConvNN_model.add(layers.Dropout(0.25))
ConvNN_model.add(layers.Dense(10, activation='softmax'))

################### Compiling a model ########################
ConvNN_model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

################### Fitting a model ##########################
ConvNN_model.fit(x = x_train, 
          y = y_train, 
          epochs = 1, 
          validation_data = (x_test, y_test))

我遇到了这个问题:

  

ValueError跟踪(最近的呼叫   持续)        在()中            23 ConvNN_model.add(layers.Conv2D(64,(3,3),激活='relu'))            24       ---> 25 ConvNN_model.add(LSTM(128,activation ='relu'))            26个ConvNN_model.add(Dropout(0.2))            27

ValueError: Input 0 of layer lstm_7 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 11,11, 64]

您能帮我吗? 预先谢谢你。

1 个答案:

答案 0 :(得分:0)

这里的技巧是使用TimeDistributed。由于行和列方向上都有序列,因此需要先编码其中之一。下面我们首先使用包装在TimeDistributed中的LSTM对行进行编码,然后使用LSTM对列进行编码。

from tensorflow.keras import models, layers
from tensorflow.keras.layers import LSTM, Dropout, Dense, TimeDistributed
################### Building a model #########################
ConvNN_model = models.Sequential()

ConvNN_model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
ConvNN_model.add(layers.MaxPooling2D((2, 2)))

ConvNN_model.add(layers.Conv2D(64, (3, 3), activation='relu'))

# encode rows of matrix
ConvNN_model.add(TimeDistributed(LSTM(128, activation='relu')))
ConvNN_model.add(Dropout(0.2))

# encode columns
ConvNN_model.add(LSTM(128, activation='relu'))

ConvNN_model.add(layers.Dense(64, activation='relu'))
ConvNN_model.add(layers.Dropout(0.25))
ConvNN_model.add(layers.Dense(10, activation='softmax'))

################### Compiling a model ########################
ConvNN_model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

################### Fitting a model ##########################
ConvNN_model.fit(x = x_train, 
          y = y_train, 
          epochs = 1, 
          validation_data = (x_test, y_test))