1

我尝试将以下代码从 TF 1.7 更新到 TF 1.14 :

def build(self, input_shape):
    self.max_length = input_shape[1]
    self.word_mapping = [x[1] for x in sorted(self.idx2word.items(), key=lambda x: x[0])]
    self.lookup_table = tf.contrib.lookup.index_to_string_table_from_tensor(self.word_mapping, default_value="<UNK>")
    self.lookup_table.init.run(session=K.get_session())
    self.elmo_model = hub.Module("https://tfhub.dev/google/elmo/2", trainable=self.trainable)
    super(ELMoEmbedding, self).build(input_shape)

def call(self, x):
    x = tf.cast(x, dtype=tf.int64)
    sequence_lengths = tf.cast(tf.count_nonzero(x, axis=1), dtype=tf.int32)
    strings = self.lookup_table.lookup(x)
    inputs = {
        "tokens": strings,
        "sequence_len": sequence_lengths
    }
    return self.elmo_model(inputs, signature="tokens", as_dict=True)[self.output_mode]

当我适合我的模型时,以下行

self.lookup_table.init.run(session=K.get_session())

生成以下错误:

“StaticHashTableV1”对象没有属性“init”。所以我按照TF r1.14 doc修改了代码

def build(self, input_shape):
    self.max_length = input_shape[1]
    self.word_mapping = [x[1] for x in sorted(self.idx2word.items(), key=lambda x: x[0])]
    #self.lookup_table = tf.contrib.lookup.index_to_string_table_from_tensor(self.word_mapping, default_value="<UNK>")
    self.lookup_table = tf.contrib.lookup.index_to_string_table_from_tensor(self.word_mapping,
            default_value='<UNK>',
            name=None
            )
    with tf.Session() as session:
        session.run(tf.compat.v1.tables_initializer)
    # Initialize the variables (i.e. assign their default value)
    self.elmo_model = hub.Module("https://tfhub.dev/google/elmo/2", trainable=self.trainable)
    super(ELMoEmbedding, self).build(input_shape)

def call(self, x):
    x = tf.cast(x, dtype=tf.int64)
    sequence_lengths = tf.cast(tf.count_nonzero(x, axis=1), dtype=tf.int32)
    strings = self.lookup_table.lookup(x)
    inputs = {
        "tokens": strings,
        "sequence_len": sequence_lengths
    }
    return self.elmo_model(inputs, signature="tokens", as_dict=True)[self.output_mode]

我得到这个错误:

Traceback (most recent call last):
  File "main.py", line 46, in <module>
    protest.train(str(args[0]), dirModel, typeCorpus)
  File "/home/???/clef_2019/protest.py", line 40, in train
    model = classification.runTrain(dataFrame)
  File "/home/???/clef_2019/classification.py", line 97, in runTrain
    model = self.runAlgo(dataFrame, column)
  File "/home/???/clef_2019/classification.py", line 328, in runAlgo
    model.fit(xtrain, y, batch_size=8, epochs = 1)
  File "/usr/local/lib/python3.6/dist-packages/kashgari/tasks/base_model.py", line 324, in fit
    **fit_kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py", line 1433, in fit_generator
    steps_name='steps_per_epoch')
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training_generator.py", line 264, in model_iteration
    batch_outs = batch_function(*batch_data)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py", line 1175, in train_on_batch
    outputs = self.train_function(ins)  # pylint: disable=not-callable
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py", line 3292, in __call__
    run_metadata=self.run_metadata)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py", line 1458, in __call__
    run_metadata_ptr)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable module/bilm/CNN/b_cnn_2 from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/module/bilm/CNN/b_cnn_2/N10tensorflow3VarE does not exist.
     [[{{node el_mo_embedding/module_apply_tokens/bilm/CNN_1/add_2/ReadVariableOp}}]]

谢谢你的帮助:)

4

1 回答 1

0

with tf.Session() as session:
    session.run(tf.compat.v1.tables_initializer)

当您超出范围时,会话将停止存在。您可以将其替换为

tf.compat.v1.tables_initializer().run(session=K.get_session())

我认为这应该可以解决问题。

于 2019-11-20T19:12:37.513 回答