nfmcclure / tensorflow_cookbook

Code for Tensorflow Machine Learning Cookbook
https://www.packtpub.com/big-data-and-business-intelligence/tensorflow-machine-learning-cookbook-second-edition
MIT License
6.24k stars 2.41k forks source link

AttributeError: 'NoneType' object has no attribute 'update' #106

Closed sherlockhoatszx closed 6 years ago

sherlockhoatszx commented 7 years ago

Hi, in 09_Recurrent_Neural_Networks/05_Creating_A_Sequence_To_Sequence_Model/ the code report error, my python is 3.5,tensorflow 1.3.0


input_vocab_size = vocab_size
output_vocab_size = vocab_size

translate_model = translation_model(sess=sess,
                                    input_vocab_size=vocab_size,
                                    output_vocab_size=vocab_size,
                                    buckets=buckets,
                                    rnn_size=rnn_size,
                                    num_layers=num_layers,
                                    max_gradient=max_gradient,
                                    learning_rate=learning_rate,
                                    lr_decay_rate=lr_decay_rate,
                                    forward_only=False)

# Tell TensorFlow to reuse the variables for the test model
with tf.variable_scope(tf.get_variable_scope(), reuse=True):
    #Reuse the variables for the test model
    test_model = translation_model(sess=sess,
                                    input_vocab_size=vocab_size,
                                    output_vocab_size=vocab_size,
                                    buckets=buckets,
                                    rnn_size=rnn_size,
                                    num_layers=num_layers,
                                    max_gradient=max_gradient,
                                    learning_rate=learning_rate,
                                    lr_decay_rate=lr_decay_rate,
                                    forward_only=True)
    test_model.batch_size = 1```

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-d80c8e004260> in <module>()
     12                                     learning_rate=learning_rate,
     13                                     lr_decay_rate=lr_decay_rate,
---> 14                                     forward_only=False)
     15 
     16 # Tell TensorFlow to reuse the variables for the test model

<ipython-input-16-ba700daa3a1e> in translation_model(sess, input_vocab_size, output_vocab_size, buckets, rnn_size, num_layers, max_gradient, learning_rate, lr_decay_rate, forward_only)
     14           lr_decay_rate,
     15           forward_only=forward_only,
---> 16           dtype=tf.float32)
     17     return(model)

/Users/sherlock/learning_stuff/models/tutorials/rnn/translate/seq2seq_model.py in __init__(self, source_vocab_size, target_vocab_size, buckets, size, num_layers, max_gradient_norm, batch_size, learning_rate, learning_rate_decay_factor, use_lstm, num_samples, forward_only, dtype)
    184           self.target_weights, buckets,
    185           lambda x, y: seq2seq_f(x, y, False),
--> 186           softmax_loss_function=softmax_loss_function)
    187 
    188     # Gradients and SGD update operation for training the model.

/Users/sherlock/anaconda/lib/python3.5/site-packages/tensorflow/contrib/legacy_seq2seq/python/ops/seq2seq.py in model_with_buckets(encoder_inputs, decoder_inputs, targets, weights, buckets, seq2seq, softmax_loss_function, per_example_loss, name)
   1204           variable_scope.get_variable_scope(), reuse=True if j > 0 else None):
   1205         bucket_outputs, _ = seq2seq(encoder_inputs[:bucket[0]],
-> 1206                                     decoder_inputs[:bucket[1]])
   1207         outputs.append(bucket_outputs)
   1208         if per_example_loss:

/Users/sherlock/learning_stuff/models/tutorials/rnn/translate/seq2seq_model.py in <lambda>(x, y)
    183           self.encoder_inputs, self.decoder_inputs, targets,
    184           self.target_weights, buckets,
--> 185           lambda x, y: seq2seq_f(x, y, False),
    186           softmax_loss_function=softmax_loss_function)
    187 

/Users/sherlock/learning_stuff/models/tutorials/rnn/translate/seq2seq_model.py in seq2seq_f(encoder_inputs, decoder_inputs, do_decode)
    142           output_projection=output_projection,
    143           feed_previous=do_decode,
--> 144           dtype=dtype)
    145 
    146     # Feeds for inputs.

/Users/sherlock/anaconda/lib/python3.5/site-packages/tensorflow/contrib/legacy_seq2seq/python/ops/seq2seq.py in embedding_attention_seq2seq(encoder_inputs, decoder_inputs, cell, num_encoder_symbols, num_decoder_symbols, embedding_size, num_heads, output_projection, feed_previous, dtype, scope, initial_state_attention)
    846     dtype = scope.dtype
    847     # Encoder.
--> 848     encoder_cell = copy.deepcopy(cell)
    849     encoder_cell = core_rnn_cell.EmbeddingWrapper(
    850         encoder_cell,

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    164             copier = getattr(x, "__deepcopy__", None)
    165             if copier:
--> 166                 y = copier(memo)
    167             else:
    168                 reductor = dispatch_table.get(cls)

/Users/sherlock/anaconda/lib/python3.5/site-packages/tensorflow/python/layers/base.py in __deepcopy__(self, memo)
    485         setattr(result, k, v)
    486       else:
--> 487         setattr(result, k, copy.deepcopy(v, memo))
    488     return result
    489 

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/Users/sherlock/anaconda/lib/python3.5/copy.py in _deepcopy_list(x, memo)
    216     memo[id(x)] = y
    217     for a in x:
--> 218         y.append(deepcopy(a, memo))
    219     return y
    220 d[list] = _deepcopy_list

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    164             copier = getattr(x, "__deepcopy__", None)
    165             if copier:
--> 166                 y = copier(memo)
    167             else:
    168                 reductor = dispatch_table.get(cls)

/Users/sherlock/anaconda/lib/python3.5/site-packages/tensorflow/python/layers/base.py in __deepcopy__(self, memo)
    485         setattr(result, k, v)
    486       else:
--> 487         setattr(result, k, copy.deepcopy(v, memo))
    488     return result
    489 

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/Users/sherlock/anaconda/lib/python3.5/copy.py in _deepcopy_list(x, memo)
    216     memo[id(x)] = y
    217     for a in x:
--> 218         y.append(deepcopy(a, memo))
    219     return y
    220 d[list] = _deepcopy_list

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    180                             raise Error(
    181                                 "un(deep)copyable object of type %s" % cls)
--> 182                 y = _reconstruct(x, rv, 1, memo)
    183 
    184     # If is its own copy, don't memoize.

/Users/sherlock/anaconda/lib/python3.5/copy.py in _reconstruct(x, info, deep, memo)
    295     if state is not None:
    296         if deep:
--> 297             state = deepcopy(state, memo)
    298         if hasattr(y, '__setstate__'):
    299             y.__setstate__(state)

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/Users/sherlock/anaconda/lib/python3.5/copy.py in _deepcopy_dict(x, memo)
    241     memo[id(x)] = y
    242     for key, value in x.items():
--> 243         y[deepcopy(key, memo)] = deepcopy(value, memo)
    244     return y
    245 d[dict] = _deepcopy_dict

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    180                             raise Error(
    181                                 "un(deep)copyable object of type %s" % cls)
--> 182                 y = _reconstruct(x, rv, 1, memo)
    183 
    184     # If is its own copy, don't memoize.

/Users/sherlock/anaconda/lib/python3.5/copy.py in _reconstruct(x, info, deep, memo)
    295     if state is not None:
    296         if deep:
--> 297             state = deepcopy(state, memo)
    298         if hasattr(y, '__setstate__'):
    299             y.__setstate__(state)

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/Users/sherlock/anaconda/lib/python3.5/copy.py in _deepcopy_dict(x, memo)
    241     memo[id(x)] = y
    242     for key, value in x.items():
--> 243         y[deepcopy(key, memo)] = deepcopy(value, memo)
    244     return y
    245 d[dict] = _deepcopy_dict

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    180                             raise Error(
    181                                 "un(deep)copyable object of type %s" % cls)
--> 182                 y = _reconstruct(x, rv, 1, memo)
    183 
    184     # If is its own copy, don't memoize.

/Users/sherlock/anaconda/lib/python3.5/copy.py in _reconstruct(x, info, deep, memo)
    295     if state is not None:
    296         if deep:
--> 297             state = deepcopy(state, memo)
    298         if hasattr(y, '__setstate__'):
    299             y.__setstate__(state)

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/Users/sherlock/anaconda/lib/python3.5/copy.py in _deepcopy_dict(x, memo)
    241     memo[id(x)] = y
    242     for key, value in x.items():
--> 243         y[deepcopy(key, memo)] = deepcopy(value, memo)
    244     return y
    245 d[dict] = _deepcopy_dict

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/Users/sherlock/anaconda/lib/python3.5/copy.py in _deepcopy_list(x, memo)
    216     memo[id(x)] = y
    217     for a in x:
--> 218         y.append(deepcopy(a, memo))
    219     return y
    220 d[list] = _deepcopy_list

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/Users/sherlock/anaconda/lib/python3.5/copy.py in _deepcopy_tuple(x, memo)
    221 
    222 def _deepcopy_tuple(x, memo):
--> 223     y = [deepcopy(a, memo) for a in x]
    224     # We're not going to put the tuple in the memo, but it's still important we
    225     # check for it, in case the tuple contains recursive mutable structures.

/Users/sherlock/anaconda/lib/python3.5/copy.py in <listcomp>(.0)
    221 
    222 def _deepcopy_tuple(x, memo):
--> 223     y = [deepcopy(a, memo) for a in x]
    224     # We're not going to put the tuple in the memo, but it's still important we
    225     # check for it, in case the tuple contains recursive mutable structures.

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/Users/sherlock/anaconda/lib/python3.5/copy.py in _deepcopy_dict(x, memo)
    241     memo[id(x)] = y
    242     for key, value in x.items():
--> 243         y[deepcopy(key, memo)] = deepcopy(value, memo)
    244     return y
    245 d[dict] = _deepcopy_dict

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    180                             raise Error(
    181                                 "un(deep)copyable object of type %s" % cls)
--> 182                 y = _reconstruct(x, rv, 1, memo)
    183 
    184     # If is its own copy, don't memoize.

/Users/sherlock/anaconda/lib/python3.5/copy.py in _reconstruct(x, info, deep, memo)
    295     if state is not None:
    296         if deep:
--> 297             state = deepcopy(state, memo)
    298         if hasattr(y, '__setstate__'):
    299             y.__setstate__(state)

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/Users/sherlock/anaconda/lib/python3.5/copy.py in _deepcopy_dict(x, memo)
    241     memo[id(x)] = y
    242     for key, value in x.items():
--> 243         y[deepcopy(key, memo)] = deepcopy(value, memo)
    244     return y
    245 d[dict] = _deepcopy_dict

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    180                             raise Error(
    181                                 "un(deep)copyable object of type %s" % cls)
--> 182                 y = _reconstruct(x, rv, 1, memo)
    183 
    184     # If is its own copy, don't memoize.

/Users/sherlock/anaconda/lib/python3.5/copy.py in _reconstruct(x, info, deep, memo)
    295     if state is not None:
    296         if deep:
--> 297             state = deepcopy(state, memo)
    298         if hasattr(y, '__setstate__'):
    299             y.__setstate__(state)

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/Users/sherlock/anaconda/lib/python3.5/copy.py in _deepcopy_dict(x, memo)
    241     memo[id(x)] = y
    242     for key, value in x.items():
--> 243         y[deepcopy(key, memo)] = deepcopy(value, memo)
    244     return y
    245 d[dict] = _deepcopy_dict

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    180                             raise Error(
    181                                 "un(deep)copyable object of type %s" % cls)
--> 182                 y = _reconstruct(x, rv, 1, memo)
    183 
    184     # If is its own copy, don't memoize.

/Users/sherlock/anaconda/lib/python3.5/copy.py in _reconstruct(x, info, deep, memo)
    295     if state is not None:
    296         if deep:
--> 297             state = deepcopy(state, memo)
    298         if hasattr(y, '__setstate__'):
    299             y.__setstate__(state)

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/Users/sherlock/anaconda/lib/python3.5/copy.py in _deepcopy_dict(x, memo)
    241     memo[id(x)] = y
    242     for key, value in x.items():
--> 243         y[deepcopy(key, memo)] = deepcopy(value, memo)
    244     return y
    245 d[dict] = _deepcopy_dict

/Users/sherlock/anaconda/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    180                             raise Error(
    181                                 "un(deep)copyable object of type %s" % cls)
--> 182                 y = _reconstruct(x, rv, 1, memo)
    183 
    184     # If is its own copy, don't memoize.

/Users/sherlock/anaconda/lib/python3.5/copy.py in _reconstruct(x, info, deep, memo)
    304                 slotstate = None
    305             if state is not None:
--> 306                 y.__dict__.update(state)
    307             if slotstate is not None:
    308                 for key, value in slotstate.items():

AttributeError: 'NoneType' object has no attribute 'update'
maxim5 commented 6 years ago

See this answer on StackOverflow and also https://github.com/tensorflow/tensorflow/issues/11157 . Basically, the workaround is this:

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)
nfmcclure commented 6 years ago

Thanks for the bug report, @sherlockhoatszx . And thanks for the workaround, @maxim5 .

I'm just getting around to triaging everything and about to go through and start updating all the code. I'll see if I can apply the fix (or something else by now) to the code.

nfmcclure commented 6 years ago

This should be fixed now. I pushed an update to chapter 9. If you run into this error again, please re-open and also share the Python version and TensorFlow version you are running. Thanks!