Shape, Rank, Axis
Tensor의 Shape와 Rank
>>> import tensorflow as tf
>>> sess = tf.Session()
>>> t1 = tf.constant([1, 2, 3, 4])
>>> tf.shape(t1).eval(session=sess)
array([4])
>>> tf.rank(t1).eval(session=sess)
1
>>> t2 = tf.constant([[1 ,2],
>>> [3, 4]])
>>> tf.shape(t2).eval(session=sess)
array([2, 2])
>>> tf.rank(t2).eval(session=sess)
2
>>> t3 = tf.constant([
[
[
[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]
],
[
[13, 14, 15, 16],
[17, 18, 19, 20],
[21, 22, 23, 24]
]
]
])
>>> tf.shape(t3).eval(session=sess)
array([1, 2, 3, 4])
>>> tf.rank(t3).eval(session=sess)
4
Tensor에서 matmul() 함수와 multiply() 함수의 차이
>>> import tensorflow as tf
>>> sess = tf.Session()
>>> matrix1 = tf.constant([
[1, 2],
[3, 4]
])
>>> matrix2 = tf.constant([
[1],
[2]
])
>>> matrix3 = tf.constant([[1, 2]])
>>> print(f'Matrix 1\'s shape is {matrix1.shape}')
Matrix 1's shape is (2, 2)
>>> print(f'Matrix 2\'s shape is {matrix2.shape}')
Matrix 2's shape is (2, 1)
>>> print(f'Matrix 2\'s shape is {matrix3.shape}')
Matrix 2's shape is (1, 2)
>>> print(tf.matmul(matrix1, matrix2).eval(session=sess))
[[ 5]
[11]]
>>> print(tf.matmul(matrix3, matrix1).eval(session=sess))
[[ 7 10]]
>>> print(tf.matmul(matrix1, matrix2).eval(session=sess))
[[1 2]
[2 4]]
>>> print(tf.matmul(matrix1, matrix3).eval(session=sess))
ValueError: Dimensions must be equal, but are 2 and 1 for 'MatMul_3' (op: 'MatMul') with input shapes: [2,2], [1,2].
>>> print(tf.multiply(matrix1, matrix2).eval(session=sess))
[[1 2]
[6 8]]
>>> print(tf.multiply(matrix3, matrix1).eval(session=sess))
[[1 4]
[3 8]]
>>> print(tf.multiply(matrix1, matrix2).eval(session=sess))
[[1 2]
[2 4]]
>>> print(tf.multiply(matrix1, matrix3).eval(session=sess))
[[1 4]
[3 8]]
reduce_mean() 함수
>>> import tensorflow as tf
>>> sess = tf.Session()
>>> t1 = tf.constant([1., 2.])
>>> t2 = tf.constant([[1., 2.],
[3., 4.]])
>>> print(tf.reduce_mean(t1).eval(session=sess))
1.5
>>> print(tf.reduce_mean(t1, axis=0).eval(session=sess))
1.5
>>> print(tf.reduce_mean(t2).eval(session=sess))
2.5
>>> print(tf.reduce_mean(t2, axis=0).eval(session=sess))
[2. 3.]
>>> print(tf.reduce_mean(t2, axis=1).eval(session=sess))
[1.5 3.5]
>>> print(tf.reduce_mean(t2, axis=-1).eval(session=sess))
[1.5 3.5]
reduce_sum() 함수
>>> import tensorflow as tf
>>> sess = tf.Session()
>>> t = tf.constant([[1., 2.],
[3., 4.]])
>>> print(tf.reduce_sum(t).eval(session=sess))
10.0
>>> print(tf.reduce_sum(t, axis=0).eval(session=sess))
[4. 6.]
>>> print(tf.reduce_sum(t, axis=1).eval(session=sess))
[3. 7.]
>>> print(tf.reduce_mean(tf.reduce_sum(t, axis=0)).eval(session=sess))
5.0
>>> print(tf.reduce_mean(tf.reduce_sum(t, axis=1)).eval(session=sess))
5.0
argmax() 함수와 argmin() 함수
>>> import tensorflow as tf
>>> sess = tf.Session()
>>> t = tf.constant([[1., 4., 5., 6.],
[3., 2., 6., 6.]])
>>> print(tf.argmax(t).eval(session=sess))
[1 0 1 0]
>>> print(tf.argmax(t, axis=0).eval(session=sess))
[1 0 1 0]
>>> print(tf.argmax(t, axis=1).eval(session=sess))
[3 2]
>>> print(tf.argmin(t, axis=0).eval(session=sess))
[0 1 0 0]
>>> print(tf.argmin(t, axis=1).eval(session=sess))
[0 1]
reshape() 함수
>>> import tensorflow as tf
>>> sess = tf.Session()
>>> t = tf.constant([[1., 4., 5., 6.],
[3., 2., 6., 6.]])
>>> print(t.shape)
(2, 2, 3)
>>> # x행 3열의 행렬로 변환(x = 12 / 3 = 4)
>>> print(tf.reshape(t, shape=[-1, 3]).eval(session=sess))
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
>>> # x행 2열의 행렬이 3개로 중첩된 3차원 행렬로 변환(x = 12 / 3 / 2 = 2)
>>> print(tf.reshape(t, shape=[3, 2, -1]).eval(session=sess))
[[[ 1 2]
[ 3 4]]
[[ 5 6]
[ 7 8]]
[[ 9 10]
[11 12]]]
squeeze() 함수
>>> import tensorflow as tf
>>> sess = tf.Session()
>>> t = tf.constant([[1], [2], [3]])
>>> print(tf.rank(t).eval(session=sess))
2
>>> s = tf.squeeze(t)
>>> print(s.eval(session=sess))
[1 2 3]
>>> print(tf.rank(s).eval(session=sess))
1
expaned_dims() 함수
>>> import tensorflow as tf
>>> sess = tf.Session()
>>> t = tf.constant([1, 2, 3, 4])
>>> print(tf.rank(t).eval(session=sess))
1
>>> s = tf.expand_dims(t, axis=0)
>>> print(s.eval(session=sess))
[[1 2 3 4]]
>>> print(tf.rank(s).eval(session=sess))
2
>>> s = tf.expand_dims(t, axis=1)
>>> print(s.eval(session=sess))
[[1]
[2]
[3]
[4]]
>>> print(tf.rank(s).eval(session=sess))
2
one_hot() 함수
>>> import tensorflow as tf
>>> sess = tf.Session()
>>> t = tf.constant([[1], [0], [2], [1], [3]])
>>> s = tf.one_hot(t, depth=4)
>>> print(s.eval(session=sess))
[[[ 0. 1. 0. 0.]]
[[ 1. 0. 0. 0.]]
[[ 0. 0. 1. 0.]]
[[ 0. 1. 0. 0.]]
[[ 0. 0. 0. 1.]]]
>>> print(tf.shape(s).eval(session=sess))
[5 1 4]
>>> s = tf.one_hot(t, depth=6)
>>> print(s.eval(session=sess))
[[[ 0. 1. 0. 0. 0. 0.]]
[[ 1. 0. 0. 0. 0. 0.]]
[[ 0. 0. 1. 0. 0. 0.]]
[[ 0. 1. 0. 0. 0. 0.]]
[[ 0. 0. 0. 1. 0. 0.]]]
>>> print(tf.shape(s).eval(session=sess))
[5 1 6]