0
votes

This problem is to do with the numpy library.

From previous experience np.dot is the same as matrix multiplication. However I was under the assumption that multiplying two vectors is impossible as the dimensionality is wrong. So my question is, what does np.dot actually do to two vectors?

Furthermore, say I had the following code and assuming np.dot produces a scalar (as it says on the documentation).

x= np.dot(dc, self.c)[:self.ys]

where x is also a vector, how does the operation [:self.ys] change the result of the two vectors in an np.dot into a vector? I cannot find anything detailing on this.

2
The first part of the question is answered in the documentation: "... *for 1-D arrays to inner product of vectors * ...". The second part is unclear. Please provide a complete code example (get rid of self and define the variables used).MB-F
I'm working on a lstm neural network in a .Net language and am using this python code as an example to work from. x = derivative of hidden state (vector), dc = derivative of cell state (vector), self.c = cell-state in timestep tS (vector), self.ys = output size (int)Alex McKinney
My question is, if the np.dot produces a scalar, but x is a vector how does [:ys] convert the scalar into a vector?Alex McKinney
That's the crux... [ ] does not convert a scalar in a vector. It gives an error IndexError: invalid index to scalar variable. if dot returns a scalar. So either the code is wrong or the input is not what you think.MB-F
The full source is link both dc and c appear to be vectors. However I may have misunderstood what self does as there is one variable called c at an lstm class level (an array) and another at the method level (vector).Alex McKinney

2 Answers

1
votes

numpy.dot performs a "dot" or inner product between two products. However, it also performs an operation called broadcasting that is common to many languages (except matlab rebrands the dimension matching part as implicit expansion). In this case the dimensions are completed to a matching product automatically for convenience and performance. More explanation is here.

[:self.ys] is a slice. Suppose the value of self.ys is 5. Then it means take the result of the dot product and only assign the first 5 items to x since Python uses zero indexing. You can omit the starting number if it is zero hence it is also for convenience. [a:b] is [a,a+1,...,b-1] if a is 0 then you can leave it out and it is assumed.

0
votes

numpy.dot() function returns the dot product of two arrays. For 2-D vectors, it is the equivalent to matrix multiplication. For 1-D arrays, it is the inner product of the vectors. For Ndimensional arrays, it is a sum product over the last axis of a and the second-last axis of b.

Source: https://www.tutorialspoint.com/numpy/numpy_dot.htm