双向LSTM与ELMO2 RuntimeError: 尝试捕获没有构建函数的EagerTensor

2024-04-20 04:06:58 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码中引发运行时错误的部分。当我调用x=Bidirectional并添加嵌入时,就会发生这种情况。有人能帮我吗?在

elmo_model = hub.Module("https://tfhub.dev/google/elmo/2", trainable=True) # For fine-tuning the model
sess.run(tf.compat.v1.global_variables_initializer())
sess.run(tf.compat.v1.tables_initializer())


# Define function to apply elmo embedding model
def ElmoEmbedding(x):
    return elmo_model(inputs={
        "tokens": tf.squeeze(tf.cast(x, tf.string)),
        "sequence_len": tf.constant(batchSize * [maxSentLen])
    },
        signature="tokens",
        as_dict=True)["elmo"]

inputLayer = Input(shape=(maxSentLen,), dtype="string")
embedding = Lambda(ElmoEmbedding, output_shape=(maxSentLen, embeddingDimension))(inputLayer)

运行时错误出现在它执行这行时

^{pr2}$

十一

xRNN = Bidirectional(LSTM(units=nUnits, return_sequences=True, recurrent_dropout=recurrentDropOutRate, dropout=dropOutRate))(x)
x = add([x, xRNN])
out = TimeDistributed(Dense(nTags, activation="softmax"))(x)
model = Model(inputLayer, out)
       # return model

model = build_model()

Tags: runtruemodelreturntf错误embeddingsess