TensorFlow
TensorFlow 기초 5 - 사칙연산, 관계연산, 논리연산, 차원 축소, 차원 확대, one-hot, argmax
코딩탕탕
2022. 11. 28. 17:37
# 연산자와 기본 함수 경험
import tensorflow as tf
import numpy as np
x = tf.constant(7)
y = tf.constant(3)
print(x + y)
print(tf.add(x, y))
print(tf.cond(x > y, lambda:tf.add(x, y), lambda:tf.subtract(x, y)))
f1 = lambda:tf.constant(123)
f2 = lambda:tf.constant(456)
print(tf.case([(tf.greater(x, y), f1)], default=f2).numpy()) # tf.less if(x > y) return 123 else return 456
print('관계 연산')
print(tf.equal(1, 2).numpy())
print(tf.not_equal(1, 2).numpy())
print(tf.greater(1, 2).numpy())
print(tf.greater_equal(1, 2).numpy())
print(tf.less(1, 2).numpy())
print('논리 연산')
print(tf.logical_and(True, False).numpy())
print(tf.logical_or(True, False).numpy())
print(tf.logical_not(True).numpy())
kbs = tf.constant([1, 2, 2, 2, 3])
val, idx = tf.unique(kbs)
print(val.numpy()) # unique
print(idx.numpy()) # index
print()
# tf.reduce~ : 차원 축소
ar = [[1,2],[3,4]]
print(tf.reduce_mean(ar).numpy()) # 전체 평균
print(tf.reduce_mean(ar, axis=0).numpy()) # 열 평균
print(tf.reduce_mean(ar, axis=1).numpy()) # 행 평균
t = np.array([[[0,1,2],[3,4,5],[6,7,8],[9,10,11]]])
print(t.shape)
print(tf.reshape(t, shape=[2, 6])) # 2행 6열 차원 변경
print(tf.reshape(t, shape=[-1, 6])) # 6열으로 지정해놨기 때문에 자동으로 변경
print(tf.reshape(t, shape=[2, -1])) # 2행으로 지정해놨기 때문에 자동으로 변경
# 차원 축소
aa = np.array([[1],[2],[3],[4]])
print(aa.shape)
print(aa)
bb = tf.squeeze(aa) # 열의 갯수가 1인 경우에만 차원이 축소된다.
print(bb.shape)
print(bb)
aa2 = np.array([[[1],[2]],[[3],[4]]])
print(aa2.shape)
print(aa2)
bb2 = tf.squeeze(aa2)
print(bb2.shape)
print(bb2)
print()
print(t.shape)
t2 = tf.squeeze(t) # 열 요소가 한 개일 때만 차원 축소
print(t2.shape)
print(t2)
print()
# 차원 확대
tarr = tf.constant([[1,2,3],[4,5,6]])
print(tf.shape(tarr))
print()
sbs = tf.expand_dims(tarr, 0) # 첫번째 차원을 추가해 확장
print(sbs, tf.shape(sbs).numpy())
print()
sbs2 = tf.expand_dims(tarr, 1) # 두번째 차원을 추가해 확장
print(sbs2, tf.shape(sbs2).numpy())
print()
sbs3 = tf.expand_dims(tarr, 2) # 세번째 차원을 추가해 확장
print(sbs3, tf.shape(sbs3).numpy())
print()
sbs4 = tf.expand_dims(tarr, -1) # 마지막 번째 차원을 추가해 확장
print(sbs4, tf.shape(sbs4).numpy())
print('one-hot, argmax')
print(tf.one_hot([0,1,2,3],depth=4)) # depth는 열 갯수 지정
print(tf.argmax(tf.one_hot([0,1,2,3],depth=4)).numpy())
<console>
tf.Tensor(10, shape=(), dtype=int32)
tf.Tensor(10, shape=(), dtype=int32)
tf.Tensor(10, shape=(), dtype=int32)
123
관계 연산
False
True
False
False
True
논리 연산
False
True
False
[1 2 3]
[0 1 1 1 2]
2
[2 3]
[1 3]
(1, 4, 3)
tf.Tensor(
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]], shape=(2, 6), dtype=int32)
tf.Tensor(
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]], shape=(2, 6), dtype=int32)
tf.Tensor(
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]], shape=(2, 6), dtype=int32)
(4, 1)
[[1]
[2]
[3]
[4]]
(4,)
tf.Tensor([1 2 3 4], shape=(4,), dtype=int32)
(2, 2, 1)
[[[1]
[2]]
[[3]
[4]]]
(2, 2)
tf.Tensor(
[[1 2]
[3 4]], shape=(2, 2), dtype=int32)
(1, 4, 3)
(4, 3)
tf.Tensor(
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]], shape=(4, 3), dtype=int32)
tf.Tensor([2 3], shape=(2,), dtype=int32)
tf.Tensor(
[[[1 2 3]
[4 5 6]]], shape=(1, 2, 3), dtype=int32) [1 2 3]
tf.Tensor(
[[[1 2 3]]
[[4 5 6]]], shape=(2, 1, 3), dtype=int32) [2 1 3]
tf.Tensor(
[[[1]
[2]
[3]]
[[4]
[5]
[6]]], shape=(2, 3, 1), dtype=int32) [2 3 1]
tf.Tensor(
[[[1]
[2]
[3]]
[[4]
[5]
[6]]], shape=(2, 3, 1), dtype=int32) [2 3 1]
one-hot, argmax
tf.Tensor(
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]], shape=(4, 4), dtype=float32)
[0 1 2 3]
차원 축소
squeeze() = 열 요소가 한 개일 때만 차원 축소
reduce~ = 행, 열을 선언하면 그에 맞게 차원 축소
faltten() = 차원 축소
차원 확대
expand_dims(x, 0) 숫자에 따라 인덱스에 차원을 추가해 확장
one-hot(원핫)인코딩이란?
단 하나의 값만 True이고 나머지는 모두 False인 인코딩을 말한다.