(2주차) 3월14일
import tensorflow as tf
import numpy as np
tf.config.experimental.list_physical_devices('GPU') # check GPU
- (2,) vector
lst = [1,2]
lst
lst[0] #
lst[1]
- (2,2) matrix
lst = [[1,2],[3,4]]
lst[0][0]
lst[1][1]
이것은 아래와 같은 매트릭스처럼 생각할 수 있음
1 2
3 4
print(lst[0][0]) # (1,1)
print(lst[0][1]) # (1,2)
print(lst[1][0]) # (2,1)
print(lst[1][1]) # (2,2)
매트릭스는 아니지만 매트릭스 같음
- (4,1) matrix
lst = [[1,2],[3,4],[5,6],[7,8]]
lst # (4,2)
lst = [[1],[3],[5],[7]]
lst # (4,1) matrix = 길이가 4인 col-vector
- (1,4) matrix
lst = [[1,2,3,4],[5,6,7,8]]
lst # (2,4) matrix
lst = [[1,2,3,4]]
lst # (1,4) matrix = 길이가 4인 row-vector
- 3차원
lst = [[[1,2],[3,4]], [[5,6],[7,8]]]
lst
print(lst[0][0][0])
print(lst[0][0][1])
print(lst[0][1][0])
print(lst[0][1][1])
print(lst[1][0][0])
print(lst[1][0][1])
print(lst[1][1][0])
print(lst[1][1][1])
- 스칼라
_scalar =tf.constant(1)
_scalar
- 벡터
_vector=tf.constant([1,2,3])
_vector
_vector[-2]
- 매트릭스
_matrix=tf.constant([[1,2],[3,4]])
_matrix
_matrix[0,1]
type(_scalar)
_matrix=tf.constant([[1,2,3],[3,4,5]])
_matrix
_matrix[0,:]
_matrix[:,0]
_matrix[:,-1]
- 불편한점
- dtype이 모든 원소가 똑같아야한다. (그럴수 있음)
- 값을 바꿀 수가 없다.
- dtype이 다르면 연산이 불가능함.
- 값을 바꿀 수 없다.
_matrix=tf.constant([[1,2,3],[3,4,5]])
_matrix
_matrix[0,0]=22
- dtype이 다르면 연산이 불가능함
tf.constant([1.1,2])+tf.constant([3,4])
tf.constant([1.1,2])+tf.constant([3.0,4.0])
- 같은 float형이라도 불가능할 때가 있음
tf.constant([1.1,2])
tf.constant([3.0,4.0],dtype=tf.float64)
tf.constant([1.1,2])+tf.constant([3.0,4.0],dtype=tf.float64)
_vector = tf.constant([1,2,3,4])
_vector
np.array(_vector) # 방법1
_vector.numpy()
- 더하기
a=tf.constant([1,2,3])
b=tf.constant([4,5,6])
a+b
tf.add(a,b)
- 곱하기
a=tf.constant([[1,2],[3,4]])
b=tf.constant([[4,5],[6,7]])
a*b
a,b
tf.multiply(a,b)
- 행렬곱
a=tf.constant([[1,0],[0,1]])
b= tf.constant([[1],[22]])
a @ b # (2,2) matrix @ (2,1) matrix
b @ a # (2,1) matrix @ (2,2) matrix
tf.matmul(a,b)
- 역행렬
a= tf.constant([[1,0],[0,2]])
a
tf.linalg.inv(a)
a= tf.constant([[1.0,2.0],[3.0,4.0]])
a
tf.linalg.inv(a)
a @ tf.linalg.inv(a)
- tf.linalg. + tab을 누르면 쓸만한게 조금 나온다.
a=tf.constant([[1.0,2.0],[3.0,4.0]])
a
tf.linalg.det(a)
tf.linalg.trace(a)
- 단순한 집계함수들
a=tf.constant([[1,2,3],[4,5,6]])
a
tf.reduce_sum(a)
tf.reduce_max(a)
- 행렬곱 고급
_I=tf.constant([[1.0,0.0],[0.0,1.0]])
_I
_x = tf.constant([22.0,33.0])
_x
(상황1)
_I @ _x
_x @ _I
(상황2)
_x = tf.constant([[1.0],[2.0]])
_x
_I @ _x
_x @ _I
(상황3)
_x = tf.constant([[1.0,2.0]])
_x
_I @ _x
_x @ _I
(넘파이는 다르다..)
np.array([22,33]) @ np.diag([1,1]) # (2,) @ (2,2) ==> (2,)를 (1,2)벡터로 해석해줘서 곱하기 수행, 수행결과는 다시 (2,)로 저장
np.array([[22],[33]]) @ np.diag([1,1]) # (2,1) @ (2,2)
- tf.reshape 기본
a=tf.constant([1,2,3,4])
a
tf.reshape(a,(2,2))
tf.reshape(a,(4,1))
tf.reshape(a,(1,4))
- tf.reshape 응용
a= tf.constant([0,1,2,3,4,5,6,7,8,9,10,11]) # length 12 vector
a
tf.reshape(a,(4,3))
tf.reshape(a,(2,2,3))
tf.reshape(a,(3,-1))
tf.reshape(a,(2,2,-1))
b=tf.reshape(a,(2,2,-1))
b
tf.reshape(b,-1)
- 대각행렬선언
tf.constant(np.diag([1,2,3]))
- 1만 포함한 텐서를 만들고 싶음
tf.ones([3,4])
tf.reshape(tf.constant([1]*12),(3,4))