pytorch机器学习
PyTorch : It is an open source machine learning library based on the Torch library (which is based on Lua Programming Language), developed by Facebook’s AI Research lab.
PyTorch:这是一个基于Torch库(基于Lua编程语言)的开源机器学习库,该库由Facebook的AI Research Lab开发。
It can also use the power of graphics processing unit and so it is one of the preferred library for deep learning. TensorFlow is another great library for tensor operations in deep learning.
它还可以利用图形处理单元的功能,因此它是深度学习的首选库之一。 TensorFlow是另一个用于深度学习中的张量操作的出色库。
PyTorch is easy to learn and integrates easily with other python packages makes it a simple choice for researchers to use.
PyTorch易于学习,并且易于与其他python软件包集成,这使其成为研究人员使用的简单选择。
Tensor : A tensor is a generalization of scalar, vectors and matrices or we can say these are the special cases of a tensor and can easily understood as a multidimensional array. All values in a tensor hold identical data type with a known (or partially known) shape.
张量:张量是标量,向量和矩阵的泛化,或者我们可以说这些是张量的特例,可以很容易地理解为多维数组。 张量中的所有值均具有相同的数据类型,且具有已知(或部分已知)的形状。
Install the PyTorch python library : from here
从这里安装PyTorch python库:
Syntax to create tensor with PyTorch :
使用PyTorch创建张量的语法:
This is how you can create n-dimensional tensors with PyTorch.
这就是使用PyTorch创建n维张量的方法。
以下是一些PyTorch函数,我们将进行探索: (Here are some of the PyTorch functions, we are going to explore:)
view, flip, ne, topk, bincount
查看,翻转,ne,topk,bincount
1.火炬。张量。视图(*形状) (1. torch.Tensor.view(*shape))
It changes the shape of a tensor. In argument it requires the shape for output tensor. It returns a new tensor with the same data as the self tensor but of a different shape.
它改变张量的形状。 在参数上,它要求输出张量的形状。 它返回一个新的张量,其数据与自张量相同,但形状不同。
Example 1: Created a 1-D tensor having different 8 values, then in the view function passed 2, 2, 2 which means changing the shape of the tensor having 2 element in each axis.
示例1:创建一个具有不同8个值的1-D张量,然后在视图函数中传递2、2、2,这意味着更改每个轴上具有2个元素的张量的形状。
Example 2: Created a 1-D tensor having values 1–8, then in the view function passed 2, 4 which means changing the shape of the tensor having 2 element in first axis and 4 element in the second axis.
示例2:创建一个具有1-8值的一维张量,然后在视图函数中传递2、4,这意味着更改张量的形状,该张量在第一个轴上包含2个元素,在第二个轴上包含4个元素。
Example 3: In this example, the input tensor is having 9 elements, then in the view function we passed 2, 2, 2 as argument. The error is because it’s not possible to change the shape such that 2 element in each axis.
示例3:在此示例中,输入张量包含9个元素,然后在视图函数中传递2、2、2作为参数。 该错误是因为无法更改形状,以使每个轴上没有2个元素。
2. torch.flip(input,dims) (2. torch.flip(input, dims))
It reverse the order of a n-D tensor along given axis in dims. It requires two argument, the first one is the input tensor and second is the list of axis you want to flip and returns the resultant tensor.
它将nD张量沿给定轴的顺序颠倒为暗。 它需要两个参数,第一个是输入张量,第二个是要翻转的轴的列表,并返回结果张量。
Example 1: In this example, input is a 2-D tensor which is 1st argument in flip function. The second argument is a list containing 1 which means that 2nd axis is to be flipped, the output is a resultant tensor.
示例1:在此示例中,输入是一个二维张量,它是翻转函数中的第一个参数。 第二个参数是包含1的列表,这意味着要翻转第二个轴,输出是结果张量。
Example 2: In this example, input is a 3-D tensor which is 1st argument in flip function. The second argument is a list containing 0, 1 which means that 1st and 2nd axis is to be flipped, the output is a resultant tensor.
示例2:在此示例中,输入是3-D张量,它是翻转函数中的第一个参数。 第二个参数是一个包含0、1的列表,这意味着要翻转第一轴和第二轴,输出是结果张量。
Example 3: In this example, input is a 1-D tensor which is 1st argument in flip function. The second argument is a list containing 1 which means that 2nd axis is to be flipped, but as we can clearly see that the input tensor is having only one axis so the operation trying to perform is impossible and that’s why it is giving error.
示例3:在此示例中,输入是一维张量,它是翻转函数中的第一个参数。 第二个参数是一个包含1的列表,这意味着第二个轴将被翻转,但是我们可以清楚地看到输入张量只有一个轴,因此试图执行的操作是不可能的,这就是为什么会出错的原因。
3. torch.ne(输入,输出) (3. torch.ne(input, output))
This function is used to check element-wise that the two tensors in argument(input and output) are unequal. input != output . Both tensors must be of the same length. It return a tensor of same length as the input containing True and False, True for unequal and False for equal.
此函数用于逐元素检查参数(输入和输出)中的两个张量是否相等。 输入!=输出。 两个张量必须具有相同的长度。 它返回一个与输入相同长度的张量,其中包含True和False,True代表不相等,False代表相等。
Example 1: In this example, we have taken two tensors [1, 3, 4, 9, 5] and [1, 3, 5, 9, 0], then pass them to the function, the result is a tensor of True and False → [False, False, True, False, True].
示例1:在此示例中,我们采用了两个张量[1、3、4、9、5]和[1、3、5、9、0],然后将它们传递给函数,结果是张量为True和False→[False,False,True,False,True]。
Example 2: In this example, we have taken two 2-D tensors, then pass them to the function, the result is a tensor of True and False of same length,True where the elements are unequal and False where elements are equal.
示例2:在此示例中,我们采用了两个2-D张量,然后将它们传递给函数,结果是长度相同的True和False的张量,在元素不相等的情况下为True,在元素相等的情况下为False。
Example 3: This example gives an error, because the number of elements in both the tensors are unequal. First tensor is having more elements than second tensor.
示例3:由于两个张量中的元素数量不相等,因此该示例给出了错误。 第一张量比第二张量具有更多的元素。
4. torch.topk(输入,k,昏暗=无,最大=真实,已排序=真实,输出=无) (4. torch.topk(input, k, dim=None, largest=True, sorted=True, out=None))
This function returns the k largest elements of the given input tensor along a given dimension. If dim is not given, the last dimension of the input is chosen. If largest is False then the k smallest elements are returned.
此函数沿给定维度返回给定输入张量的k个最大元素。 如果未指定dim,则选择输入的最后一个尺寸。 如果最大为False,则返回k个最小元素。
Example 1: In this example, we have taken a 1-D tensor then pass it to topk function, we have given k=3 which means we want top 3, largest= True which means we need 3 largest numbers and also we kept sorted=True which means the output should be sorted in descending order. We got indices of top 3 sorted largest number as well.
示例1:在此示例中,我们采用了一维张量,然后将其传递给topk函数,给定k = 3,这意味着我们想要前3个,largest = True,这意味着我们需要3个最大的数字,并且我们一直在排序= True,表示输出应按降序排序。 我们也获得了排名前三位的最大数字索引。
Example 2: In this example we have taken the 2-D tensor. Taken k=2, largest=False and sorted=True, and not taken dim, so it will automatically take the last dim. And so it sort it in ascending order from each inner list and gives the top 2 as output along with their indices.
示例2:在此示例中,我们采用了二维张量。 取k = 2,最大= False和sorted = True,并且不取暗,因此它将自动取最后一个暗。 因此,它从每个内部列表中以升序对其进行排序,并给出输出的前2名及其索引。
Example 3: In this example, we have kept the same tensor from previous example and taken largest=True, k=4 with dim=0. Now in dim 0 there are only 3 lists present, and we asked for 4 largest element so it is giving error. If we have taken k=1, then it would have return [30, 24, 5, 25, 15] as output because this list is having the largest element out of all.
例3:在本例中,我们保持了与上例相同的张量,并取larget = True,k = 4,dim = 0。 现在在昏暗的0中,只有3个列表存在,并且我们要求有4个最大的元素,因此它给出了错误。 如果我们使k = 1,则它将返回[30、24、5、25、15]作为输出,因为此列表中的元素最大。
5. torch.bincount(input, weights = None ,minlength = 0) (5. torch.bincount(input, weights=None, minlength=0))
This function is used to count the frequency of each value in an array of non-negative integers. It looks for largest integer in input array or minlength and then produce an output array having frequency of each number in input from 0 to the largest.
此函数用于计算非负整数数组中每个值的频率。 它在输入数组或最小长度中寻找最大的整数,然后生成一个输出数组,其输入中每个数字的频率从0到最大。
Example 1: In this example the input tensor have the largest element 9, so the output is an array having frequency of all element in input from 0 to 9. for example- 4 comes twice so in the output array at index 4, the value is 2.
示例1:在此示例中,输入张量具有最大的元素9,因此输出是一个数组,其输入中所有元素的频率为0到9。例如-4出现两次,因此在输出数组中的索引为4,即值是2。
Example 2: In this example, suppose we know that the input array is having values between 0 to 11. So we can decide the length of output array which should be having 12 element for 0–11. So we kept another argument minlength=12 in the function, which will generate the array of minimum 12 element in output. It output the frequency as 0 for the elements which are not there in input.
示例2:在此示例中,假设我们知道输入数组的值在0到11之间。因此我们可以确定输出数组的长度,对于0-11,该数组应该具有12个元素。 因此,我们在函数中保留了另一个参数minlength = 12,它将在输出中生成最少12个元素的数组。 对于输入中不存在的元素,它将频率输出为0。
Example 3: This function can only work on 1-D non negative integers. In our case the input is having negative and float values. So it gives the error.
示例3:此函数只能对一维非负整数起作用。 在我们的例子中,输入具有负值和浮点值。 因此,它给出了错误。
We have covered the 5 functions of PyTorch that can be used to make tensor operations easier. There are a lot of other functions which are needed to be explored, anyone can easily learn their usage by visiting the documentation.
我们已经介绍了PyTorch的5个功能,这些功能可用于简化张量操作。 还有许多其他功能需要探索,任何人都可以通过访问文档轻松地学习其用法。
Checkout xyzeelearn.com for more such posts by me.
在xyzeelearn.com上查看我的更多此类帖子。
Here are the links to some references.
这里是一些参考的链接。
翻译自: https://medium.com/xyzeelearn/machine-learning-pytorch-590b09409236
pytorch机器学习


所有评论(0)