일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 백준
- 시간초과
- mysql
- Queue
- c++
- 프로그래머스
- deep learning
- Linear Regression
- Neural Network
- 정렬
- DFS
- 알고리즘 고득점 kit
- join
- 큐
- SQL
- sort
- deque
- 모두를 위한 딥러닝
- 한화오션
- TensorFlow
- stl
- Programmers
- PIR
- CSAP
- sung kim
- 모두를 위한 머신러닝
- BOJ
- Machine learning
- softmax
- ML
Archives
- Today
- Total
hello, world!
[ML lab 08] Tensor Manipulation 본문
Axis(축)
가장 바깥 []이 0번 axis이고 안쪽으로 들어갈수록 커진다.
가장 안쪽 []은 -1번이라고 하기도 한다.
matmul() 등 matrix 연산을 할 때에는 shape을 보고 가능한지 주의해야한다.
ex. (2, 2)와 (2,1)의 multiply는 가능하다.
broadcasting (warning!)
shape이 동일하지 않은 경우에 matrix 연산을 시도하면, 연산을 할 수 있도록 shape을 자동으로 맞추어 준다.
reduce_mean()
항상 각 element가 float type이 되도록 주의해야 한다.
축(axis)을 무엇으로 정하는지에 따라 다른 값이 나온다. (축을 명시하지 않으면 모든 element의 평균을 구한다.)
tf.reduce_mean([1, 2], axis = 0).eval()
# 1
x = [[1., 2.],
[3., 4.]]
tf.reduce_mean(x).eval()
# 2.5
tf.reduce_mean(x, axis = 0).eval()
# array([2., 3.], dtype = float32)
tf.reduce_mean(x, axis = 1).eval()
# array([1.5, 3.5], dtype = float32)
tf.reduce_mean(x, axis = -1).eval()
# array([1.5, 3.5], dtype = float32)
argmax()
최대값의 위치를 반환한다.
x = [[0, 1, 2],
[2, 1, 0]]
tf.argmax(x, axis = 0).eval()
# array([1, 0, 0])
tf.argmax(x, axis = 1).eval()
# array([2, 0])
tf.argmax(x, axis = -1).eval()
# array([2, 0])
reshape()
X = [[[0, 1, 2],
[2, 1, 0]],
[[6, 7, 8],
[9, 10, 11]]]
t.shape
# (2, 2, 3)
tf.reshape(t, shape[-1, 3]).eval()
"""
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9, 10, 11]])
"""
tf.reshape(t, shape = [-1, 1, 3]).eval()
"""
array([[[0, 1, 2]],
[[3, 4, 5]],
[[6, 7, 8]],
[[9, 10, 11]]])
"""
squeeze()와 expand_dims()를 이용한 reshape
tf.squeeze([[0], [1], [2]]).eval()
# array([0, 1, 2], dtype = int32
tf.expand_dims([0, 1, 2], 1).eval()
"""
array([0],
[1],
[2]], dtype = int32)
"""
one_hot()
tf.one_hot([[0], [1], [2], [0]], depth = 3).eval() # depth는 0~2라서 3
"""
array([[[1., 0., 0.]],
[[0., 1., 0.]],
[[0., 0., 1.]],
[[1., 0., 0.]]], dtype = float32)
"""
# one_hot()을 하면 자동으로 1차원이 커진다.
# 따라서 reshape을 통해 원래의 차원으로 바꿔준다.
t = tf.one_hot([[0], [1], [2], [0]], depth = 3)
tf.reshape(t, shape = [-1, 3]).eval()
"""
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
[1., 0., 0.]], dtype = float32)
"""
cast()
tf.cast([1.8, 2.2, 3.3, 4.9], tf.int32).eval()
# array([1, 2, 3, 4], dtype = int32)
tf.cast([True, False, 1==1, 0==1], tf.int32).eval()
# array([1, 0, 1, 0], dtype = int 32)
stack()
x = [1, 4]
y = [2, 5]
z = [3, 6]
# Pack along first dim
tf.stack([x, y, z]).eval()
"""
array([[1, 4],
[2, 5],
[3, 6], dtype = int32)
"""
tf.stack([x, y, z], axis = 1).eval()
"""
array([[1, 2, 3],
[4, 5, 6], dtype = int32)
"""
ones_like() and zeros_like()
x = [[0, 1, 2],
[2, 1, 0]]
tf.ones_like(x).eval()
"""
array([[1, 1, 1],
[1, 1, 1]], dtype = int32)
"""
tf.zeros_like(x.eval()
"""
array([[0, 0, 0],
[0, 0, 0]], dtype = int32)
"""
zip()
for x, y in zip([1, 2, 3], [4, 5, 6]):
print(x, y)
"""
1 4
2 5
3 6
"""
for x, y, z in zip([1, 2, 3], [4, 5, 6], [7, 8, 9]):
print(x, y, z)
"""
1 4 7
2 5 8
3 6 9
"""
'AI > 모두를 위한 ML (SungKim)' 카테고리의 다른 글
[ML lab 09-2] Tensorboard (Neural Net for XOR) (0) | 2021.02.21 |
---|---|
[ML lab 09-1] Neural Net for XOR (0) | 2021.02.21 |
[ML alb 07-2] Meet MNIST Dataset (0) | 2021.02.20 |
[ML lab 07-1] training/test dataset, learning rate, normalization (0) | 2021.02.20 |
[ML lab 06-2] TensorFlow로 Fancy Softmax Classification 구현하기 (0) | 2021.02.20 |
Comments