当占位符提供形状

时间:2018-03-22 22:48:24

标签: python tensorflow sparse-matrix

我有一个问题是将稀疏张量(稀疏占位符)乘以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的索引/值/形状分别转换为带有dtypes int64 / float32 / int64的numpy数组,但问题仍然存在。我还尝试将传递给稀疏占位符的(3,3)形状转换为numpy int64数组,但也失败了。

有什么想法吗?我错过了很明显的东西吗?我在Windows上使用Python 3.5和Tensorflow v1.6.0(CPU)。

谢谢!

1 个答案:

答案 0 :(得分:0)

When you say shape = (3, 3) TensorFlow treats that shape as a constant for shape inference, and disallows feeding that shape tensor. We should probably detect that you're feeding its actual value and let it pass. File a github issue.