如何在张量流中将向量保存在字典中? [英] How to keep vectors in a dictionary in tensorflow?

查看:22
本文介绍了如何在张量流中将向量保存在字典中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎 tf.lookup.experimental.DenseHashTable 不能容纳向量,我找不到如何使用它的示例.

It seems that tf.lookup.experimental.DenseHashTable cannot hold vectors and I could not find examples of how to use it.

推荐答案

您可以在下面找到 Tensorflow 中向量字典的简单实现.也是tf.lookup.experimental.DenseHashTabletf.TensorArray的用法示例.

Below you can find a simple implementation of dictionary of vectors in Tensorflow. It is also an example of usage of tf.lookup.experimental.DenseHashTable and tf.TensorArray.

如前所述,向量不能保存在tf.lookup.experimental.DenseHashTable中,因此tf.TensorArray用于保存实际的向量.

As said, vectors cannot be kept in tf.lookup.experimental.DenseHashTable, and therefore tf.TensorArray is used to keep the actual vectors.

当然,这是一个简单的例子,它不包括删除字典中的条目——这个操作需要对数组的空闲单元进行一些管理.此外,您应该阅读 tf.lookup.experimental.DenseHashTabletf.TensorArray 各自的 API 页面,了解如何根据您的需要调整它们.

Of course, this is a simple example, and it does not include deletion of entries in the dictionary - an operation that will require some management of the free cells of the array. Also, you should read in the respective API pages of tf.lookup.experimental.DenseHashTable and tf.TensorArray how to tune them for your needs.

import tensorflow as tf


class DictionaryOfVectors:

  def __init__(self, dtype):
    empty_key = tf.constant('')
    deleted_key = tf.constant('deleted')

    self.ht = tf.lookup.experimental.DenseHashTable(key_dtype=tf.string,
                                                    value_dtype=tf.int32,
                                                    default_value=-1,
                                                    empty_key=empty_key,
                                                    deleted_key=deleted_key)
    self.ta = tf.TensorArray(dtype, size=0, dynamic_size=True, clear_after_read=False)
    self.inserts_counter = 0

  @tf.function
  def insertOrAssign(self, key, vec):
    # Insert the vector to the TensorArray. The write() method returns a new
    # TensorArray object with flow that ensures the write occurs. It should be 
    # used for subsequent operations.
    with tf.init_scope():
      self.ta = self.ta.write(self.inserts_counter, vec)

      # Insert the same counter value to the hash table
      self.ht.insert_or_assign(key, self.inserts_counter)
      self.inserts_counter += 1

  @tf.function
  def lookup(self, key):
    with tf.init_scope():
      index = self.ht.lookup(key)
      return self.ta.read(index)

dictionary_of_vectors = DictionaryOfVectors(dtype=tf.float32)
dictionary_of_vectors.insertOrAssign('first', [1,2,3,4,5])
print(dictionary_of_vectors.lookup('first'))

这个例子有点复杂,因为插入和查找方法用 @tf.function 修饰.因为这些方法改变了在它们之外定义的变量,所以使用了 tf.init_scope().您可能会问 lookup() 方法中发生了什么变化,因为它实际上只从哈希表和数组中读取.原因是在图形模式下,从 lookup() 调用返回的索引是一个 Tensor,而在 TensorArray 实现中有一行包含 if index <;0: 失败:

The example is a bit more sophisticated, as the insert and lookup methods are decorated with @tf.function. Because the methods change variables defined outside of them, the tf.init_scope() is used. You might ask what is changed in the lookup() method as it actually only reads from the hash table and the array. The reason is that in graph mode, the index that is returned from the lookup() call is a Tensor, and in the TensorArray implementation there is a line containing if index < 0: which fails with:

OperatorNotAllowedInGraphError:不允许使用 tf.Tensor 作为 Python bool.

OperatorNotAllowedInGraphError: using a tf.Tensor as a Python bool is not allowed.

当我们使用 tf.init_scope() 时,如其 API 文档中所述,即使在跟踪 tf.functiontf.function代码>".因此,在这种情况下,该索引不是张量而是标量.

When we use the tf.init_scope(), as explained in its API documentation, "code inside an init_scope block runs with eager execution enabled even when tracing a tf.function". So in that case that index is not a Tensor but as scalar.

这篇关于如何在张量流中将向量保存在字典中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆