Python 3.x 从.hdf5中的权重和.json中的选项实例化keras中的ELMo模型

Python 3.x 从.hdf5中的权重和.json中的选项实例化keras中的ELMo模型,python-3.x,machine-learning,keras,Python 3.x,Machine Learning,Keras,预先培训的ELMo模型提供于 我将如何使用提供的文件 我认为我必须从json文件重建模型,然后将.hdf5文件中的权重加载到模型中。 但是json格式似乎不适用于json中的keras.models.model。我得到了一个错误: value错误:配置格式不正确:…使用tensorflow\u hub加载ELMo模型,示例: import tensorflow as tf import tensorflow_hub as hub from keras.layers import Lambda f

预先培训的ELMo模型提供于

我将如何使用提供的文件

我认为我必须从json文件重建模型,然后将.hdf5文件中的权重加载到模型中。 但是json格式似乎不适用于json中的keras.models.model。我得到了一个错误:
value错误:配置格式不正确:…

使用
tensorflow\u hub
加载ELMo模型,示例:

import tensorflow as tf
import tensorflow_hub as hub
from keras.layers import Lambda
from keras.models import Input
from keras import backend as K
sess = tf.Session()
K.set_session(sess)

batch_size = 64
max_len = 100
elmo_model = hub.Module("https://tfhub.dev/google/elmo/2", trainable=True)
sess.run(tf.global_variables_initializer())
sess.run(tf.tables_initializer())

def ElmoEmbedding(x):
    return elmo_model(inputs={"tokens": tf.squeeze(tf.cast(x,tf.string)),
                              "sequence_len": tf.constant(batch_size*[max_len])},
                      signature="tokens",
                      as_dict=True)["elmo"]
input_x = Input(shape=(max_len,), dtype=tf.string)
embedding = Lambda(ElmoEmbedding, output_shape=(max_len, 128))(input_x)

使用
tensorflow\u hub
加载ELMo模型,示例:

import tensorflow as tf
import tensorflow_hub as hub
from keras.layers import Lambda
from keras.models import Input
from keras import backend as K
sess = tf.Session()
K.set_session(sess)

batch_size = 64
max_len = 100
elmo_model = hub.Module("https://tfhub.dev/google/elmo/2", trainable=True)
sess.run(tf.global_variables_initializer())
sess.run(tf.tables_initializer())

def ElmoEmbedding(x):
    return elmo_model(inputs={"tokens": tf.squeeze(tf.cast(x,tf.string)),
                              "sequence_len": tf.constant(batch_size*[max_len])},
                      signature="tokens",
                      as_dict=True)["elmo"]
input_x = Input(shape=(max_len,), dtype=tf.string)
embedding = Lambda(ElmoEmbedding, output_shape=(max_len, 128))(input_x)
但首先要注意的是:

from allennlp.commands.elmo import ElmoEmbedder
您也可以用实际文件替换链接。 希望能有帮助

但首先要注意的是:

from allennlp.commands.elmo import ElmoEmbedder
您也可以用实际文件替换链接。 希望能有帮助