Я нашел в tenorflow do c следующую функцию для вычисления и применения словаря к строковому тензору, но он все еще использовал tf.session
, и я не могу заставить его работать с tf.function
:
import tensorflow as tf
import tensorflow_transform as tft
@tf.function(input_signature=(tf.TensorSpec(shape=[None], dtype=tf.string),))
def string_to_one_hot(labels):
codes = tft.compute_and_apply_vocabulary(labels)
return tf.one_hot(codes, depth=tf.cast(tf.reduce_max(codes), tf.int32))
test_labels = tf.constant(['a', 'b', 'a', 'c'])
test_one_hot = string_to_one_hot(test_labels)
> tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'compute_and_apply_vocabulary/vocabulary/Placeholder' with dtype string
[[node compute_and_apply_vocabulary/vocabulary/Placeholder (defined at /Users/clementwalter/.pyenv/versions/keras_fsl/lib/python3.6/site-packages/tensorflow_transform/analyzer_nodes.py:102) ]] [Op:__inference_string_to_one_hot_52]
РЕДАКТИРОВАТЬ
Мне удалось создать такую функцию с непосредственным использованием средств ha sh. Однако мне пришлось использовать жестко заданный параметр bucket_size / глубины. Есть идеи?
@tf.function(input_signature=(tf.TensorSpec(shape=[None], dtype=tf.string),))
def string_to_one_hot(labels):
one_hot = tf.one_hot(tf.strings.to_hash_bucket_fast(labels, 1024), depth=1024)
return tf.boolean_mask(one_hot, tf.reduce_sum(one_hot, axis=0) > 0, axis=1)