Given a variable name, retrieves a handle on the tensorflow Variable.
(var_name)
| 171 | |
| 172 | |
| 173 | def get_variable_by_name(var_name): |
| 174 | """Given a variable name, retrieves a handle on the tensorflow Variable.""" |
| 175 | |
| 176 | candidate_vars = ops.get_collection( |
| 177 | ops.GraphKeys.GLOBAL_VARIABLES, scope="{}:0".format(var_name)) |
| 178 | if len(candidate_vars) >= 1: |
| 179 | # Filter out non-trainable variables. |
| 180 | candidate_vars = [v for v in candidate_vars if v.trainable] |
| 181 | else: |
| 182 | raise ValueError("Unsuccessful at finding variable {}.".format(var_name)) |
| 183 | |
| 184 | if len(candidate_vars) == 1: |
| 185 | return candidate_vars[0] |
| 186 | elif len(candidate_vars) > 1: |
| 187 | raise ValueError( |
| 188 | "Unsuccessful at finding trainable variable {}. " |
| 189 | "Number of candidates: {}. " |
| 190 | "Candidates: {}".format(var_name, len(candidate_vars), candidate_vars)) |
| 191 | else: |
| 192 | # The variable is not trainable. |
| 193 | return None |
| 194 | |
| 195 | |
| 196 | def get_dependent_variables(input_ops, output_ops): |
no test coverage detected