This is the seventh day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Now, the title design of the article is a key step in getting more read. I thought about it for a long time, repeatedly overturning and finally using a simple title – “Pytorch Introduction and Improvement (1)”, but I hope we can give you value by sharing the content.

Introducing dependencies, in addition to today’s main character, we also introduce a numpy

``````import numpy as np
import torch
Copy the code``````

### Create a Tensor

``````data = [[1.2], [3.4]]
x_data = torch.tensor(data)
Copy the code``````

Tensor is a simple thing to do. We’ve created a Tensor data type by entering data directly into Torch. Tensor.

### Tensor is created based on numpy and list

There are many ways to create a Tensor, but let’s start with creating a Tensor based on the existing numpy and list collection types.

#### Tensor is created with numpy

Numpy can be found everywhere in Python projects, from machine learning to data analysis and mining. So some of the most popular deep learning frameworks, such as TensorFlow and PyTorch, provide support for NumPY.

``````a = np.array([1.1.2])
Copy the code``````

Use a tensor of PyTorch’s ndarray type to convert numpy to a tensor of PyTorch’s, leaving the same type in Float64

``````a_tensor = torch.from_numpy(a)
a_tensor
Copy the code``````
``Tensor ([1.0000, 1.2000])Copy the code``

#### A Tensor is created from a List

In addition to creating a Tensor from Numpy’s Ndarray above, PyTorch also supports creating a Tensor from a list.

``````a_list = [1.1.2]
a_tensor = torch.tensor(a_list)
a_tensor
Copy the code``````
``Tensor ([1.0000, 1.2000])Copy the code``
``````b_tensor = torch.FloatTensor(a_list)
b_tensor
Copy the code``````
``Tensor ([1.0000, 1.2000], dtype = torch. Float32)Copy the code``

Notice that we use a torch.Tensor to create a tensor, and then we have a capital one in the torch that starts with a torch.Tensor, they have different uses, they take different parameters to create a tensor, A lowercase tensor takes a list or numpy object as an argument to create a tensor, and an uppercase tensor takes in addition the representation size to create a randomly initialized tensor.

``````a_empty = torch.empty(2.3)
a_empty
Copy the code``````
``E+77 tensor ([[2.3158, 2.3158 e+77, 5.9288 e-323], [e-314 e+00 0.0000, 2.1220, 2.9805 e+179]])Copy the code``
``````a_tensor = torch.FloatTensor(2.3.3.3)
a_tensor
Copy the code``````
``Tensor ([[[[2.4174e-06, 2.1780e-04, 1.2914e-11], [1.6691e+22, 2.6705e+23, 8.2662e-10], [2.7300e-06, 8.1714e+20, 3.2944e-09]], [4.2248e-05, 2.3328e-18, 1.6678e+19], [7.0976e+22, 1.2853e+31, 3.0979e+32], [2.3329e-18, 4.5447e+30, 7.0062e+22]], [2.1058e+35, 2.6447e+20, 6.4825e-10], [1.2849e+31, 1.8395e+25, 6.1963e-04], [2.0662e+20, 8.1936e-10, 2.1344 e-07]]], [[[e-05 e-08, 1.3236 4.3964, and 6.7498 e-07], [6.6288 e-10 e+22 1.3679, 2.3049 e-12], [7.1429 e+31, E-18, e-10], [1.0015e-11, 1.2352e-08, 2.6707e-06], [4.0518e-11, 2.6079e-09, 2.9572e-18], [7.2646e+22, 7.2250 e+28, 2.5226 e-18]], [[e-43 e-18 2.4283, 1.7516, 0.0000 e+00], [e+09 e+00 0.0000, 8.5899, 1.8190 e-27], [3.6902 e+19, E-44, 1.2612-8.5899 e+09]]]], dtype = torch. Float32)Copy the code``

An uninitialized Tensor is created using Empty or FloatTensor, and actually there are random given values in a Tensor, but they are either too large or too small.

### Setting the default type

If we create tesnor without specifying a data type, the system uses the default data type torch.FloatTensor.

``````a = torch.tensor([1,]).type()
a
Copy the code``````

To set the default type, you can set the default tensor data type with set_default_tensor_type.

``````torch.set_default_tensor_type(torch.DoubleTensor)
Copy the code``````
``````a = torch.tensor([1.2.2.1]).type()
a
Copy the code``````
``````'torch.DoubleTensor'
Copy the code``````

### A tensor is randomly created using the RAND method

A tensor can be created by calling the RAND method. The RAND method creates a tensor that is initialized by some random number. The RAND method takes a parameter specifying the shape of the tensor.

``````torch.rand(2.3)
Copy the code``````
``Tensor ([[0.4056, 0.9493, 0.2661], [0.2156, 0.4715, 0.4217]])Copy the code``

### A tensor is created using the rand_like method

The difference between rand_like and RAND is that rand_like takes a tensor as an argument, and the tensor created by rand_like is the same shape as the input tensor.

``````a = torch.tensor([[2..3..4.], [1..2..3.]])
res = torch.rand_like(a)
print(res.shape)
Copy the code``````
``````torch.Size([2, 3])
Copy the code``````

Normally when you’ve created a Tensor we need to look at the Tensor shape

### Initialize a Tensor with a random number

#### Randint is used to create a Tensor of integer random numbers

``````# [min,max)
torch.randint(1.10, (2.3))
Copy the code``````
• The first parameter (low), which defaults to 0, defines a random starting value from a range of random numbers
• The second parameter (high) is the end value of the range
• The third parameter (size) creates the tensor shape
``````
tensor([[6, 1, 9],
[1, 2, 8]])
Copy the code``````
``````# Normal distribution. Randn means a normal distribution with mean 0 and variance 1
torch.randn(3.3)
Copy the code``````
``Tensor ([[0.7808, 0.9283, 0.3443], [0.6449, 0.7878, 0.2888], [0.5749, 0.0562, 2.5841]])Copy the code``
``````For example we want to create a 2 * 5 normal distribution. Let's create a 10 number first, then a 10 mean, then a 10 variance
a = torch.normal(mean=torch.full([10].0.),std=torch.arange(1.0, -0.1))
Tensor with 10 = 0
a
Copy the code``````
``Tensor ([-0.7043, 2.0245, -0.3698, 0.3927, 0.0138, -0.2230, -0.8763, 0.0090, 0.2446, 0.0955])Copy the code``
``````torch.normal(mean=0.5, std=torch.arange(1..6.))
Copy the code``````
``Tensor ([1.0381, 1.3676, 5.2665, 5.1679, 8.0477])Copy the code``

#### Normal method to create a Tensor

Normal is used to create a tensor containing random numbers extracted from the discrete normal distribution of the given parameter means, STD. Mean means is a Tensor that contains the mean of the normal distribution associated with each output element. STD is a tensor that contains the standard deviation of the normal distribution associated with each output element. The mean and standard deviation shapes don’t have to match, but each tensor has to have the same number of elements.

``````torch.normal(mean=torch.arange(1..11.), std=torch.arange(1.0, -0.1))
Copy the code``````
``Tensor ([0.6371, 3.1117, 3.2735, 3.7251, 3.8135, 5.7626, 7.0770, 8.1662, 9.1001, 9.9700])Copy the code``
``````tensor_full_a = torch.full((2.3),7.)
tensor_full_a
Copy the code``````
``````tensor([[7., 7., 7.],
[7., 7., 7.]])
Copy the code``````
``````torch.full((2.3), 3.141592)
Copy the code``````
``Tensor ([[3.1416, 3.1416, 3.1416], [3.1416, 3.1416, 3.1416]])Copy the code``
``````# Shape Change using Shape
a = a.reshape(2.5)
a
Copy the code``````
``Tensor ([[0.7043, 2.0245, 0.3698, 0.3927, 0.0138], [0.2230, 0.8763, 0.0090, 0.2446, 0.0955]])Copy the code``

#### Linspace method to create a Tensor

``````torch.linspace(0.10,steps=4)
Copy the code``````

A Tensor is created, so linspace can create a Tensor, the first and second parameters define the start and end positions of the interval, the number of samples generated by steps between start and end.

``Tensor ([0.0000, 3.3333, 6.6667, 10.0000])Copy the code``
``````torch.logspace(0, -1,steps=10)
Copy the code``````
``Tensor ([1.0000, 0.7743, 0.5995, 0.4642, 0.3594, 0.2783, 0.2154, 0.1668, 0.1292, 0.1000]Copy the code``
``Copy the code``

#### You can also get a new Tensor by changing its shape to a Tensor

``````b = torch.tensor([[2.3.4], [1.2.3]])
b.reshape(3.2)
Copy the code``````
``````tensor([[2, 3],
[4, 1],
[2, 3]])
Copy the code``````

### The full method creates a tensor

The full method takes two arguments, one to specify the shape of the tensor to be created, and the second to fill the tensor’s value.

``````# a.r eshape (3, 2)
Copy the code``````
``````torch.full([2.3].7.)
Copy the code``````
``````tensor([[7., 7., 7.],
[7., 7., 7.]])
Copy the code``````
``````# Create a scalar
torch.full([],7)
Copy the code``````
``````tensor(7.)
Copy the code``````
``````Create a vector
torch.full([1].7)
Copy the code``````
``````tensor([7.])
Copy the code``````

### The arange method creates tensor

Tensor created by arange accepts arange, which follows the left closed and right open principle,[)

``````torch.arange(0.10)
Copy the code``````
``````tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Copy the code``````
``````# The third parameter represents the step
torch.arange(0.10.2)
Copy the code``````
``````tensor([0, 2, 4, 6, 8])
Copy the code``````

### The bisecting method creates a tensor

Linspace is a step partition within a specified interval, such as steps.

``````torch.linspace(0.10,steps=4)
Copy the code``````
``Tensor ([0.0000, 3.3333, 6.6667, 10.0000])Copy the code``
``````torch.linspace(0.10,steps=10)
Copy the code``````
``Tensor ([0.0000, 1.1111, 2.2222, 3.3333, 4.4444, 5.5556, 6.6667, 7.7778, 8.8889, 10.0000])Copy the code``
``````torch.linspace(0.10,steps=11)
Copy the code``````
``````tensor([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
Copy the code``````
``````torch.logspace(0, -1,steps=10)
Copy the code``````
``Tensor ([1.0000, 0.7743, 0.5995, 0.4642, 0.3594, 0.2783, 0.2154, 0.1668, 0.1292, 0.1000]Copy the code``
``````torch.logspace(0.1,steps=10)
Copy the code``````
``Tensor ([1.0000, 1.2915, 1.6681, 2.1544, 2.7826, 3.5938, 4.6416, 5.9948, 7.7426, 10.0000])Copy the code``

### The other method creates a tensor

• The ones method creates a tensor that is all 1, and the ones takes a parameter that specifies the tensor shape
• The Zeros method creates an all zero tensor, and the ones takes an argument that specifies the tensor shape
• The Eye method creates a tensor with all 1’s diagonals and 0’s everywhere else, and the Ones take parameter specifies the tensor shape
``````
torch.ones(3.3)
Copy the code``````
``````tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
Copy the code``````
``````torch.zeros(3.3)
Copy the code``````
``````tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
Copy the code``````
``````torch.eye(3.2)
Copy the code``````
``````tensor([[1., 0.],
[0., 1.],
[0., 0.]])
Copy the code``````
``````torch.eye(3)
Copy the code``````
``````tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
Copy the code``````
``````a = torch.zeros(3.3)
torch.ones_like(a)
Copy the code``````
``````tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
Copy the code``````
``````# random shatter [0,10]
torch.randperm(10)
Copy the code``````
``````tensor([1, 3, 0, 2, 9, 5, 6, 7, 4, 8])
Copy the code``````
``````a = torch.rand(2.3)
b = torch.rand(2.2)
Copy the code``````
``````idx = torch.randperm(2)
idx
Copy the code``````
``````tensor([0, 1])
Copy the code``````
``````a[idx]
Copy the code``````
``Tensor ([[0.9753, 0.6958, 0.7198], [0.9417, 0.3410, 0.9740]])Copy the code``
``````b[idx]
Copy the code``````
``Tensor ([[0.0652, 0.7491], [0.9848, 0.7652]])Copy the code``
``Copy the code``