【AI】《动手学-深度学习-PyTorch版》笔记(三):PyTorch常用函数

1、torch.arange

返回一维张量(一维数组),官网说明,常见的三种用法如下

输入:torch.arange(5)
输出:tensor([0, 1, 2, 3, 4])
输入:torch.arange(5, 16)
输出:tensor([ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])
输入:torch.arange(1, 25, 2) 
输出:tensor([ 1,  3,  5,  7,  9, 11, 13, 15, 17, 19, 21, 23])

2、tensor.shape、tensor.reshape

tensor.shape:查看张量的形状
tensor.reshape:返回改变形状后的张量,原张量不变

输入:x=torch.arange(12)
输入:x
输出:tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
输入:x.shape
输出:torch.Size([12])

输入:X = x.reshape(3, 4)
输入:X
输出:tensor([[ 0,  1,  2,  3],
	    	 [ 4,  5,  6,  7],
    		 [ 8,  9, 10, 11]])
输入:X.shape
输出࿱

你可能感兴趣的:(AI,人工智能,深度学习,pytorch)