Help with the tensordot function?
Can someone help me replace a slow expression with a faster one based on tensordot? I've read the documentation and I'm still confused. I have two matrices b and d. b is n x m and d is m x m. I want to replace the expression
bdb = zeros(n,'d') for i in xrange(n): bdb[i,:] = dot(b[i,:],dot(d,b[i,:])
with something that doesn't have the for loop and thus is a bit faster. The first step is
bd = dot(b,d)
However, following this with
bdb = dot(bd,b.T)
doesn't work, since it yields a n x n matrix instead of an n x 1 vector. Reading the notes on tensordot makes me think it's the function to use, but I'm having trouble grokking the axes argument. Can anyone help? Thanks in advance! -- Rick Muller rpmuller@gmail.com
Hi Rick! On Fri, Sep 3, 2010 at 4:02 AM, Rick Muller <rpmuller@gmail.com> wrote:
Can someone help me replace a slow expression with a faster one based on tensordot? I've read the documentation and I'm still confused.
I have two matrices b and d. b is n x m and d is m x m. I want to replace the expression
bdb = zeros(n,'d') for i in xrange(n): bdb[i,:] = dot(b[i,:],dot(d,b[i,:])
I am first trying to reproduce this --- the above is missing one ")" and also dot() seems to produce a number, but you are assigning it to bdb[i,:], also you declare bdb as a 1D array. So I tried this: http://gist.github.com/568879/
with something that doesn't have the for loop and thus is a bit faster.
The first step is
bd = dot(b,d)
However, following this with
bdb = dot(bd,b.T)
doesn't work, since it yields a n x n matrix instead of an n x 1 vector. Reading the notes on tensordot makes me think it's the function to use, but I'm having trouble grokking the axes argument. Can anyone help?
In the above gist, I did the following: bd = dot(b, d) bdb = diag(dot(bd, b.T)) print bdb which printed the same as: for i in xrange(n): bdb[i] = dot(b[i,:], dot(d, b[i, :])) print bdb but I think that this is not what you want, is it? I think you want to do something like: b * d * b.T but since b is a (n, m) matrix, the result is a matrix, not a vector, right? Ondrej
On Tue, Sep 7, 2010 at 12:23 PM, Ondrej Certik <ondrej@certik.cz> wrote:
Hi Rick!
On Fri, Sep 3, 2010 at 4:02 AM, Rick Muller <rpmuller@gmail.com> wrote:
Can someone help me replace a slow expression with a faster one based on tensordot? I've read the documentation and I'm still confused.
I have two matrices b and d. b is n x m and d is m x m. I want to replace the expression
bdb = zeros(n,'d') for i in xrange(n): bdb[i,:] = dot(b[i,:],dot(d,b[i,:])
I am first trying to reproduce this --- the above is missing one ")" and also dot() seems to produce a number, but you are assigning it to bdb[i,:], also you declare bdb as a 1D array. So I tried this:
http://gist.github.com/568879/
with something that doesn't have the for loop and thus is a bit faster.
The first step is
bd = dot(b,d)
However, following this with
bdb = dot(bd,b.T)
doesn't work, since it yields a n x n matrix instead of an n x 1 vector. Reading the notes on tensordot makes me think it's the function to use, but I'm having trouble grokking the axes argument. Can anyone help?
In the above gist, I did the following:
bd = dot(b, d) bdb = diag(dot(bd, b.T)) print bdb
which printed the same as:
for i in xrange(n): bdb[i] = dot(b[i,:], dot(d, b[i, :])) print bdb
but I think that this is not what you want, is it? I think you want to do something like:
b * d * b.T
but since b is a (n, m) matrix, the result is a matrix, not a vector, right?
Ah, I just noticed you got this resolved in the other thread. Ondrej
Ondrej, I was confused an unclear in my original question. I subsequently posted a followup, titled "Simplified question on tensordot' where I explained myself a lot better, and got some really good help. So, thank you very much for looking into this issue, but I believe that this has been resolved. Thanks! Rick http://mail.scipy.org/pipermail/numpy-discussion/2010-September/052584.html On Tue, Sep 7, 2010 at 1:23 PM, Ondrej Certik <ondrej@certik.cz> wrote:
Hi Rick!
On Fri, Sep 3, 2010 at 4:02 AM, Rick Muller <rpmuller@gmail.com> wrote:
Can someone help me replace a slow expression with a faster one based on tensordot? I've read the documentation and I'm still confused.
I have two matrices b and d. b is n x m and d is m x m. I want to replace the expression
bdb = zeros(n,'d') for i in xrange(n): bdb[i,:] = dot(b[i,:],dot(d,b[i,:])
I am first trying to reproduce this --- the above is missing one ")" and also dot() seems to produce a number, but you are assigning it to bdb[i,:], also you declare bdb as a 1D array. So I tried this:
http://gist.github.com/568879/
with something that doesn't have the for loop and thus is a bit faster.
The first step is
bd = dot(b,d)
However, following this with
bdb = dot(bd,b.T)
doesn't work, since it yields a n x n matrix instead of an n x 1 vector. Reading the notes on tensordot makes me think it's the function to use,
but
I'm having trouble grokking the axes argument. Can anyone help?
In the above gist, I did the following:
bd = dot(b, d) bdb = diag(dot(bd, b.T)) print bdb
which printed the same as:
for i in xrange(n): bdb[i] = dot(b[i,:], dot(d, b[i, :])) print bdb
but I think that this is not what you want, is it? I think you want to do something like:
b * d * b.T
but since b is a (n, m) matrix, the result is a matrix, not a vector, right?
Ondrej _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- Rick Muller rpmuller@gmail.com 505-750-7557
participants (2)
-
Ondrej Certik -
Rick Muller