Python 使用tf.sparse_matmul的矩阵乘法使用SparseTensor失败
为什么这不起作用:Python 使用tf.sparse_matmul的矩阵乘法使用SparseTensor失败,python,tensorflow,Python,Tensorflow,为什么这不起作用: pl_input = tf.sparse_placeholder('float32',shape=[None,30]) W = tf.Variable(tf.random_normal(shape=[30,1]), dtype='float32') layer1a = tf.sparse_matmul(pl_input, weights, a_is_sparse=True, b_is_sparse=False) 错误消息是 TypeError:无法将类型的对象转换为Tens
pl_input = tf.sparse_placeholder('float32',shape=[None,30])
W = tf.Variable(tf.random_normal(shape=[30,1]), dtype='float32')
layer1a = tf.sparse_matmul(pl_input, weights, a_is_sparse=True, b_is_sparse=False)
错误消息是
TypeError:无法将类型的对象转换为Tensor。内容:SparseTensor(索引=张量(“占位符_11:0”,形状=(?,?),数据类型=int64),值=张量(“占位符_10:0,形状=(?,),数据类型=float32),密集_形状=张量(“占位符_9:0”,形状=(?,),数据类型=int64))。将铸造元素考虑为支持类型。< /代码>
我希望创建一个sparsetenservalue,从中检索批,然后将批输入到pl_输入中 TL;博士
使用tf.sparse\u tensor\u densite\u matmul
代替tf.sparse\u matmul
;使用tf.nn.embedded\u lookup\u sparse
查看以寻找替代方法
关于稀疏矩阵和稀疏传感器
这个问题并不特定于稀疏占位符
,而是由于tensorflow的术语混乱造成的
你有稀疏矩阵。然后就是SparseTensor
。两者是相关的,但概念不同
- 稀疏传感器是一种索引其值的结构,可以有效地表示稀疏矩阵或张量
- 稀疏矩阵是一种主要填充
0
的矩阵。在tensorflow的文档中,它通常不是指SparseTensor
,而是指一个普通的Tensor
,主要填充0
s
因此,了解函数参数的预期类型非常重要
因此,例如,在中,操作数需要是普通的张量
s,而不是稀疏传感器
s,独立于xxx\u is\u稀疏
标志的值,这解释了错误。当这些标志为True
时,tf.sparse_matmul
实际期望的是一个(密集的)张量。换句话说,这些标志服务于而不是输入类型约束。(顺便说一句,这些优化似乎只对你有用)