本文整理汇总了Python中operator.matmul方法的典型用法代码示例。如果您正苦于以下问题:Python operator.matmul方法的具体用法?Python operator.matmul怎么用?Python operator.matmul使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类operator
的用法示例。
在下文中一共展示了operator.matmul方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_forward
# 需要导入模块: import operator [as 别名]
# 或者: from operator import matmul [as 别名]
def check_forward(self, x_data, y_data):
if self.left_const:
x = x_data
else:
x = chainer.Variable(x_data)
if self.right_const:
y = y_data
else:
y = chainer.Variable(y_data)
z = operator.matmul(x, y)
if self.dtype == numpy.float16:
options = {'atol': 2e-3, 'rtol': 2e-3}
else:
options = {'atol': 2e-7, 'rtol': 2e-7}
testing.assert_allclose(
self._get_forward_answer(self.x, self.y), z.data, **options)
示例2: check_backward
# 需要导入模块: import operator [as 别名]
# 或者: from operator import matmul [as 别名]
def check_backward(self, x_data, y_data, z_grad):
if self.right_const:
def op(x):
return operator.matmul(x, y_data)
data = x_data,
elif self.left_const:
def op(y):
return operator.matmul(x_data, y)
data = y_data,
else:
op = operator.matmul
data = x_data, y_data
if self.dtype == numpy.float16:
options = {'atol': 1e-3, 'rtol': 1e-2}
else:
options = {'atol': 1e-4, 'rtol': 1e-4}
gradient_check.check_backward(
op, data, z_grad, dtype=numpy.float64, **options)
示例3: check_double_backward
# 需要导入模块: import operator [as 别名]
# 或者: from operator import matmul [as 别名]
def check_double_backward(
self, x_data, y_data, z_grad, x_grad_grad, y_grad_grad):
if self.right_const:
def op(x):
return operator.matmul(x, y_data.astype(x.dtype))
data = x_data,
grad_grad = x_grad_grad,
elif self.left_const:
def op(y):
return operator.matmul(x_data.astype(y.dtype), y)
data = y_data,
grad_grad = y_grad_grad,
else:
op = operator.matmul
data = x_data, y_data
grad_grad = x_grad_grad, y_grad_grad
if self.dtype == numpy.float16:
options = {'atol': 1e-3, 'rtol': 1e-2}
else:
options = {'atol': 1e-4, 'rtol': 1e-4}
gradient_check.check_double_backward(
op, data, z_grad, grad_grad, dtype=numpy.float64, **options)
示例4: test_matmul
# 需要导入模块: import operator [as 别名]
# 或者: from operator import matmul [as 别名]
def test_matmul(self):
if not TEST_MATMUL:
pytest.skip("matmul is only tested in Python 3.5+")
M = self.spmatrix(matrix([[3,0,0],[0,1,0],[2,0,3.0],[2,3,0]]))
B = self.spmatrix(matrix([[0,1],[1,0],[0,2]],'d'))
col = matrix([1,2,3]).T
# check matrix-vector
assert_array_almost_equal(operator.matmul(M, col),
M.todense() * col)
# check matrix-matrix
assert_array_almost_equal(operator.matmul(M, B).todense(),
(M * B).todense())
assert_array_almost_equal(operator.matmul(M.todense(), B),
(M * B).todense())
assert_array_almost_equal(operator.matmul(M, B.todense()),
(M * B).todense())
# check error on matrix-scalar
assert_raises(ValueError, operator.matmul, M, 1)
assert_raises(ValueError, operator.matmul, 1, M)
示例5: test_matmul
# 需要导入模块: import operator [as 别名]
# 或者: from operator import matmul [as 别名]
def test_matmul(self):
if not TEST_MATMUL:
pytest.skip("matmul is only tested in Python 3.5+")
D = {'shape': self.A.shape,
'matvec': lambda x: np.dot(self.A, x).reshape(self.A.shape[0]),
'rmatvec': lambda x: np.dot(self.A.T.conj(),
x).reshape(self.A.shape[1]),
'matmat': lambda x: np.dot(self.A, x)}
A = interface.LinearOperator(**D)
B = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
b = B[0]
assert_equal(operator.matmul(A, b), A * b)
assert_equal(operator.matmul(A, B), A * B)
assert_raises(ValueError, operator.matmul, A, 2)
assert_raises(ValueError, operator.matmul, 2, A)
示例6: test_matmul
# 需要导入模块: import operator [as 别名]
# 或者: from operator import matmul [as 别名]
def test_matmul(a_shape, b_shape):
sa = sparse.random(a_shape, density=0.5)
sb = sparse.random(b_shape, density=0.5)
a = sa.todense()
b = sb.todense()
assert_eq(np.matmul(a, b), sparse.matmul(sa, sb))
assert_eq(sparse.matmul(sa, b), sparse.matmul(a, sb))
assert_eq(np.matmul(a, b), sparse.matmul(sa, sb))
if a.ndim == 2 or b.ndim == 2:
assert_eq(
np.matmul(a, b),
sparse.matmul(
scipy.sparse.coo_matrix(a) if a.ndim == 2 else sa,
scipy.sparse.coo_matrix(b) if b.ndim == 2 else sb,
),
)
if hasattr(operator, "matmul"):
assert_eq(operator.matmul(a, b), operator.matmul(sa, sb))
示例7: test_dot
# 需要导入模块: import operator [as 别名]
# 或者: from operator import matmul [as 别名]
def test_dot(a_shape, b_shape):
sa = sparse.random(a_shape, density=0.5)
sb = sparse.random(b_shape, density=0.5)
a = sa.todense()
b = sb.todense()
assert_eq(a.dot(b), sa.dot(sb))
assert_eq(np.dot(a, b), sparse.dot(sa, sb))
assert_eq(sparse.dot(sa, b), sparse.dot(a, sb))
assert_eq(np.dot(a, b), sparse.dot(sa, sb))
if hasattr(operator, "matmul"):
# Basic equivalences
assert_eq(operator.matmul(a, b), operator.matmul(sa, sb))
# Test that SOO's and np.array's combine correctly
# Not possible due to https://github.com/numpy/numpy/issues/9028
# assert_eq(eval("a @ sb"), eval("sa @ b"))
示例8: __init__
# 需要导入模块: import operator [as 别名]
# 或者: from operator import matmul [as 别名]
def __init__(self, factors, n_terms=None, complex=False, fixed_order=False, softmax_fn='softmax'):
super().__init__()
self.factors = nn.ModuleList(factors)
if n_terms is None:
n_terms = len(factors)
self.n_terms = n_terms
self.complex = complex
self.matmul_op = complex_matmul if complex else operator.matmul
self.fixed_order = fixed_order
if not self.fixed_order:
assert softmax_fn in ['softmax', 'sparsemax']
self.logit = nn.Parameter(torch.randn((self.n_terms, len(factors))))
if softmax_fn == 'softmax':
self.softmax_fn = lambda logit: nn.functional.softmax(logit, dim=-1)
else:
self.softmax_fn = sparsemax
示例9: test_matmul
# 需要导入模块: import operator [as 别名]
# 或者: from operator import matmul [as 别名]
def test_matmul(self):
array = np.array([1, 2], dtype=np.float64)
array_like = ArrayLike(array)
expected = ArrayLike(np.float64(5))
_assert_equal_type_and_value(expected, np.matmul(array_like, array))
if not PY2:
_assert_equal_type_and_value(
expected, operator.matmul(array_like, array))
_assert_equal_type_and_value(
expected, operator.matmul(array, array_like))
示例10: _get_forward_answer
# 需要导入模块: import operator [as 别名]
# 或者: from operator import matmul [as 别名]
def _get_forward_answer(self, x, y):
if x.ndim <= 2 or y.ndim == 1:
return numpy.dot(x, y)
elif hasattr(numpy, 'matmul'):
# Note: NumPy 1.14.0 has a bug in einsum (numpy/numpy#10343),
# so we use matmul if available to avoid it
return numpy.matmul(x, y)
else:
return numpy.einsum('...ij,...jk->...ik', x, y)
示例11: test_invalid_type
# 需要导入模块: import operator [as 别名]
# 或者: from operator import matmul [as 别名]
def test_invalid_type(self):
x = chainer.Variable(self.x)
y = chainer.Variable(self.y)
with pytest.raises(type_check.InvalidType):
operator.matmul(x, y)
示例12: assert_in
# 需要导入模块: import operator [as 别名]
# 或者: from operator import matmul [as 别名]
def assert_in(member, collection, msg=None):
assert_(member in collection, msg=msg if msg is not None else "%r not found in %r" % (member, collection))
# Only test matmul operator (A @ B) when available (Python 3.5+)
示例13: test_operator_matmul
# 需要导入模块: import operator [as 别名]
# 或者: from operator import matmul [as 别名]
def test_operator_matmul(self, xp, dtype1, dtype2):
x1 = testing.shaped_arange(self.shape_pair[0], xp, dtype1)
x2 = testing.shaped_arange(self.shape_pair[1], xp, dtype2)
return operator.matmul(x1, x2)
示例14: test_cupy_matmul
# 需要导入模块: import operator [as 别名]
# 或者: from operator import matmul [as 别名]
def test_cupy_matmul(self, xp, dtype1, dtype2):
x1 = testing.shaped_arange(self.shape_pair[0], xp, dtype1)
x2 = testing.shaped_arange(self.shape_pair[1], xp, dtype2)
return xp.matmul(x1, x2)
示例15: test_invalid_shape
# 需要导入模块: import operator [as 别名]
# 或者: from operator import matmul [as 别名]
def test_invalid_shape(self):
for xp in (numpy, cupy):
shape1, shape2 = self.shape_pair
x1 = testing.shaped_arange(shape1, xp, numpy.float32)
x2 = testing.shaped_arange(shape2, xp, numpy.float32)
with pytest.raises(ValueError):
xp.matmul(x1, x2)