I essentially have eighty 500x1 vectors as a 3-dimensional tensor 500x1x80, and one 500x1 vector. I want to take the outer product of the two, with the goal of obtaining eighty 500x500 matrices, in the form of a 500x500x80 tensor, however I'm having difficulties on numpy to get the dimensions right. I am replicating a paper, which states that one takes the outer product of the 500x1x80 tensor and the 500x1 vector to get the previously mentioned 500x500x80 tensor, however I'm not sure how that works considering the dimensions, and there's no further explanation in the paper. I'd appreciate any help, thanks.
I have so far tried to reshape the two tensors and use np.tensordot and np.outer, but this hasn't worked out to the right dimensions.
The main issue here is that numpy's outer
method only accepts one-dimensional vectors. The documentation also says that "Input is flattened if not already 1-dimensional.", and that is probably why you are having trouble getting to the right shape. Several options for calculating the outer product of arrays of dimension larger than one are discussed here.
However, since you said you are representing your 500x1x80 tensor as "eighty 500x1 vectors", one option is to calculate the outer product 80 times, resulting in eighty 500x500 matrices (representing the 500x500x80 tensor). Supposing you have something like this:
vectors = [np.ones([500])] * 80
v = np.ones([500])
Calculating the outer products could be done with
outer_prods = [np.outer(v, w) for w in vectors]