Can someone please explain me how to do inner product of two tensors in python to get one dimensional array. For example, I have two tensors with size (6,6,6,6,6) and (6,6,6,6). I need an one dimensional array of size (6,1) or (1,6).
Numpy has a function tensordot, check it out:
https://numpy.org/doc/stable/reference/generated/numpy.tensordot.html
Related
I have a sparse matrix of type torch.sparse_coo. I want to find the top-k elements of each row. However, torch.topk() function can't be used for that sparse matrix.
Could anyone give me some advice on what to do?
Thanks!
want to sort np.ndarray indexes of an array such as
[[.5,.7, .9], [.6, .0, .8]]
result would look like this
[[1,1],[0,1],[1,0],[0,1],[1,2],[0,3]]
applying those indexes will get correct sorting order and at same time can be applied to other structures that match the data.
I tried np.argsort, but that doesn't give indexes for ndarray
You can use np.argsort on the flat array and then use np.divmod to get the indexes of your previous shape.
Edit: np.unravel_index is the divmod alternative for higher dimensions, see https://numpy.org/doc/stable/reference/generated/numpy.unravel_index.html
A friend would like ndarray.reshape to figure out how many columns are needed given that she'd like N values to be formed into R rows. She speculates that perhaps just providing one argument to reshape might cause the other to default, but it doesn't. For example, she tried to get three rows of four columns like this:
np.array(range(12)).reshape(3)
ValueError: cannot reshape array of size 12 into shape (3,)
What could she do to get the number of rows automatically calculated?
I've read the numpy.reshape documentation. I tried testing, but none of them look like anything automatic except:
I have a list of tuples in the form below. A given tuple represents a given pair of movies that a given user liked. All tuples, together, capture every combination of movie likes found in my data.
[(movie_a,movie_b),...(movie_a,movie_b)]
My task is to create movie embeddings, similar to word embeddings. The idea is to train a single hidden layer NN to predict the most likely movie which any user might like, given a movie supplied. Much like word embeddings, the task is inconsequential; it's the weight matrix I'm after, which maps movies to vectors.
Reference: https://arxiv.org/vc/arxiv/papers/1603/1603.04259v2.pdf
In total, there are 19,000,000 tuples (training examples.) Likewise, there are 9,000 unique movie IDs in my data. My initial goal was to create an input variable, X where each row represented a unique movie_id, and each column represented a unique observation. In any given column, only one cell would be set to 1, with all other values set to 0.
As an intermediate step, I tried creating a matrix of zeros of the right dimensions
X = np.zeros([9000,19000000])
Understandably, my computer crashed, simply trying to allocate sufficient memory to X.
Is there a memory efficient way to pass my list of values into PyTorch, such that a binary vector is created for every training example?
Likewise, I tried randomly sampling 500,000 observations. But similarly, passing 9000,500000 to np.zeroes() resulted in another crash.
My university has a GPU server available, and that's my next stop. But I'd like to know if there's a memory efficient way that I should be doing this, especially since I'll be using shared resources.
I would like to save different numpy arrays with np.savetxt('xx.csv'). The np.arrays are (m,n) and I would like to add before the first column a list of index and above each line a list of header. Both lists consist of strings.
When I do multiple savetxt it erased the previous values.
I considered creating a huge matrix by merging every numpy arrays but doubt about the optimality.
Thanks for your help