提交 38a1316e authored 作者: Frederic's avatar Frederic

new sparse opt. that remove dense_from_sparse(sparse_from_dense(x)) -> x

上级 480440a6
......@@ -6,7 +6,7 @@ import scipy
import theano
from theano import gof, scalar, tensor
from theano.tensor import blas
from theano.tensor.opt import register_specialize
from theano.tensor.opt import register_specialize, register_canonicalize
from theano.sparse import (CSC, CSR, csm_properties,
csm_grad, usmm, csm_indices, csm_indptr,
csm_data)
......@@ -177,6 +177,16 @@ theano.compile.optdb.register('local_inplace_addsd_ccode',
60, 'fast_run', 'inplace')
@register_canonicalize("fast_compile")
@register_specialize
@gof.local_optimizer([sparse.DenseFromSparse])
def local_dense_from_sparse_sparse_from_dense(node):
if isinstance(node.op, sparse.DenseFromSparse):
inp = node.inputs[0]
if inp.owner and isinstance(inp.owner.op, sparse.SparseFromDense):
return inp.owner.inputs
@gof.local_optimizer([sparse.AddSD])
def local_addsd_ccode(node):
"""
......
......@@ -139,3 +139,17 @@ def test_local_sampling_dot_csr():
# be inserted
assert not any(isinstance(node.op, sparse.opt.SamplingDotCSR) for node
in f.maker.fgraph.toposort())
def test_local_dense_from_sparse_sparse_from_dense():
mode = theano.compile.mode.get_default_mode()
mode = mode.including("local_dense_from_sparse_sparse_from_dense")
m = theano.tensor.matrix()
for op in [theano.sparse.csr_from_dense, theano.sparse.csc_from_dense]:
s = op(m)
o = theano.sparse.dense_from_sparse(s)
f = theano.function([m], o, mode=mode)
# We should just have a deep copy.
assert len(f.maker.fgraph.apply_nodes) == 1
f([[1,2], [3, 4]])
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论