python - tensorflow :使用 tf.matmul 将稀疏矩阵与稠密矩阵相乘时出错

标签 python tensorflow sparse-matrix

在下面的代码中,我希望稠密矩阵 B 左乘稀疏矩阵 A,但出现错误。

import tensorflow as tf
import numpy as np

A = tf.sparse_placeholder(tf.float32)
B = tf.placeholder(tf.float32, shape=(5,5))
C = tf.matmul(B,A,a_is_sparse=False,b_is_sparse=True)
sess = tf.InteractiveSession()
indices = np.array([[3, 2], [1, 2]], dtype=np.int64)
values = np.array([1.0, 2.0], dtype=np.float32)
shape = np.array([5,5], dtype=np.int64)
Sparse_A = tf.SparseTensorValue(indices, values, shape)
RandB = np.ones((5, 5))
print sess.run(C, feed_dict={A: Sparse_A, B: RandB})

错误信息如下:

TypeError: Failed to convert object of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'> 
to Tensor. Contents: SparseTensor(indices=Tensor("Placeholder_4:0", shape=(?, ?), dtype=int64), values=Tensor("Placeholder_3:0", shape=(?,), dtype=float32), dense_shape=Tensor("Placeholder_2:0", shape=(?,), dtype=int64)). 
Consider casting elements to a supported type.

我的代码有什么问题吗?

我正在按照 documentation 执行此操作它说我们应该使用a_is_sparse来表示第一个矩阵是否稀疏,与b_is_sparse类似。为什么我的代码是错误的?

按照vijay的建议,我应该使用C = tf.matmul(B,tf.sparse_tensor_to_dense(A),a_is_sparse=False,b_is_sparse=True)

我尝试了这个,但遇到了另一个错误:

Caused by op u'SparseToDense', defined at:
  File "a.py", line 19, in <module>
    C = tf.matmul(B,tf.sparse_tensor_to_dense(A),a_is_sparse=False,b_is_sparse=True)
  File "/home/fengchao.pfc/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/sparse_ops.py", line 845, in sparse_tensor_to_dense
    name=name)
  File "/home/mypath/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/sparse_ops.py", line 710, in sparse_to_dense
    name=name)
  File "/home/mypath/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_sparse_ops.py", line 1094, in _sparse_to_dense
    validate_indices=validate_indices, name=name)
  File "/home/mypath/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "/home/mypath/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2506, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/home/mypath/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1269, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): indices[1] = [1,2] is out of order
[[Node: SparseToDense = SparseToDense[T=DT_FLOAT, Tindices=DT_INT64, validate_indices=true, _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_Placeholder_4_0_2, _arg_Placeholder_2_0_0, _arg_Placeholder_3_0_1, SparseToDense/default_value)]]

谢谢大家对我的帮助!

最佳答案

tf.matmul中,标志a_is_sparseb_is_sparse并不指示操作数是SparseTensors,而是指示,它们是调用更有效的方法来计算两个密集张量乘法的算法提示。在你的代码中应该是:

C = tf.matmul(B,tf.sparse_tensor_to_dense(A),a_is_sparse=False,b_is_sparse=True)

要对 SparseTensordense 张量进行 matmul,您还可以使用 tf.sparse_tensor_dense_matmul() 代替。

关于python - tensorflow :使用 tf.matmul 将稀疏矩阵与稠密矩阵相乘时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45734487/

相关文章:

python - Python 中多处理的同步问题,双 for 循环

python - 如何将多个 url 参数传递给 RedirectView?

tensorflow - 损失图以及判断是否收敛的标准

windows - 当稀疏数组变大时,如何解决元素不再显示的问题?

python - 有没有办法使用python进一步缩短稀疏解决时间?

python - 递归满足条件时返回值

python - 从 Cassandra 检索有序计数器的最佳方法

python - 如何计算Tensorflow中训练RNN语言模型的准确率?

tensorflow - 如何微调现有的 Tensorflow 对象检测模型以识别其他类?

python - 有没有类似 coo_matrix 的东西,但是对于稀疏向量?