요즘에 이런저런 코드를 보다보니까, 제가 텐서플로우의 메소드를 잘 모른다는 것을 깨달았습니다.
따라서 코드 리뷰를 진행하면서 몰랐던 메소드에 대해서 공식 홈페이지를 통해서 확인하고, 이에 대한 간단한 사용법을 한글로
리포스팅하려고 합니다.
오늘 포스팅은 tf.range에 대해서 포스팅하려고 합니다.
1. tf.range
tf.range는 Tensorflow Document에 나와있습니다.
range(limit, delta=1, dtype=None, name='range') range(start, limit, delta=1, dtype=None, name='range')
정의는 tensorflow/python/ops/math_ops.py 에 되어있다고 합니다.
설명은 아래와 같이 되어있습니다.
Creates a sequence of numbers.
Creates a sequence of numbers that begins at start
and extends by
increments of delta
up to but not including limit
.
The dtype of the resulting tensor is inferred from the inputs unless it is provided explicitly.
Like the Python builtin range
, start
defaults to 0, so that
range(n) = range(0, n)
.
순차적인 숫자를 만듭니다.
시작은 start에서 시작하고, 끝은 limit까지 끝나며, 증가 스텝은 delta입니다.
출력 tensor의 dtype은 따로 명시하지 않은 이상 입력값에서 추론됩니다. (입력값의 dtype을 따릅니다.)
공식 Document에서 제공해주는 예제는 다음과 같습니다.
start = 3 limit = 18 delta = 3 tf.range(start, limit, delta) # [3, 6, 9, 12, 15] start = 3 limit = 1 delta = -0.5 tf.range(start, limit, delta) # [3, 2.5, 2, 1.5] limit = 5 tf.range(limit) # [0, 1, 2, 3, 4]
파라미터는 다음과 같습니다.
Start :
0-D Tensor (Scalar), limit이 None이 아니라면 제일 첫번째 파라미터값이 Start로 적용됩니다. 그렇지 않으면 limit으로 작동합니다. Start의 default값은 0입니다.
limit :
0-D Tensor (Scalar), 상한선을 의미합니다. 만약 None이라면 Start가 0이고, limit값이 Start값으로 설정됩니다.
delta :
0-D Tensor (Scalar), 숫자를 얼마만큼 증가시킬지에 대한 값입니다. default는 1입니다.
dtype :
결과 tensor에 대한 dytype입니다. default는 입력 tensor의 dtype입니다.
name :
해당 operation의 이름입니다. default는 "range"입니다.
Return :
특정 dtype의 1-D Tensor 입니다.
* np.arange와 같은 방식으로 작동합니다.
2. define
def range(start, limit=None, delta=1, dtype=None, name="range"): """Creates a sequence of numbers. Creates a sequence of numbers that begins at `start` and extends by increments of `delta` up to but not including `limit`. The dtype of the resulting tensor is inferred from the inputs unless it is provided explicitly. Like the Python builtin `range`, `start` defaults to 0, so that `range(n) = range(0, n)`. For example: ```python start = 3 limit = 18 delta = 3 tf.range(start, limit, delta) # [3, 6, 9, 12, 15] start = 3 limit = 1 delta = -0.5 tf.range(start, limit, delta) # [3, 2.5, 2, 1.5] limit = 5 tf.range(limit) # [0, 1, 2, 3, 4] ``` Args: start: A 0-D `Tensor` (scalar). Acts as first entry in the range if `limit` is not None; otherwise, acts as range limit and first entry defaults to 0. limit: A 0-D `Tensor` (scalar). Upper limit of sequence, exclusive. If None, defaults to the value of `start` while the first entry of the range defaults to 0. delta: A 0-D `Tensor` (scalar). Number that increments `start`. Defaults to 1. dtype: The type of the elements of the resulting tensor. name: A name for the operation. Defaults to "range". Returns: An 1-D `Tensor` of type `dtype`. @compatibility(numpy) Equivalent to np.arange @end_compatibility """ if limit is None: start, limit = 0, start with ops.name_scope(name, "Range", [start, limit, delta]) as name: start = ops.convert_to_tensor(start, dtype=dtype, name="start") limit = ops.convert_to_tensor(limit, dtype=dtype, name="limit") delta = ops.convert_to_tensor(delta, dtype=dtype, name="delta") # infer dtype if not explicitly provided if dtype is None: dtype_hierarchy = [ dtypes.int32, dtypes.int64, dtypes.float32, dtypes.float64 ] assert all(arg.dtype in dtype_hierarchy for arg in [start, limit, delta]) inferred_dtype = max( [arg.dtype for arg in [start, limit, delta]], key=dtype_hierarchy.index) start = cast(start, inferred_dtype) limit = cast(limit, inferred_dtype) delta = cast(delta, inferred_dtype) return gen_math_ops._range(start, limit, delta, name=name)
3. _range :: gen_math_ops
Tensorflow Package가 설치된 파일들을 살펴보면, gen_math_ops.py를 찾을 수 있습니다.
gen_math_ops.py의 _range는 다음과 같이 정의되어있습니다.
이....이건... Tensorflow 밑바닥까지 내려가봐야할 것 같습니다.
이런 부분은 나중에 더 자세히 다루도록 하겠습니다.
def _range(start, limit, delta, name=None): r"""Creates a sequence of numbers. This operation creates a sequence of numbers that begins at `start` and extends by increments of `delta` up to but not including `limit`. For example: ``` # 'start' is 3 # 'limit' is 18 # 'delta' is 3 tf.range(start, limit, delta) ==> [3, 6, 9, 12, 15] ``` Args: start: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `int64`. 0-D (scalar). First entry in the sequence. limit: A `Tensor`. Must have the same type as `start`. 0-D (scalar). Upper limit of sequence, exclusive. delta: A `Tensor`. Must have the same type as `start`. 0-D (scalar). Optional. Default is 1. Number that increments `start`. name: A name for the operation (optional). Returns: A `Tensor`. Has the same type as `start`. 1-D. """ _ctx = _context.context() if _ctx.in_graph_mode(): _, _, _op = _op_def_lib._apply_op_helper( "Range", start=start, limit=limit, delta=delta, name=name) _result = _op.outputs[:] _inputs_flat = _op.inputs _attrs = ("Tidx", _op.get_attr("Tidx")) else: _attr_Tidx, _inputs_Tidx = _execute.args_to_matching_eager([start, limit, delta], _ctx, _dtypes.int32) (start, limit, delta) = _inputs_Tidx _attr_Tidx = _attr_Tidx.as_datatype_enum _inputs_flat = [start, limit, delta] _attrs = ("Tidx", _attr_Tidx) _result = _execute.execute(b"Range", 1, inputs=_inputs_flat, attrs=_attrs, ctx=_ctx, name=name) _execute.record_gradient( "Range", _inputs_flat, _attrs, _result, name) _result, = _result return _result
'IT > TensorFlow' 카테고리의 다른 글
[Tensorflow] tf.tile (0) | 2017.12.13 |
---|---|
[VggNet16 / 일지] VggNet Scratch from bottom with Tensorflow (0) | 2017.10.23 |
Install GPU enabled TensorFlow in ubuntu 16.04 (CUDA7.5/CUDNN v4/ALIENWARE 17 R3) (6) | 2016.11.16 |