Skip to content

Instantly share code, notes, and snippets.

@Ending2015a
Last active November 10, 2019 11:47
Show Gist options
  • Save Ending2015a/e53d5c7e92d6dc95fcb30f9bc9c6e51e to your computer and use it in GitHub Desktop.
Save Ending2015a/e53d5c7e92d6dc95fcb30f9bc9c6e51e to your computer and use it in GitHub Desktop.
The minimal code that would cause an unexpected KeyError in TF2.0
import tensorflow as tf #tf2.0
from collections import OrderedDict
class MyNamedTuple(OrderedDict):
def __iter__(self): # iterate over values to unpack, e.g., arg1, arg2, arg3 = MyNamedTuple(...)
for v in self.values():
yield v
class MyModule(tf.Module):
def __call__(self):
self.module = MyNamedTuple({'arg': 'hello'}) # cause KeyError
self.v = tf.Variable([1, 2, 3]) # some variables
module = MyModule('my_module')
module()
print(module.variables) #KeyError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment