def sg_cast(tensor, opt):
    r"""Casts a tensor to a new type.
    
    See `tf.cast()` in tensorflow.

    Args:
      tensor: A `Tensor` or `SparseTensor` (automatically given by chain).
      opt:
        dtype : The destination type.
        name : If provided, it replaces current tensor's name

    Returns:
      A `Tensor` or `SparseTensor` with same shape as `tensor`.
    """
    assert opt.dtype is not None, 'dtype is mandatory.'
    return tf.cast(tensor, opt.dtype, name=opt.name)