The key here, is that the last n dimentions of the first matrix, need to align with the first n dimentions of the second matrix. If you want to element-wise multiply tensors of shape and for example, such that each 2x2 matrix will be multiplied by the corresponding value, you could rearrange the dimentions as by permute(2,3,0,1), then perform the multiplication by a * b and then return to the original shape by permute(2,3,0,1) again. Remember when talking about the 4th dimention for instance, its representation as an argument is 3 (not 4). We can use a hash set to mark which elements have been at the 1st index of current recursion. 1 has already been at the 1st index of current recursion level, so the last possibility is redundant. And the third and forth dimention will move to the second and third dimention, because the 2 and 3 indices are located in the second and third place. At recursion level 0, we have 1 + permute2,3,4,1, 2 + permute1,3,4,1, 3 + permute2,1,4,1, 4 + permute2,3,1,1, 1 + permute2,3,4,1. 8 permute 2: 90: Find the Number of Possibilities: 7 choose 1: 91: Find the Number of Possibilities: 6 permute 2: 92: Find the Number of Possibilities: 4 permute 2: 93: Find the Number of Possibilities: 4 permute 3: 94: Find the Number of Possibilities: 3 permute 3: 95: Find the Number of Possibilities: 46 choose 6: 96: Find the Number of. But it works with it's arguments numbering the current shape of by 0 to 3, and permutes as you inserted the arguments, meaning for a.permute(0,2,3,1) it will keep the first dimention in its place, since the first argument is 0, the second dimention it will move to the forth dimention, since the index 1 is the forth argument. A typical combination lock for example, should technically be called a permutation lock by mathematical standards, since the order of the numbers entered is important 1-2-9 is not the same as 2-9-1, whereas for a combination, any order of those three numbers would suffice. You can read about permute() in the docs. desired shape of torch.Size() by permute(0,3,1,2). After we finish the multiplication we transpose it again, using permute(), to the. I.e, a.permute(0,2,3,1) will be of shape torch.Size() which fits the shape of b (torch.Size()) for matrix multiplication, since the last dimention of a equals the first dimention of b. ![]() The permute() function transposes the dimention in the order of it's arguments. ![]() ![]() You can use either a * b or torch.mul(a, b) but you must use permute() before and after you multiply, in order to have the compatible shape: import torchĬ = (a.permute(0,2,3,1) * b).permute(0,3,1,2)Ĭ = torch.mul(a.permute(0,2,3,1), b).permute(0,3,1,2)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |