本文整理汇总了Python中theano.sparse.csr_matrix方法的典型用法代码示例。如果您正苦于以下问题:Python sparse.csr_matrix方法的具体用法?Python sparse.csr_matrix怎么用?Python sparse.csr_matrix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano.sparse
的用法示例。
在下文中一共展示了sparse.csr_matrix方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: placeholder
# 需要导入模块: from theano import sparse [as 别名]
# 或者: from theano.sparse import csr_matrix [as 别名]
def placeholder(shape=None, ndim=None, dtype=None, sparse=False, name=None):
'''Instantiate an input data placeholder variable.
'''
if dtype is None:
dtype = floatx()
if shape is None and ndim is None:
raise ValueError('Specify either a shape or ndim value.')
if shape is not None:
ndim = len(shape)
else:
shape = tuple([None for _ in range(ndim)])
broadcast = (False,) * ndim
if sparse:
_assert_sparse_module()
x = th_sparse_module.csr_matrix(name=name, dtype=dtype)
else:
x = T.TensorType(dtype, broadcast)(name)
x._keras_shape = shape
x._uses_learning_phase = False
return x
示例2: test_csm_properties_grad
# 需要导入模块: from theano import sparse [as 别名]
# 或者: from theano.sparse import csr_matrix [as 别名]
def test_csm_properties_grad(self):
sp_types = {'csc': sp.csc_matrix,
'csr': sp.csr_matrix}
for format in ['csc', 'csr']:
for dtype in ['float32', 'float64']:
spmat = sp_types[format](random_lil((4, 3), dtype, 3))
verify_grad_sparse(lambda *x: CSMProperties()(*x)[0], [spmat],
structured=True)
verify_grad_sparse(lambda *x: CSMProperties()(*x)[1], [spmat],
structured=True)
verify_grad_sparse(lambda *x: CSMProperties()(*x)[2], [spmat],
structured=True)
verify_grad_sparse(lambda *x: CSMProperties()(*x)[2], [spmat],
structured=True)
示例3: test_csm_properties
# 需要导入模块: from theano import sparse [as 别名]
# 或者: from theano.sparse import csr_matrix [as 别名]
def test_csm_properties(self):
sp_types = {'csc': sp.csc_matrix,
'csr': sp.csr_matrix}
for format in ['csc', 'csr']:
for dtype in ['float32', 'float64']:
x = SparseType(format, dtype=dtype)()
f = theano.function([x], csm_properties(x))
spmat = sp_types[format](random_lil((4, 3), dtype, 3))
data, indices, indptr, shape = f(spmat)
assert numpy.all(data == spmat.data)
assert numpy.all(indices == spmat.indices)
assert numpy.all(indptr == spmat.indptr)
assert numpy.all(shape == spmat.shape)
示例4: test_csm_unsorted
# 需要导入模块: from theano import sparse [as 别名]
# 或者: from theano.sparse import csr_matrix [as 别名]
def test_csm_unsorted(self):
"""
Test support for gradients of unsorted inputs.
"""
sp_types = {'csc': sp.csc_matrix,
'csr': sp.csr_matrix}
for format in ['csr', 'csc', ]:
for dtype in ['float32', 'float64']:
x = tensor.tensor(dtype=dtype, broadcastable=(False,))
y = tensor.ivector()
z = tensor.ivector()
s = tensor.ivector()
# Sparse advanced indexing produces unsorted sparse matrices
a = sparse_random_inputs(format, (4, 3), out_dtype=dtype,
unsorted_indices=True)[1][0]
# Make sure it's unsorted
assert not a.has_sorted_indices
def my_op(x):
y = tensor.constant(a.indices)
z = tensor.constant(a.indptr)
s = tensor.constant(a.shape)
return tensor.sum(
dense_from_sparse(CSM(format)(x, y, z, s) * a))
verify_grad_sparse(my_op, [a.data])
示例5: test_csm
# 需要导入模块: from theano import sparse [as 别名]
# 或者: from theano.sparse import csr_matrix [as 别名]
def test_csm(self):
sp_types = {'csc': sp.csc_matrix,
'csr': sp.csr_matrix}
for format in ['csc', 'csr']:
for dtype in ['float32', 'float64']:
x = tensor.tensor(dtype=dtype, broadcastable=(False,))
y = tensor.ivector()
z = tensor.ivector()
s = tensor.ivector()
f = theano.function([x, y, z, s], CSM(format)(x, y, z, s))
spmat = sp_types[format](random_lil((4, 3), dtype, 3))
res = f(spmat.data, spmat.indices, spmat.indptr,
numpy.asarray(spmat.shape, 'int32'))
assert numpy.all(res.data == spmat.data)
assert numpy.all(res.indices == spmat.indices)
assert numpy.all(res.indptr == spmat.indptr)
assert numpy.all(res.shape == spmat.shape)
示例6: test_dot_sparse_sparse
# 需要导入模块: from theano import sparse [as 别名]
# 或者: from theano.sparse import csr_matrix [as 别名]
def test_dot_sparse_sparse(self):
# test dot for 2 input sparse matrix
sparse_dtype = 'float64'
sp_mat = {'csc': sp.csc_matrix,
'csr': sp.csr_matrix,
'bsr': sp.csr_matrix}
for sparse_format_a in ['csc', 'csr', 'bsr']:
for sparse_format_b in ['csc', 'csr', 'bsr']:
a = SparseType(sparse_format_a, dtype=sparse_dtype)()
b = SparseType(sparse_format_b, dtype=sparse_dtype)()
d = theano.dot(a, b)
f = theano.function([a, b], theano.Out(d, borrow=True))
topo = f.maker.fgraph.toposort()
for M, N, K, nnz in [(4, 3, 2, 3),
(40, 30, 20, 3),
(40, 30, 20, 30),
(400, 3000, 200, 6000),
]:
a_val = sp_mat[sparse_format_a](
random_lil((M, N), sparse_dtype, nnz))
b_val = sp_mat[sparse_format_b](
random_lil((N, K), sparse_dtype, nnz))
f(a_val, b_val)
示例7: setUp
# 需要导入模块: from theano import sparse [as 别名]
# 或者: from theano.sparse import csr_matrix [as 别名]
def setUp(self):
super(DotTests, self).setUp()
x_size = (10, 100)
y_size = (100, 1000)
utt.seed_rng()
self.x_csr = scipy.sparse.csr_matrix(
numpy.random.binomial(1, 0.5, x_size), dtype=theano.config.floatX)
self.x_csc = scipy.sparse.csc_matrix(
numpy.random.binomial(1, 0.5, x_size), dtype=theano.config.floatX)
self.y = numpy.asarray(numpy.random.uniform(-1, 1, y_size),
dtype=theano.config.floatX)
self.y_csr = scipy.sparse.csr_matrix(
numpy.random.binomial(1, 0.5, y_size), dtype=theano.config.floatX)
self.y_csc = scipy.sparse.csc_matrix(
numpy.random.binomial(1, 0.5, y_size), dtype=theano.config.floatX)
self.v_10 = numpy.asarray(numpy.random.uniform(-1, 1, 10),
dtype=theano.config.floatX)
self.v_100 = numpy.asarray(numpy.random.uniform(-1, 1, 100),
dtype=theano.config.floatX)
示例8: test_csr_dense
# 需要导入模块: from theano import sparse [as 别名]
# 或者: from theano.sparse import csr_matrix [as 别名]
def test_csr_dense(self):
x = theano.sparse.csr_matrix('x')
y = theano.tensor.matrix('y')
v = theano.tensor.vector('v')
for (x, y, x_v, y_v) in [(x, y, self.x_csr, self.y),
(x, v, self.x_csr, self.v_100),
(v, x, self.v_10, self.x_csr)]:
f_a = theano.function([x, y], theano.sparse.dot(x, y))
f_b = lambda x, y: x * y
utt.assert_allclose(f_a(x_v, y_v), f_b(x_v, y_v))
# Test infer_shape
self._compile_and_check([x, y], [theano.sparse.dot(x, y)],
[x_v, y_v],
(Dot, Usmm, UsmmCscDense))
示例9: test_size
# 需要导入模块: from theano import sparse [as 别名]
# 或者: from theano.sparse import csr_matrix [as 别名]
def test_size():
"""
Ensure the `size` attribute of sparse matrices behaves as in numpy.
"""
for sparse_type in ('csc_matrix', 'csr_matrix'):
x = getattr(theano.sparse, sparse_type)()
y = getattr(scipy.sparse, sparse_type)((5, 7)).astype(config.floatX)
get_size = theano.function([x], x.size)
def check():
assert y.size == get_size(y)
# We verify that the size is correctly updated as we store more data
# into the sparse matrix (including zeros).
check()
y[0, 0] = 1
check()
y[0, 1] = 0
check()
示例10: test_infer_shape
# 需要导入模块: from theano import sparse [as 别名]
# 或者: from theano.sparse import csr_matrix [as 别名]
def test_infer_shape(self):
mat = (numpy.arange(12) + 1).reshape((4, 3))
mat[0, 1] = mat[1, 0] = mat[2, 2] = 0
x_csc = theano.sparse.csc_matrix(dtype=theano.config.floatX)
mat_csc = sp.csc_matrix(mat, dtype=theano.config.floatX)
self._compile_and_check([x_csc],
[Remove0()(x_csc)],
[mat_csc],
self.op_class)
x_csr = theano.sparse.csr_matrix(dtype=theano.config.floatX)
mat_csr = sp.csr_matrix(mat, dtype=theano.config.floatX)
self._compile_and_check([x_csr],
[Remove0()(x_csr)],
[mat_csr],
self.op_class)
示例11: test_GetItem2Lists
# 需要导入模块: from theano import sparse [as 别名]
# 或者: from theano.sparse import csr_matrix [as 别名]
def test_GetItem2Lists(self):
a, A = sparse_random_inputs('csr', (4, 5))
b, B = sparse_random_inputs('csc', (4, 5))
y = a[0][[0, 0, 1, 3], [0, 1, 2, 4]]
z = b[0][[0, 0, 1, 3], [0, 1, 2, 4]]
fa = theano.function([a[0]], y)
fb = theano.function([b[0]], z)
t_geta = fa(A[0])
t_getb = fb(B[0])
s_geta = numpy.asarray(scipy.sparse.csr_matrix(A[0])[[0, 0, 1, 3], [0, 1, 2, 4]])
s_getb = numpy.asarray(scipy.sparse.csc_matrix(B[0])[[0, 0, 1, 3], [0, 1, 2, 4]])
utt.assert_allclose(t_geta, s_geta)
utt.assert_allclose(t_getb, s_getb)
示例12: test_mul_s_v
# 需要导入模块: from theano import sparse [as 别名]
# 或者: from theano.sparse import csr_matrix [as 别名]
def test_mul_s_v(self):
sp_types = {'csc': sp.csc_matrix,
'csr': sp.csr_matrix}
for format in ['csr', 'csc']:
for dtype in ['float32', 'float64']:
x = theano.sparse.SparseType(format, dtype=dtype)()
y = tensor.vector(dtype=dtype)
f = theano.function([x, y], mul_s_v(x, y))
spmat = sp_types[format](random_lil((4, 3), dtype, 3))
mat = numpy.asarray(numpy.random.rand(3), dtype=dtype)
out = f(spmat, mat)
utt.assert_allclose(spmat.toarray() * mat, out.toarray())
示例13: placeholder
# 需要导入模块: from theano import sparse [as 别名]
# 或者: from theano.sparse import csr_matrix [as 别名]
def placeholder(shape=None, ndim=None, dtype=None, sparse=False, name=None):
"""Instantiate an input data placeholder variable.
"""
if dtype is None:
dtype = floatx()
if shape is None and ndim is None:
raise ValueError('Specify either a shape or ndim value.')
if shape is not None:
ndim = len(shape)
else:
shape = tuple([None for _ in range(ndim)])
name = _prepare_name(name, 'placeholder')
broadcast = (False,) * ndim
if sparse:
_assert_sparse_module()
x = th_sparse_module.csr_matrix(name=name, dtype=dtype)
else:
x = T.TensorType(dtype, broadcast)(name)
x._keras_shape = shape
x._uses_learning_phase = False
x._theano_placeholder = True
return x
示例14: as_sparse_format
# 需要导入模块: from theano import sparse [as 别名]
# 或者: from theano.sparse import csr_matrix [as 别名]
def as_sparse_format(data, format):
if format == 'csc':
return scipy.sparse.csc_matrix(data)
elif format == 'csr':
return scipy.sparse.csr_matrix(data)
else:
raise NotImplementedError()
示例15: test_grad_fail
# 需要导入模块: from theano import sparse [as 别名]
# 或者: from theano.sparse import csr_matrix [as 别名]
def test_grad_fail(self):
self.assertRaises(verify_grad_sparse.E_grad,
verify_grad_sparse,
self.FailOp(structured=False),
[sp.csr_matrix(random_lil((10, 40),
config.floatX, 3))])
self.assertRaises(verify_grad_sparse.E_grad,
verify_grad_sparse,
self.FailOp(structured=True),
[sp.csr_matrix(random_lil((10, 40),
config.floatX, 3))])