Pytorch tensor dimension

  Tensor class member function Dim () can return tensor dimensions, attributes and member functions Shape size () Returns the specific dimensions tensor components, the following code defines a tensor two rows and three columns:

  f = torch.randn(2, 3)

  print(f.dim())

  print(f.size())

  print(f.shape)

  Output:

  2

  torch.Size([2, 3])

  torch.Size([2, 3])

  dim = 0 scalar

  Tensor 0 dimension of scalar scalar Loss is generally used in such a place. Following code defines a scalar:

  import torch

  a = torch.tensor (1.6) # dimension is the scalar is 0

  print(a, a.type())

  Some dimensions of the test method #

  print(a.dim())

  print(a.shape, a.size())

  print (referred to as (a.shape), only the (a.size ()))

  Output:

  tensor(1.6000) torch.FloatTensor

  0

  torch.Size([]) torch.Size([])

  0 0

  Definition of scalar very simple way, as long as incoming values ​​can be initialized in a scalar function tensor, note the specific data.

  note:

  torch.tensor Pytorch in () is a function, the following function prototype:

  torch.tensor(data, dtype=None, device=None, requires_grad=False)

  Wherein data can be: list, tuple, NumPy ndarray, scalar, and other types. torch.tensor do copy (not directly reference) from the data in the data section, generating a corresponding torch.LongTensor, torch.FloatTensor and torch.DoubleTensor original data type.

  And torch.Tensor () is a class of Pytorch, the default type tensor torch.FloatTensor () alias.

  Tensor difference between the tensor and specific reference https://blog.csdn.net/tfcy694/article/details/85338745

  Note that, torch.tensor () is the specific parameters of the received data, and torch.Tensor () parameters can also receive data that is received component dimension shape.

  dim = 1 tensor

  dim = Tensor 1 is generally used in such a local input Linear Bais, neural network or linear layer Input, for example, an image data set with MINST shape = [784] is represented Tensor.

  dim = 1 corresponds to only one dimension, but this dimension may have a plurality of components (the same as one-dimensional array), there are many one-dimensional tensor implementation, the following three implementations are:

  import torch

  import numpy as np

  def printMsg(k):

  "" "Tensor information output, dimensions, shape" ""

  print(k, k.dim(), k.size(), k.shape)

  # 1. torch.tensor (), passing a parameter list is configured dim = Tensor 1

  a = torch.tensor([1.1])

  printMsg(a)

  b = torch.tensor([1.1, 2.2])

  printMsg(b)

  print("-" * 20)

  # 2. torch.Tensor (), a random configuration of dim = Tensor 1

  # Here is the incoming shape = 1, there is an element

  c = torch.FloatTensor(1)

  printMsg(c)

  # Here is the incoming shape = 2, there are two elements

  d = torch.FloatTensor(2)

  printMsg(d)

  print("-" * 20)

  # 3. numpy configured to dim = Tensor 1

  e = np.ones (2)

  print (e)

  e = torch.from_numpy(e)

  printMsg (e)

  Output:

  tensor([1.1000]) 1 torch.Size([1]) torch.Size([1])

  tensor([1.1000, 2.2000]) 1 torch.Size([2]) torch.Size([2])

  --------------------

  tensor([1.5056e-38]) 1 torch.Size([1]) torch.Size([1])

  tensor([0., 0.]) 1 torch.Size([2]) torch.Size([2])

  --------------------

  [1. 1.]

  tensor([1., 1.], dtype=torch.float64) 1 torch.Size([2]) torch.Size([2])

  dim = 2 tensor

  dim = 2 tensor with Linear Input is generally used in batch, for example, k pictures MNIST data set further Tensor if put in, then the shape = [k, 784].

  The following example:

  import torch

  # Dim = 2, shape = [2,3], randomly generated Tensor

  a = torch.FloatTensor(2, 3)

  print(a.shape)

  print(a.shape[0])

  print(a.shape[1])

  print(a.size())

  print(a.size(0))

  print(a.size(1))

  Output:

  torch.Size([2, 3])

  2

  3

  torch.Size([2, 3])

  2

  3

  As can be seen from the above, the components can take Tensor each dimension.

  dim = tensor 3

  dim = 3 tensor RNN and well suited for the NLP, such as 20 words, 10 words of each sentence, each word is represented by vector components 100, is obtained Tensor shape = [20,10,100].

  Sample code:

  import torch

  # Dim = 3, shape = [1,2,3], randomly chosen number between 0 and 1

  a = torch.rand(1, 2, 3)

  print(a)

  print(a.dim())

  print(a.shape)

  print(a.size())

  b = a [0] # taken in the first dimension, is obtained shape = [2,3] in the dim = Tensor 2

  print(b.dim())

  print(b.shape)

  print(b.size())

  # The torch.Size () converted to form a list of python

  list(a.shape)

  Output:

  tensor([[[0.3503, 0.2163, 0.7880],

  [0.0933, 0.0372, 0.9077]]])

  3

  torch.Size([1, 2, 3])

  torch.Size([1, 2, 3])

  2

  torch.Size([2, 3])

  torch.Size([2, 3])

  [1, 2, 3]

  dim = 4 Tensor

  dim = 4 tensor representing an image suitable for CNN, for example, 100 grayscale MNIST handwritten data sets (the number of channels is 1, if the number of channels is an RGB image is 3), each figure = 28 pixels high, width = 28 pixels, so the Tensor of shape = [100,1,28,28], which is a batch of data dimensions: [batch_size, channel, height, width].

  Was constructed as follows a shape = [2, 3, 28, 28] of Tensor:

  import torch

  a = torch.rand(2, 3, 28, 28)

  print(a)

  print(a.shape)

  Output:

  tensor([[[[0.0788, 0.1869, 0.2921, ..., 0.0653, 0.3229, 0.1024],

  [0.5636, 0.4804, 0.6982, ..., 0.3832, 0.9984, 0.5522],

  [0.5650, 0.8758, 0.2636, ..., 0.5024, 0.5592, 0.2962],

  ..., Wuxi see male hospital Which https://yyk.familydoctor.com.cn/20612/

  [0.6311, 0.8914, 0.1481, ..., 0.9255, 0.4388, 0.6771],

  [0.1691, 0.6803, 0.0111, ..., 0.8962, 0.4907, 0.7369],

  [0.7360, 0.3643, 0.9757, ..., 0.6954, 0.5979, 0.2019]],

  [[0.1387, 0.1379, 0.7730, ..., 0.3476, 0.6346, 0.5387],

  [0.2940, 0.2296, 0.5864, ..., 0.0558, 0.4093, 0.4563],

  [0.1498, 0.1705, 0.4542, ..., 0.0370, 0.5867, 0.4178],

  ...,

  [0.6814, 0.3024, 0.8031, ..., 0.0425, 0.4695, 0.3253],

  [0.5721, 0.9202, 0.1709, ..., 0.7571, 0.6944, 0.7455],

  [0.4426, 0.3546, 0.9652, ..., 0.2672, 0.0161, 0.3755]],

  [[0.1455, 0.2020, 0.2242, ..., 0.5136, 0.5918, 0.9750],

  [0.1039, 0.1641, 0.1945, ..., 0.1314, 0.6887, 0.2439],

  [0.0275, 0.1987, 0.5682, ..., 0.0732, 0.7392, 0.8445],

  ...,

  [0.0180, 0.7265, 0.0541, ..., 0.0939, 0.4592, 0.7898],

  [0.2253, 0.2115, 0.6485, ..., 0.0314, 0.0185, 0.5788],

  [0.4660, 0.9765, 0.1458, ..., 0.3520, 0.7162, 0.4895]]],

  [[[0.3837, 0.1531, 0.9617, ..., 0.3220, 0.3959, 0.6391],

  [0.4072, 0.0711, 0.5841, ..., 0.9417, 0.5799, 0.9794],

  [0.0307, 0.1862, 0.0766, ..., 0.8336, 0.6978, 0.4468],

  ...,

  [0.8934, 0.0480, 0.9964, ..., 0.8817, 0.3746, 0.6780],

  [0.7201, 0.5030, 0.2582, ..., 0.9542, 0.8955, 0.0750],

  [0.5892, 0.8647, 0.3251, ..., 0.5489, 0.5575, 0.3004]],

  [[0.0630, 0.1473, 0.7858, ..., 0.3695, 0.2419, 0.4843],

  [0.7736, 0.6974, 0.8900, ..., 0.3137, 0.4747, 0.9298],

  [0.8021, 0.3490, 0.8568, ..., 0.5600, 0.2382, 0.0054],

  ...,

  [0.4725, 0.7952, 0.5935, ..., 0.3612, 0.9396, 0.2763],

  [0.3715, 0.8464, 0.0258, ..., 0.2218, 0.0808, 0.9323],

  [0.2634, 0.8852, 0.7021, ..., 0.4925, 0.6998, 0.0013]],

  [[0.7005, 0.2593, 0.1891, ..., 0.2242, 0.9181, 0.4466],

  [0.2316, 0.1313, 0.6365, ..., 0.2096, 0.4597, 0.0361],

  [0.2662, 0.8333, 0.4361, ..., 0.5952, 0.0086, 0.8168],

  ...,

  [0.0641, 0.5702, 0.9628, ..., 0.0303, 0.9381, 0.9755],

  [0.6629, 0.5515, 0.5912, ..., 0.5070, 0.1345, 0.2626],

  [0.6316, 0.1251, 0.1719, ..., 0.6973, 0.2206, 0.2461]]]])

  torch.Size([2, 3, 28, 28])

  Calculating the number of elements in Tensor

  Sample code:

  import torch

  a = torch.rand(10, 1, 28, 28)

  print(a.numel()) # number of element 10*1*28*28=7840

  Output:

  7840

Guess you like

Origin www.cnblogs.com/djw12333/p/11199956.html