2

I trained the following net and saved it. When compiling the reloaded network, the error appears:

ValueError: Error when checkingModelTarget: expected dense_3 to haveFast (None, 1) but got array with shape (10000, 10)

What can be the reason? The solutions of many similar problems do not really help me.

code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Convolution2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from keras import backend as K
from keras.models import model_from_json

K.set_image_dim_ordering('th')

# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)

# load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# reshape to be [samples][pixels][width][height]
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28).astype('float32')

# normalize inputs from 0-255 to 0-1
X_train = X_train / 255
X_test = X_test / 255
# one hot encode outputs
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]

def larger_model():
    # create model
    model = Sequential()
    model.add(Convolution2D(30, 5, 5, border_mode='valid', input_shape=(1, 28, 28), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Convolution2D(15, 3, 3, activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.2))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dense(50, activation='relu'))
    model.add(Dense(num_classes, activation='softmax'))
    # Compile model
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

# build the model
model = larger_model()
# Fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=1, batch_size=200, verbose=2)
# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)
print("Baseline Error: %.2f%%" % (100-scores[1]*100))


# save model and weights
print("Saving model...")
model_json = model.to_json()
with open('mnist_model.json', 'w') as json_file:
    json_file.write(model_json)
model.save_weights("mnist_weights.h5")
print("model saved to disk")

# load model and weights
print("Laoding model...")
with open('mnist_model.json') as json_file:
    model_json = json_file.read()

model = model_from_json(model_json)
model.load_weights('mnist_weights.h5')
print("mode loaded from disk")

print("compiling model...")
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

scores = model.evaluate(X_test, y_test, verbose=0)
print("Baseline Error: %.2f%%" % (100-scores[1]*100))
2
  • It could be that your image ordering is set to TF and the input shape is in Theano image ordering, and you are using TF as backend. Commented Apr 4, 2017 at 7:02
  • Could you print out model.summary()? Commented Apr 4, 2017 at 7:18

1 Answer 1

1

Why do you do this after loading your model ? :

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

Your base model uses categorical_crossentropy, the difference is that the latest expects categorical, one hot encoded targets, and the sparse version expects indices and calls np.utils.to_categorical() in the background. So here, keras complains because you use the sparse version, it is expecting indices so a shape (?, 1) but you feed y_test, encoded as one-hot with a shape (?, 10).

Solution, either don't change the type of loss and use :

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

after loading the model, or reverse the one hot encoded y_test:

y_test = np.argmax(y_test)

I hope this helps :-)

Sign up to request clarification or add additional context in comments.

1 Comment

thx! I changed from 'spare_categorical_crossentropy' to 'categorical_crossentropy' and it is working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.