gpt4 book ai didi

python-3.x - 根据 .hdf5 中的权重和 .json 中的选项在 keras 中实例化 ELMo 模型

转载 作者:行者123 更新时间:2023-11-30 09:32:04 25 4
gpt4 key购买 nike

预训练的 ELMo 模型位于 https://allennlp.org/elmo .

我将如何使用所提供的文件?

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

最佳答案

使用tensorflow_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)

关于python-3.x - 根据 .hdf5 中的权重和 .json 中的选项在 keras 中实例化 ELMo 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53808036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com