본문 바로가기

AI Theory/CS182_2021

tf.constant() 함수

tf.constant() 함수

 

tf.constant()는 이름 그대로 상수 텐서(constant tensor)를 return 해주는 함수이다.

 여기서 상수는 변하지 않는 수, 텐서는 일종의 행렬을 의미한다.

즉, 상수 텐서는 변하지않는 행렬을 의미.

 

변수 텐서를 만들기 위해서는 tf.Variable() 함수를 사용하면 된다.

 

tf.constant의 기본 구조는 아래와 같다.

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

value(필수)

dtype(옵션): 함수의 리턴값인 텐서의 각 요소가 갖게되는 데이터타입

dtype이 지정되지 않았을 경우, 리턴값 텐서의 dtype은 value의 데이터타입을 추측해서 정해진다.

반대로 dtype이 지정되었을 경우, 리턴값 텐서의 value는 지정된 dtype에 맞게 변형된다.

 

shape(옵션): 함수 리턴값인 텐서의 차원

shape가 지정되면 value는 해당 shape를 만족하기 위해 reshape된다.

tf.constant(0, shape=(2, 3))
'''
>>> tf.Tensor shape=(2,3), dtype=int32, numpy=array([[0,0,0],[0,0,0]], dtype=int32)>

'''


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

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

'''

 

name(옵션): 함수 리턴값인 텐서의 이름

'AI Theory > CS182_2021' 카테고리의 다른 글

CS182 Lecture 1: Introduction  (0) 2023.08.24