【问题标题】:Passing a tf.dataset as the keys of a dictionary将 tf.dataset 作为字典的键传递
【发布时间】:2021-06-17 19:20:40
【问题描述】:

我有一个与将 tf.dictionary 的元素作为字典键传递相关的问题。我已将其简化为以下最小示例:

def example(x,d):
   w=tf.vectorized_map(lambda y: d[y],tf.cast(x, tf.string))
   return w


dataset = tf.data.Dataset.from_tensor_slices([['a','d','s'],['b','e','a'],['c','f','d']])
d={'a':1,'b':2,'c':3,'d':4,'e':6,'f':5,'s':1}
dataset.map(lambda x: example(x,d))

我得到错误:

TypeError: Failed to convert object of type <class 'tensorflow.python.util.object_identity.Reference'> to Tensor. Contents: <Reference wrapping <tf.Tensor 'args_0:0' shape=(3,) dtype=string>>. Consider casting elements to a supported type.

我试图解决它,删除tf.cast(x, tf.string) 并将tf.vectorized_map 更改为tf.map_fn。在这两种情况下,我都会遇到相同的错误。

如何运行代码?

【问题讨论】:

    标签: python tensorflow tensorflow-datasets


    【解决方案1】:

    您可以使用tf.lookup.StaticHashTable 来实现此目的。

    import tensorflow as tf
    keys_tensor = tf.constant(['a', 'b', 'c', 'd', 'e', 'f', 's'])
    vals_tensor = tf.constant([1, 2, 3, 4, 6, 5, 1])
    table = tf.lookup.StaticHashTable(
        tf.lookup.KeyValueTensorInitializer(keys_tensor, vals_tensor),
        default_value=-1)
    
    dataset = tf.data.Dataset.from_tensor_slices([['a','d','s'],['b','e','a'],['c','f','d']])
    ds=dataset.map(lambda x:table[x])
    
    for x in ds:
      print(x)
    '''
    tf.Tensor([1 4 1], shape=(3,), dtype=int32)
    tf.Tensor([2 6 1], shape=(3,), dtype=int32)
    tf.Tensor([3 5 4], shape=(3,), dtype=int32)
    '''
    

    【讨论】:

      猜你喜欢
      • 2012-12-10
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 2019-11-18
      • 2021-09-26
      • 2021-09-27
      • 2012-09-15
      相关资源
      最近更新 更多