Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64254 > unrolled thread
| Started by | Kevin K <richyokevin@gmail.com> |
|---|---|
| First post | 2014-01-18 12:51 -0800 |
| Last post | 2014-01-19 15:46 +0000 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
Need help vectorizing code Kevin K <richyokevin@gmail.com> - 2014-01-18 12:51 -0800
Re: Need help vectorizing code Joshua Landau <joshua@landau.ws> - 2014-01-18 21:04 +0000
Re: Need help vectorizing code Kevin K <richyokevin@gmail.com> - 2014-01-18 13:18 -0800
Re: Need help vectorizing code Peter Otten <__peter__@web.de> - 2014-01-18 22:50 +0100
Re: Need help vectorizing code Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-01-19 15:46 +0000
| From | Kevin K <richyokevin@gmail.com> |
|---|---|
| Date | 2014-01-18 12:51 -0800 |
| Subject | Need help vectorizing code |
| Message-ID | <140f8aea-d3c5-4c0d-94f5-6aa064e353d1@googlegroups.com> |
I have some code that I need help vectorizing.
I want to convert the following to vector form, how can I? I want to get rid of the inner loop - apparently, it's possible to do so.
X is an NxD matrix. y is a 1xD vector.
def foo(X, y, mylambda, N, D, epsilon):
...
for j in xrange(D):
aj = 0
cj = 0
for i in xrange(N):
aj += 2 * (X[i,j] ** 2)
cj += 2 * (X[i,j] * (y[i] - w.transpose()*X[i].transpose() + w[j]*X[i,j]))
...
If I call numpy.vectorize() on the function, it throws an error at runtime.
Thanks
[toc] | [next] | [standalone]
| From | Joshua Landau <joshua@landau.ws> |
|---|---|
| Date | 2014-01-18 21:04 +0000 |
| Message-ID | <mailman.5689.1390079454.18130.python-list@python.org> |
| In reply to | #64254 |
On 18 January 2014 20:51, Kevin K <richyokevin@gmail.com> wrote: > def foo(X, y, mylambda, N, D, epsilon): > ... > for j in xrange(D): > aj = 0 > cj = 0 > for i in xrange(N): > aj += 2 * (X[i,j] ** 2) > cj += 2 * (X[i,j] * (y[i] - w.transpose()*X[i].transpose() + w[j]*X[i,j])) Currently this just computes and throws away values...
[toc] | [prev] | [next] | [standalone]
| From | Kevin K <richyokevin@gmail.com> |
|---|---|
| Date | 2014-01-18 13:18 -0800 |
| Message-ID | <6bd7e22e-cfc8-4a80-9f17-3cc3ff8bad67@googlegroups.com> |
| In reply to | #64254 |
I didn't paste the whole function, note the ... before and after. I do use the values. I want to get rid of one of the loops so that the computation becomes O(D). Assume vectors a and c should get populated during the compute, each being 1xD. Thanks On Saturday, January 18, 2014 12:51:25 PM UTC-8, Kevin K wrote: > I have some code that I need help vectorizing. > > I want to convert the following to vector form, how can I? I want to get rid of the inner loop - apparently, it's possible to do so. > > X is an NxD matrix. y is a 1xD vector. > > > > def foo(X, y, mylambda, N, D, epsilon): > > ... > > for j in xrange(D): > > aj = 0 > > cj = 0 > > for i in xrange(N): > > aj += 2 * (X[i,j] ** 2) > > cj += 2 * (X[i,j] * (y[i] - w.transpose()*X[i].transpose() + w[j]*X[i,j])) > > > > ... > > > > If I call numpy.vectorize() on the function, it throws an error at runtime. > > > > Thanks
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-01-18 22:50 +0100 |
| Message-ID | <mailman.5691.1390081794.18130.python-list@python.org> |
| In reply to | #64254 |
Kevin K wrote: > I have some code that I need help vectorizing. > I want to convert the following to vector form, how can I? I want to get > rid of the inner loop - apparently, it's possible to do so. X is an NxD > matrix. y is a 1xD vector. > > def foo(X, y, mylambda, N, D, epsilon): > ... > for j in xrange(D): > aj = 0 > cj = 0 > for i in xrange(N): > aj += 2 * (X[i,j] ** 2) > cj += 2 * (X[i,j] * (y[i] - w.transpose()*X[i].transpose() > + w[j]*X[i,j])) > > ... > > If I call numpy.vectorize() on the function, it throws an error at > runtime. Maybe a = (2*X**2).sum(axis=0) c = no idea. Judging from the code y should be 1xN rather than 1xD. Also, should w.transpose()*X[i].transpose() be a vector or a scalar? If the latter, did you mean numpy.dot(w, X[i]) ?
[toc] | [prev] | [next] | [standalone]
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
|---|---|
| Date | 2014-01-19 15:46 +0000 |
| Message-ID | <mailman.5707.1390146433.18130.python-list@python.org> |
| In reply to | #64254 |
On 18 January 2014 20:51, Kevin K <richyokevin@gmail.com> wrote: > I have some code that I need help vectorizing. > I want to convert the following to vector form, how can I? I want to get rid of the inner loop - apparently, it's possible to do so. > X is an NxD matrix. y is a 1xD vector. > > def foo(X, y, mylambda, N, D, epsilon): > ... > for j in xrange(D): > aj = 0 > cj = 0 > for i in xrange(N): > aj += 2 * (X[i,j] ** 2) > cj += 2 * (X[i,j] * (y[i] - w.transpose()*X[i].transpose() + w[j]*X[i,j])) As Peter said the y[i] above suggests that y has the shape (1, N) or (N, 1) or (N,) but not (1, D). Is that an error? Should it actually be y[j]? You don't give the shape of w but I guess that it is (1, D) since you index it with j. That means that w.transpose() is (D, 1). But then X[i] has the shape (D,). Broadcasting those two shapes gives a shape of (D, D) for cj. OTOH if w has the shape (D, 1) then cj has the shape (1, D). Basically your description is insufficient for me to know what your code is doing in terms of all the array shapes. So I can't really offer a vectorisation of it. > > ... > > If I call numpy.vectorize() on the function, it throws an error at runtime. You've misunderstood what the numpy.vectorize function is for. The vectorize function is a convenient way of generating a function that can operate on arrays of arbitrary shape out of a function that operates only on scalar values. Oscar
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web