2014-11-20 210 views
2

我試圖在Theano中實現一個帶有稀疏輸入的自編碼器。稀疏矩陣乘法的Theano梯度

我得到了稀疏自動編碼器與平方誤差成本函數一起工作。但是,如果我想申請一個交叉熵錯誤,其中包含矩陣乘法,我得到以下錯誤:

AsTensorError: ('Variable type field must be a TensorType.', SparseVariable{csr,float64}, Sparse[float64, csr]) 

我上傳一個例子筆記本出在http://nbviewer.ipython.org/urls/gist.githubusercontent.com/peterroelants/4946cdbf189c5e75f2b7/raw/2ee7d3e533a4a6ac2707a2ffa310b81a86e70afd/gistfile1.json問題。

我將問題解決到矩陣乘法cost = T.sum(x * T.log(z))。這在密集的情況下工作[見單元格2],但在稀疏的情況下給出錯誤[請參閱單元格3]。請注意,將稀疏情況[單元格3]中的此成本函數更改爲平方錯誤(cost = T.sum((x-z)**2))將導致工作結果。

任何人都可以指出我做錯了什麼?並告訴我如何獲得具有交叉熵錯誤的稀疏輸入自動編碼器在Theano中工作?

回答

1

你不能在稀疏變量上使用T. *函數。在這種情況下,你可以使用:

theano.sparse.sp_sum((x * T.log(z)) 

\編輯在Theano這種差異修復修復此故障:

diff --git a/theano/sparse/basic.py b/theano/sparse/basic.py 
index 4620c5a..a352b9a 100644 
--- a/theano/sparse/basic.py 
+++ b/theano/sparse/basic.py 
@@ -2244,7 +2244,7 @@ class MulSD(gof.op.Op): 
    def grad(self, (x, y), (gz,)): 
     assert _is_sparse_variable(x) and _is_dense_variable(y) 
     assert _is_sparse_variable(gz) 
-  return y * gz, x * gz 
+  return y * gz, dense_from_sparse(x * gz) 

    def infer_shape(self, node, shapes): 
     return [shapes[0]] 

我會盡力得到修復本週Theano合併。

+0

如果我將'cost = T.sum(x * T.log(z))'更改爲'cost = theano.sparse.sp_sum(x * T.log(z))',那麼我仍然會得到以下錯誤:'AsTensorError:('變量類型字段必須是TensorType。',SparseVariable {csr,float64},Sparse [float64,csr])'。 我在以下筆記本所示的該: http://nbviewer.ipython.org/urls/gist.githubusercontent.com/peterroelants/22990100d2c2973e75dc/raw/f30f2921789bd837df8e3021a1c2f6503e1fc6cc/gistfile1.txt – Xochipilli 2014-11-24 15:45:22

+1

這是固定在主Theano。感謝報告。 – nouiz 2014-11-27 16:43:12