Returns the value of the attr of this op with the given `name`. Args: name: The name of the attr to fetch. Returns: The value of the attr, as a Python object. Raises: ValueError: If this op does not have an attr with the given `name`.
(self, name)
| 2363 | # pylint: enable=protected-access |
| 2364 | |
| 2365 | def get_attr(self, name): |
| 2366 | """Returns the value of the attr of this op with the given `name`. |
| 2367 | |
| 2368 | Args: |
| 2369 | name: The name of the attr to fetch. |
| 2370 | |
| 2371 | Returns: |
| 2372 | The value of the attr, as a Python object. |
| 2373 | |
| 2374 | Raises: |
| 2375 | ValueError: If this op does not have an attr with the given `name`. |
| 2376 | """ |
| 2377 | fields = ("s", "i", "f", "b", "type", "shape", "tensor", "func") |
| 2378 | try: |
| 2379 | with c_api_util.tf_buffer() as buf: |
| 2380 | c_api.TF_OperationGetAttrValueProto(self._c_op, name, buf) |
| 2381 | data = c_api.TF_GetBuffer(buf) |
| 2382 | except errors.InvalidArgumentError as e: |
| 2383 | # Convert to ValueError for backwards compatibility. |
| 2384 | raise ValueError(str(e)) |
| 2385 | x = attr_value_pb2.AttrValue() |
| 2386 | x.ParseFromString(data) |
| 2387 | |
| 2388 | oneof_value = x.WhichOneof("value") |
| 2389 | if oneof_value is None: |
| 2390 | return [] |
| 2391 | if oneof_value == "list": |
| 2392 | for f in fields: |
| 2393 | if getattr(x.list, f): |
| 2394 | if f == "type": |
| 2395 | return [dtypes.as_dtype(t) for t in x.list.type] |
| 2396 | else: |
| 2397 | return list(getattr(x.list, f)) |
| 2398 | return [] |
| 2399 | if oneof_value == "type": |
| 2400 | return dtypes.as_dtype(x.type) |
| 2401 | assert oneof_value in fields, "Unsupported field type in " + str(x) |
| 2402 | return getattr(x, oneof_value) |
| 2403 | |
| 2404 | def _get_attr_type(self, name): |
| 2405 | """Returns the value of the attr of this op with the given `name`. |