when keras loading model, =missing 1 required positional argument error

Viewed 715

i train my model, model_lstm() train code is below.

model = model_lstm(train_X.shape)
ckpt = ModelCheckpoint('weights_{}.h5'.format(idx), save_best_only=True, save_weights_only=True, verbose=1, monitor='val_matthews_correlation', mode='max')
model.fit(train_X[:100,:], train_y[:100], batch_size=10, epochs=10, validation_data=[val_X, val_y], callbacks=[ckpt])

model.save('lstm_model.h5')

next, when i load model, i have problem. i use custom object (attention).

What I'm curious about is that when using a custom object that requires an argument, the keras model is loaded.

I tried various methods such as saving and loading as a dict, but it didn't work.

conv_base = load_model('lstm_model.h5',custom_objects={'Attention':Attention()})

then.. I get the errors like

__init__() missing 1 required positional argument: 'step_dim'

my attention function is below.

class Attention(Layer):
    def __init__(self,  step_dim,
                 W_regularizer=None, b_regularizer=None,
                 W_constraint=None, b_constraint=None,
                 bias=True, **kwargs):
        self.supports_masking = True
#         self.name = 'ww'
        self.init = initializers.get('glorot_uniform')

        self.W_regularizer = regularizers.get(W_regularizer)
        self.b_regularizer = regularizers.get(b_regularizer)

        self.W_constraint = constraints.get(W_constraint)
        self.b_constraint = constraints.get(b_constraint)

        self.bias = bias
        self.step_dim = step_dim
        self.features_dim = 0
        super(Attention, self).__init__(**kwargs)

    def build(self, input_shape):
        assert len(input_shape) == 3

        self.W = self.add_weight(shape = (input_shape[-1],),
                                 initializer=self.init,
                                 name='{}_W'.format(self.name),
                                 regularizer=self.W_regularizer,
                                 constraint=self.W_constraint)
        self.features_dim = input_shape[-1]

        if self.bias:
            self.b = self.add_weight(shape = (input_shape[1],),
                                     initializer='zero',
                                     name='{}_b'.format(self.name),
                                     regularizer=self.b_regularizer,
                                     constraint=self.b_constraint)
        else:
            self.b = None

        self.built = True

    def compute_mask(self, input, input_mask=None):
        return None

    def call(self, x, mask=None):
        features_dim = self.features_dim
        step_dim = self.step_dim

        eij = K.reshape(K.dot(K.reshape(x, (-1, features_dim)),
                        K.reshape(self.W, (features_dim, 1))), (-1, step_dim))

        if self.bias:
            eij += self.b

        eij = K.tanh(eij)

        a = K.exp(eij)

        if mask is not None:
            a *= K.cast(mask, K.floatx())

        a /= K.cast(K.sum(a, axis=1, keepdims=True) + K.epsilon(), K.floatx())

        a = K.expand_dims(a)
        weighted_input = x * a
        return K.sum(weighted_input, axis=1)

    def compute_output_shape(self, input_shape):
        return input_shape[0],  self.features_dim
    def get_config(self):
        return super(Attention,self).get_config()
1 Answers

Did you add step_dim after training model? Then just give it a default value in your attention function:

class Attention(Layer):
    def __init__(self,  step_dim=None,
    ...

Or you could try to retrain the model.

Related