Directory
1. Tensor operations
1. How to create
2. Create linear and random tensors
3. Create 01 tensor
4. Tensor element type conversion
2. Calculation of tensor
1. Basic operation of tensor
2. Adama product
3. Dot product operation
4. Designated Computing Devices
3. Tensor type conversion
1. Tensor conversion to numpy
2. Convert numpy to tensor
3. Conversion of scalar tensors and numbers
4. Tensor splicing
1. The torch.cat function can stitch two Zhang Liang together according to the specified dimension
2. The torch.stack function can superimpose two tensors according to the specified dimension
5. Tensor index operation
1. Simple row and column index
2. Boolean index, multidimensional index
1. Tensor operations
1. Creation method
1.torch.tensor creates tensors based on specified data
def test01(): #1 Using scalars data = torch. tensor(10) print(data) #2numpy array data = np.random.randn(2,3) data = torch. tensor(data) print(data) #3 list list data = [[10.,20.,30.,],[40.,50.,60.]] data = torch. tensor(data) print(data) tensor(10) tensor([[ 0.5863, 0.6107, -0.1272], [ 0.8330, -1.2949, -0.2462]], dtype=torch.float64) tensor([[10., 20., 30.], [40., 50., 60.]])
2.torch.Tensor creates tensors based on shape, and can also be used to create tensors of specified data
#Create a specified shape tensor def test02(): #2.1 Create a tensor with 2 rows and 3 columns data = torch.Tensor(2,3) print(data) #2.2 Create tensors with specified values data = torch. Tensor([2,3]) print(data) tensor([[0., 0., 0.], [0., 0., 0.]]) tensor([2., 3.])
3. torch.IntTensor, torch.FloatTensor, torch.DoubleTensor create tensors of the specified type
2. Create linear and random tensors
1. torch.arange and torch.linspace create linear tensors
def test01(): # 1.1 Create a tensor with a specified step size # First parameter: start value # Second parameter: end value # The third parameter: step size data = torch.arange(0,10,2) print(data) #2.2 Specify the number of elements in the specified interval data = torch.linspace(0,11,10) print(data) tensor([0, 2, 4, 6, 8]) tensor([ 0.0000, 1.2222, 2.4444, 3.6667, 4.8889, 6.1111, 7.3333, 8.5556, 9.7778, 11.0000])
2.torch.random.init_seed and torch.manaual_seed random seed settings
3. torch.randn creates random tensors
#Create a random tensor def test02(): # Fixed random number seed torch.random.manual_seed(0) #2.1 Create a random tensor data = torch.randn(2,3) print(data) print('random number seed', torch.random.initial_seed()) tensor([[ 1.5410, -0.2934, -2.1788], [ 0.5684, -1.0845, -1.3986]]) random number seed 0
3. Create 01 tensor
1. torch.ones and torch.ones_like create all 1 tensors
2.torch.zeros and torch.zeros_like create all 0 tensors
3. torch.full and torch.full_like create all specified value tensors
#1.1 Create a tensor with all 0s data = torch.zeros(2,3) print(data) #1.2 Create an all-0 tensor based on the shape of other tensors data = torch. zeros_like(data) print(data) #2. Create a tensor with all 1s data = torch.ones(2,3) print(data) #2.2 Create an all-0 tensor based on the shape of other tensors data = torch.ones_like(data) print(data) #2. Create a tensor with all specified values data = torch.full([2,3],9) print(data) #Create a tensor with the same shape as data, but all values are 200 data = torch.full([2,3],200) print(data) tensor([[0., 0., 0.], [0., 0., 0.]]) tensor([[0., 0., 0.], [0., 0., 0.]]) tensor([[1., 1., 1.], [1., 1., 1.]]) tensor([[1., 1., 1.], [1., 1., 1.]]) tensor([[9, 9, 9], [9, 9, 9]]) tensor([[200, 200, 200], [200, 200, 200]])
4. Tensor element type conversion
1. tensor.type(torch.DoubleTensor)
2. torch. double()
data = torch.full((2,3),8) print(data. dtype) # return a new value data = data.type(torch.DoubleTensor) print(data. dtype) # return a new tensor data = data.int() print(data. dtype) torch.int64 torch.float64 torch.int32
2. Tensor calculation
1. Basic operations of tensors
add, sub, mul, div, neg should not be original data
add_, sub_, mul_, div_, neg_ modify the original data
data = torch.randint(0,10,(2,3)) print(data) data = data. add(10) print(data) # data.sub() subtraction # data.mul() multiplication # data.div() division # data.neg() Negative
2, Adama product
Multiply the elements in the corresponding positions of the matrix
data1 = torch.tensor([[1,2],[3,4]]) data2 = torch.tensor([[5,6],[7,8]]) data = data1.mul(data2) print(data) data3 = data1 * data2 print(data3) tensor([[ 5, 12], [21, 32]]) tensor([[ 5, 12], [21, 32]])
3. Dot product operation
Calculate the dot product of matrix n*m and matrix m*p to get matrix n*p
1. The operator @ is used to perform point multiplication of two matrices
2. torch.mm is used for two-matrix point multiplication, requiring the input matrix to be 2-dimensional
3. torch.bmm is used for matrix point multiplication in batches, and the matrix is required to be 3-dimensional
4. torch.matmul performs dot multiplication of matrices
- For input are two-dimensional equivalent mm
- For input, all three bits are equivalent to bmm
- For tensors with different input shapes, the corresponding last few dimensions must conform to the matrix operation rules
4. Specified Computing Device
PyTorch creates tensors in CPU memory by default. Want to move tensors to the GPU
1. Use the cuda method
2. Create tensors directly on the GPU,
3 Use the to method to specify the device
data = torch. tensor([10,20,30]) print("Storage device:", data.device) data = data. cuda() print("Storage device:", data.device) data = torch.tensor([10,20,30],device='cuda:0') print("Storage device:", data.device) data = torch.tensor([10,20,30]) print("Storage device:", data.device) data = data.to('cuda:0') print("Storage device:", data.device) storage device: cpu Storage device: cuda:0 Storage device: cuda:0 storage device: cpu Storage device: cuda:0
3. Tensor type conversion
1. Convert tensor to numpy
Use the Tensor.numpy function to convert Zhang Liang to ndarray, but share memory, you can use the copy function to avoid sharing
def test01(): data_tensor = torch.tensor([2,3,4]) data_numpy = data_tensor. numpy() data_numpy1 = data_tensor.numpy().copy() #Do not share memory data_tensor[0] = 100 print(data_tensor) print(data_numpy) print(data_numpy1) tensor([100, 3, 4]) [100 3 4] [2 3 4]
2.numpy converted to tensor
1. Use from_numpy to convert the ndarray array to Tensor, the default shared memory, use the copy function to avoid sharing
2. Use torch.tensor to convert ndarray array to Tensor, which does not share memory by default
data_numpy = np.array([2,3,4]) data_tensor = torch.from_numpy(data_numpy) #shared memory data_tensor1 = torch.from_numpy(data_numpy.copy()) #Do not share memory data_tensor2 = torch.tensor(data_numpy) #Do not share memory data_numpy[0] = 100 print(data_numpy) print(data_tensor) print(data_tensor1) print(data_tensor2) [100 3 4] tensor([100, 3, 4], dtype=torch.int32) tensor([2, 3, 4], dtype=torch.int32) tensor([2, 3, 4], dtype=torch.int32)
3. Conversion of scalar tensor and number
For tensors with only one element, use the item method to extract the value from the tensor.
t1 = torch.tensor(30) t2 = torch.tensor([30]) t3 = torch.tensor([[30]]) print(t1. shape) print(t2. shape) print(t3. shape) print(t1. item()) print(t2. item()) print(t3. item()) torch. Size([]) torch. Size([1]) torch. Size([1, 1]) 30 30 30
4. Tensor concatenation
1.torch.cat function can splice two Zhangliang according to the specified dimension
torch.manual_seed(0) data1 = torch.randint(0,10,[3,4,5]) data2 = torch.randint(0,10,[3,4,5]) print(data1. shape) print(data2. shape) torch. Size([3, 4, 5]) torch. Size([3, 4, 5]) torch.Size([6, 4, 5])
2.torch.stack function can stack two tensors according to the specified dimension
torch.manual_seed(0) data1 = torch.randint(0,10,[2,3]) data2 = torch.randint(0,10,[2,3]) print(data1) print(data2) print('-' * 30) new_data = torch.stack([data1,data2],dim=0) #Stack according to the 0th dimension print(new_data. shape) print(new_data) print('-' * 30) new_data = torch.stack([data1,data2],dim=1) #Stack according to the 0th dimension print(new_data. shape) print(new_data) print('-' * 30) new_data = torch.stack([data1,data2],dim=2) #Stack according to elements print(new_data. shape) print(new_data) print('-' * 30) tensor([[4, 9, 3], [0, 3, 9]]) tensor([[7, 3, 7], [3, 1, 6]]) ------------------------------ torch. Size([2, 2, 3]) tensor([[[4, 9, 3], [0, 3, 9]], [[7, 3, 7], [3, 1, 6]]]) ------------------------------ torch. Size([2, 2, 3]) tensor([[[4, 9, 3], [7, 3, 7]], [[0, 3, 9], [3, 1, 6]]]) ------------------------------ torch. Size([2, 3, 2]) tensor([[[4, 7], [9, 3], [3, 7]], [[0, 3], [3, 1], [9, 6]]]) ------------------------------
5. Tensor index operation
1. Simple row and column index
torch.manual_seed(0) data = torch.randint(0,10,[4,5]) print(data) print('-'*30) # Get the elements of the specified row print(data[2]) # Get the elements of a column print(data[:,2]) # Get an element at the specified position print(data[1,2],data[1][2]) # Indicates that the first three rows are obtained first, and the data in the third column is obtained print(data[:3,2]) # If the indexed rows are both a 1D index, the lengths of the two lists must be equal # Indicates to obtain the elements of three positions (0, 0) (2, 1) (3, 2) print(data[[0,2,3],[0,1,2]]) #Represents columns 0, 1, and 2 of rows 0, 2, and 3 print(data[[[0],[2],[3]],[0,1,2]]) tensor([[4, 9, 3, 0, 3], [9, 7, 3, 7, 3], [1, 6, 6, 9, 8], [6, 6, 8, 4, 3]]) ------------------------------ tensor([1, 6, 6, 9, 8]) tensor([3, 3, 6, 8]) tensor(3) tensor(3) tensor([3, 3, 6]) tensor([4, 6, 8]) tensor([[4, 9, 3], [1, 6, 6], [6, 6, 8]])
2. Boolean index, multidimensional index
torch.manual_seed(0) data = torch.randint(0,10,[4,5]) print(data) # Get all elements greater than 3 in the tensor print(data[data >3]) # Get rows where the second column in the tensor is greater than 6 print(data[data[:,1] >6]) # Get all the columns in the tensor whose row 2 is greater than 3 print(data[:,data[1] >3]) tensor([[4, 9, 3, 0, 3], [9, 7, 3, 7, 3], [1, 6, 6, 9, 8], [6, 6, 8, 4, 3]]) tensor([4, 9, 9, 7, 7, 6, 6, 9, 8, 6, 6, 8, 4]) tensor([[4, 9, 3, 0, 3], [9, 7, 3, 7, 3]]) tensor([[4, 9, 0], [9, 7, 7], [1, 6, 9], [6, 6, 4]])
torch.manual_seed(0) data = torch.randint(0,10,[3,4,5]) print(data) print('-'*30) # Select the 0th element according to the 0th dimension, 4 rows and 5 columns print(data[0,:,:]) print('-' * 30) # Select the 0th element according to the 1st dimension, 4 rows and 5 columns print(data[:,0,:]) print('-' * 30) # Select the 0th element according to the second dimension, elements in 4 rows and 5 columns print(data[:,:,0]) print('-' * 30)