gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:53:50 24 4
gpt4 key购买 nike

在下面的代码中,我希望稠密矩阵 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com