I'm trying to get started with using Elmo with Keras & Tensorflow. On running the code below I'm getting an error that seems like the weights aren't initialized. I have also tried defining ElmoLayer class as it's defined here (link), but still the same error.
Any idea what I'm doing wrong?
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
My Keras and TF versions:
print(keras.__version__)
2.3.1
print(tf.__version__)
2.0.0
CodePudding user response:
I got past that error by
1: Imported Adam: "from tensorflow.keras.optimizers import Adam"
2: Replaced the Lambda layer with: hub.KerasLayer("https://tfhub.dev/google/elmo/3", trainable=False)
CodePudding user response:
Please make sure you have this version of python and TensorFlow installed python3.6
Check please, tensorflow and keras compatibility. https://mckayward.github.io/floyd-docs/guides/environments/