728x90
728x90

텐서플로우 인덱스 참조

- 텐서의 인덱스 참조도 넘파이와 유사

 

1차원 인덱싱

- 1차원 텐서 만들기 

>>> t = tf.constant([1,2,3,])
>>> t

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

- 인덱싱

>>> t[:2]
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([1, 2], dtype=int32)>

 

2차원 인덱싱

- 2차원 텐서만들기

>>> t = tf.constant([[1,2,3],[4,5,6]])
>>> t

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

- 2차원 인덱싱

>>> t[:1]

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

>>> t[:,2]

<tf.Tensor: shape=(2,), dtype=int32, numpy=array([3, 6], dtype=int32)>

>>> t[:2,:2]

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

 

3차원 인덱싱

- 3차원 텐서만들기

>>> t = tf.constant([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
>>> t

<tf.Tensor: shape=(2, 2, 3), dtype=int32, numpy=
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

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

- 3차원 인덱싱

>>> t[:2,1:,:2]

<tf.Tensor: shape=(2, 1, 2), dtype=int32, numpy=
array([[[ 4,  5]],

       [[10, 11]]], dtype=int32)>
728x90

'Development Study > tensorflow' 카테고리의 다른 글

[tensorflow] 텐서플로우 - 텐서 만들기 #1  (0) 2021.03.10
728x90

tf.constant()

 

- tf.constant() 함수로 텐서를 만들 수 있음

tf.constant(value, dtype=None, shape=None, name='Const')

 

이것저것 해보기

 

import tensorflow as tf

>>> tf.constant([1,2,3,])

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

- dtype 지정

>>> tf.constant([1,2,3],dtype=float)

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

- shape 지정

- value의 shape 에 맞춰서 지정

>>> tf.constant([1,2,3],dtype=float,shape=(3,1))

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

 

 

728x90
728x90
728x90

+ Recent posts