def get_zeros_dtype(t): """Return the dtype for the default gradient for a Tensor.""" if t.dtype == dtypes.resource: handle_data = resource_variable_ops.get_eager_safe_handle_data(t) if (handle_data is None or not handle_data.is_set or len(handle_data.shape_and_type) != 1): raise ValueError("Internal error: Tried to take gradients (or similar) " "of a variable without handle data:\n%s" % str(t)) return handle_data.shape_and_type[0].dtype return t.dtype
def shape_and_dtype(t): """Return the shape and dtype for the default gradient for a Tensor.""" if t.dtype == dtypes.resource: handle_data = resource_variable_ops.get_eager_safe_handle_data(t) if (handle_data is None or not handle_data.is_set or len(handle_data.shape_and_type) != 1): return tensor_shape.TensorShape(None), dtypes.float32 else: shape_and_type = handle_data.shape_and_type[0] return (tensor_shape.TensorShape(shape_and_type.shape), dtypes.as_dtype(shape_and_type.dtype)) return t.shape, t.dtype
def get_zeros_dtype(t): """Return the dtype for the default gradient for a Tensor.""" if t.dtype == dtypes.resource: handle_data = resource_variable_ops.get_eager_safe_handle_data(t) if (handle_data is None or not handle_data.is_set or len(handle_data.shape_and_type) != 1): # TODO(srbs): Ideally we should raise an error here but returning float32 # for backwards compatibility. return dtypes.float32 else: return handle_data.shape_and_type[0].dtype return t.dtype
def supports_default_grad(t): """Whether tensor `t` supports creating a default gradient. This function assumes that `t` is of a trainable type. Args: t: Tensor Returns: Bool """ if t.dtype == dtypes.resource: handle_data = resource_variable_ops.get_eager_safe_handle_data(t) if (handle_data is None or not handle_data.is_set or len(handle_data.shape_and_type) != 1): return False return True
def _get_handle_data(tensor): return resource_variable_ops.get_eager_safe_handle_data(tensor)