Pytorch初学_Assignment_1-1 Tensor Basics
Introduction
Tensor Basics
Creating and Accessing tensors
首先确定一下tensor的含义。
tensor即张量,数学中定义的张量是基于向量和矩阵的推广。
换而言之,标量可视为零阶张量,矢量可视为一阶张量,矩阵可视为二阶张量。
Examples
#e.g.1
# Create a rank 1 tensor from a Python list
a = torch.tensor([1, 2, 3])
print('Here is a:')
print(a)
print('type(a): ', type(a))
print('rank of a: ', a.dim())
print('a.shape: ', a.shape)
运行结果:
Here is a: tensor([1, 2, 3])
rank of a: 1
type(a): <class ‘torch.Tensor’>
a.shape: torch.Size ([3])
tensor的rank是它的行数,tensor的shape是一个整数元组,给出了每个维度上数组的大小。
torch tensor 是一个多维的数值网格,所有值都是同一个类型,由非负数整数的元组索引:
#e.g.2
# Access elements using square brackets
print()
print('a[0]: ', a[0])
print('type(a[0]): ', type(a[0]))
print('type(a[0].item()): ', type(a[0].item()))
运行结果:
tensor(1)
type(a[0]): <class ‘torch.Tensor’>
type(a[0].item()): <class ‘int’>
从PyTorch tensor访问一个元素将返回一个PyTorch标量。
我们可以使用.item()方法将其转换为Python标量:
#e.g.3
# Mutate elements using sq**uare brackets
a[1] = 10
print()
print('a after mutating:')
print(a)
运行结果:
a after mutating:
tensor([ 1, 10, 3])
More examples
二维
#e.g.1
# Create a two-dimensional tensor
b = torch.tensor([[1, 2, 3], [4, 5, 5]])
print('Here is b:')
print(b)
print('rank of b:', b.dim())
print('b.shape: ', b.shape)
运行结果:
Here is b:
tensor([[1, 2, 3],
[4, 5, 5]])
rank of b: 2
b.shape: torch.Size([2, 3])
.dim() 获取维度
#e.g.2
# Access elements from a multidimensional tensor
print()
print('b[0, 1]:', b[0, 1])
print('b[1, 2]:', b[1, 2])
运行结果:
b[0,1]: tensor(2)
b[1, 2]: tensor(5)
#e.g.3
# Mutate elements of a multidimensional tensor
b[1, 1] = 100
print()
print('b after mutating:')
print(b)
运行结果:
b after murating:
tensor([[ 1, 2, 3],
[ 4, 100, 5]])
Tensor constructors
PyTorch 提供了很多方法创建 tensor; 避免了使用 Python lists. For example:
torch.zeros: 创建全是0的tensor
torch.ones: 创建全是1的tensor
torch.rand: 创建都是随机数的tensor
You can find a full list of tensor creation operations in the documentation.
Examples
#e.g.
# Create a tensor of all zeros
a = torch.zeros(2, 3)
print('tensor of zeros:')
print(a)
# Create a tensor of all ones
b = torch.ones(1, 2)
print('\ntensor of ones:')
print(b)
# Create a 3x3 identity matrix
c = torch.eye(3)
print('\nidentity matrix:')
print(c)
# Tensor of random values
d = torch.rand(4, 5)
print('\nrandom tensor:')
print(d)
运行结果:
tensor of zeros:
tensor([[0., 0., 0.],
[0., 0., 0.]])
tensor of ones:
tensor([[1., 1.]])
identity matrix:
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
random tensor:
tensor([[0.3001, 0.5240, 0.9980, 0.7438, 0.7942],
[0.3682, 0.8547, 0.4766, 0.5108, 0.3509],
[0.4050, 0.1757, 0.2234, 0.3544, 0.6618],
[0.0518, 0.0821, 0.6579, 0.9482, 0.1165]])
Datatypes
每个torch都有一个dtype属性,可以用来检查其数据类型:
Examples
e.g.1
# Let torch choose the datatype
x0 = torch.tensor([1, 2]) # List of integers
x1 = torch.tensor([1., 2.]) # List of floats
x2 = torch.tensor([1., 2]) # Mixed list
print('dtype when torch chooses for us:')
print('List of integers:', x0.dtype)
print('List of floats:', x1.dtype)
print('Mixed list:', x2.dtype)
运行结果:
dtype when torch chooses for us:
List of integers: torch.int64
List of floats: torch.float32
Mixed list: torch.float32
e.g.2
# Force a particular datatype
y0 = torch.tensor([1, 2], dtype=torch.float32) # 32-bit float
y1 = torch.tensor([1, 2], dtype=torch.int32) # 32-bit (signed) integer
y2 = torch.tensor([1, 2], dtype=torch.int64) # 64-bit (signed) integer
print('\ndtype when we force a datatype:')
print('32-bit float: ', y0.dtype)
print('32-bit integer: ', y1.dtype)
print('64-bit integer: ', y2.dtype)
运行结果:
dtype when we force a datatype:
32-bit float: torch.float32
32-bit integer: torch.int32
64-bit integer: torch.int64
e.g.3
# Other creation ops also take a dtype argument
z0 = torch.ones(1, 2) # Let torch choose for us
z1 = torch.ones(1, 2, dtype=torch.int16) # 16-bit (signed) integer
z2 = torch.ones(1, 2, dtype=torch.uint8) # 8-bit (unsigned) integer
print('\ntorch.ones with different dtypes')
print('default dtype:', z0.dtype)
print('16-bit integer:', z1.dtype)
print('8-bit unsigned integer:', z2.dtype)
运行结果:
torch.ones with different dtypes
default dtype: torch.float32
16-bit integer: torch.int16
8-bit unsigned integer: torch.uint8
我们可以使用 .to() 方法将torch转换为另一种数据类型;
还有一些方便的方法,如 .float() 和 .long() 可以强制转换为特定的数据类型:
x0 = torch.eye(3, dtype=torch.int64)
x1 = x0.float() # Cast to 32-bit float
x2 = x0.double() # Cast to 64-bit float
x3 = x0.to(torch.float32) # Alternate way to cast to 32-bit float
x4 = x0.to(torch.float64) # Alternate way to cast to 64-bit float
print('x0:', x0.dtype)
print('x1:', x1.dtype)
print('x2:', x2.dtype)
print('x3:', x3.dtype)
print('x4:', x4.dtype)
运行结果:
x0: torch.int64
x1: torch.float32
x2: torch.float64
x3: torch.float32
x4: torch.float64
PyTorch提供了几种方法来创建与另一个torch具有相同数据类型的torch:
torch.zeros_like():创建与给定torch的shape和type相同的新torch
.new_zeros():创建相同type但可能不同shape的张量
.to():可以把一个torch作为一个参数,新torch
x0 = torch.eye(3, dtype=torch.float64) # Shape (3, 3), dtype torch.float64
x1 = torch.zeros_like(x0) # Shape (3, 3), dtype torch.float64
x2 = x0.new_zeros(4, 5) # Shape (4, 5), dtype torch.float64
x3 = torch.ones(6, 7).to(x0) # Shape (6, 7), dtype torch.float64)
print('x0 shape is %r, dtype is %r' % (x0.shape, x0.dtype))
print('x1 shape is %r, dtype is %r' % (x1.shape, x1.dtype))
print('x2 shape is %r, dtype is %r' % (x2.shape, x2.dtype))
print('x3 shape is %r, dtype is %r' % (x3.shape, x3.dtype))
运行结果:
x0 shape is torch.Size([3, 3]), dtype is torch.float64
x1 shape is torch.Size([3, 3]), dtype is torch.float64
x2 shape is torch.Size([4, 5]), dtype is torch.float64
x3 shape is torch.Size([6, 7]), dtype is torch.float64
尽管PyTorch提供了大量的数字数据类型,但最常用的数据类型是:
torch.float32:标准浮点类型;几乎所有的算术都是使用这种类型完成的。
torch.int64: 通常用于存储索引。
torch.uint8:通常用于存储布尔值,0为false, 1为true。
torch.float16:用于混合精度运算,通常在带有张量核的NVIDIA gpu上使用。