3-1 tensor

In the context of deep learning, tensors refer to the generalization of vectors and matrices to an arbitrary number of dimensions, as we can see in figure 3.2. Another name for the same concept is multidimensional array. The dimensionality of a tensor coincides with the number of indexes used to refer to scalar values within the tensor.

在这里插入图片描述

Compared to NumPy arrays, PyTorch tensors have a few superpowers, such as the ability to perform very fast operations on graphical processing units (GPUs), distribute operations on multiple devices or machines, and keep track of the graph of computations that created them.

1.The world as floating-point numbers
Since floating-point numbers are the way a network deals with information, we need a way to encode real-world data of the kind we want to process into something digestible by a network and then decode the output back to something we can understand and use for our purpose.

A deep neural network typically learns the transformation from one form of data to another in stages, which means the partially transformed data between each stage can be thought of as a sequence of intermediate representations.In general, such intermediate representations are collections of floating-point numbers that characterize the input and capture the data’s structure in a way that is instrumental for describing how inputs are mapped to the outputs of the neural network.

在这里插入图片描述

2.Tensors: Multidimensional arrays
(1)列表的索引访问和修改
在这里插入图片描述
在这里插入图片描述

(2)Constructing our first tensors

创建了一个以1填充大小为3的一维张量

在这里插入图片描述

创建一个3行4列的二维张量

在这里插入图片描述

我们同样可以进行索引和修改操作,索引取出的每个数都是张量形式

在这里插入图片描述

通过float可以转化为一个具体的数

在这里插入图片描述

(3)The essence of tensors

Python 列表或数字元组是单独分配在内存中的 Python 对象的集合

在这里插入图片描述
PyTorch 张量或 NumPy 数组(通常)是连续内存块上的视图

在这里插入图片描述
为张量设置具体值,以下两种方法等价

在这里插入图片描述

在这里插入图片描述

To get the coordinates of the first point(1.0 , 2.0), we do the following:

在这里插入图片描述

严格来说这只是一个二维点,而非坐标
我们可以通过二维张量来实现坐标

在这里插入图片描述

张量的访问

在这里插入图片描述

获取一个点的二维坐标
输出结果是张量,下例的新张量是一个大小为2的一维张量,它引用了b张量中第一行的值

在这里插入图片描述

3.Indexing tensors

回顾列表
在这里插入图片描述
我们可以对PyTorch张量使用相同的符号,注意对多维的处理和增维

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45825865/article/details/131349416
3-1