0

这是我的模型架构

word_in = 独特的词

chr_in = 唯一字符(a,b,c,-,...)

word_in = Input(shape=(max_len,))
emb_word = Embedding(input_dim=n_words + 2, output_dim=20,
                     input_length=max_len, mask_zero=True)(word_in)

# input and embeddings for characters
char_in = Input(shape=(max_len, max_len_char,))
emb_char = TimeDistributed(Embedding(input_dim=n_chars + 2, output_dim=10,
                           input_length=max_len_char, mask_zero=True))(char_in)
# character LSTM to get word encodings by characters
char_enc = TimeDistributed(LSTM(units=20, return_sequences=False, recurrent_dropout=0.5))(emb_char)

# main LSTM
x = concatenate([emb_word, char_enc])
x = SpatialDropout1D(0.3)(x)
main_lstm = LSTM(units=50, return_sequences=True,recurrent_dropout=0.5)(x)

out = TimeDistributed(Dense(n_tags + 1, activation="sigmoid"))(main_lstm)

model = Model([word_in, char_in], out)

我在训练后保存了上述模型及其权重。

现在,当我加载 .h5 文件时,我无法预测。

我尝试了几件事,但它的抛出错误。

首先加载模型进行预测

model=load_model('/content/drive/MyDrive/model_bidirectional.h5')
#model.summary()
p=model.predict(x_char_te)

第二次加载权重和模型一起预测

tmodel=load_model('/content/drive/MyDrive/model_bidirectional.h5')
#model.summary()
tmodel.load_weights('/content/drive/MyDrive/model_lstm_weights.h5')
p=tmodel.predict(x_char_te)

加载 LSTM 架构并附加权重

word_in = Input(shape=(max_len,))
emb_word = Embedding(input_dim=n_words + 2, output_dim=20,
                     input_length=max_len, mask_zero=True)(word_in)

# input and embeddings for characters
char_in = Input(shape=(max_len, max_len_char,))
emb_char = TimeDistributed(Embedding(input_dim=n_chars + 2, output_dim=10,
                           input_length=max_len_char, mask_zero=True))(char_in)
# character LSTM to get word encodings by characters
char_enc = TimeDistributed(LSTM(units=20, return_sequences=False, recurrent_dropout=0.5))(emb_char)

# main LSTM
x = concatenate([emb_word, char_enc])
x = SpatialDropout1D(0.3)(x)
main_lstm = LSTM(units=50, return_sequences=True,recurrent_dropout=0.5)(x)

out = TimeDistributed(Dense(n_tags + 1, activation="sigmoid"))(main_lstm)

model = Model([word_in, char_in], out)

model.load_weights('/content/drive/MyDrive/model_lstm_weights.h5')

无法理解错误是什么

/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:1586 predict_function  *
        return step_function(self, iterator)
    /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:1576 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1286 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2849 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3632 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:1569 run_step  **
        outputs = model.predict_step(data)
    /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:1537 predict_step
        return self(x, training=False)
    /usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py:1020 __call__
        input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
    /usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py:202 assert_input_compatibility
        ' input tensors. Inputs received: ' + str(inputs))

    ValueError: Layer model_10 expects 2 input(s), but it received 3322 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:1' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:2' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:3' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:4' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:5' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:6' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:7' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:8' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:9' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:10' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:11' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:12' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:13' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:14' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:15' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:16' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:17' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:18' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:19' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:20' shape=(None, 20) dtype=int64>, <tf.Tensor 'IteratorGetNext:21' shape=(None, 20) dtype=int64>, <tf.Tensor 'Iter...

我可以知道我哪里出错了,我该如何解决?

4

0 回答 0