【问题标题】:Theano gradient of sparse matrix multiplication稀疏矩阵乘法的 Theano 梯度
【发布时间】:2017-09-03 22:58:19
【问题描述】:

我正在尝试在 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 中工作?

【问题讨论】:

    标签: python numpy scipy sparse-matrix theano


    【解决方案1】:

    您不能对稀疏变量使用 T.* 函数。在这种情况下,您可以使用:

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

    \edit 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 中。

    【讨论】:

    • 如果我将cost = T.sum(x * T.log(z)) 更改为cost = theano.sparse.sp_sum(x * T.log(z)),我仍然会收到以下错误:AsTensorError: ('Variable type field must be a TensorType.', SparseVariable{csr,float64}, Sparse[float64, csr])。我在以下笔记本中对此进行了说明:nbviewer.ipython.org/urls/gist.githubusercontent.com/…
    • 此问题已在 Theano master 中修复。感谢您的报告。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    • 2013-06-09
    • 2018-07-27
    • 1970-01-01
    相关资源
    最近更新 更多