pytorch 和tensorflow 中最重要的概念就是tensor了,tensorflow 这个框架的名字中很直白,就是tensor的流动,所以学习深度学习的第一课就是得搞懂tensor到底是个什么东西了,今天就来学习下,OK,起飞

1.tensor到底是啥


tensor 即“张量”(翻译的真难理解,破概念)。实际上跟numpy数组、向量、矩阵的格式基本一样。但是是专门针对GPU来设计的,可以运行在GPU上来加快计算效率,不要被吓到。

  1. PyTorch中,张量Tensor是最基础的运算单位,**与NumPy中的NDArray类似**,张量表示的是一个多维矩阵。不同的是,PyTorch中的Tensor可以运行在GPU上,而NumPyNDArray只能运行在CPU上。由于Tensor能在GPU上运行,因此大大加快了运算速度。

x. 其他知识点

整个Pytorch的数据结构 , 矩阵的运算与基础结构。
add() 跟 add_()的区别是原始值是否改变。

x. torch.tensor的一些接口

虽然接口可以现查,但是对结构的熟悉程度直接决定了是否拿下一份工作,可以对接口进行分门别类。

接口名称 简介 掌握程度
Tensor.new_tensor Returns a new Tensor with data as the tensor data.
Tensor.new_full Returns a Tensor of size size filled with fill_value.
Tensor.new_empty

| Returns a Tensor of size size filled with uninitialized data. | | | Tensor.new_ones

| Returns a Tensor of size size filled with 1. | | | Tensor.new_zeros

| Returns a Tensor of size size filled with 0. | | | Tensor.is_cuda

| Is True if the Tensor is stored on the GPU, False otherwise. | | | Tensor.is_quantized | Is True if the Tensor is quantized, False otherwise. | 不懂 | | Tensor.is_meta | Is True if the Tensor is a meta tensor, False otherwise. | 不懂 | | Tensor.device | Is the torch.device where this Tensor is. | | | Tensor.grad | This attribute is None by default and becomes a Tensor the first time a call to backward() computes gradients for self.

| 非常重要 | | Tensor.ndim | Alias for dim() | | | Tensor.real | Returns a new tensor containing real values of the self tensor. | | | Tensor.imag | Returns a new tensor containing imaginary values of the self tensor. | | | Tensor.abs | See torch.abs() | | | Tensor.abs_ | In-place version of abs() | | | Tensor.absolute | Alias for abs() | | | Tensor.absolute_ | In-place version of absolute() Alias for abs_() | | | Tensor.acos | See torch.acos() | | | Tensor.acos_ | In-place version of acos() | | | Tensor.arccos | See torch.arccos() | | | Tensor.arccos_ | In-place version of arccos() | | | Tensor.add | Add a scalar or tensor to self tensor. | | | Tensor.add_ | In-place version of add() | | | Tensor.addbmm | See torch.addbmm() | 不懂 | | Tensor.addbmm_ | In-place version of addbmm() | | | Tensor.addcdiv | See torch.addcdiv() | | | Tensor.addcdiv_ | In-place version of addcdiv() | | | Tensor.addcmul | See torch.addcmul() | | | Tensor.addcmul_ | In-place version of addcmul() | |

参考

https://bbs.huaweicloud.com/blogs/300211
PyTorch实现深度学习(3):始于张量