Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
a.assign(b) unexpected results when a.shape() = b.transpose.shape() #6545
I ran into the issue where I've been using
For vectors that works just fine, so if a has the shape [1, x] and b has the shape [x, 1] they are written as expected - just transposed.
For matrices this behavior however doesn't work. This examples shows what happens:
final INDArray target = Nd4j.zeros(7, 2); final INDArray source = Nd4j.linspace(1, 14, 14).reshape(2, 7); System.out.println(source); /* [[ 1,0000, 2,0000, 3,0000, 4,0000, 5,0000, 6,0000, 7,0000], [ 8,0000, 9,0000, 10,0000, 11,0000, 12,0000, 13,0000, 14,0000]] */ target.assign(source); System.out.println(target); /* [[ 1,0000, 2,0000], [ 3,0000, 4,0000], [ 5,0000, 6,0000], [ 7,0000, 8,0000], [ 9,0000, 10,0000], [ 11,0000, 12,0000], [ 13,0000, 14,0000]] */
This allows for some very subtle and hard to find bugs - I've questioned my sanity several times before I notices what was going on there. So I think this case should also throw an exception.
I think this was originally intended behaviour, but on reflection I'm in favor of making it explicit (as you've found out, it's a source of unintended silent errors).
I'll add validation to allow the both scalar and both vector cases, but otherwise the shapes must be equal. Users can reshape manually for the other cases.