source : www.tensorflow.org/api_docs/python/tf/lookup

 

Module: tf.lookup  |  TensorFlow Core v2.3.0

Public API for tf.lookup namespace.

www.tensorflow.org


tf.lookup 에는 다음과 같은 Classes를 갖고있다.

  • class KeyValueTensorInitializer

  • class StaticHashTable

  • class StaticVocabularyTable

  • class TextFileIndex

  • class TextFileInitializer


먼저 class KeyValueTensorInitializer에 대해서 알아보자

일단 설명하기로는 주어진 키와 값 텐서를 테이블로 초기화하는 초기화자로 써있는데...

tf.lookup.KeyValueTensorInitializer(
	keys, values, key_dtype = None, value_dtype = None, name = None
)

 

  • keys : The tensor for the keys, key 텐서
  • values : The tensor for the values,  value 텐서
  • key_dtype : The keys data type. Used when keys is a python array. key의 데이터 형식을 지정할 수 있다.
  • value_dtype : The values data type. Used when values is a python array. value의 데이터 형식을 지정할 수 있다.
  • name : A name for the operation(optional). 옵션으로 해도 되고 안해도 되지만 한다면 이름을 지정하는것 같다...

예제를 만들어서 살펴보자

keys = ["연필", "지우개", "볼펜"]
values = [1,2,3]
table_init = tf.lookup.KeyValueTensorInitializer(keys, values)

근데 막상 이렇게 만들어서 초기화 한 값을 출력해보면 주속 값만 나오게 되는데 

이렇게 한 테이블을 출력하기 위해선 다른 명령어를 입력해서 할당해야하는데(이유는 나중에 다시 살펴보도록하자...)

본래 tensorflow에서 텍스트 파일을 리스트로 만들기위해서 이리저리 알아보다가 여기까지 오게되었는데...아무튼 입력한 값을 출력하기 위해서는 다음과 같은 명령어가 필요하다.

num_oov_buckets = 2
table = tf.lookup.StaticVocabularyTable(table_init, num_oov_buckets)
keys1 = tf.constant(keys)
table.lookup(keys1)
<tf.Tensor: shape=(3,), dtype=int64, numpy=array([1,2,3])>

물론 다음과 같이 출력되게 하기 위해서 아래의 조치도 취했다.

import tensorflow as tf
values = [1,2,3]
valuse = tf.dtypes.cast(values, tf.int64)

 

int64의 형태로 되어야 tf.lookup.StaticVocabularyTable이 먹히는것 같다.


그렇다면 tf.lookup.StaticVocabularyTable은 뭘하는 클래스일까...

역시나 다음 출처에서 알아보았다.

SOURCE : www.tensorflow.org/api_docs/python/tf/lookup/StaticVocabularyTable

 

tf.lookup.StaticVocabularyTable  |  TensorFlow Core v2.3.0

String to Id table wrapper that assigns out-of-vocabulary keys to buckets.

www.tensorflow.org

String to ld table wrapper that assigns out-of-vocabulary keys to buckets.

라고 쓰여있는데...

흠 다음에 기회가되면 더 알아보도록 해야할것 같다...

+ Recent posts