Keras ELMO:读取资源变量时出错

Keras ELMO: Error while reading resource variable

我正在尝试开始将 Elmo 与 Keras 和 Tensorflow 结合使用。在 运行 下面的代码中,我收到一个错误,似乎权重未初始化。我也尝试过定义 ElmoLayer class,因为它在此处定义 (link),但仍然出现相同的错误。

知道我做错了什么吗?

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: Error while reading resource variable module/bilm/RNN_0/RNN/MultiRNNCell/Cell0/rnn/lstm_cell/projection/kernel from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/module/bilm/RNN_0/RNN/MultiRNNCell/Cell0/rnn/lstm_cell/projection/kernel/class tensorflow::Var does not exist. [[node lambda_1/module_apply_default/bilm/RNN_0/RNN/MultiRNNCell/Cell0/rnn/lstm_cell/projection/kernel/Read/ReadVariableOp (defined at C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\framework\ops.py:1751) ]] [Op:__inference_keras_scratch_graph_5942]

Function call stack: keras_scratch_graph

我的 Keras 和 TF 版本:

print(keras.__version__)
2.3.1

print(tf.__version__)
2.0.0

我通过

克服了那个错误

1:导入亚当:“来自 tensorflow.keras.optimizers 导入亚当”

2:将 Lambda 层替换为: hub.KerasLayer("https://tfhub.dev/google/elmo/3", trainable=False)

请确保您安装了此版本的 python 和 TensorFlow python3.6

请检查 tensorflow 和 keras 兼容性。 https://mckayward.github.io/floyd-docs/guides/environments/