Search code examples
pythonmodelkerassequential

keras, what is the meaning of model(data)


class MNISTModel:
    def __init__(self, restore, session=None):
        self.num_channels = 1
        self.image_size = 28
        self.num_labels = 10

        model = Sequential()

        model.add(Conv2D(32, (3, 3),
                         input_shape=(28, 28, 1)))
        model.add(Activation('relu'))
        model.add(Conv2D(32, (3, 3)))
        model.add(Activation('relu'))
        model.add(MaxPooling2D(pool_size=(2, 2)))

        model.add(Conv2D(64, (3, 3)))
        model.add(Activation('relu'))
        model.add(Conv2D(64, (3, 3)))
        model.add(Activation('relu'))
        model.add(MaxPooling2D(pool_size=(2, 2)))

        model.add(Flatten())
        model.add(Dense(200))
        model.add(Activation('relu'))
        model.add(Dense(200))
        model.add(Activation('relu'))
        model.add(Dense(10))
        model.load_weights(restore)

        self.model = model
        print('selfMNIST')

    def predict(self, data):
        tmp=self.model(data)  #Question is here
        return tmp

What is this sentence "tmp=self.model(data)" for? "model" here is the variable of Class sequential, I never see such usage. This code is taken from ## Copyright (C) 2016, Nicholas Carlini


Solution

  • Good question, it depends on what data is but certainly the code snippet is misleading. To clarify:

    • If you call model directly you need to pass tensors, it acts like another layer. model(anotherlayer) works for example. But if you pass actual data such as a NumPy error it will give this error:

      ValueError: Layer sequential_1 was called with an input that isn't a symbolic tensor. Received type: . Full input: [array(...)]. All inputs to the layer should be tensors.

    • If you want to predict, as in pass some numeric data and run the network / propagate values / compute output then you need to model.predict(data) which is why it is misleading as it is a predict function.

    My guess is that the author needed to chain models together and edited the predict function of the class instead of creating a different class function.