2

我训练我的模型,model_lstm() 训练代码如下。

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')

接下来,当我加载模型时,我遇到了问题。我使用自定义对象(注意)。

我很好奇的是,当使用需要参数的自定义对象时,会加载 keras 模型。

我尝试了各种方法,例如保存和加载为字典,但没有奏效。

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

然后..我得到像这样的错误

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

我的注意力功能如下。

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()
4

1 回答 1

0

你在训练模型后添加了step_dim吗?然后在你的注意力函数中给它一个默认值:

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

或者您可以尝试重新训练模型。

于 2021-04-19T22:31:34.213 回答