텐서 :: 대화형 AI - mindscale
Skip to content

텐서

import tensorflow as tf
import numpy as np
t = tf.convert_to_tensor(1)
tf.rank(t)
<tf.Tensor: shape=(), dtype=int32, numpy=0>
t = tf.convert_to_tensor([1, 2])
tf.rank(t)
<tf.Tensor: shape=(), dtype=int32, numpy=1>
t[0]
<tf.Tensor: shape=(), dtype=int32, numpy=1>
t[1]
<tf.Tensor: shape=(), dtype=int32, numpy=2>
t = tf.convert_to_tensor(
    [
     [1, 2],
     [3, 4]
    ]
)
tf.rank(t)
<tf.Tensor: shape=(), dtype=int32, numpy=2>
t[0, 1]
<tf.Tensor: shape=(), dtype=int32, numpy=2>
t[1, 0]
<tf.Tensor: shape=(), dtype=int32, numpy=3>
t = tf.convert_to_tensor(
    [
        [
            [1, 2],
            [3, 4],
        ],
        [
            [5, 6],
            [7, 8],
        ],
    ]
)
tf.rank(t)
<tf.Tensor: shape=(), dtype=int32, numpy=3>
t[0, 0, 1]
<tf.Tensor: shape=(), dtype=int32, numpy=2>
t.shape
TensorShape([2, 2, 2])
t = tf.convert_to_tensor(
    [
        [
            [1, 2, 3, 4],
            [5, 6, 7, 8],
            [9, 10, 12, 12],
        ],
        [
            [1, 2, 3, 4],
            [5, 6, 7, 8],
            [9, 10, 12, 12],
        ],
    ]
)
tf.rank(t)
<tf.Tensor: shape=(), dtype=int32, numpy=3>
t.shape
TensorShape([2, 3, 4])
t[0, 1, 2]
<tf.Tensor: shape=(), dtype=int32, numpy=7>
t[0, :, 2]
<tf.Tensor: shape=(3,), dtype=int32, numpy=array([ 3,  7, 12], dtype=int32)>
t[:, 1, 2]
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([7, 7], dtype=int32)>
t[0, 1, :]
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([5, 6, 7, 8], dtype=int32)>
t[0, 1, 2:4]
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([7, 8], dtype=int32)>
t = tf.convert_to_tensor([1, 2])
tf.rank(t)
<tf.Tensor: shape=(), dtype=int32, numpy=1>
t.shape
TensorShape([2])
t2 = tf.expand_dims(t, 1)
t2.shape
TensorShape([2, 1])
tf.rank(t2)
<tf.Tensor: shape=(), dtype=int32, numpy=2>
t2
<tf.Tensor: shape=(2, 1), dtype=int32, numpy=
array([[1],
       [2]], dtype=int32)>
t2[1, 0]
<tf.Tensor: shape=(), dtype=int32, numpy=2>
t3 = tf.expand_dims(t, 0)
t3
<tf.Tensor: shape=(1, 2), dtype=int32, numpy=array([[1, 2]], dtype=int32)>
t3[0, 1]
<tf.Tensor: shape=(), dtype=int32, numpy=2>
t3 + 1
<tf.Tensor: shape=(1, 2), dtype=int32, numpy=array([[2, 3]], dtype=int32)>