Python operator.matmul() Examples
The following are 30
code examples of operator.matmul().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
operator
, or try the search function
.
Example #1
Source File: butterfly_old.py From learning-circuits with Apache License 2.0 | 6 votes |
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
Example #2
Source File: test_coo.py From sparse with BSD 3-Clause "New" or "Revised" License | 6 votes |
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"))
Example #3
Source File: test_basic_math.py From chainer with MIT License | 6 votes |
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)
Example #4
Source File: test_basic_math.py From chainer with MIT License | 6 votes |
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)
Example #5
Source File: test_basic_math.py From chainer with MIT License | 6 votes |
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)
Example #6
Source File: test_coo.py From sparse with BSD 3-Clause "New" or "Revised" License | 6 votes |
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))
Example #7
Source File: test_base.py From GraphicDesignPatternByPython with MIT License | 6 votes |
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)
Example #8
Source File: test_interface.py From GraphicDesignPatternByPython with MIT License | 6 votes |
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)
Example #9
Source File: test_mixins.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
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))
Example #10
Source File: core.py From pythonflow with Apache License 2.0 | 5 votes |
def __rmatmul__(self, other): return matmul(other, self, graph=self.graph)
Example #11
Source File: core.py From pythonflow with Apache License 2.0 | 5 votes |
def __matmul__(self, other): return matmul(self, other, graph=self.graph)
Example #12
Source File: test_coo.py From sparse with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_dot_nocoercion(): sa = sparse.random((3, 4, 5), density=0.5) sb = sparse.random((5, 6), density=0.5) a = sa.todense() b = sb.todense() la = a.tolist() lb = b.tolist() if hasattr(operator, "matmul"): # Operations with naive collection (list) assert_eq(operator.matmul(la, b), operator.matmul(la, sb)) assert_eq(operator.matmul(a, lb), operator.matmul(sa, lb))
Example #13
Source File: test_coo.py From sparse with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_matmul_errors(): with pytest.raises(ValueError): sa = sparse.random((3, 4, 5, 6), 0.5) sb = sparse.random((3, 6, 5, 6), 0.5) sparse.matmul(sa, sb)
Example #14
Source File: test_matmul.py From cupy with MIT License | 5 votes |
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)
Example #15
Source File: test_matmul.py From cupy with MIT License | 5 votes |
def test_cupy_matmul(self, xp, dtype1, dtype2): if ((dtype1, dtype2) in self.skip_dtypes or (dtype2, dtype1) in self.skip_dtypes): return xp.array([]) shape1, shape2 = self.shape_pair x1 = testing.shaped_random(shape1, xp, dtype1) x2 = testing.shaped_random(shape2, xp, dtype2) return xp.matmul(x1, x2)
Example #16
Source File: test_matmul.py From cupy with MIT License | 5 votes |
def test_operator_matmul(self, xp, dtype1, dtype2): if ((dtype1, dtype2) in self.skip_dtypes or (dtype2, dtype1) in self.skip_dtypes): return xp.array([]) x1 = testing.shaped_random(self.shape_pair[0], xp, dtype1) x2 = testing.shaped_random(self.shape_pair[1], xp, dtype2) return operator.matmul(x1, x2)
Example #17
Source File: test_matmul.py From cupy with MIT License | 5 votes |
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)
Example #18
Source File: test_matmul.py From cupy with MIT License | 5 votes |
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)
Example #19
Source File: array.py From dislib with Apache License 2.0 | 5 votes |
def _multiply_block_groups(hblock, vblock): blocks = [] for blocki, blockj in zip(hblock, vblock): blocks.append(_block_apply(operator.matmul, blocki, blockj)) while len(blocks) > 1: blocks.append(_block_apply(operator.add, blocks.pop(0), blocks.pop(0))) return blocks[0]
Example #20
Source File: test_base.py From GraphicDesignPatternByPython with MIT License | 5 votes |
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+)
Example #21
Source File: test_basic_math.py From chainer with MIT License | 5 votes |
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)
Example #22
Source File: test_basic_math.py From chainer with MIT License | 5 votes |
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)
Example #23
Source File: test_mixins.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
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))
Example #24
Source File: test_mixins.py From coffeegrindsize with MIT License | 5 votes |
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))
Example #25
Source File: test_mixins.py From recruit with Apache License 2.0 | 5 votes |
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))
Example #26
Source File: test_analytics.py From twitter-stock-recommendation with MIT License | 4 votes |
def test_matmul(self): # matmul test is for GH #10259 a = DataFrame(np.random.randn(3, 4), index=['a', 'b', 'c'], columns=['p', 'q', 'r', 's']) b = DataFrame(np.random.randn(4, 2), index=['p', 'q', 'r', 's'], columns=['one', 'two']) # DataFrame @ DataFrame result = operator.matmul(a, b) expected = DataFrame(np.dot(a.values, b.values), index=['a', 'b', 'c'], columns=['one', 'two']) tm.assert_frame_equal(result, expected) # DataFrame @ Series result = operator.matmul(a, b.one) expected = Series(np.dot(a.values, b.one.values), index=['a', 'b', 'c']) tm.assert_series_equal(result, expected) # np.array @ DataFrame result = operator.matmul(a.values, b) expected = np.dot(a.values, b.values) tm.assert_almost_equal(result, expected) # nested list @ DataFrame (__rmatmul__) result = operator.matmul(a.values.tolist(), b) expected = DataFrame(np.dot(a.values, b.values), index=['a', 'b', 'c'], columns=['one', 'two']) tm.assert_almost_equal(result.values, expected.values) # mixed dtype DataFrame @ DataFrame a['q'] = a.q.round().astype(int) result = operator.matmul(a, b) expected = DataFrame(np.dot(a.values, b.values), index=['a', 'b', 'c'], columns=['one', 'two']) tm.assert_frame_equal(result, expected) # different dtypes DataFrame @ DataFrame a = a.astype(int) result = operator.matmul(a, b) expected = DataFrame(np.dot(a.values, b.values), index=['a', 'b', 'c'], columns=['one', 'two']) tm.assert_frame_equal(result, expected) # unaligned df = DataFrame(randn(3, 4), index=[1, 2, 3], columns=lrange(4)) df2 = DataFrame(randn(5, 3), index=lrange(5), columns=[1, 2, 3]) with tm.assert_raises_regex(ValueError, 'aligned'): operator.matmul(df, df2)
Example #27
Source File: test_analytics.py From twitter-stock-recommendation with MIT License | 4 votes |
def test_matmul(self): # matmul test is for GH #10259 a = Series(np.random.randn(4), index=['p', 'q', 'r', 's']) b = DataFrame(np.random.randn(3, 4), index=['1', '2', '3'], columns=['p', 'q', 'r', 's']).T # Series @ DataFrame result = operator.matmul(a, b) expected = Series(np.dot(a.values, b.values), index=['1', '2', '3']) assert_series_equal(result, expected) # DataFrame @ Series result = operator.matmul(b.T, a) expected = Series(np.dot(b.T.values, a.T.values), index=['1', '2', '3']) assert_series_equal(result, expected) # Series @ Series result = operator.matmul(a, a) expected = np.dot(a.values, a.values) assert_almost_equal(result, expected) # np.array @ Series (__rmatmul__) result = operator.matmul(a.values, a) expected = np.dot(a.values, a.values) assert_almost_equal(result, expected) # mixed dtype DataFrame @ Series a['p'] = int(a.p) result = operator.matmul(b.T, a) expected = Series(np.dot(b.T.values, a.T.values), index=['1', '2', '3']) assert_series_equal(result, expected) # different dtypes DataFrame @ Series a = a.astype(int) result = operator.matmul(b.T, a) expected = Series(np.dot(b.T.values, a.T.values), index=['1', '2', '3']) assert_series_equal(result, expected) pytest.raises(Exception, a.dot, a.values[:3]) pytest.raises(ValueError, a.dot, b.T)
Example #28
Source File: test_analytics.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 4 votes |
def test_matmul(self): # matmul test is for GH 10259 a = DataFrame(np.random.randn(3, 4), index=['a', 'b', 'c'], columns=['p', 'q', 'r', 's']) b = DataFrame(np.random.randn(4, 2), index=['p', 'q', 'r', 's'], columns=['one', 'two']) # DataFrame @ DataFrame result = operator.matmul(a, b) expected = DataFrame(np.dot(a.values, b.values), index=['a', 'b', 'c'], columns=['one', 'two']) tm.assert_frame_equal(result, expected) # DataFrame @ Series result = operator.matmul(a, b.one) expected = Series(np.dot(a.values, b.one.values), index=['a', 'b', 'c']) tm.assert_series_equal(result, expected) # np.array @ DataFrame result = operator.matmul(a.values, b) assert isinstance(result, DataFrame) assert result.columns.equals(b.columns) assert result.index.equals(pd.Index(range(3))) expected = np.dot(a.values, b.values) tm.assert_almost_equal(result.values, expected) # nested list @ DataFrame (__rmatmul__) result = operator.matmul(a.values.tolist(), b) expected = DataFrame(np.dot(a.values, b.values), index=['a', 'b', 'c'], columns=['one', 'two']) tm.assert_almost_equal(result.values, expected.values) # mixed dtype DataFrame @ DataFrame a['q'] = a.q.round().astype(int) result = operator.matmul(a, b) expected = DataFrame(np.dot(a.values, b.values), index=['a', 'b', 'c'], columns=['one', 'two']) tm.assert_frame_equal(result, expected) # different dtypes DataFrame @ DataFrame a = a.astype(int) result = operator.matmul(a, b) expected = DataFrame(np.dot(a.values, b.values), index=['a', 'b', 'c'], columns=['one', 'two']) tm.assert_frame_equal(result, expected) # unaligned df = DataFrame(np.random.randn(3, 4), index=[1, 2, 3], columns=lrange(4)) df2 = DataFrame(np.random.randn(5, 3), index=lrange(5), columns=[1, 2, 3]) with pytest.raises(ValueError, match='aligned'): operator.matmul(df, df2)
Example #29
Source File: test_analytics.py From vnpy_crypto with MIT License | 4 votes |
def test_matmul(self): # matmul test is for GH #10259 a = DataFrame(np.random.randn(3, 4), index=['a', 'b', 'c'], columns=['p', 'q', 'r', 's']) b = DataFrame(np.random.randn(4, 2), index=['p', 'q', 'r', 's'], columns=['one', 'two']) # DataFrame @ DataFrame result = operator.matmul(a, b) expected = DataFrame(np.dot(a.values, b.values), index=['a', 'b', 'c'], columns=['one', 'two']) tm.assert_frame_equal(result, expected) # DataFrame @ Series result = operator.matmul(a, b.one) expected = Series(np.dot(a.values, b.one.values), index=['a', 'b', 'c']) tm.assert_series_equal(result, expected) # np.array @ DataFrame result = operator.matmul(a.values, b) expected = np.dot(a.values, b.values) tm.assert_almost_equal(result, expected) # nested list @ DataFrame (__rmatmul__) result = operator.matmul(a.values.tolist(), b) expected = DataFrame(np.dot(a.values, b.values), index=['a', 'b', 'c'], columns=['one', 'two']) tm.assert_almost_equal(result.values, expected.values) # mixed dtype DataFrame @ DataFrame a['q'] = a.q.round().astype(int) result = operator.matmul(a, b) expected = DataFrame(np.dot(a.values, b.values), index=['a', 'b', 'c'], columns=['one', 'two']) tm.assert_frame_equal(result, expected) # different dtypes DataFrame @ DataFrame a = a.astype(int) result = operator.matmul(a, b) expected = DataFrame(np.dot(a.values, b.values), index=['a', 'b', 'c'], columns=['one', 'two']) tm.assert_frame_equal(result, expected) # unaligned df = DataFrame(randn(3, 4), index=[1, 2, 3], columns=lrange(4)) df2 = DataFrame(randn(5, 3), index=lrange(5), columns=[1, 2, 3]) with tm.assert_raises_regex(ValueError, 'aligned'): operator.matmul(df, df2)
Example #30
Source File: test_analytics.py From vnpy_crypto with MIT License | 4 votes |
def test_matmul(self): # matmul test is for GH #10259 a = Series(np.random.randn(4), index=['p', 'q', 'r', 's']) b = DataFrame(np.random.randn(3, 4), index=['1', '2', '3'], columns=['p', 'q', 'r', 's']).T # Series @ DataFrame result = operator.matmul(a, b) expected = Series(np.dot(a.values, b.values), index=['1', '2', '3']) assert_series_equal(result, expected) # DataFrame @ Series result = operator.matmul(b.T, a) expected = Series(np.dot(b.T.values, a.T.values), index=['1', '2', '3']) assert_series_equal(result, expected) # Series @ Series result = operator.matmul(a, a) expected = np.dot(a.values, a.values) assert_almost_equal(result, expected) # np.array @ Series (__rmatmul__) result = operator.matmul(a.values, a) expected = np.dot(a.values, a.values) assert_almost_equal(result, expected) # mixed dtype DataFrame @ Series a['p'] = int(a.p) result = operator.matmul(b.T, a) expected = Series(np.dot(b.T.values, a.T.values), index=['1', '2', '3']) assert_series_equal(result, expected) # different dtypes DataFrame @ Series a = a.astype(int) result = operator.matmul(b.T, a) expected = Series(np.dot(b.T.values, a.T.values), index=['1', '2', '3']) assert_series_equal(result, expected) pytest.raises(Exception, a.dot, a.values[:3]) pytest.raises(ValueError, a.dot, b.T)