Tensor는 multi-dimensional array를 나타내는 말로, TensorFlow의 기본 data type이다.
보통 1차원은 vector, 2차원은 metrix, 3차원 그 이상부터는 tensor 라고도 하지만
tensorflow에서 tensor는 vector랑 metrix를 합쳐서 multi-dimensional array를 지칭하는 용어로서 쓰인다.
tensor를 간단하게 불러와서 확인해보면
(tf.constant method를 이용하여 tensor를 간단하게 생성할 수 있다.)
hello = tf.constant([3,3], dtype=tf.float32)
print(hello)
# 출력
tf.Tensor([3. 3.], shape=(2,), dtype=float32)
이전 포스팅했던 넘파이와는 다르게 array 외에도 shape과 data type를 같이 출력한다.
문자형 외에 상수형 tensor 또한 같은 method를 사용하여 생성한다.
x = tf.constant([[1.0, 2.0],
[3.0, 4.0]])
print(x)
print(type(x))
# 출력
tf.Tensor(
[[1. 2.]
[3. 4.]], shape=(2, 2), dtype=float32)
<class 'tensorflow.python.framework.ops.EagerTensor'>
여기서 EagerTensor는 tensorflow가 버전이 올라가면서 기존 1.n version에서는 출력값을 바로 보여주지 않았던 것에 반해
출력값을 한번에 보여주는 것이므로 딱히 신경쓰지는 않아도 될 것 같다.
numpy array 나 list도 tensor로 변환이 가능하다.
method는 tf.constant()나 conver_to_tensor()의 두 가지 방법이 있는데 위에서 쓰지 않은 conver_to_tensor()를 이용해보자.
# numpy ndarray, python의 list 지정
x_np = np.array([[1.0, 2.0],
[3.0, 4.0]])
x_list = [[1.0, 2.0],
[3.0, 4.0]]
# tensor로 변환
x_np = tf.convert_to_tensor(x_np)
x_list = tf.convert_to_tensor(x_list)
print(type(x_np))
print(type(x_list))
# 출력
<class 'tensorflow.python.framework.ops.EagerTensor'>
<class 'tensorflow.python.framework.ops.EagerTensor'>
'Python > Pandas' 카테고리의 다른 글
판다스 자료구조 - 시리즈 (0) | 2022.08.09 |
---|
댓글