我正在尝试将 Elmo 与 Keras 和 Tensorflow 一起使用。在运行下面的代码时,我收到一个似乎权重未初始化的错误。我也尝试过定义这里定义的 ElmoLayer 类(链接),但仍然是同样的错误。
知道我做错了什么吗?
def ElmoEmbeddingLayer(x):
print(x.shape)
module = hub.Module("https://tfhub.dev/google/elmo/3", trainable=False)
embeddings = module(tf.squeeze(tf.cast(x, tf.string)), signature="default", as_dict=True)["elmo"]
return embeddings
def build_model():
input_text = Input(shape=(1,), dtype="string")
#embedding = ElmoEmbeddingLayer()(input_text)
embedding = Lambda(ElmoEmbeddingLayer, output_shape=(1,1024))(input_text)
dense = Dense(256, activation='relu')(embedding)
dense = Flatten()(dense)
pred = Dense(3, activation='sigmoid')(dense)
model = Model(inputs=[input_text], outputs=pred)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
return model
FailedPreconditionError:从容器读取资源变量模块/bilm/RNN_0/RNN/MultiRNNCell/Cell0/rnn/lstm_cell/projection/kernel 时出错:localhost。这可能意味着该变量未初始化。未找到:资源 localhost/module/bilm/RNN_0/RNN/MultiRNNCell/Cell0/rnn/lstm_cell/projection/kernel/class tensorflow::Var 不存在。[[节点 lambda_1/module_apply_default/bilm/RNN_0/RNN/MultiRNNCell/Cell0/rnn/lstm_cell/projection/kernel/Read/ReadVariableOp(定义在 C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\framework\ ops.py:1751) ]] [Op:__inference_keras_scratch_graph_5942]
函数调用栈:keras_scratch_graph
我的 Keras 和 TF 版本:
print(keras.__version__)
2.3.1
print(tf.__version__)
2.0.0