Tensorflow element Wise operator


title: “Tenssorflow에서 element wise 연산” categories: Tensorflow –

일단 x와 y를 생성하겠습니다.

>>> x = tf.reshape(tf.range(3*10*4), [3,10,4])
>>> y = tf.reshape(tf.range(3*1*4), [3,1,4])
>>> x
<tf.Tensor: id=5, shape=(3, 10, 4), 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]]], dtype=int32)>

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

       [[ 4,  5,  6,  7]],

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

*연산과 tf.multiply 연산 먼저 확인하겠습니다.

element-wise연산이므로 수식이 바뀌더라도 동일한 결과를 내놓게 됩니다.

또한 *연산은 tf.multiply와 같은 연산을 합니다.

>>> x*y
<tf.Tensor: id=16, shape=(3, 10, 4), dtype=int32, numpy=
array([[[   0,    1,    4,    9],
        [   0,    5,   12,   21],
        [   0,    9,   20,   33],
        [   0,   13,   28,   45],
        [   0,   17,   36,   57],
        [   0,   21,   44,   69],
        [   0,   25,   52,   81],
        [   0,   29,   60,   93],
        [   0,   33,   68,  105],
        [   0,   37,   76,  117]],

       [[ 160,  205,  252,  301],
        [ 176,  225,  276,  329],
        [ 192,  245,  300,  357],
        [ 208,  265,  324,  385],
        [ 224,  285,  348,  413],
        [ 240,  305,  372,  441],
        [ 256,  325,  396,  469],
        [ 272,  345,  420,  497],
        [ 288,  365,  444,  525],
        [ 304,  385,  468,  553]],

       [[ 640,  729,  820,  913],
        [ 672,  765,  860,  957],
        [ 704,  801,  900, 1001],
        [ 736,  837,  940, 1045],
        [ 768,  873,  980, 1089],
        [ 800,  909, 1020, 1133],
        [ 832,  945, 1060, 1177],
        [ 864,  981, 1100, 1221],
        [ 896, 1017, 1140, 1265],
        [ 928, 1053, 1180, 1309]]], dtype=int32)>

>>> y*x
<tf.Tensor: id=18, shape=(3, 10, 4), dtype=int32, numpy=
array([[[   0,    1,    4,    9],
        [   0,    5,   12,   21],
        [   0,    9,   20,   33],
        [   0,   13,   28,   45],
        [   0,   17,   36,   57],
        [   0,   21,   44,   69],
        [   0,   25,   52,   81],
        [   0,   29,   60,   93],
        [   0,   33,   68,  105],
        [   0,   37,   76,  117]],

       [[ 160,  205,  252,  301],
        [ 176,  225,  276,  329],
        [ 192,  245,  300,  357],
        [ 208,  265,  324,  385],
        [ 224,  285,  348,  413],
        [ 240,  305,  372,  441],
        [ 256,  325,  396,  469],
        [ 272,  345,  420,  497],
        [ 288,  365,  444,  525],
        [ 304,  385,  468,  553]],

       [[ 640,  729,  820,  913],
        [ 672,  765,  860,  957],
        [ 704,  801,  900, 1001],
        [ 736,  837,  940, 1045],
        [ 768,  873,  980, 1089],
        [ 800,  909, 1020, 1133],
        [ 832,  945, 1060, 1177],
        [ 864,  981, 1100, 1221],
        [ 896, 1017, 1140, 1265],
        [ 928, 1053, 1180, 1309]]], dtype=int32)>

>>> tf.multiply(x,y)
<tf.Tensor: id=12, shape=(3, 10, 4), dtype=int32, numpy=
array([[[   0,    1,    4,    9],
        [   0,    5,   12,   21],
        [   0,    9,   20,   33],
        [   0,   13,   28,   45],
        [   0,   17,   36,   57],
        [   0,   21,   44,   69],
        [   0,   25,   52,   81],
        [   0,   29,   60,   93],
        [   0,   33,   68,  105],
        [   0,   37,   76,  117]],

       [[ 160,  205,  252,  301],
        [ 176,  225,  276,  329],
        [ 192,  245,  300,  357],
        [ 208,  265,  324,  385],
        [ 224,  285,  348,  413],
        [ 240,  305,  372,  441],
        [ 256,  325,  396,  469],
        [ 272,  345,  420,  497],
        [ 288,  365,  444,  525],
        [ 304,  385,  468,  553]],

       [[ 640,  729,  820,  913],
        [ 672,  765,  860,  957],
        [ 704,  801,  900, 1001],
        [ 736,  837,  940, 1045],
        [ 768,  873,  980, 1089],
        [ 800,  909, 1020, 1133],
        [ 832,  945, 1060, 1177],
        [ 864,  981, 1100, 1221],
        [ 896, 1017, 1140, 1265],
        [ 928, 1053, 1180, 1309]]], dtype=int32)>

tf.add와 + 연산도 마찬가지입니다.

>>> x+y
<tf.Tensor: id=12, shape=(3, 10, 4), dtype=int32, numpy=
array([[[  0,   2,   4,   6],
        [  4,   6,   8,  10],
        [  8,  10,  12,  14],
        [ 12,  14,  16,  18],
        [ 16,  18,  20,  22],
        [ 20,  22,  24,  26],
        [ 24,  26,  28,  30],
        [ 28,  30,  32,  34],
        [ 32,  34,  36,  38],
        [ 36,  38,  40,  42]],

       [[ 44,  46,  48,  50],
        [ 48,  50,  52,  54],
        [ 52,  54,  56,  58],
        [ 56,  58,  60,  62],
        [ 60,  62,  64,  66],
        [ 64,  66,  68,  70],
        [ 68,  70,  72,  74],
        [ 72,  74,  76,  78],
        [ 76,  78,  80,  82],
        [ 80,  82,  84,  86]],

       [[ 88,  90,  92,  94],
        [ 92,  94,  96,  98],
        [ 96,  98, 100, 102],
        [100, 102, 104, 106],
        [104, 106, 108, 110],
        [108, 110, 112, 114],
        [112, 114, 116, 118],
        [116, 118, 120, 122],
        [120, 122, 124, 126],
        [124, 126, 128, 130]]], dtype=int32)>

>>> tf.add(x,y)
<tf.Tensor: id=18, shape=(3, 10, 4), dtype=int32, numpy=
array([[[  0,   2,   4,   6],
        [  4,   6,   8,  10],
        [  8,  10,  12,  14],
        [ 12,  14,  16,  18],
        [ 16,  18,  20,  22],
        [ 20,  22,  24,  26],
        [ 24,  26,  28,  30],
        [ 28,  30,  32,  34],
        [ 32,  34,  36,  38],
        [ 36,  38,  40,  42]],

       [[ 44,  46,  48,  50],
        [ 48,  50,  52,  54],
        [ 52,  54,  56,  58],
        [ 56,  58,  60,  62],
        [ 60,  62,  64,  66],
        [ 64,  66,  68,  70],
        [ 68,  70,  72,  74],
        [ 72,  74,  76,  78],
        [ 76,  78,  80,  82],
        [ 80,  82,  84,  86]],

       [[ 88,  90,  92,  94],
        [ 92,  94,  96,  98],
        [ 96,  98, 100, 102],
        [100, 102, 104, 106],
        [104, 106, 108, 110],
        [108, 110, 112, 114],
        [112, 114, 116, 118],
        [116, 118, 120, 122],
        [120, 122, 124, 126],
        [124, 126, 128, 130]]], dtype=int32)>

Updated:

Leave a comment