강의영상

import

import tensorflow as tf
import numpy as np
tf.config.experimental.list_physical_devices('GPU') # check GPU 
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

tf.constant

예비학습: 중첩리스트

- (2,) vector

lst = [1,2] 
lst
[1, 2]
lst[0] # 
1
lst[1]
2

- (2,2) matrix

lst = [[1,2],[3,4]]
lst[0][0]
1
lst[1][1]
4

이것은 아래와 같은 매트릭스처럼 생각할 수 있음

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) 
1
2
3
4

매트릭스는 아니지만 매트릭스 같음

- (4,1) matrix

lst = [[1,2],[3,4],[5,6],[7,8]]
lst # (4,2) 
[[1, 2], [3, 4], [5, 6], [7, 8]]
lst = [[1],[3],[5],[7]]
lst # (4,1) matrix = 길이가 4인 col-vector 
[[1], [3], [5], [7]]

- (1,4) matrix

lst = [[1,2,3,4],[5,6,7,8]]
lst # (2,4) matrix  
[[1, 2, 3, 4], [5, 6, 7, 8]]
lst = [[1,2,3,4]]
lst # (1,4) matrix = 길이가 4인 row-vector  
[[1, 2, 3, 4]]

- 3차원

lst = [[[1,2],[3,4]], [[5,6],[7,8]]]
lst 
[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
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])
1
2
3
4
5
6
7
8

선언

- 스칼라

_scalar =tf.constant(1)
_scalar
<tf.Tensor: shape=(), dtype=int32, numpy=1>

- 벡터

_vector=tf.constant([1,2,3])
_vector
<tf.Tensor: shape=(3,), dtype=int32, numpy=array([1, 2, 3], dtype=int32)>
_vector[-2]
<tf.Tensor: shape=(), dtype=int32, numpy=2>

- 매트릭스

_matrix=tf.constant([[1,2],[3,4]])
_matrix
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
       [3, 4]], dtype=int32)>
_matrix[0,1]
<tf.Tensor: shape=(), dtype=int32, numpy=2>

타입

type(_scalar)
tensorflow.python.framework.ops.EagerTensor

인덱싱

_matrix=tf.constant([[1,2,3],[3,4,5]])
_matrix
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[1, 2, 3],
       [3, 4, 5]], dtype=int32)>
_matrix[0,:] 
<tf.Tensor: shape=(3,), dtype=int32, numpy=array([1, 2, 3], dtype=int32)>
_matrix[:,0] 
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([1, 3], dtype=int32)>
_matrix[:,-1] 
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([3, 5], dtype=int32)>

tf.constant는 불편하다.

- 불편한점

  1. dtype이 모든 원소가 똑같아야한다. (그럴수 있음)
  2. 값을 바꿀 수가 없다.
  3. dtype이 다르면 연산이 불가능함.

- 값을 바꿀 수 없다.

_matrix=tf.constant([[1,2,3],[3,4,5]])
_matrix
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[1, 2, 3],
       [3, 4, 5]], dtype=int32)>
_matrix[0,0]=22
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [138], in <cell line: 1>()
----> 1 _matrix[0,0]=22

TypeError: 'tensorflow.python.framework.ops.EagerTensor' object does not support item assignment

- dtype이 다르면 연산이 불가능함

tf.constant([1.1,2])+tf.constant([3,4])
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
Input In [139], in <cell line: 1>()
----> 1 tf.constant([1.1,2])+tf.constant([3,4])

File ~/anaconda3/envs/py310/lib/python3.10/site-packages/tensorflow/python/util/traceback_utils.py:153, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    151 except Exception as e:
    152   filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153   raise e.with_traceback(filtered_tb) from None
    154 finally:
    155   del filtered_tb

File ~/anaconda3/envs/py310/lib/python3.10/site-packages/tensorflow/python/framework/ops.py:7107, in raise_from_not_ok_status(e, name)
   7105 def raise_from_not_ok_status(e, name):
   7106   e.message += (" name: " + name if name is not None else "")
-> 7107   raise core._status_to_exception(e) from None

InvalidArgumentError: cannot compute AddV2 as input #1(zero-based) was expected to be a float tensor but is a int32 tensor [Op:AddV2]
tf.constant([1.1,2])+tf.constant([3.0,4.0])
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([4.1, 6. ], dtype=float32)>

- 같은 float형이라도 불가능할 때가 있음

tf.constant([1.1,2])
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([1.1, 2. ], dtype=float32)>
tf.constant([3.0,4.0],dtype=tf.float64)
<tf.Tensor: shape=(2,), dtype=float64, numpy=array([3., 4.])>
tf.constant([1.1,2])+tf.constant([3.0,4.0],dtype=tf.float64)
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
Input In [143], in <cell line: 1>()
----> 1 tf.constant([1.1,2])+tf.constant([3.0,4.0],dtype=tf.float64)

File ~/anaconda3/envs/py310/lib/python3.10/site-packages/tensorflow/python/util/traceback_utils.py:153, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    151 except Exception as e:
    152   filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153   raise e.with_traceback(filtered_tb) from None
    154 finally:
    155   del filtered_tb

File ~/anaconda3/envs/py310/lib/python3.10/site-packages/tensorflow/python/framework/ops.py:7107, in raise_from_not_ok_status(e, name)
   7105 def raise_from_not_ok_status(e, name):
   7106   e.message += (" name: " + name if name is not None else "")
-> 7107   raise core._status_to_exception(e) from None

InvalidArgumentError: cannot compute AddV2 as input #1(zero-based) was expected to be a float tensor but is a double tensor [Op:AddV2]

tf.constant $\to$ 넘파이

_vector = tf.constant([1,2,3,4])
_vector
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([1, 2, 3, 4], dtype=int32)>
np.array(_vector) # 방법1
array([1, 2, 3, 4], dtype=int32)
_vector.numpy()
array([1, 2, 3, 4], dtype=int32)

연산

- 더하기

a=tf.constant([1,2,3])
b=tf.constant([4,5,6])
a+b
<tf.Tensor: shape=(3,), dtype=int32, numpy=array([5, 7, 9], dtype=int32)>
tf.add(a,b)
<tf.Tensor: shape=(3,), dtype=int32, numpy=array([5, 7, 9], dtype=int32)>

- 곱하기

a=tf.constant([[1,2],[3,4]])
b=tf.constant([[4,5],[6,7]])
a*b
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[ 4, 10],
       [18, 28]], dtype=int32)>
a,b
(<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
 array([[1, 2],
        [3, 4]], dtype=int32)>,
 <tf.Tensor: shape=(2, 2), dtype=int32, numpy=
 array([[4, 5],
        [6, 7]], dtype=int32)>)
tf.multiply(a,b)
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[ 4, 10],
       [18, 28]], dtype=int32)>

- 행렬곱

a=tf.constant([[1,0],[0,1]]) 
b= tf.constant([[1],[22]]) 
a @ b # (2,2) matrix @ (2,1) matrix 
<tf.Tensor: shape=(2, 1), dtype=int32, numpy=
array([[ 1],
       [22]], dtype=int32)>
b @ a # (2,1) matrix @ (2,2) matrix
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
Input In [154], in <cell line: 1>()
----> 1 b @ a

File ~/anaconda3/envs/py310/lib/python3.10/site-packages/tensorflow/python/util/traceback_utils.py:153, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    151 except Exception as e:
    152   filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153   raise e.with_traceback(filtered_tb) from None
    154 finally:
    155   del filtered_tb

File ~/anaconda3/envs/py310/lib/python3.10/site-packages/tensorflow/python/framework/ops.py:7107, in raise_from_not_ok_status(e, name)
   7105 def raise_from_not_ok_status(e, name):
   7106   e.message += (" name: " + name if name is not None else "")
-> 7107   raise core._status_to_exception(e) from None

InvalidArgumentError: Matrix size-incompatible: In[0]: [2,1], In[1]: [2,2] [Op:MatMul]
tf.matmul(a,b)
<tf.Tensor: shape=(2, 1), dtype=int32, numpy=
array([[ 1],
       [22]], dtype=int32)>

- 역행렬

a= tf.constant([[1,0],[0,2]]) 
a
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 0],
       [0, 2]], dtype=int32)>
tf.linalg.inv(a)
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
Input In [157], in <cell line: 1>()
----> 1 tf.linalg.inv(a)

File ~/anaconda3/envs/py310/lib/python3.10/site-packages/tensorflow/python/ops/gen_linalg_ops.py:1506, in matrix_inverse(input, adjoint, name)
   1504   return _result
   1505 except _core._NotOkStatusException as e:
-> 1506   _ops.raise_from_not_ok_status(e, name)
   1507 except _core._FallbackException:
   1508   pass

File ~/anaconda3/envs/py310/lib/python3.10/site-packages/tensorflow/python/framework/ops.py:7107, in raise_from_not_ok_status(e, name)
   7105 def raise_from_not_ok_status(e, name):
   7106   e.message += (" name: " + name if name is not None else "")
-> 7107   raise core._status_to_exception(e) from None

InvalidArgumentError: Value for attr 'T' of int32 is not in the list of allowed values: double, float, half, complex64, complex128
	; NodeDef: {{node MatrixInverse}}; Op<name=MatrixInverse; signature=input:T -> output:T; attr=adjoint:bool,default=false; attr=T:type,allowed=[DT_DOUBLE, DT_FLOAT, DT_HALF, DT_COMPLEX64, DT_COMPLEX128]> [Op:MatrixInverse]
a= tf.constant([[1.0,2.0],[3.0,4.0]]) 
a
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1., 2.],
       [3., 4.]], dtype=float32)>
tf.linalg.inv(a)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[-2. ,  1. ],
       [ 1.5, -0.5]], dtype=float32)>
a @ tf.linalg.inv(a)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1., 0.],
       [0., 1.]], dtype=float32)>

- tf.linalg. + tab을 누르면 쓸만한게 조금 나온다.

a=tf.constant([[1.0,2.0],[3.0,4.0]])
a
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1., 2.],
       [3., 4.]], dtype=float32)>
tf.linalg.det(a)
<tf.Tensor: shape=(), dtype=float32, numpy=-2.0>
tf.linalg.trace(a)
<tf.Tensor: shape=(), dtype=float32, numpy=5.0>

- 단순한 집계함수들

a=tf.constant([[1,2,3],[4,5,6]])
a
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[1, 2, 3],
       [4, 5, 6]], dtype=int32)>
tf.reduce_sum(a)
<tf.Tensor: shape=(), dtype=int32, numpy=21>
tf.reduce_max(a)
<tf.Tensor: shape=(), dtype=int32, numpy=6>

- 행렬곱 고급

_I=tf.constant([[1.0,0.0],[0.0,1.0]])
_I
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1., 0.],
       [0., 1.]], dtype=float32)>
_x = tf.constant([22.0,33.0])
_x
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([22., 33.], dtype=float32)>

(상황1)

_I @ _x
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
Input In [169], in <cell line: 1>()
----> 1 _I @ _x

File ~/anaconda3/envs/py310/lib/python3.10/site-packages/tensorflow/python/util/traceback_utils.py:153, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    151 except Exception as e:
    152   filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153   raise e.with_traceback(filtered_tb) from None
    154 finally:
    155   del filtered_tb

File ~/anaconda3/envs/py310/lib/python3.10/site-packages/tensorflow/python/framework/ops.py:7107, in raise_from_not_ok_status(e, name)
   7105 def raise_from_not_ok_status(e, name):
   7106   e.message += (" name: " + name if name is not None else "")
-> 7107   raise core._status_to_exception(e) from None

InvalidArgumentError: In[0] and In[1] has different ndims: [2,2] vs. [2] [Op:MatMul]
_x @ _I 
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
Input In [170], in <cell line: 1>()
----> 1 _x @ _I

File ~/anaconda3/envs/py310/lib/python3.10/site-packages/tensorflow/python/util/traceback_utils.py:153, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    151 except Exception as e:
    152   filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153   raise e.with_traceback(filtered_tb) from None
    154 finally:
    155   del filtered_tb

File ~/anaconda3/envs/py310/lib/python3.10/site-packages/tensorflow/python/framework/ops.py:7107, in raise_from_not_ok_status(e, name)
   7105 def raise_from_not_ok_status(e, name):
   7106   e.message += (" name: " + name if name is not None else "")
-> 7107   raise core._status_to_exception(e) from None

InvalidArgumentError: In[0] and In[1] has different ndims: [2] vs. [2,2] [Op:MatMul]

(상황2)

_x = tf.constant([[1.0],[2.0]])
_x
<tf.Tensor: shape=(2, 1), dtype=float32, numpy=
array([[1.],
       [2.]], dtype=float32)>
_I @ _x 
<tf.Tensor: shape=(2, 1), dtype=float32, numpy=
array([[1.],
       [2.]], dtype=float32)>
_x @ _I
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
Input In [173], in <cell line: 1>()
----> 1 _x @ _I

File ~/anaconda3/envs/py310/lib/python3.10/site-packages/tensorflow/python/util/traceback_utils.py:153, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    151 except Exception as e:
    152   filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153   raise e.with_traceback(filtered_tb) from None
    154 finally:
    155   del filtered_tb

File ~/anaconda3/envs/py310/lib/python3.10/site-packages/tensorflow/python/framework/ops.py:7107, in raise_from_not_ok_status(e, name)
   7105 def raise_from_not_ok_status(e, name):
   7106   e.message += (" name: " + name if name is not None else "")
-> 7107   raise core._status_to_exception(e) from None

InvalidArgumentError: Matrix size-incompatible: In[0]: [2,1], In[1]: [2,2] [Op:MatMul]

(상황3)

_x = tf.constant([[1.0,2.0]])
_x
<tf.Tensor: shape=(1, 2), dtype=float32, numpy=array([[1., 2.]], dtype=float32)>
_I @ _x 
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
Input In [175], in <cell line: 1>()
----> 1 _I @ _x

File ~/anaconda3/envs/py310/lib/python3.10/site-packages/tensorflow/python/util/traceback_utils.py:153, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    151 except Exception as e:
    152   filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153   raise e.with_traceback(filtered_tb) from None
    154 finally:
    155   del filtered_tb

File ~/anaconda3/envs/py310/lib/python3.10/site-packages/tensorflow/python/framework/ops.py:7107, in raise_from_not_ok_status(e, name)
   7105 def raise_from_not_ok_status(e, name):
   7106   e.message += (" name: " + name if name is not None else "")
-> 7107   raise core._status_to_exception(e) from None

InvalidArgumentError: Matrix size-incompatible: In[0]: [2,2], In[1]: [1,2] [Op:MatMul]
_x @ _I 
<tf.Tensor: shape=(1, 2), dtype=float32, numpy=array([[1., 2.]], dtype=float32)>

(넘파이는 다르다..)

np.array([22,33]) @ np.diag([1,1]) # (2,) @ (2,2) ==> (2,)를 (1,2)벡터로 해석해줘서 곱하기 수행, 수행결과는 다시 (2,)로 저장
array([22, 33])
np.array([[22],[33]]) @ np.diag([1,1]) # (2,1) @ (2,2)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [181], in <cell line: 1>()
----> 1 np.array([[22],[33]]) @ np.diag([1,1])

ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 2 is different from 1)

형태변환

- tf.reshape 기본

a=tf.constant([1,2,3,4])
a
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([1, 2, 3, 4], dtype=int32)>
tf.reshape(a,(2,2))
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
       [3, 4]], dtype=int32)>
tf.reshape(a,(4,1))
<tf.Tensor: shape=(4, 1), dtype=int32, numpy=
array([[1],
       [2],
       [3],
       [4]], dtype=int32)>
tf.reshape(a,(1,4))
<tf.Tensor: shape=(1, 4), dtype=int32, numpy=array([[1, 2, 3, 4]], dtype=int32)>

- tf.reshape 응용

a= tf.constant([0,1,2,3,4,5,6,7,8,9,10,11]) # length 12 vector
a
<tf.Tensor: shape=(12,), dtype=int32, numpy=array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11], dtype=int32)>
tf.reshape(a,(4,3))
<tf.Tensor: shape=(4, 3), dtype=int32, numpy=
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]], dtype=int32)>
tf.reshape(a,(2,2,3))
<tf.Tensor: shape=(2, 2, 3), dtype=int32, numpy=
array([[[ 0,  1,  2],
        [ 3,  4,  5]],

       [[ 6,  7,  8],
        [ 9, 10, 11]]], dtype=int32)>
tf.reshape(a,(3,-1))
<tf.Tensor: shape=(3, 4), dtype=int32, numpy=
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]], dtype=int32)>
tf.reshape(a,(2,2,-1))
<tf.Tensor: shape=(2, 2, 3), dtype=int32, numpy=
array([[[ 0,  1,  2],
        [ 3,  4,  5]],

       [[ 6,  7,  8],
        [ 9, 10, 11]]], dtype=int32)>
b=tf.reshape(a,(2,2,-1))
b
<tf.Tensor: shape=(2, 2, 3), dtype=int32, numpy=
array([[[ 0,  1,  2],
        [ 3,  4,  5]],

       [[ 6,  7,  8],
        [ 9, 10, 11]]], dtype=int32)>
tf.reshape(b,-1)
<tf.Tensor: shape=(12,), dtype=int32, numpy=array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11], dtype=int32)>

선언고급

- 대각행렬선언

tf.constant(np.diag([1,2,3]))
<tf.Tensor: shape=(3, 3), dtype=int64, numpy=
array([[1, 0, 0],
       [0, 2, 0],
       [0, 0, 3]])>

- 1만 포함한 텐서를 만들고 싶음

tf.ones([3,4])
<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]], dtype=float32)>
tf.reshape(tf.constant([1]*12),(3,4))
<tf.Tensor: shape=(3, 4), dtype=int32, numpy=
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]], dtype=int32)>