【问题标题】:TypeError: can't pickle _thread.lock objects in Seq2SeqTypeError:无法在 Seq2Seq 中腌制 _thread.lock 对象
【发布时间】:2017-12-04 22:53:03
【问题描述】:

我在我的 Tensorflow 模型中使用存储桶时遇到问题。当我使用buckets = [(100, 100)] 运行它时,它运行良好。当我使用buckets = [(100, 100), (200, 200)] 运行它时,它根本不起作用(底部的堆栈跟踪)。

有趣的是,运行 Tensorflow 的 Seq2Seq 教程会出现类似的问题,但堆栈跟踪几乎相同。出于测试目的,存储库的链接是here

我不确定问题是什么,但拥有多个存储桶似乎总是会触发它。

此代码不能作为独立代码运行,但这是它崩溃的函数 - 请记住,将 buckets[(100, 100)] 更改为 [(100, 100), (200, 200)] 会触发崩溃。

class MySeq2Seq(object):
    def __init__(self, source_vocab_size, target_vocab_size, buckets, size, num_layers, batch_size, learning_rate):
        self.source_vocab_size = source_vocab_size
        self.target_vocab_size = target_vocab_size
        self.buckets = buckets
        self.batch_size = batch_size

        cell = single_cell = tf.nn.rnn_cell.GRUCell(size)
        if num_layers > 1:
            cell = tf.nn.rnn_cell.MultiRNNCell([single_cell] * num_layers)

        # The seq2seq function: we use embedding for the input and attention
        def seq2seq_f(encoder_inputs, decoder_inputs, do_decode):
            return tf.contrib.legacy_seq2seq.embedding_attention_seq2seq(
                encoder_inputs, decoder_inputs, cell,
                num_encoder_symbols=source_vocab_size,
                num_decoder_symbols=target_vocab_size,
                embedding_size=size,
                feed_previous=do_decode)

        # Feeds for inputs
        self.encoder_inputs = []
        self.decoder_inputs = []
        self.target_weights = []
        for i in range(buckets[-1][0]):
            self.encoder_inputs.append(tf.placeholder(tf.int32, shape=[None], name="encoder{0}".format(i)))
        for i in range(buckets[-1][1] + 1):
            self.decoder_inputs.append(tf.placeholder(tf.int32, shape=[None], name="decoder{0}".format(i)))
            self.target_weights.append(tf.placeholder(tf.float32, shape=[None], name="weight{0}".format(i)))

        # Our targets are decoder inputs shifted by one
        targets = [self.decoder_inputs[i + 1] for i in range(len(self.decoder_inputs) - 1)]
        self.outputs, self.losses = tf.contrib.legacy_seq2seq.model_with_buckets(
            self.encoder_inputs, self.decoder_inputs, targets,
            self.target_weights, [(100, 100)],
            lambda x, y: seq2seq_f(x, y, False))

        # Gradients update operation for training the model
        params = tf.trainable_variables()
        self.updates = []
        for b in range(len(buckets)):
            self.updates.append(tf.train.AdamOptimizer(learning_rate).minimize(self.losses[b]))

        self.saver = tf.train.Saver(tf.global_variables())

堆栈跟踪:

    Traceback (most recent call last):
  File "D:/Stuff/IdeaProjects/myproject/src/main.py", line 38, in <module>
    model = predict.make_model(input_vocab_size, output_vocab_size, buckets, cell_size, model_layers, batch_size, learning_rate)
  File "D:\Stuff\IdeaProjects\myproject\src\predictor.py", line 88, in make_model
    size=cell_size, num_layers=model_layers, batch_size=batch_size, learning_rate=learning_rate)
  File "D:\Stuff\IdeaProjects\myproject\src\predictor.py", line 45, in __init__
    lambda x, y: seq2seq_f(x, y, False))
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\contrib\legacy_seq2seq\python\ops\seq2seq.py", line 1206, in model_with_buckets
    decoder_inputs[:bucket[1]])
  File "D:\Stuff\IdeaProjects\myproject\src\predictor.py", line 45, in <lambda>
    lambda x, y: seq2seq_f(x, y, False))
  File "D:\Stuff\IdeaProjects\myproject\src\predictor.py", line 28, in seq2seq_f
    feed_previous=do_decode)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\contrib\legacy_seq2seq\python\ops\seq2seq.py", line 848, in embedding_attention_seq2seq
    encoder_cell = copy.deepcopy(cell)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 161, in deepcopy
    y = copier(memo)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\layers\base.py", line 476, in __deepcopy__
    setattr(result, k, copy.deepcopy(v, memo))
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 215, in _deepcopy_list
    append(deepcopy(a, memo))
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\copy.py", line 169, in deepcopy
    rv = reductor(4)
TypeError: can't pickle _thread.lock objects

【问题讨论】:

    标签: python-3.x tensorflow nlp lstm sequence-to-sequence


    【解决方案1】:

    此解决方案对我不起作用。有什么新的解决方案吗?

    这两个解决方案对我有用:

    更改 /yourpath/tensorflow/contrib/legacy_seq2seq/python/ops/ 下的 seq2seq.py

    #encoder_cell = copy.deepcopy(cell)
    encoder_cell = core_rnn_cell.EmbeddingWrapper(
        cell, #encoder_cell,
    

    for nextBatch in tqdm(batches, desc="Training"):
        _, step_loss = model.step(...)
    

    一步喂一桶

    【讨论】:

      【解决方案2】:

      问题在于seq2seq.py 的最新更改。将此添加到您的脚本中,它将避免单元格的深度处理:

      setattr(tf.contrib.rnn.GRUCell, '__deepcopy__', lambda self, _: self)
      setattr(tf.contrib.rnn.BasicLSTMCell, '__deepcopy__', lambda self, _: self)
      setattr(tf.contrib.rnn.MultiRNNCell, '__deepcopy__', lambda self, _: self)
      

      【讨论】:

      • 这个问题非常实际。我刚刚使用最新的 tf 进入了这个领域,并找到了这个解决方法。
      猜你喜欢
      • 2017-10-23
      • 1970-01-01
      • 2017-12-18
      • 2021-05-30
      • 2021-05-16
      • 2019-05-11
      • 2019-10-20
      • 2018-04-14
      • 2019-01-21
      相关资源
      最近更新 更多