본문 바로가기
Pytorch/Summarize

1. 텐서(tensor)란?_텐서 생성하기

by 소소한 혜밍씨 2022. 8. 24.

텐서란?

 

Tensor는 multi-dimensional array를 나타내는 말로, TensorFlow의 기본 data type 이다.

 

 

텐서를 만들기 위해서는 간단하게 tf.constant 를 이용하여 만들 수 있다.

 

 

 

1.텐서 생성하기_기본형

 

hello = tf.constant([3,3], dtype=tf.float32)

 

결과는 넘파이의 array 와는 다르게 숫자와 함께 shape 과 데이터타입을 함께 출력한다.

 

tf.Tensor([3. 3.], shape=(2,), dtype=float32)

 

 

 

글자를 넣어서 만들 수도 있는데 

 

hello = tf.constant('hello world')

 

이때의 결과는 다음과 같다.

 

tf.Tensor(b'hello world', shape=(), dtype=string)

 

 

 

 

2. 텐서 생성하기_array 형

 

x = tf.constant([[1.0, 2.0],
                 [3.0, 4.0]])

 

결과는 아래와 같다.

 

tf.Tensor(
[[1. 2.]
 [3. 4.]], shape=(2, 2), dtype=float32)

 

 

 

 

 

 

댓글