Understanding PyTorch Tensor Shape - python

I have a simple question regarding the shape of tensor we define in PyTorch. Let's say if I say:
input = torch.randn(32, 35)
This will create a matrix with 32 row and 35 columns.
Now when I define:
input2 = torch.randn(1,2,32, 35)
What can I say about the dimension of the new matrix input2?
How can I define the rows and columns here? I mean do I have two matrices with shapes 32*35 packed by the tensor?
I want to better understand the geometry behind this. Thanks.

Consider tensor shapes as the number of lists that a dimension holds. For instance, a tensor shaped (4, 4, 2) will have four elements, which will all contain 4 elements, which in turn have 2 elements.
The first holds 4 elements.
The second holds 4 elements.
The third dimension holds 2 elements.
Here's what the data would look like:
[[[0.86471446, 0.26302726],
[0.04137454, 0.00349315],
[0.06559607, 0.45617865],
[0.0219786, 0.27513594]],
[[0.60555118, 0.10853228],
[0.07059685, 0.32746256],
[0.99684617, 0.07496456],
[0.55169005, 0.39024103]],
[[0.55891377, 0.41151245],
[0.3434965, 0.12956237],
[0.74908291, 0.69889266],
[0.98600141, 0.8570597]],
[[0.7903229, 0.93017741],
[0.54663242, 0.72318166],
[0.6099451, 0.96090241],
[0.63772238, 0.78605599]]]
In other words, four elements of four elements of two elements.

Yes, that is correct. Your input2 tensor has a rank of 4. (Rank is the Dimension) and the bounds of each dimension are (1,2,32,35)
The first dimension can hold one element.
The second can hold two.
The third can hold 32 elements.
The forth dimension can hold 35
elements.
EDIT: I find it is useful to think of higher-dimensional arrays as a series of lists. In your case, a rank 4 tensor, would be a list of lists of lists of lists.

Related

How does the transpose work in three or more dimensions?

For two-dimensaol array, according Guide to numpy
The two-styles of memory layout for arrays are connected through the transpose operation. Thus, if A is a (contiguous) C-style array, then the same block memory can be used to represent AT as a (contiguous) Fortran-style array. This kindof undorctondina non ho ucoful mhon trrina to orn mmonnina of Fortron
In three dimensional matrix
A = np.arange(6).reshape([1,2,3])
we can view A as a 1 by 1 block matrix, which means A has one entry, and that entry is a matrix with two rows and three columns.
So we can iteratively take the transpose according to the rules above.
My question is:
A= np.arange(6).reshape([1,2,3])
B = A.transpose(1,2,0)
In this case, how does it work. Is there a rule that can tell me how the elements of B are arranged

Difference in numpy np.zeros((1,2)) vs np.zeros((2,))

I am stuck in this question. Can anybody explain me the difference between these two?
np.zeros ((1,2))
which yields
[[0. 0.]]
and
np.zeros((2,))
which yields
[0. 0.]
For each element in the main argument of np.zeros the function will add a new dimension to the output vector.
Your first code np.zeros ((1,2)) yields an array with two dimensions, one element in the first dimension and two elements in the second dimension, thus
[[0.]
[0.]]
The second piece of code has only one element in the main argument, which is translated to "one single dimension, two elements in that dimension". Thus, the output to your np.zeros((2,)) will be the same as the one for np.zeros(2):
array([0., 0.])
You could try with a third dimension to see it further:
np.zeros((1,2,1))
array([[[0.],
[0.]]])
I short, each square bracket adds to a new dimension based on the elements in the first argument of the function np.zeros.
Here's how I think about it.
This answer helpfully points out that "rows" and "columns" aren't exact parallels for NumPy arrays, which can have n dimensions. Rather, each dimension, or axis, is represented by a number (the size, how many members it has) and in notation by an additional pair of square brackets.
So a 1-dimensional array of size 5 isn't a row or a column, just a 1-dimensional array. When you initialise np.zeros ((1,2)) your first dimension has size 1, and your second size 2, so you get a 1 x 2 matrix with two pairs of brackets. When you call np.zeros((2,)) it's just one dimension of size two, so you get array([0., 0.]). I also find this confusing - hope it makes sense!
In the first, the items would be indexed as [0][0] and [0][1], and in the second, the items would be indexed by [0] and [1].
A shape of (1,2) means two dimensions, where the first dimensions happens to have only one index, i.e. it's a matrix with one row.

Get range/slice from numpy array the size of another array

I have two numpy arrays, one bigger than another, but both with the same number of dimensions.
I want to get a slice from the bigger array that matches the size of the smaller array. (Starting from 0,0,0....)
So, imagine the big array has shape (10,5,7).
And the small array has shape (10,4,6).
I want to get from the bigger array this slice:
biggerArray[:10,:4,:6]
The length of the shape tuple may vary, and I want to do it for any number of dimensions (Both will always have the same number of dimensions).
How to do that? Is there a way to use tuples as ranges in slices?
Construct the tuple of slice objects manually. biggerArray[:10, :4, :6] is syntactic sugar for biggerArray[(slice(10), slice(4), slice(6))], so:
biggerArray[tuple(map(slice, smallerArray.shape))]
or
biggerArray[tuple(slice(0, n) for n in smallerArray.shape)]
You may want to assert result.shape == smallerArray.shape afterwards, just in case the input shapes weren't what you thought they were.

About Numpy shape

I'm new to numpy & have a question about it :
according to docs.scipy.org, the "shape" method is "the dimensions of the array. For a matrix with n rows and m columns, shape will be (n,m)"
Suppose I am to create a simple array as below:
np.array([[0,2,4],[1,3,5]])
Using the "shape" method, it returns (2,3) (i.e. the array has 2 rows & 3 columns)
However, for an array ([0,2,4]), the shape method would return (3,) (which means it has 3 rows according to the definition above)
I'm confused : the array ([0,2,4]) should have 3 columns not 3 rows so I expect it to return (,3) instead.
Can anyone help to clarify ? Thanks a lot.
This is just notation - in Python, tuples are distinguished from expression grouping (or order of operations stuff) by the use of commas - that is, (1,2,3) is a tuple and (2x + 4) ** 5 contains an expression 2x + 4. In order to keep single-element tuples distinct from single-element expressions, which would otherwise be ambiguous ((1) vs (1) - which is the single-element tuple and which a simple expression that evaluates to 1?), we use a trailing comma to denote tuple-ness.
What you're getting is a single dimension response, since there's only one dimension to measure, packed into a tuple type.
Numpy supports not only 2-dimensional arrays, but multi-dimensional arrays, and by multi-dimension I mean 1-D, 2-D, 3-D .... n-D, And there is a format for representing respective dimension array. The len of array.shape would get you the number of dimensions of that array. If the array is 1-D, the there is no need to represent as (m, n) or if the array is 3-D then it (m, n) would not be sufficient to represent its dimensions.
So the output of array.shape would not always be in (m, n) format, it would depend upon the array itself and you will get different outputs for different dimensions.

How to intepret the shape of the array in Python?

I am using a package and it is returning me an array. When I print the shape it is (38845,). Just wondering why this ','.
I am wondering how to interpret this.
Thanks.
Python has tuples, which are like lists but of fixed size. A two-element tuple is (a, b); a three-element one is (a, b, c). However, (a) is just a in parentheses. To represent a one-element tuple, Python uses a slightly odd syntax of (a,). So there is only one dimension, and you have a bunch of elements in that one dimension.
It sounds like you're using Numpy. If so, the shape (38845,) means you have a 1-dimensional array, of size 38845.
It seems you're talking of a Numpy array.
shape returns a tuple with the same size as the number of dimensions of the array. Each value of the tuple is the size of the array along the corresponding dimensions, or, as the tutorial says:
An array has a shape given by the number of elements along each axis.
Here you have a 1D-array (as indicated with a 1-element tuple notation, with the coma (as #Amadan) said), and the size of the 1st (and only dimension) is 38845.
For example (3,4) would be a 2D-array of size 3 for the 1st dimension and 4 for the second.
You can check the documentation for shape here: http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.shape.html
Just wondering why this ','.
Because (38845) is the same thing as 38845, but a tuple is expected here, not an int (since in general, your array could have multiple dimensions). (38845,) is a 1-tuple.

Categories

Resources