tensorflow - 如何在使用 tf-hub 的 tf 2.0 Keras 中使用 ELMO 嵌入作为第一个嵌入层?
问题描述
我正在尝试建立一个使用嵌入的NER
模型。所以我在本教程中遇到了困难并开始实施。我有很多错误,其中一些是:Keras
ELMO
import tensorflow as tf
import tensorflow_hub as hub
from keras import backend as K
sess = tf.Session()
K.set_session(sess)
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_text = Input(shape=(max_len,), dtype=tf.string)
embedding = Lambda(ElmoEmbedding, output_shape=(None, 1024))(input_text)
它给了我 AttributeError: module 'tensorflow' has no attribute 'Session'
。因此,如果我注释掉sess=
代码并运行,它会给我AttributeError: module 'keras.backend' has no attribute 'set_session'
.
再说一次,Elmo
代码行给了我RuntimeError: Exporting/importing meta graphs is not supported when eager execution is enabled. No graph exists when eager execution is enabled.
.
我有以下配置:
tf.__version__
'2.3.1'
keras.__version__
'2.4.3'
import sys
sys.version
'3.8.3 (default, Jul 2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)]'
如何ELMO
在 Keras 模型中使用嵌入?
解决方案
您使用的是旧的 Tensorflow 1.x 语法,但您安装了 tensorflow 2。
这是在 TF2 中执行 elmo 的新方法 使用 tensorflow 提取 ELMo 特征并将它们转换为 numpy
推荐阅读
- firebase - 新的 Firebase 消息传递服务器密钥是否仅限于消息传递?
- javascript - Json 从服务器到 html 表
- sql-server - SQL Server 2008 FactFinance 主键
- java - 如何修复我的日志记录配置文件以以正确的格式返回正确的日志级别?
- ansible - 如何从ansible获取需要重启的服务器列表
- c++ - c++:Xcode 中架构 x86_64 的未定义符号
- c++ - 如何将非空终止字符串输出到 iostream,但保持格式化
- swift - OSX extension doesn't call tableView viewFor method
- javascript - Typescript - 导入 React 功能组件时出错
- excel - Excel Sum(Indirect))