mardi 28 juin 2016

Change from sparse to dense

I have the following code:

import numpy 
import theano
import theano.tensor
import theano.sparse
from theano.sparse import dense_from_sparse
import theano.printing

def step(i_t, w, x):
    return theano.sparse.dot(x[i_t:i_t + 1], w)


def main():
    x = theano.sparse.csr_matrix()
    y = theano.tensor.scalar()
    ww = numpy.asarray([[1, 2, 3], [4, 5, 6]])

    w = theano.shared(ww.astype(theano.config.floatX))
    theano.printing.debugprint(w)
    hh, _ = theano.scan(step, sequences=[theano.tensor.arange(x.shape[0], dtype='int64')], non_sequences=[w, x], strict=True)

#    gy = theano.tensor.grad(hh.sum(), w)


main()

The problem here is that simply taking the gradient returns an error saying GetItem2d.grad is not implemented. I want to convert the contents returned by scan from sparse to dense, such that the dot product is done in the sparse way but after scan completes and is passed to tensor.grad, I want the dense gradient to be calculated as the sparse version of that isn't implemented.

Question: Is there a way in which the sparse dot can be utilised but then while computing the gradient the dense matrix is passed into it (the returned contents of scan)

I tried this but it failed:

def step(i_t, w, x):
    print w.get_value()
    return theano.sparse.dot(x[i_t:i_t + 1], w)

def main():
    x = theano.sparse.csr_matrix()
    ww = numpy.asarray([[1, 2, 3], [4, 5, 6]])

    w = theano.shared(ww.astype(theano.config.floatX))
    hh, _ = theano.scan(step, sequences=[theano.tensor.arange(x.shape[0], dtype='int64')], non_sequences=[w, x], strict=True)

    hh.owner.inputs[-1] = dense_from_sparse(hh.owner.inputs[-1])

    gy = theano.tensor.grad(hh.sum(), w)

The error I received :

TypeError: Cannot convert Type TensorType(float64, matrix) (of Variable DenseFromSparse{structured_grad=True}.0) into Type Sparse[float64, csr]. 
You can try to manually convert DenseFromSparse{structured_grad=True}.0 into a Sparse[float64, csr].

Aucun commentaire:

Enregistrer un commentaire