[Tensorflow] Tensorflow 함수 정리
Tensorflow 내장함수 설명
Tensorflow 함수에 대해서 알아보고자 합니다.
Useful functions
Tensorflow에서 유용하게 사용하는 함수를 정리하였습니다.
tf.cast
tf.cast(x, dtype)
tf.cast는 x
를 dtype
에 맞는 새로운 자료형으로 변환합니다.
tf.identity
tf.identity(input)
tf.identity는 Tensor
복사 기능으로 input
과 동일한 shape 및 contents를 반환합니다.
tf.one_hot
tf.one_hot(indices, depth, on_value=None, off_value=None, axis=None, dtype=None)
tf.one_hot은 one-hot 인코딩하는 함수입니다. 기본적으로는 아래와 같이 사용됩니다.
tf.one_hot(indices=[0, 1, 2], depth=3)
# output shape: [3 x 3]
[[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]]
저는 주로 Tensor에서 원하는 부분을 0으로 변경하기 위해 사용합니다.
tf.one_hot을 통해 단위행렬(identity matrix)을 만든 후 0으로 masking할 구간의 인덱스를 -1로 설정합니다.
mask_indices = np.arange(5) # output: [ 0 1 2 3 4]
mask_indices[1:3] = -1 # output: [ 0 -1 -1 3 4]
mask = tf.one_hot(indices=mask_indices, depth=5, dtype=tf.int32)
print(mask)
# output shape: [5 x 5]
[[1 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 1 0]
[0 0 0 0 1]]
example = tf.fill([5, 5], 3)
print(example)
# output shape: [5 x 5]
[[3 3 3 3 3]
[3 3 3 3 3]
[3 3 3 3 3]
[3 3 3 3 3]
[3 3 3 3 3]]
print(tf.linalg.matmul(example, mask))
# output shape: [5 x 5]
[[3 0 0 3 3]
[3 0 0 3 3]
[3 0 0 3 3]
[3 0 0 3 3]
[3 0 0 3 3]]
Boolean functions
True 또는 False를 반환해주는 함수들입니다.
tf.cond
tf.cond(pred, true_fn=None, false_fn=None)
Predicate pred
가 true이면 true_fn
을, false면 false_fn
을 반환해줍니다.
tf.math.less
tf.math.less(x, y)
y
가 x
보다 크면 true를, 작거나 같으면 fasle를 반환홥니다.
tf.math.equal
tf.math.eqaul(x, y)
x
와 y
를 비교하여 두 텐서의 원소가 같으면 True
를, 같지 않다면 False
를 출력하여 x
및 y
의 shape과 동일한 크기의 bool
type tensor를 반환합니다.
Etc
기타 함수들입니다.
tf.fill
tf.fill(dims, value)
tf.fill은 동일한 상수 값으로 채워진 텐서를 생성합니다.
tf.fill([10], 3)
# output shape: [10,]
[3 3 3 3 3 3 3 3 3 3]
tf.fill([2, 3], 5)
# output shape: [2 x 3]
[[5 5 5]
[5 5 5]]
tf.fill([2, 3, 5], 6)
# output shape: [2 x 3 x 5]
[[[6 6 6 6 6]
[6 6 6 6 6]
[6 6 6 6 6]]
[[6 6 6 6 6]
[6 6 6 6 6]
[6 6 6 6 6]]]
tf.tile
tf.tile(input, multiples)
tf.tile를 통해 Tensor를 복사해서 붙여널을 수 있으며 np.tile과 유사합니다.
multiples를 통해 각 axis에 대해 얼마나 복붙할 것인지 정해줍니다.
example1 = tf.constant([1, 2, 3, 4, 5])
print(example1)
# output shape: [5,]
[1 2 3 4 5]
result1 = tf.tile(example1, multiples=[3])
print(result1)
# output shape: [15,]
[1 2 3 4 5 1 2 3 4 5 1 2 3 4 5]
example2 = tf.constant([[1, 2, 3],
[4, 5, 6]])
result2 = tf.tile(example2, multiples=[3, 2])
print(result2)
# output shape: [6 x 6]
[[1 2 3 1 2 3]
[4 5 6 4 5 6]
[1 2 3 1 2 3]
[4 5 6 4 5 6]
[1 2 3 1 2 3]
[4 5 6 4 5 6]]
tf.Graph()
[1] https://sdc-james.gitbook.io/onebook/4.-and/5.3.-mnist-dataset/5.4.1.-tensorflow#:~:text=TensorFlow%20graph%EB%8A%94%20Python%EC%9D%98,%EA%B3%84%EC%82%B0%EC%9D%84%20%EC%88%98%ED%96%89%ED%95%98%EC%A7%80%20%EC%95%8A%EC%8A%B5%EB%8B%88%EB%8B%A4.&text=%EB%B9%8C%EB%93%9C%20%EC%A4%80%EB%B9%84%EA%B0%80%20%EB%81%9D%EB%82%98%EA%B3%A0%20%EB%AA%A8%EB%93%A0,session%EC%9D%84%20%EC%83%9D%EC%84%B1%ED%95%B4%EC%95%BC%20%ED%95%A9%EB%8B%88%EB%8B%A4.
Reference
tf.less
[1] https://docs.w3cub.com/tensorflow~python/tf/less
[2] https://docs.w3cub.com/tensorflow~python/tf/less
tf.cast
[1] https://codetorial.net/tensorflow/reference/tf_cast.html
tf.equal
[1] https://chan-lab.tistory.com/9 [2] https://docs.w3cub.com/tensorflow~python/tf/equal