Variable Utils

Common helpers for ResourceVariables and RefVariables.

is_read_var_op(op, version=None)[source]

Determines if an op is a read var op.

Checks if it is a ReadVariableOp or an IdentityOp. This is because ResourceVariables use ReadVariableOps and RefVariables use IdentityOps.

Parameters
  • op (Operation) – the operation to inspect

  • version (int) – TF major version integer

Returns

Whether or not the op is a read var op

Return type

bool

get_read_var_ops(var_op, exclude_snapshot=False)[source]

Given a resource handle op, get all its read variable ops.

Parameters
  • var_op (Operation) – VarHandleOp for ResourceVariable, VariableV2 or Variable for RefVariable

  • exclude_snapshot (bool) – whether to skip the default ReadVariableOp bundled (i.e. “/Read/ReadVariableOp”). no extra snapshot to exclude for RefVariable, even if exclude_snapshot=True.

Returns

List of read var ops of it.

Return type

List[Operation]

get_read_var_tensor(var_op)[source]

Given a var op, get the tensor of its default readable value.

Parameters

var_op (Operation) – the variable op

Returns

The tensor of its default readable value

Return type

Tensor

gen_read_var_op(var_op, dtype)[source]

Given a var op, generate the op for reading its value.

Parameters
  • var_op (Operation) – The var op

  • dtype (dtype) – The dtype of the data to read

Returns

The value-reading operation

Return type

Operation