pytorch机器学习_机器学习— PyTorch
pytorch機(jī)器學(xué)習(xí)
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:這是一個(gè)基于Torch庫(kù)(基于Lua編程語(yǔ)言)的開(kāi)源機(jī)器學(xué)習(xí)庫(kù),該庫(kù)由Facebook的AI Research Lab開(kāi)發(fā)。
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.
它還可以利用圖形處理單元的功能,因此它是深度學(xué)習(xí)的首選庫(kù)之一。 TensorFlow是另一個(gè)用于深度學(xué)習(xí)中的張量操作的出色庫(kù)。
PyTorch is easy to learn and integrates easily with other python packages makes it a simple choice for researchers to use.
PyTorch易于學(xué)習(xí),并且易于與其他python軟件包集成,這使其成為研究人員使用的簡(jiǎn)單選擇。
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.
張量:張量是標(biāo)量,向量和矩陣的泛化,或者我們可以說(shuō)這些是張量的特例,可以很容易地理解為多維數(shù)組。 張量中的所有值均具有相同的數(shù)據(jù)類型,且具有已知(或部分已知)的形狀。
Install the PyTorch python library : from here
從這里安裝PyTorch python庫(kù):
Syntax to create tensor with PyTorch :
使用PyTorch創(chuàng)建張量的語(yǔ)法:
This is how you can create n-dimensional tensors with PyTorch.
這就是使用PyTorch創(chuàng)建n維張量的方法。
以下是一些PyTorch函數(shù),我們將進(jìn)行探索: (Here are some of the PyTorch functions, we are going to explore:)
view, flip, ne, topk, bincount
查看,翻轉(zhuǎn),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.
它改變張量的形狀。 在參數(shù)上,它要求輸出張量的形狀。 它返回一個(gè)新的張量,其數(shù)據(jù)與自張量相同,但形狀不同。
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:創(chuàng)建一個(gè)具有不同8個(gè)值的1-D張量,然后在視圖函數(shù)中傳遞2、2、2,這意味著更改每個(gè)軸上具有2個(gè)元素的張量的形狀。
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:創(chuàng)建一個(gè)具有1-8值的一維張量,然后在視圖函數(shù)中傳遞2、4,這意味著更改張量的形狀,該張量在第一個(gè)軸上包含2個(gè)元素,在第二個(gè)軸上包含4個(gè)元素。
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個(gè)元素,然后在視圖函數(shù)中傳遞2、2、2作為參數(shù)。 該錯(cuò)誤是因?yàn)闊o(wú)法更改形狀,以使每個(gè)軸上沒(méi)有2個(gè)元素。
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張量沿給定軸的順序顛倒為暗。 它需要兩個(gè)參數(shù),第一個(gè)是輸入張量,第二個(gè)是要翻轉(zhuǎn)的軸的列表,并返回結(jié)果張量。
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:在此示例中,輸入是一個(gè)二維張量,它是翻轉(zhuǎn)函數(shù)中的第一個(gè)參數(shù)。 第二個(gè)參數(shù)是包含1的列表,這意味著要翻轉(zhuǎn)第二個(gè)軸,輸出是結(jié)果張量。
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張量,它是翻轉(zhuǎn)函數(shù)中的第一個(gè)參數(shù)。 第二個(gè)參數(shù)是一個(gè)包含0、1的列表,這意味著要翻轉(zhuǎn)第一軸和第二軸,輸出是結(jié)果張量。
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:在此示例中,輸入是一維張量,它是翻轉(zhuǎn)函數(shù)中的第一個(gè)參數(shù)。 第二個(gè)參數(shù)是一個(gè)包含1的列表,這意味著第二個(gè)軸將被翻轉(zhuǎn),但是我們可以清楚地看到輸入張量只有一個(gè)軸,因此試圖執(zhí)行的操作是不可能的,這就是為什么會(huì)出錯(cuò)的原因。
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.
此函數(shù)用于逐元素檢查參數(shù)(輸入和輸出)中的兩個(gè)張量是否相等。 輸入!=輸出。 兩個(gè)張量必須具有相同的長(zhǎng)度。 它返回一個(gè)與輸入相同長(zhǎng)度的張量,其中包含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:在此示例中,我們采用了兩個(gè)張量[1、3、4、9、5]和[1、3、5、9、0],然后將它們傳遞給函數(shù),結(jié)果是張量為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:在此示例中,我們采用了兩個(gè)2-D張量,然后將它們傳遞給函數(shù),結(jié)果是長(zhǎng)度相同的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:由于兩個(gè)張量中的元素?cái)?shù)量不相等,因此該示例給出了錯(cuò)誤。 第一張量比第二張量具有更多的元素。
4. torch.topk(輸入,k,昏暗=無(wú),最大=真實(shí),已排序=真實(shí),輸出=無(wú)) (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.
此函數(shù)沿給定維度返回給定輸入張量的k個(gè)最大元素。 如果未指定dim,則選擇輸入的最后一個(gè)尺寸。 如果最大為False,則返回k個(gè)最小元素。
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函數(shù),給定k = 3,這意味著我們想要前3個(gè),largest = True,這意味著我們需要3個(gè)最大的數(shù)字,并且我們一直在排序= True,表示輸出應(yīng)按降序排序。 我們也獲得了排名前三位的最大數(shù)字索引。
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,并且不取暗,因此它將自動(dòng)取最后一個(gè)暗。 因此,它從每個(gè)內(nèi)部列表中以升序?qū)ζ溥M(jìn)行排序,并給出輸出的前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。 現(xiàn)在在昏暗的0中,只有3個(gè)列表存在,并且我們要求有4個(gè)最大的元素,因此它給出了錯(cuò)誤。 如果我們使k = 1,則它將返回[30、24、5、25、15]作為輸出,因?yàn)榇肆斜碇械脑刈畲蟆?
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.
此函數(shù)用于計(jì)算非負(fù)整數(shù)數(shù)組中每個(gè)值的頻率。 它在輸入數(shù)組或最小長(zhǎng)度中尋找最大的整數(shù),然后生成一個(gè)輸出數(shù)組,其輸入中每個(gè)數(shù)字的頻率從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,因此輸出是一個(gè)數(shù)組,其輸入中所有元素的頻率為0到9。例如-4出現(xiàn)兩次,因此在輸出數(shù)組中的索引為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:在此示例中,假設(shè)我們知道輸入數(shù)組的值在0到11之間。因此我們可以確定輸出數(shù)組的長(zhǎng)度,對(duì)于0-11,該數(shù)組應(yīng)該具有12個(gè)元素。 因此,我們?cè)诤瘮?shù)中保留了另一個(gè)參數(shù)minlength = 12,它將在輸出中生成最少12個(gè)元素的數(shù)組。 對(duì)于輸入中不存在的元素,它將頻率輸出為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:此函數(shù)只能對(duì)一維非負(fù)整數(shù)起作用。 在我們的例子中,輸入具有負(fù)值和浮點(diǎn)值。 因此,它給出了錯(cuò)誤。
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.
我們已經(jīng)介紹了PyTorch的5個(gè)功能,這些功能可用于簡(jiǎn)化張量操作。 還有許多其他功能需要探索,任何人都可以通過(guò)訪問(wèn)文檔輕松地學(xué)習(xí)其用法。
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機(jī)器學(xué)習(xí)
總結(jié)
以上是生活随笔為你收集整理的pytorch机器学习_机器学习— PyTorch的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 快手戳一下怎么关闭功能
- 下一篇: 检测和语义分割_分割和对象检测-第1部分