Python源码示例:theano.sparse.dot()
示例1
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)
示例2
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))
示例3
def test_csc_dense(self):
x = theano.sparse.csc_matrix('x')
y = theano.tensor.matrix('y')
v = theano.tensor.vector('v')
for (x, y, x_v, y_v) in [(x, y, self.x_csc, self.y),
(x, v, self.x_csc, self.v_100),
(v, x, self.v_10, self.x_csc)]:
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))
示例4
def test_int32_dtype(self):
# Reported on the theano-user mailing-list:
# https://groups.google.com/d/msg/theano-users/MT9ui8LtTsY/rwatwEF9zWAJ
size = 9
intX = 'int32'
C = tensor.matrix('C', dtype=intX)
I = tensor.matrix('I', dtype=intX)
fI = I.flatten()
data = tensor.ones_like(fI)
indptr = tensor.arange(data.shape[0] + 1, dtype='int32')
m1 = sparse.CSR(data, fI, indptr, (8, size))
m2 = sparse.dot(m1, C)
y = m2.reshape(shape=(2, 4, 9), ndim=3)
f = theano.function(inputs=[I, C], outputs=y)
i = numpy.asarray([[4, 3, 7, 7], [2, 8, 4, 5]], dtype=intX)
a = numpy.asarray(numpy.random.randint(0, 100, (size, size)),
dtype=intX)
f(i, a)
示例5
def test_op_ss(self):
for format in sparse.sparse_formats:
for dtype in sparse.all_dtypes:
variable, data = sparse_random_inputs(format,
shape=(10, 10),
out_dtype=dtype,
n=2,
p=0.1)
f = theano.function(variable, self.op(*variable))
tested = f(*data)
x, y = [m.toarray() for m in data]
expected = numpy.dot(x, y)
assert tested.format == format
assert tested.dtype == expected.dtype
tested = tested.toarray()
utt.assert_allclose(tested, expected)
示例6
def test_op_sd(self):
for format in sparse.sparse_formats:
for dtype in sparse.all_dtypes:
variable, data = sparse_random_inputs(format,
shape=(10, 10),
out_dtype=dtype,
n=2,
p=0.1)
variable[1] = tensor.TensorType(dtype=dtype,
broadcastable=(False, False))()
data[1] = data[1].toarray()
f = theano.function(variable, self.op(*variable))
tested = f(*data)
expected = numpy.dot(data[0].toarray(), data[1])
assert tested.format == format
assert tested.dtype == expected.dtype
tested = tested.toarray()
utt.assert_allclose(tested, expected)
示例7
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)
示例8
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
def test_csc_dense(self):
x = theano.sparse.csc_matrix('x')
y = theano.tensor.matrix('y')
v = theano.tensor.vector('v')
for (x, y, x_v, y_v) in [(x, y, self.x_csc, self.y),
(x, v, self.x_csc, self.v_100),
(v, x, self.v_10, self.x_csc)]:
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))
示例10
def test_int32_dtype(self):
# Reported on the theano-user mailing-list:
# https://groups.google.com/d/msg/theano-users/MT9ui8LtTsY/rwatwEF9zWAJ
size = 9
intX = 'int32'
C = tensor.matrix('C', dtype=intX)
I = tensor.matrix('I', dtype=intX)
fI = I.flatten()
data = tensor.ones_like(fI)
indptr = tensor.arange(data.shape[0] + 1, dtype='int32')
m1 = sparse.CSR(data, fI, indptr, (8, size))
m2 = sparse.dot(m1, C)
y = m2.reshape(shape=(2, 4, 9), ndim=3)
f = theano.function(inputs=[I, C], outputs=y)
i = numpy.asarray([[4, 3, 7, 7], [2, 8, 4, 5]], dtype=intX)
a = numpy.asarray(numpy.random.randint(0, 100, (size, size)),
dtype=intX)
f(i, a)
示例11
def test_op_ss(self):
for format in sparse.sparse_formats:
for dtype in sparse.all_dtypes:
variable, data = sparse_random_inputs(format,
shape=(10, 10),
out_dtype=dtype,
n=2,
p=0.1)
f = theano.function(variable, self.op(*variable))
tested = f(*data)
x, y = [m.toarray() for m in data]
expected = numpy.dot(x, y)
assert tested.format == format
assert tested.dtype == expected.dtype
tested = tested.toarray()
utt.assert_allclose(tested, expected)
示例12
def test_op_sd(self):
for format in sparse.sparse_formats:
for dtype in sparse.all_dtypes:
variable, data = sparse_random_inputs(format,
shape=(10, 10),
out_dtype=dtype,
n=2,
p=0.1)
variable[1] = tensor.TensorType(dtype=dtype,
broadcastable=(False, False))()
data[1] = data[1].toarray()
f = theano.function(variable, self.op(*variable))
tested = f(*data)
expected = numpy.dot(data[0].toarray(), data[1])
assert tested.format == format
assert tested.dtype == expected.dtype
tested = tested.toarray()
utt.assert_allclose(tested, expected)
示例13
def squared_euclidean_distances(x_1, x_2):
"""
Compute the euclidian distances in 3D between all the points in x_1 and x_2
Args:
x_1 (theano.tensor.matrix): shape n_points x number dimension
x_2 (theano.tensor.matrix): shape n_points x number dimension
Returns:
theano.tensor.matrix: Distancse matrix. shape n_points x n_points
"""
# T.maximum avoid negative numbers increasing stability
sqd = T.sqrt(T.maximum(
(x_1 ** 2).sum(1).reshape((x_1.shape[0], 1)) +
(x_2 ** 2).sum(1).reshape((1, x_2.shape[0])) -
2 * x_1.dot(x_2.T), 1e-12
))
return sqd
示例14
def extend_dual_kriging(self, weights, grid_shape):
# TODO Think what object is worth to save to speed up computation
"""
Tile the dual kriging vector to cover all the points to interpolate.So far I just make a matrix with the
dimensions len(DK)x(grid) but in the future maybe I have to try to loop all this part so consume less memory
Returns:
theano.tensor.matrix: Matrix with the Dk parameters repeated for all the points to interpolate
"""
DK_parameters = weights
# Creation of a matrix of dimensions equal to the grid with the weights for every point (big 4D matrix in
# ravel form)
# TODO IMP: Change the tile by a simple dot op -> The DOT version in gpu is slower
DK_weights = T.tile(DK_parameters, (grid_shape, 1)).T
return DK_weights
# endregion
# region Evaluate Geology
示例15
def select_finite_faults(self, grid):
fault_points = T.vertical_stack(T.stack([self.ref_layer_points[0]], axis=0), self.rest_layer_points).T
ctr = T.mean(fault_points, axis=1)
x = fault_points - ctr.reshape((-1, 1))
M = T.dot(x, x.T)
U, D, V = T.nlinalg.svd(M)
rotated_x = T.dot(T.dot(grid, U), V)
rotated_fault_points = T.dot(T.dot(fault_points.T, U), V)
rotated_ctr = T.mean(rotated_fault_points, axis=0)
a_radius = (rotated_fault_points[:, 0].max() - rotated_fault_points[:, 0].min()) / 2
b_radius = (rotated_fault_points[:, 1].max() - rotated_fault_points[:, 1].min()) / 2
ellipse_factor = (rotated_x[:, 0] - rotated_ctr[0])**2 / a_radius**2 + \
(rotated_x[:, 1] - rotated_ctr[1])**2 / b_radius**2
if "select_finite_faults" in self.verbose:
ellipse_factor = theano.printing.Print("h")(ellipse_factor)
return ellipse_factor
示例16
def P(self, lat, lon):
"""Compute the pixelization matrix, no filters or illumination."""
# Get the Cartesian points
xpt, ypt, zpt = self.latlon_to_xyz(lat, lon)
# Compute the polynomial basis at the point
pT = self.pT(xpt, ypt, zpt)[:, : (self.ydeg + 1) ** 2]
# Transform to the Ylm basis
pTA1 = ts.dot(pT, self.A1)
# NOTE: The factor of `pi` ensures the correct normalization.
# This is *different* from the derivation in the paper, but it's
# due to the fact that the in starry we normalize the spherical
# harmonics in a slightly strange way (they're normalized so that
# the integral of Y_{0,0} over the unit sphere is 4, not 4pi).
# This is useful for thermal light maps, where the flux from a map
# with Y_{0,0} = 1 is *unity*. But it messes up things for reflected
# light maps, so we need to account for that here.
if self._reflected:
pTA1 *= np.pi
# We're done
return pTA1
示例17
def compute_ortho_grid_inc_obl(self, res, inc, obl):
"""Compute the polynomial basis on the plane of the sky, accounting
for the map inclination and obliquity."""
# See NOTE on tt.mgrid bug in `compute_ortho_grid`
dx = 2.0 / (res - 0.01)
y, x = tt.mgrid[-1:1:dx, -1:1:dx]
z = tt.sqrt(1 - x ** 2 - y ** 2)
y = tt.set_subtensor(y[tt.isnan(z)], np.nan)
x = tt.reshape(x, [1, -1])
y = tt.reshape(y, [1, -1])
z = tt.reshape(z, [1, -1])
Robl = self.RAxisAngle(tt.as_tensor_variable([0.0, 0.0, 1.0]), -obl)
Rinc = self.RAxisAngle(
tt.as_tensor_variable([tt.cos(obl), tt.sin(obl), 0.0]),
-(0.5 * np.pi - inc),
)
R = tt.dot(Robl, Rinc)
xyz = tt.dot(R, tt.concatenate((x, y, z)))
x = tt.reshape(xyz[0], [1, -1])
y = tt.reshape(xyz[1], [1, -1])
z = tt.reshape(xyz[2], [1, -1])
lat = tt.reshape(0.5 * np.pi - tt.arccos(y), [1, -1])
lon = tt.reshape(tt.arctan2(x, z), [1, -1])
return tt.concatenate((lat, lon)), tt.concatenate((x, y, z))
示例18
def test_csr_correct_output_faster_than_scipy(self):
# contrast with test_grad, we put csr in float32, csc in float64
sparse_dtype = 'float32'
dense_dtype = 'float32'
a = SparseType('csr', dtype=sparse_dtype)()
b = tensor.matrix(dtype=dense_dtype)
d = theano.dot(a, b)
f = theano.function([a, b], d)
for M, N, K, nnz in [(4, 3, 2, 3),
(40, 30, 20, 3),
(40, 30, 20, 30),
(400, 3000, 200, 6000),
]:
spmat = sp.csr_matrix(random_lil((M, N), sparse_dtype, nnz))
mat = numpy.asarray(numpy.random.randn(N, K), dense_dtype)
t0 = time.time()
theano_result = f(spmat, mat)
t1 = time.time()
scipy_result = spmat * mat
t2 = time.time()
theano_time = t1 - t0
scipy_time = t2 - t1
# print 'theano took', theano_time,
# print 'scipy took', scipy_time
overhead_tol = 0.002 # seconds
overhead_rtol = 1.1 # times as long
utt.assert_allclose(scipy_result, theano_result)
if (not theano.config.mode in ["DebugMode", "DEBUG_MODE"] and
theano.config.cxx):
self.assertFalse(
theano_time > overhead_rtol * scipy_time + overhead_tol,
(theano_time,
overhead_rtol * scipy_time + overhead_tol,
scipy_time, overhead_rtol, overhead_tol))
示例19
def test_cuda(self):
import theano.sandbox.cuda as cuda
if not cuda.cuda_available:
raise SkipTest("Optional package cuda not available")
a = sparse.csr_matrix('a', dtype='float32')
b = cuda.float32_shared_constructor(
numpy.random.rand(3, 4).astype('float32'))
d = sparse.dot(a, b)
f = theano.function([a], d)
a_val = scipy.sparse.csr_matrix(random_lil((5, 3), 'float32', 5))
d_theano = f(a_val)
d_numpy = a_val * b.get_value()
utt.assert_allclose(d_numpy, d_theano)
示例20
def test_csr_correct_output_faster_than_scipy(self):
# contrast with test_grad, we put csr in float32, csc in float64
sparse_dtype = 'float32'
dense_dtype = 'float32'
a = SparseType('csr', dtype=sparse_dtype)()
b = tensor.matrix(dtype=dense_dtype)
d = theano.dot(a, b)
f = theano.function([a, b], d)
for M, N, K, nnz in [(4, 3, 2, 3),
(40, 30, 20, 3),
(40, 30, 20, 30),
(400, 3000, 200, 6000),
]:
spmat = sp.csr_matrix(random_lil((M, N), sparse_dtype, nnz))
mat = numpy.asarray(numpy.random.randn(N, K), dense_dtype)
t0 = time.time()
theano_result = f(spmat, mat)
t1 = time.time()
scipy_result = spmat * mat
t2 = time.time()
theano_time = t1 - t0
scipy_time = t2 - t1
# print 'theano took', theano_time,
# print 'scipy took', scipy_time
overhead_tol = 0.002 # seconds
overhead_rtol = 1.1 # times as long
utt.assert_allclose(scipy_result, theano_result)
if (not theano.config.mode in ["DebugMode", "DEBUG_MODE"] and
theano.config.cxx):
self.assertFalse(
theano_time > overhead_rtol * scipy_time + overhead_tol,
(theano_time,
overhead_rtol * scipy_time + overhead_tol,
scipy_time, overhead_rtol, overhead_tol))
示例21
def test_cuda(self):
import theano.sandbox.cuda as cuda
if not cuda.cuda_available:
raise SkipTest("Optional package cuda not available")
a = sparse.csr_matrix('a', dtype='float32')
b = cuda.float32_shared_constructor(
numpy.random.rand(3, 4).astype('float32'))
d = sparse.dot(a, b)
f = theano.function([a], d)
a_val = scipy.sparse.csr_matrix(random_lil((5, 3), 'float32', 5))
d_theano = f(a_val)
d_numpy = a_val * b.get_value()
utt.assert_allclose(d_numpy, d_theano)
示例22
def multivariate_normal(datasets, weights, hyperparams, residuals):
"""
Calculate posterior Likelihood of a Multivariate Normal distribution.
Uses plain inverse of the covariances.
DEPRECATED! Is currently not being used in beat.
Can only be executed in a `with model context`.
Parameters
----------
datasets : list
of :class:`heart.SeismicDataset` or :class:`heart.GeodeticDataset`
weights : list
of :class:`theano.shared`
Square matrix of the inverse of the covariance matrix as weights
hyperparams : dict
of :class:`theano.`
residual : list or array of model residuals
Returns
-------
array_like
"""
n_t = len(datasets)
logpts = tt.zeros((n_t), tconfig.floatX)
for l, data in enumerate(datasets):
M = tt.cast(shared(
data.samples, name='nsamples', borrow=True), 'int16')
hp_name = get_hyper_name(data)
norm = (M * (2 * hyperparams[hp_name] + log_2pi))
logpts = tt.set_subtensor(
logpts[l:l + 1],
(-0.5) * (
data.covariance.slog_pdet +
norm +
(1 / tt.exp(hyperparams[hp_name] * 2)) *
(residuals[l].dot(weights[l]).dot(residuals[l].T))))
return logpts
示例23
def compRelationProbsFunc(self, xFeats):
# xFeats [l, h] matrix
# xFeats = theano.printing.Print("xFeats")(xFeats)
# self.Wb = theano.printing.Print("Wb ") (self.Wb)
# self.W = theano.printing.Print("W ") (self.W)
# scores of each role by a classifier
relationScores = sparse.dot(xFeats, self.W) + self.Wb # [l, h] x [h, r] => [l, r]
#relationScores = theano.printing.Print("relationScores=")(relationScores)
# convert it to probabilities
relationProbs = T.nnet.softmax(relationScores)
#relationProbs = theano.printing.Print("relationProbs = ")(relationProbs)
return relationProbs # [l, r]
示例24
def labelFunct(self, batchSize, xFeats):
# xFeats [l, h]
# l = batchSize
# self.W = theano.printing.Print("W ") (self.W)
# self.Wb = theano.printing.Print("Wb ") (self.Wb)
scores = sparse.dot(xFeats, self.W) + self.Wb # [l, h] x [h, r] => [l, r]
relationProbs = T.nnet.softmax(scores)
# scores = theano.printing.Print("scores ") (scores)
labels = T.argmax(scores, axis=1) # [l, r] => [l]
# labels = theano.printing.Print("labels ") (labels)
return (labels, relationProbs)
示例25
def flux(
self, theta, xo, yo, zo, ro, inc, obl, y, u, f, alpha, tau, delta
):
"""Compute the light curve."""
return tt.dot(
self.X(theta, xo, yo, zo, ro, inc, obl, u, f, alpha, tau, delta), y
)
示例26
def compute_moll_grid(self, res):
"""Compute the polynomial basis on a Mollweide grid."""
# See NOTE on tt.mgrid bug in `compute_ortho_grid`
dx = 2 * np.sqrt(2) / (res - 0.01)
y, x = tt.mgrid[
-np.sqrt(2) : np.sqrt(2) : dx,
-2 * np.sqrt(2) : 2 * np.sqrt(2) : 2 * dx,
]
# Make points off-grid nan
a = np.sqrt(2)
b = 2 * np.sqrt(2)
y = tt.where((y / a) ** 2 + (x / b) ** 2 <= 1, y, np.nan)
# https://en.wikipedia.org/wiki/Mollweide_projection
theta = tt.arcsin(y / np.sqrt(2))
lat = tt.arcsin((2 * theta + tt.sin(2 * theta)) / np.pi)
lon0 = 3 * np.pi / 2
lon = lon0 + np.pi * x / (2 * np.sqrt(2) * tt.cos(theta))
# Back to Cartesian, this time on the *sky*
x = tt.reshape(tt.cos(lat) * tt.cos(lon), [1, -1])
y = tt.reshape(tt.cos(lat) * tt.sin(lon), [1, -1])
z = tt.reshape(tt.sin(lat), [1, -1])
R = self.RAxisAngle(tt.as_tensor_variable([1.0, 0.0, 0.0]), -np.pi / 2)
return (
tt.concatenate(
(
tt.reshape(lat, (1, -1)),
tt.reshape(lon - 1.5 * np.pi, (1, -1)),
)
),
tt.dot(R, tt.concatenate((x, y, z))),
)
示例27
def latlon_to_xyz(self, lat, lon):
"""Convert lat-lon points to Cartesian points."""
if lat.ndim == 0:
lat = tt.shape_padleft(lat, 1)
if lon.ndim == 0:
lon = tt.shape_padleft(lon, 1)
R1 = self.RAxisAngle([1.0, 0.0, 0.0], -lat)
R2 = self.RAxisAngle([0.0, 1.0, 0.0], lon)
R = tt.batched_dot(R2, R1)
xyz = tt.transpose(tt.dot(R, [0.0, 0.0, 1.0]))
return xyz[0], xyz[1], xyz[2]
示例28
def intensity(self, mu, u):
"""Compute the intensity at a set of points."""
if self.udeg == 0:
mu_f = tt.reshape(mu, (-1,))
intensity = tt.ones_like(mu_f)
intensity = tt.set_subtensor(intensity[tt.isnan(mu_f)], np.nan)
return intensity
else:
basis = tt.reshape(1.0 - mu, (-1, 1)) ** np.arange(self.udeg + 1)
return -tt.dot(basis, u)
示例29
def test_csc_correct_output_faster_than_scipy(self):
sparse_dtype = 'float64'
dense_dtype = 'float64'
a = SparseType('csc', dtype=sparse_dtype)()
b = tensor.matrix(dtype=dense_dtype)
d = theano.dot(a, b)
f = theano.function([a, b], theano.Out(d, borrow=True))
for M, N, K, nnz in [(4, 3, 2, 3),
(40, 30, 20, 3),
(40, 30, 20, 30),
(400, 3000, 200, 6000),
]:
spmat = sp.csc_matrix(random_lil((M, N), sparse_dtype, nnz))
mat = numpy.asarray(numpy.random.randn(N, K), dense_dtype)
theano_times = []
scipy_times = []
for i in xrange(5):
t0 = time.time()
theano_result = f(spmat, mat)
t1 = time.time()
scipy_result = spmat * mat
t2 = time.time()
theano_times.append(t1 - t0)
scipy_times.append(t2 - t1)
theano_time = numpy.min(theano_times)
scipy_time = numpy.min(scipy_times)
speedup = scipy_time / theano_time
# print scipy_times
# print theano_times
# print ('M=%(M)s N=%(N)s K=%(K)s nnz=%(nnz)s theano_time'
# '=%(theano_time)s speedup=%(speedup)s') % locals()
# fail if Theano is slower than scipy by more than a certain amount
overhead_tol = 0.003 # seconds overall
overhead_rtol = 1.2 # times as long
utt.assert_allclose(scipy_result, theano_result)
if not theano.config.mode in ["DebugMode", "DEBUG_MODE"]:
self.assertFalse(theano_time > overhead_rtol * scipy_time +
overhead_tol)
示例30
def test_infer_shape(self):
def mat(format, name, dtype):
if format == 'dense':
return theano.tensor.matrix(name, dtype=dtype)
else:
return theano.sparse.matrix(format, name, dtype=dtype)
params = [('float32', 'float64', 'int16', 'complex64', 'csc', 'dense'),
('float32', 'float64', 'int16', 'complex64', 'csr', 'dense')]
for dtype1, dtype2, dtype3, dtype4, format1, format2 in params:
if format1 == 'dense' and format2 == 'dense':
# Usmm won't be used!
continue
x = mat(format1, 'x', dtype1)
y = mat(format2, 'y', dtype2)
a = theano.tensor.scalar('a', dtype=dtype3)
z = theano.shared(numpy.asarray(self.z, dtype=dtype4).copy())
f_b = lambda z, a, x, y: z - a * (x * y)
x_data = numpy.asarray(self.x, dtype=dtype1)
if format1 != 'dense':
x_data = as_sparse_format(x_data, format1)
y_data = numpy.asarray(self.y, dtype=dtype2)
if format2 != 'dense':
y_data = as_sparse_format(y_data, format2)
a_data = numpy.asarray(1.5, dtype=dtype3)
z_data = numpy.asarray(self.z, dtype=dtype4)
f_b_out = f_b(z_data, a_data, x_data, y_data)
# Can it work inplace?
inplace = dtype4 == theano.scalar.upcast(dtype1, dtype2, dtype3)
# To make it easier to check the toposort
mode = theano.compile.mode.get_default_mode().excluding('fusion')
# test infer_shape of Dot got applied
f_shape = theano.function([a, x, y],
(z - a * theano.sparse.dot(x, y)).shape,
mode=mode)
assert all(f_shape(a_data, x_data, y_data) == f_b_out.shape)
topo = f_shape.maker.fgraph.toposort()
if theano.config.mode != 'FAST_COMPILE':
nb = 0
else:
nb = 1
assert sum([isinstance(node.op, (Dot, Usmm, UsmmCscDense))
for node in topo]) == nb