2017-07-14 33 views
0

爲什麼這個不行:與tf.sparse_matmul矩陣乘法失敗SparseTensor

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: Failed to convert object of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'> to Tensor. Contents: SparseTensor(indices=Tensor("Placeholder_11:0", shape=(?, ?), dtype=int64), values=Tensor("Placeholder_10:0", shape=(?,), dtype=float32), dense_shape=Tensor("Placeholder_9:0", shape=(?,), dtype=int64)). Consider casting elements to a supported type.

我希望創建一個我找回批次SparseTensorValue從,然後將批次送入pl_input。

回答

0

TL; DR

使用tf.sparse_tensor_dense_matmul代替tf.sparse_matmul;請使用tf.nn.embedding_lookup_sparse查看documentation的備選方案。

關於稀疏矩陣和SparseTensors

的問題是不特定於sparse_placeholder,但由於tensorflow的術語混淆。

你有稀疏矩陣。然後你有SparseTensor。兩者都是相關但不同的概念。

  • A SparseTensor是一種索引其值並可以有效表示稀疏矩陣或張量的結構。
  • 稀疏矩陣是一個矩陣,主要填充0。在張量流的文檔中,它通常不是而是是指SparseTensor,而是指的是一個普通的舊Tensor,其大部分填充0 s。

因此重要的是看看函數參數的預期類型。

因此,例如,在the documentation of tf.matmul,操作數必須是xxx_is_sparse標誌的值,這說明你的錯誤的獨立平原Tensor秒且不SparseTensor S,。當這些標誌是True時,tf.sparse_matmul實際上預期的是(密集的)Tensor。換句話說,這些標誌服務於some optimization purposes而不是輸入類型約束。 (這些優化似乎僅適用於rather larger matrices)。