def _dual_var_for_layer(dual_var_getter, layer, name, shape):
  """Creates a dual variable for the given layer shape.

  Args:
    dual_var_getter: Function(name, shape) returning a dual variable.
    layer: Layer for which dual variables should be generated.
    name: Name to use for dual variable.
    shape: Shape of the dual variable, or a possibly nested dict of shapes.

  Returns:
    Dual variable tensors of the given shape, or a possibly nested dict of
    such tensors according to the structure of `shape`.
  """
  if isinstance(shape, dict):
    return {k: _dual_var_for_layer(dual_var_getter, layer, name + '_' + k, v)
            for k, v in shape.items()}
  else:
    return dual_var_getter(layer, name, shape)