강의영상

import

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

tf.constant

선언고급

- 대각행렬선언

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)>

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

tf.zeros((3,3))
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]], dtype=float32)>

- 배열선언

tf.constant(range(12))
<tf.Tensor: shape=(12,), dtype=int32, numpy=array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11], dtype=int32)>
tf.constant(range(2,6)) # 2,3,4,5
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([2, 3, 4, 5], dtype=int32)>
tf.constant(range(2,21,3))
<tf.Tensor: shape=(7,), dtype=int32, numpy=array([ 2,  5,  8, 11, 14, 17, 20], dtype=int32)>
tf.linspace(0,1,14)
<tf.Tensor: shape=(14,), dtype=float64, numpy=
array([0.        , 0.07692308, 0.15384615, 0.23076923, 0.30769231,
       0.38461538, 0.46153846, 0.53846154, 0.61538462, 0.69230769,
       0.76923077, 0.84615385, 0.92307692, 1.        ])>
tf.linspace(-1,20,14)
<tf.Tensor: shape=(14,), dtype=float64, numpy=
array([-1.        ,  0.61538462,  2.23076923,  3.84615385,  5.46153846,
        7.07692308,  8.69230769, 10.30769231, 11.92307692, 13.53846154,
       15.15384615, 16.76923077, 18.38461538, 20.        ])>
tf.linspace([0,-1],[1,20],14,axis=0) # axis=0은 생략가능 
<tf.Tensor: shape=(14, 2), dtype=float64, numpy=
array([[ 0.        , -1.        ],
       [ 0.07692308,  0.61538462],
       [ 0.15384615,  2.23076923],
       [ 0.23076923,  3.84615385],
       [ 0.30769231,  5.46153846],
       [ 0.38461538,  7.07692308],
       [ 0.46153846,  8.69230769],
       [ 0.53846154, 10.30769231],
       [ 0.61538462, 11.92307692],
       [ 0.69230769, 13.53846154],
       [ 0.76923077, 15.15384615],
       [ 0.84615385, 16.76923077],
       [ 0.92307692, 18.38461538],
       [ 1.        , 20.        ]])>
tf.linspace([0,-1],[1,20],14,axis=1)
<tf.Tensor: shape=(2, 14), dtype=float64, numpy=
array([[ 0.        ,  0.07692308,  0.15384615,  0.23076923,  0.30769231,
         0.38461538,  0.46153846,  0.53846154,  0.61538462,  0.69230769,
         0.76923077,  0.84615385,  0.92307692,  1.        ],
       [-1.        ,  0.61538462,  2.23076923,  3.84615385,  5.46153846,
         7.07692308,  8.69230769, 10.30769231, 11.92307692, 13.53846154,
        15.15384615, 16.76923077, 18.38461538, 20.        ]])>

- 랜덤

tf.random.normal([3,3])
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[-1.5421661 , -1.5808113 ,  1.1388296 ],
       [-2.6040976 ,  1.0960393 ,  0.02348358],
       [-0.53655666,  1.2877383 , -0.17504291]], dtype=float32)>
tf.random.normal([10])
<tf.Tensor: shape=(10,), dtype=float32, numpy=
array([ 0.32824913,  0.01634766,  1.157158  ,  0.60476935, -0.03189921,
        1.0994794 ,  0.7413038 , -0.6990549 , -1.6825281 , -1.2637503 ],
      dtype=float32)>
tf.random.uniform([3,3])
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[0.04361665, 0.17482626, 0.50426793],
       [0.71013105, 0.6988857 , 0.49747908],
       [0.74961853, 0.6538025 , 0.09697211]], dtype=float32)>
np.random.randn(10)
array([-0.53429023,  1.13505005, -1.43100272, -2.13399699, -1.05582216,
       -1.61879362, -0.11808444,  0.87534624,  2.64117769, -0.8208995 ])
tf.random.randn(10)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [39], in <cell line: 1>()
----> 1 tf.random.randn(10)

AttributeError: module 'tensorflow._api.v2.random' has no attribute 'randn'

tf.concat

- (2,1) concat (2,1) => (2,2)

  • 두번쨰축이 1에서 2로 바뀌네? // axis=1
a=tf.constant([[1],[2]])
b=tf.constant([[3],[4]])
tf.concat([a,b],axis=1)
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 3],
       [2, 4]], dtype=int32)>

- (2,1) concat (2,1) => (4,1)

  • 첫번쨰 축이 바뀜 // axis=0
a=tf.constant([[1],[2]])
b=tf.constant([[3],[4]])
tf.concat([a,b],axis=0)
<tf.Tensor: shape=(4, 1), dtype=int32, numpy=
array([[1],
       [2],
       [3],
       [4]], dtype=int32)>

- (1,2) concat (1,2) => (2,2) `

  • 첫번쨰축// axis=0
a=tf.constant([[1,2]])
b=tf.constant([[3,4]])
tf.concat([a,b],axis=0)
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
       [3, 4]], dtype=int32)>

- (1,2) concat (1,2) => (1,4) `

  • 두번쨰축// axis=1
a=tf.constant([[1,2]])
b=tf.constant([[3,4]])
tf.concat([a,b],axis=1)
<tf.Tensor: shape=(1, 4), dtype=int32, numpy=array([[1, 2, 3, 4]], dtype=int32)>

- (2,2,3) concat (2,2,3) => (4,2,3)

  • 첫번쨰축 // axis=0
a=tf.reshape(tf.constant(range(12)),(2,2,3))
b=-a
a,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.Tensor: shape=(2, 2, 3), dtype=int32, numpy=
 array([[[  0,  -1,  -2],
         [ -3,  -4,  -5]],
 
        [[ -6,  -7,  -8],
         [ -9, -10, -11]]], dtype=int32)>)
tf.concat([a,b],axis=0)
<tf.Tensor: shape=(4, 2, 3), dtype=int32, numpy=
array([[[  0,   1,   2],
        [  3,   4,   5]],

       [[  6,   7,   8],
        [  9,  10,  11]],

       [[  0,  -1,  -2],
        [ -3,  -4,  -5]],

       [[ -6,  -7,  -8],
        [ -9, -10, -11]]], dtype=int32)>

- (2,2,3) concat (2,2,3) => (2,4,3)

  • 두번쨰축 // axis=1
a=tf.reshape(tf.constant(range(12)),(2,2,3))
b=-a
a,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.Tensor: shape=(2, 2, 3), dtype=int32, numpy=
 array([[[  0,  -1,  -2],
         [ -3,  -4,  -5]],
 
        [[ -6,  -7,  -8],
         [ -9, -10, -11]]], dtype=int32)>)
tf.concat([a,b],axis=1)
<tf.Tensor: shape=(2, 4, 3), dtype=int32, numpy=
array([[[  0,   1,   2],
        [  3,   4,   5],
        [  0,  -1,  -2],
        [ -3,  -4,  -5]],

       [[  6,   7,   8],
        [  9,  10,  11],
        [ -6,  -7,  -8],
        [ -9, -10, -11]]], dtype=int32)>

- (2,2,3) concat (2,2,3) => (2,2,6)

  • 세번쨰축 // axis=2, axis = -1
a=tf.reshape(tf.constant(range(12)),(2,2,3))
b=-a
a,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.Tensor: shape=(2, 2, 3), dtype=int32, numpy=
 array([[[  0,  -1,  -2],
         [ -3,  -4,  -5]],
 
        [[ -6,  -7,  -8],
         [ -9, -10, -11]]], dtype=int32)>)
tf.concat([a,b],axis=-1)
<tf.Tensor: shape=(2, 2, 6), dtype=int32, numpy=
array([[[  0,   1,   2,   0,  -1,  -2],
        [  3,   4,   5,  -3,  -4,  -5]],

       [[  6,   7,   8,  -6,  -7,  -8],
        [  9,  10,  11,  -9, -10, -11]]], dtype=int32)>

- (4,) concat (4,) => (8,)

  • 첫번쨰 축
a=tf.constant([1,2,3,4])
b=-a
a,b
(<tf.Tensor: shape=(4,), dtype=int32, numpy=array([1, 2, 3, 4], dtype=int32)>,
 <tf.Tensor: shape=(4,), dtype=int32, numpy=array([-1, -2, -3, -4], dtype=int32)>)
tf.concat([a,b],axis=0)
<tf.Tensor: shape=(8,), dtype=int32, numpy=array([ 1,  2,  3,  4, -1, -2, -3, -4], dtype=int32)>

- (4,) concat (4,) => (4,2)

a=tf.constant([1,2,3,4])
b=-a
a,b
(<tf.Tensor: shape=(4,), dtype=int32, numpy=array([1, 2, 3, 4], dtype=int32)>,
 <tf.Tensor: shape=(4,), dtype=int32, numpy=array([-1, -2, -3, -4], dtype=int32)>)
tf.concat([a,b],axis=1)
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
Input In [78], in <cell line: 1>()
----> 1 tf.concat([a,b],axis=1)

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: ConcatOp : Expected concatenating dimensions in the range [-1, 1), but got 1 [Op:ConcatV2] name: concat
  • tf.concat은 차원을 증가시키면서 결합해주는것은 아니다.

tf.stack

- (4) stack (4) => (4,2)

  • (4,) stack (4,) => (4,2) // 두번째 축이 비어있다고 인식
a=tf.constant([1,2,3,4])
b=-a
a,b
(<tf.Tensor: shape=(4,), dtype=int32, numpy=array([1, 2, 3, 4], dtype=int32)>,
 <tf.Tensor: shape=(4,), dtype=int32, numpy=array([-1, -2, -3, -4], dtype=int32)>)
tf.stack([a,b],axis=1)
<tf.Tensor: shape=(4, 2), dtype=int32, numpy=
array([[ 1, -1],
       [ 2, -2],
       [ 3, -3],
       [ 4, -4]], dtype=int32)>

- (4) stack (4) => (2,4)

  • (,4) stack (,4) => (2,4) // 첫번째 축이 비어있다고 인식
a=tf.constant([1,2,3,4])
b=-a
a,b
(<tf.Tensor: shape=(4,), dtype=int32, numpy=array([1, 2, 3, 4], dtype=int32)>,
 <tf.Tensor: shape=(4,), dtype=int32, numpy=array([-1, -2, -3, -4], dtype=int32)>)
tf.stack([a,b],axis=0)
<tf.Tensor: shape=(2, 4), dtype=int32, numpy=
array([[ 1,  2,  3,  4],
       [-1, -2, -3, -4]], dtype=int32)>

- (2,3,4,5) stack (2,3,4,5) => (2,2,3,4,5)

  • 첫번째축이 비어있다고 인식
a=tf.reshape(tf.constant(range(2*3*4*5)),(2,3,4,5))
b=-a
tf.stack([a,b],axis=0)
<tf.Tensor: shape=(2, 2, 3, 4, 5), dtype=int32, numpy=
array([[[[[   0,    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,   32,   33,   34],
          [  35,   36,   37,   38,   39]],

         [[  40,   41,   42,   43,   44],
          [  45,   46,   47,   48,   49],
          [  50,   51,   52,   53,   54],
          [  55,   56,   57,   58,   59]]],


        [[[  60,   61,   62,   63,   64],
          [  65,   66,   67,   68,   69],
          [  70,   71,   72,   73,   74],
          [  75,   76,   77,   78,   79]],

         [[  80,   81,   82,   83,   84],
          [  85,   86,   87,   88,   89],
          [  90,   91,   92,   93,   94],
          [  95,   96,   97,   98,   99]],

         [[ 100,  101,  102,  103,  104],
          [ 105,  106,  107,  108,  109],
          [ 110,  111,  112,  113,  114],
          [ 115,  116,  117,  118,  119]]]],



       [[[[   0,   -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,  -32,  -33,  -34],
          [ -35,  -36,  -37,  -38,  -39]],

         [[ -40,  -41,  -42,  -43,  -44],
          [ -45,  -46,  -47,  -48,  -49],
          [ -50,  -51,  -52,  -53,  -54],
          [ -55,  -56,  -57,  -58,  -59]]],


        [[[ -60,  -61,  -62,  -63,  -64],
          [ -65,  -66,  -67,  -68,  -69],
          [ -70,  -71,  -72,  -73,  -74],
          [ -75,  -76,  -77,  -78,  -79]],

         [[ -80,  -81,  -82,  -83,  -84],
          [ -85,  -86,  -87,  -88,  -89],
          [ -90,  -91,  -92,  -93,  -94],
          [ -95,  -96,  -97,  -98,  -99]],

         [[-100, -101, -102, -103, -104],
          [-105, -106, -107, -108, -109],
          [-110, -111, -112, -113, -114],
          [-115, -116, -117, -118, -119]]]]], dtype=int32)>

- (2,3,4,5) stack (2,3,4,5) => (2,2,3,4,5)

  • 두번째축이 비어있다고 인식
  • (2, ,3,4,5) stack (2, ,3,4,5)
  • (2,2,3,4,5)
a=tf.reshape(tf.constant(range(2*3*4*5)),(2,3,4,5))
b=-a
tf.stack([a,b],axis=1)
<tf.Tensor: shape=(2, 2, 3, 4, 5), dtype=int32, numpy=
array([[[[[   0,    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,   32,   33,   34],
          [  35,   36,   37,   38,   39]],

         [[  40,   41,   42,   43,   44],
          [  45,   46,   47,   48,   49],
          [  50,   51,   52,   53,   54],
          [  55,   56,   57,   58,   59]]],


        [[[   0,   -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,  -32,  -33,  -34],
          [ -35,  -36,  -37,  -38,  -39]],

         [[ -40,  -41,  -42,  -43,  -44],
          [ -45,  -46,  -47,  -48,  -49],
          [ -50,  -51,  -52,  -53,  -54],
          [ -55,  -56,  -57,  -58,  -59]]]],



       [[[[  60,   61,   62,   63,   64],
          [  65,   66,   67,   68,   69],
          [  70,   71,   72,   73,   74],
          [  75,   76,   77,   78,   79]],

         [[  80,   81,   82,   83,   84],
          [  85,   86,   87,   88,   89],
          [  90,   91,   92,   93,   94],
          [  95,   96,   97,   98,   99]],

         [[ 100,  101,  102,  103,  104],
          [ 105,  106,  107,  108,  109],
          [ 110,  111,  112,  113,  114],
          [ 115,  116,  117,  118,  119]]],


        [[[ -60,  -61,  -62,  -63,  -64],
          [ -65,  -66,  -67,  -68,  -69],
          [ -70,  -71,  -72,  -73,  -74],
          [ -75,  -76,  -77,  -78,  -79]],

         [[ -80,  -81,  -82,  -83,  -84],
          [ -85,  -86,  -87,  -88,  -89],
          [ -90,  -91,  -92,  -93,  -94],
          [ -95,  -96,  -97,  -98,  -99]],

         [[-100, -101, -102, -103, -104],
          [-105, -106, -107, -108, -109],
          [-110, -111, -112, -113, -114],
          [-115, -116, -117, -118, -119]]]]], dtype=int32)>

- (2,3,4,5) stack (2,3,4,5) => (2,3,4,5,2)

  • 마지막축이 비어있다고 인식
  • (2,3,4,5,) stack (2,3,4,5,)
  • (2,3,4,5,2)
a=tf.reshape(tf.constant(range(2*3*4*5)),(2,3,4,5))
b=-a
c=-a*2

아래의 코드에서 ??를 어떻게 바꿔야??

tf.stack([a,b],axis=??) ## 숙제