我正在尝试使用 tf.nn.embedding_lookup() 并收到以下警告:
UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.
我读到这个 link为了避免这个问题,我们应该确保 tf.nn.embedding_lookup() 的参数输入是一个 tf.Variable
但是我传递给 tf.nn.embedding_lookup() 的已经是一个张量,它是另一个操作的输出,我想我不能用它来初始化 tf.Variable 。
有没有办法将张量转换为 tf.Variable 或用另一个张量初始化张量?
请您参考如下方法:
A
tf.Variablerepresents a tensor whose value can be changed by running ops on it.Internally, a
tf.Variablestores a persistent tensor.
来源:https://www.tensorflow.org/guide/variables
您可以使用 Tensor 对象初始化 tf.Variable:tf.Tensor
# Create a variable.
w = tf.Variable(<initial-value>, name=<optional-name>)
initial_value:一个张量,或可转换为张量的 Python 对象,它是变量的初始值。
除非 validate_shape 设置为 False,否则初始值必须指定形状。也可以是不带参数的可调用对象,在调用时返回初始值。在这种情况下,必须指定 dtype。
(请注意,init_ops.py 中的初始化函数必须先绑定(bind)到一个形状,然后才能在此处使用。)
总而言之,您可以使用另一个 Tensor 对象初始化一个 tf.Variable。
