Guest User

Untitled

a guest
May 27th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. def unique_variable_finder(key):
  2. """
  3. Attempts to find a variable and return its handle by a key.
  4. Raises error if none found or multiple matches occur.
  5.  
  6. :param key:
  7. :return:
  8. """
  9. variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
  10. variable_names = [variable.name for variable in variables]
  11. found = 0
  12. for i, variable_name in enumerate(variable_names):
  13. if key in variable_name:
  14. found += 1
  15. return_me = variables[i]
  16. assert found > 0, 'Variable not found.'
  17. assert found < 2, 'Multiple matches found.'
  18. return return_me
  19.  
  20. def variable_finder(key):
  21. """
  22. Attempts to find variables and return their handles in a list.
  23. Raises error if none found.
  24.  
  25. :param key:
  26. :return:
  27. """
  28. variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
  29. variable_names = [variable.name for variable in variables]
  30. return_me = []
  31. found = 0
  32. for i, variable_name in enumerate(variable_names):
  33. if key in variable_name:
  34. found += 1
  35. return_me.append(variables[i])
  36. assert found > 0, 'Variable not found.'
  37. return return_me
Add Comment
Please, Sign In to add comment