首页 文章

当占位符提供形状时,Tensorflow稀疏到密集操作失败

提问于
浏览
2

我有一个问题是将稀疏张量(稀疏占位符)乘以Tensorflow中的密集张量 . 我也遇到了直接将稀疏张量转换为致密张量的问题 . 我搜索过,但到目前为止还没有找到这个问题的一个例子 .

TL; DR

如果稀疏占位符将 shape 参数设置为 None ,则计算有效,但如果我提供类似 (3,3) 的形状则失败 .

这是有效的代码:

import tensorflow as tf
import numpy as np

matrix_place = tf.placeholder(tf.float32, name="foo", shape=(3,2))
sparse_place = tf.sparse_placeholder(tf.float32, name="bar", shape=None) # Note shape is None

mul_result = tf.sparse_tensor_dense_matmul(sparse_place, matrix_place)

matrix_input = np.ones((3,2))
sparse_input = tf.SparseTensorValue([[0,0], [1,1], [2,2]], [1, 2, 3], (3, 3))

with tf.Session() as sess:
    result = sess.run(mul_result, feed_dict={sparse_place: sparse_input, matrix_place: matrix_input})
    print(result)

输出(如预期):

[[1. 1.]
 [2. 2.]
 [3. 3.]]

现在,如果我改变这一行:

sparse_place = tf.sparse_placeholder(tf.float32, name="bar", shape=None)

对此:

sparse_place = tf.sparse_placeholder(tf.float32, name="bar", shape=(3,3))

(它与我正在喂食的 tf.SparseTenorValue(...) 的形状相匹配),我收到以下错误:

Traceback (most recent call last):
  File "testing_sparse3.py", line 13, in <module>
    result = sess.run(mul_result, feed_dict={sparse_place: sparse_input, matrix_place: matrix_input})
  File "C:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\client\session.py", line 905, in run
    run_metadata_ptr)
  File "C:\Anaconda3\envs\tensorflow-cpu\lib\site-packages\tensorflow\python\client\session.py", line 1115, in _run
    raise ValueError('Tensor %s may not be fed.' % subfeed_t)
ValueError: Tensor Tensor("bar/shape:0", shape=(2,), dtype=int64) may not be fed.

我试过的事情

如果我从矩阵乘法(涉及2个张量)切换:

sparse_place = tf.sparse_placeholder(tf.float32, name="bar", shape=(3,3))

mul_result = tf.sparse_tensor_dense_matmul(sparse_place, matrix_place)

简单地总结稀疏张量的元素(只涉及稀疏张量):

sparse_place = tf.sparse_placeholder(tf.float32, name="bar", shape=(3,3))

mul_result = tf.sparse_tensor_reduce_sum(sparse_place)

它不会产生错误并给出正确的结果 . 但是,如果我尝试将稀疏张量转换为密集张量(也只涉及稀疏张量的op):

sparse_place = tf.sparse_placeholder(tf.float32, name="bar", shape=(3,3))

mul_result = tf.sparse_tensor_to_dense(sparse_place)

错误返回 .

我确实看到了related问题,但似乎已经merged了 . 我尝试将传递给 tf.SparseTensorValue 的indices / values / shape分别转换为带有dtypes int64 / float32 / int64 的numpy数组,但问题仍然存在 . 我也尝试将传递给稀疏占位符的 (3,3) 形状转换为numpy int64 数组,但也失败了 .

有任何想法吗?我错过了一些非常明显的事吗?我在Windows上使用Python 3.5和Tensorflow v1.6.0(CPU) .

谢谢!

1 回答

  • 0

    当你说 shape = (3, 3) TensorFlow将该形状视为形状推断的常数,并且不允许喂养该形状张量 . 我们应该检测到你正在提供它的实际 Value 并让它通过 . 提交github问题 .

相关问题