viennacl / pyviennacl-dev

Developer repository for PyViennaCL. Visit http://viennacl.sourceforge.net/ for latest releases.
MIT License
32 stars 6 forks source link

1-D matrix multiplication #29

Open mortonjt opened 9 years ago

mortonjt commented 9 years ago

First off, just wanted to thank you guys for putting together this slick piece of software. I've compared the pyviennacl matrix multiplication to numpy matrix multiplication and noticed a 4000x speedup!

Anyways, I noticed that only 2-D matrices are supported. Granted, if you want to compute A*x where A is a matrix and x is a column vector, you can just use the corresponding pyviennacl objects.

But suppose that you want to compute x*A where A is a matrix and x is a row vector, then what? From what I can tell, this operation is not yet supported. Will there be any plans in the future to support this?

If you guys need contributors, I would be happy to submit some pull requests.

ptillet commented 9 years ago

Hey.

Thanks for your support :). Aren't you talking about x being a row vector ? Otherwise A is a scalar ! If x is a row vector then this is equivalent to computing the column vector A.T*x. On Dec 19, 2014 11:49 AM, "mortonjt" notifications@github.com wrote:

First off, just wanted to thank you guys for putting together this slick piece of software. I've compared the pyviennacl matrix multiplication to numpy matrix multiplication and noticed a 4000x speedup!

Anyways, I noticed that only 2-D matrices are supported. Granted, if you want to compute A*x where A is a matrix and x is a column vector, you can just use the corresponding pyviennacl objects.

But suppose that you want to compute x*A where A is a matrix and x is a column vector, then what? From what I can tell, this operation is not yet supported. Will there be any plans in the future to support this?

If you need developers, I would be happy to submit some pull requests.

— Reply to this email directly or view it on GitHub https://github.com/viennacl/pyviennacl-dev/issues/29.

tsmithe commented 9 years ago

Hmm, it should be supported just using Vector; if it's not, it's a bug in the old version. I'm going to release a new (much improved) version in the next week. Could you post the traceback of any failure you get?

Anyhow, I'm glad you like it!

tsmithe commented 9 years ago

@PhilippeTillet: re-reading the report, the problem seemed to be "compute x*A where A is a matrix and x is a row vector" ;)

mortonjt commented 9 years ago

I'm attaching my code below and traceback below

import pyviennacl as p
import numpy as np
a_width = 10
a_height = 20
v = np.random.rand(1,a_height).astype(np.float32)
f = np.random.rand(a_height, a_width).astype(np.float32)
ans = np.matrix(v)*np.matrix(f) ##Works

x, a = p.Vector(v), p.Matrix(f)
ans = x*a ##Doesn't work
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-156-6c68c0a72cb7> in <module>()
----> 1 ans = x*a

/Users/mortonjt/.virtualenvs/asa/lib/python2.7/site-packages/pyviennacl/pycore.pyc in __mul__(self, rhs)
   1287             if issubclass(rhs.result_container_type, Vector):
   1288                 return ElementProd(self, rhs)
-> 1289         return Mul(self, rhs)
   1290 
   1291 

/Users/mortonjt/.virtualenvs/asa/lib/python2.7/site-packages/pyviennacl/pycore.pyc in __init__(self, *args)
   1903                 self.operands.reverse()
   1904 
-> 1905         self._node_init()
   1906 
   1907         if self.operation_node_type is None:

/Users/mortonjt/.virtualenvs/asa/lib/python2.7/site-packages/pyviennacl/pycore.pyc in _node_init(self)
   2656                 # Need to make sure that matrix and vector shapes are aligned
   2657                 if self.operands[0].shape[1] != self.operands[1].shape[0]:
-> 2658                     raise ValueError("Operand shapes not correctly aligned")
   2659                 self.operation_node_type = _v.operation_node_type.OPERATION_BINARY_MAT_VEC_PROD_TYPE
   2660                 self.shape = (self.operands[0].shape[0],)

ValueError: Operand shapes not correctly aligned
ptillet commented 9 years ago

Yes,

PyViennaCL stores vectors as column vectors. You need to do ans = a.T*x :-)

On Fri, Dec 19, 2014 at 12:36 PM, mortonjt notifications@github.com wrote:

I'm attaching my code below and traceback below

import pyviennacl as p import numpy as np

a_width = 10 a_height = 20 v = np.random.rand(1,a_height).astype(np.float32) f = np.random.rand(a_height, a_width).astype(np.float32) ans = np.matrix(v)*np.matrix(f) ##Works

x, a = p.Vector(v), p.Matrix(f) ans = x*a ##Doesn't work


ValueError Traceback (most recent call last)

in () ----> 1 ans = x*a /Users/mortonjt/.virtualenvs/asa/lib/python2.7/site-packages/pyviennacl/pycore.pyc in **mul**(self, rhs) 1287 if issubclass(rhs.result_container_type, Vector): 1288 return ElementProd(self, rhs) -> 1289 return Mul(self, rhs) 1290 1291 /Users/mortonjt/.virtualenvs/asa/lib/python2.7/site-packages/pyviennacl/pycore.pyc in **init**(self, *args) 1903 self.operands.reverse() 1904 -> 1905 self._node_init() 1906 1907 if self.operation_node_type is None: /Users/mortonjt/.virtualenvs/asa/lib/python2.7/site-packages/pyviennacl/pycore.pyc in _node_init(self) 2656 # Need to make sure that matrix and vector shapes are aligned 2657 if self.operands[0].shape[1] != self.operands[1].shape[0]: -> 2658 raise ValueError("Operand shapes not correctly aligned") 2659 self.operation_node_type = _v.operation_node_type.OPERATION_BINARY_MAT_VEC_PROD_TYPE 2660 self.shape = (self.operands[0].shape[0],) ValueError: Operand shapes not correctly aligned — Reply to this email directly or view it on GitHub https://github.com/viennacl/pyviennacl-dev/issues/29#issuecomment-67669959 .