Пример #1
0
 def _applyOpToNonzerosOfDense(self, op, expr):
     # useful subroutine
     sparseExpr = TSB.clean(TSB.csr_from_dense(expr))
     newData = op(TSB.csm_data(sparseExpr)).flatten()
     newSparse = TS.CSR(newData, TSB.csm_indices(sparseExpr),
                        TSB.csm_indptr(sparseExpr),
                        TSB.csm_shape(sparseExpr))
     return TSB.dense_from_sparse(newSparse)
def shared_sparse(arr):
    data = arr.data
    indices = arr.indices
    indptr = arr.indptr
    shape = np.array(arr.shape)
    return sparse.CSR(data, indices, indptr, shape)
Пример #3
0
# if shape[0] > shape[1], use csr. Otherwise, use csc
# but, not all ops are available for both yet
# so use the one that has what you need

# to and fro

x = sparse.csc_matrix(name='x', dtype='float32')
y = sparse.dense_from_sparse(x)
z = sparse.csc_from_dense(y)

# resconstruct a csc from a csr

x = sparse.csc_matrix(name='x', dtype='int64')
data, indices, indptr, shape = sparse.csm_properties(x)
y = sparse.CSR(data, indices, indptr, shape)
f = theano.function([x], y)
a = sp.csc_matrix(np.asarray([[0, 1, 1], [0, 0, 0], [1, 0, 0]]))
print a.toarray()
print f(a).toarray()

# "structured" operations
# act only on (originally) nonzero elements

x = sparse.csc_matrix(name='x', dtype='float32')
y = sparse.structured_add(x, 2)
f = theano.function([x], y)
a = sp.csc_matrix(
    np.asarray([[0, 0, -1], [0, -2, 1], [3, 0, 0]], dtype='float32'))
print a.toarray()
print f(a).toarray()
Пример #4
0
    print w[0]
    #print db.rowAsSymbolDict(w[0])

    print 'f2(w=a,c=b)'
    tw = S.csr_matrix('w')  #weighter
    tc = S.csr_matrix('c')  #constant
    r2 = B.sp_sum(tw * 1.7, sparse_grad=True)
    s2 = tc * r2
    f2 = theano.function(inputs=[tw, tc], outputs=[s2])
    w = f2(va, vb)
    print w[0]

    print 'f3(w=a), b constant'
    tw3 = S.csr_matrix('w')  #weighter
    #y = sparse.CSR(data, indices, indptr, shape)
    tc3 = S.CSR(vb.data, vb.indices, vb.indptr, vb.shape)
    r3 = B.sp_sum(tw3 * 1.7, sparse_grad=True)
    s3 = tc3 * r3
    f3 = theano.function(inputs=[tw3], outputs=[s3])
    w = f3(va)
    print w[0]

#    r2 = B.sp_sum(tw+tw,sparse_grad=True)
#    #tb = S.csc_matrix(vb,name='tb')  #constant
#    bcoo = vb.tocoo()
#    tb = S.csc_matrix((bcoo.data,bcoo.row,bcoo.col),name='tb')
#    s2 = tb*r2
#    #s2 = r1*va

#
#    print 'f2'
Пример #5
0
 def theanoExpr(self, env):
     #convert to a sparse vector constant
     c = env.db.onehot(self.onehotConst)
     theanoConstVec = S.CSR(c.data, c.indices, c.indptr, c.shape)
     env.binding[self.dst] = theanoConstVec * B.sp_sum(
         env.binding[self.weighter], sparse_grad=True)
Пример #6
0
 def theanoExpr(self, env):
     c = env.db.onehot(self.onehotConst)
     theanoConstVec = S.CSR(c.data, c.indices, c.indptr, c.shape)
     env.binding[self.dst] = env.binding[self.src] * theanoConstVec