def cast_tensor_type(tens, dtype):
    """
    tens: pytorch tens
    dtype: string, eg 'float', 'int', 'byte'
    """
    if dtype is not None:
        assert hasattr(tens, dtype)
        return getattr(tens, dtype)()
    else:
        return tens