简体   繁体   中英

Keras correct input shape for multilayer perceptron

I'm trying to make a basic MLP example in keras. My input data has the shape train_data.shape = (2000,75,75) and my testing data has the shape test_data.shape = (500,75,75) . 2000 and 500 are the numbers of samples of training and test data (in other words, the shape of the data is (75,75) , but there are 2000 and 500 pieces of training and testing data). The output should have two classes.

I'm unsure what value to use for the input_shape parameter on the first layer of the network. Using the code from the mnist example in the keras repository, I have ( updated ):

from six.moves    import cPickle
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.utils  import np_utils
from keras.optimizers import RMSprop

# Globals
NUM_CLASSES = 2
NUM_EPOCHS  = 10
BATCH_SIZE  = 250

def loadData():
    fData = open('data.pkl','rb')
    fLabels = open('labels.pkl','rb')
    data = cPickle.load(fData)
    labels = cPickle.load(fLabels)

    train_data = data[0:2000]
    train_labels = labels[0:2000]
    test_data = data[2000:]
    test_labels = labels[2000:]
    return (train_data, train_labels, test_data, test_labels)

# Load data and corresponding labels for model
train_data, train_labels, test_data, test_labels = loadData()

train_labels = np_utils.to_categorical(train_labels, NUM_CLASSES)
test_labels  = np_utils.to_categorical(test_labels, NUM_CLASSES)

print(train_data.shape)
print(test_data.shape)

model = Sequential()
model.add(Dense(512, input_shape=(5625,)))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(2))
model.add(Activation('softmax'))

model.summary()

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

history = model.fit(train_data, train_labels, validation_data=(test_data, test_labels), 
                    batch_size=BATCH_SIZE, nb_epoch=NUM_EPOCHS,
                    verbose=1)
score = model.evaluate(test_data, test_labels, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])

where 5625 is 75 * 75 (emulating the MNIST example). The error I get is:

Error when checking model input: expected dense_input_1 to have 2 dimensions, but got array with shape (2000, 75, 75)

Any ideas?

From keras MLP example, https://github.com/fchollet/keras/blob/master/examples/mnist_mlp.py

# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()

X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)

And the model input

model = Sequential()
model.add(Dense(512, input_shape=(784,)))

So you should reshape your train and test to (2000,75*75) and (500,75*75) with

train_data = train_data.reshape(2000, 75*75)
test_data = test_data.reshape(500, 75*75)

and then set the model input shape as you did

model.add(Dense(512, input_shape=(75*75,)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM